2021-04-27 03:52:18 +00:00
|
|
|
use crate::{
|
|
|
|
geometry::{rect::RectF, vector::Vector2F},
|
|
|
|
AfterLayoutContext, AppContext, DebugContext, Element, ElementBox, Event, EventContext,
|
|
|
|
LayoutContext, PaintContext, SizeConstraint, ValueHandle,
|
|
|
|
};
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
pub struct MouseEventHandler {
|
|
|
|
state: ValueHandle<MouseState>,
|
|
|
|
child: ElementBox,
|
2021-04-28 16:00:45 +00:00
|
|
|
click_handler: Option<Box<dyn FnMut(&mut EventContext)>>,
|
2021-04-27 03:52:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 17:37:14 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Default)]
|
2021-04-27 03:52:18 +00:00
|
|
|
pub struct MouseState {
|
2021-04-27 17:58:59 +00:00
|
|
|
pub hovered: bool,
|
|
|
|
pub clicked: bool,
|
2021-04-27 03:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MouseEventHandler {
|
2021-04-27 15:23:44 +00:00
|
|
|
pub fn new<Tag, F>(id: usize, ctx: &AppContext, render_child: F) -> Self
|
|
|
|
where
|
|
|
|
Tag: 'static,
|
|
|
|
F: FnOnce(MouseState) -> ElementBox,
|
|
|
|
{
|
2021-04-28 00:35:24 +00:00
|
|
|
let state_handle = ctx.value::<Tag, _>(id);
|
|
|
|
let state = state_handle.read(ctx, |state| *state);
|
|
|
|
let child = render_child(state);
|
|
|
|
Self {
|
|
|
|
state: state_handle,
|
|
|
|
child,
|
2021-04-28 16:00:45 +00:00
|
|
|
click_handler: None,
|
2021-04-28 00:35:24 +00:00
|
|
|
}
|
2021-04-27 03:52:18 +00:00
|
|
|
}
|
2021-04-28 16:00:45 +00:00
|
|
|
|
|
|
|
pub fn on_click(mut self, handler: impl FnMut(&mut EventContext) + 'static) -> Self {
|
|
|
|
self.click_handler = Some(Box::new(handler));
|
|
|
|
self
|
|
|
|
}
|
2021-04-27 03:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Element for MouseEventHandler {
|
|
|
|
type LayoutState = ();
|
|
|
|
type PaintState = ();
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&mut self,
|
|
|
|
constraint: SizeConstraint,
|
|
|
|
ctx: &mut LayoutContext,
|
|
|
|
) -> (Vector2F, Self::LayoutState) {
|
|
|
|
(self.child.layout(constraint, ctx), ())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn after_layout(
|
|
|
|
&mut self,
|
|
|
|
_: Vector2F,
|
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
ctx: &mut AfterLayoutContext,
|
|
|
|
) {
|
|
|
|
self.child.after_layout(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn paint(
|
|
|
|
&mut self,
|
|
|
|
bounds: RectF,
|
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
ctx: &mut PaintContext,
|
|
|
|
) -> Self::PaintState {
|
|
|
|
self.child.paint(bounds.origin(), ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dispatch_event(
|
|
|
|
&mut self,
|
|
|
|
event: &Event,
|
|
|
|
bounds: RectF,
|
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
_: &mut Self::PaintState,
|
|
|
|
ctx: &mut EventContext,
|
|
|
|
) -> bool {
|
2021-04-28 16:00:45 +00:00
|
|
|
let click_handler = self.click_handler.as_mut();
|
|
|
|
|
2021-04-27 17:37:14 +00:00
|
|
|
let handled_in_child = self.child.dispatch_event(event, ctx);
|
|
|
|
|
2021-04-28 00:35:24 +00:00
|
|
|
self.state.update(ctx.app, |state| match event {
|
2021-04-27 03:52:18 +00:00
|
|
|
Event::MouseMoved { position } => {
|
|
|
|
let mouse_in = bounds.contains_point(*position);
|
|
|
|
if state.hovered != mouse_in {
|
|
|
|
state.hovered = mouse_in;
|
2021-04-27 17:37:14 +00:00
|
|
|
ctx.notify();
|
2021-04-27 03:52:18 +00:00
|
|
|
true
|
|
|
|
} else {
|
2021-04-27 17:37:14 +00:00
|
|
|
handled_in_child
|
2021-04-27 03:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::LeftMouseDown { position, .. } => {
|
2021-04-27 17:37:14 +00:00
|
|
|
if !handled_in_child && bounds.contains_point(*position) {
|
2021-04-27 03:52:18 +00:00
|
|
|
state.clicked = true;
|
2021-04-27 17:37:14 +00:00
|
|
|
ctx.notify();
|
2021-04-27 03:52:18 +00:00
|
|
|
true
|
|
|
|
} else {
|
2021-04-27 17:37:14 +00:00
|
|
|
handled_in_child
|
2021-04-27 03:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-28 16:00:45 +00:00
|
|
|
Event::LeftMouseUp { position, .. } => {
|
2021-04-27 17:37:14 +00:00
|
|
|
if !handled_in_child && state.clicked {
|
2021-04-27 03:52:18 +00:00
|
|
|
state.clicked = false;
|
2021-04-27 17:37:14 +00:00
|
|
|
ctx.notify();
|
2021-04-28 16:00:45 +00:00
|
|
|
if let Some(handler) = click_handler {
|
|
|
|
if bounds.contains_point(*position) {
|
|
|
|
handler(ctx);
|
|
|
|
}
|
|
|
|
}
|
2021-04-27 03:52:18 +00:00
|
|
|
true
|
|
|
|
} else {
|
2021-04-27 17:37:14 +00:00
|
|
|
handled_in_child
|
2021-04-27 03:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-27 17:37:14 +00:00
|
|
|
_ => handled_in_child,
|
2021-04-27 03:52:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn debug(
|
|
|
|
&self,
|
|
|
|
_: RectF,
|
|
|
|
_: &Self::LayoutState,
|
|
|
|
_: &Self::PaintState,
|
|
|
|
ctx: &DebugContext,
|
|
|
|
) -> serde_json::Value {
|
|
|
|
json!({
|
|
|
|
"type": "MouseEventHandler",
|
|
|
|
"child": self.child.debug(ctx),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|