2021-08-27 16:04:21 +00:00
|
|
|
use std::ops::DerefMut;
|
|
|
|
|
2021-04-27 03:52:18 +00:00
|
|
|
use crate::{
|
|
|
|
geometry::{rect::RectF, vector::Vector2F},
|
2021-08-27 17:01:49 +00:00
|
|
|
platform::CursorStyle,
|
|
|
|
CursorStyleHandle, DebugContext, Element, ElementBox, ElementStateHandle, Event, EventContext,
|
|
|
|
LayoutContext, MutableAppContext, PaintContext, SizeConstraint,
|
2021-04-27 03:52:18 +00:00
|
|
|
};
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
pub struct MouseEventHandler {
|
2021-08-27 16:04:21 +00:00
|
|
|
state: ElementStateHandle<MouseState>,
|
2021-04-27 03:52:18 +00:00
|
|
|
child: ElementBox,
|
2021-08-27 17:01:49 +00:00
|
|
|
cursor_style: Option<CursorStyle>,
|
2021-04-28 16:00:45 +00:00
|
|
|
click_handler: Option<Box<dyn FnMut(&mut EventContext)>>,
|
2021-08-27 12:30:08 +00:00
|
|
|
drag_handler: Option<Box<dyn FnMut(Vector2F, &mut EventContext)>>,
|
2021-04-27 03:52:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 17:01:49 +00:00
|
|
|
#[derive(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-08-27 12:30:08 +00:00
|
|
|
prev_drag_position: Option<Vector2F>,
|
2021-08-27 17:01:49 +00:00
|
|
|
cursor_style_handle: Option<CursorStyleHandle>,
|
2021-04-27 03:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MouseEventHandler {
|
2021-08-27 16:04:21 +00:00
|
|
|
pub fn new<Tag, F, C>(id: usize, cx: &mut C, render_child: F) -> Self
|
2021-04-27 15:23:44 +00:00
|
|
|
where
|
|
|
|
Tag: 'static,
|
2021-08-27 16:04:21 +00:00
|
|
|
F: FnOnce(&MouseState, &mut C) -> ElementBox,
|
|
|
|
C: DerefMut<Target = MutableAppContext>,
|
2021-04-27 15:23:44 +00:00
|
|
|
{
|
2021-08-27 16:04:21 +00:00
|
|
|
let state_handle = cx.element_state::<Tag, _>(id);
|
|
|
|
let child = state_handle.update(cx, |state, cx| render_child(state, cx));
|
2021-04-28 00:35:24 +00:00
|
|
|
Self {
|
|
|
|
state: state_handle,
|
|
|
|
child,
|
2021-08-27 17:01:49 +00:00
|
|
|
cursor_style: None,
|
2021-04-28 16:00:45 +00:00
|
|
|
click_handler: None,
|
2021-08-27 12:30:08 +00:00
|
|
|
drag_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
|
|
|
|
2021-08-27 17:01:49 +00:00
|
|
|
pub fn with_cursor_style(mut self, cursor: CursorStyle) -> Self {
|
|
|
|
self.cursor_style = Some(cursor);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
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-08-27 12:30:08 +00:00
|
|
|
|
|
|
|
pub fn on_drag(mut self, handler: impl FnMut(Vector2F, &mut EventContext) + 'static) -> Self {
|
|
|
|
self.drag_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,
|
2021-05-28 22:25:15 +00:00
|
|
|
cx: &mut LayoutContext,
|
2021-04-27 03:52:18 +00:00
|
|
|
) -> (Vector2F, Self::LayoutState) {
|
2021-05-28 22:25:15 +00:00
|
|
|
(self.child.layout(constraint, cx), ())
|
2021-04-27 03:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn paint(
|
|
|
|
&mut self,
|
|
|
|
bounds: RectF,
|
|
|
|
_: &mut Self::LayoutState,
|
2021-05-28 22:25:15 +00:00
|
|
|
cx: &mut PaintContext,
|
2021-04-27 03:52:18 +00:00
|
|
|
) -> Self::PaintState {
|
2021-05-28 22:25:15 +00:00
|
|
|
self.child.paint(bounds.origin(), cx);
|
2021-04-27 03:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn dispatch_event(
|
|
|
|
&mut self,
|
|
|
|
event: &Event,
|
|
|
|
bounds: RectF,
|
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
_: &mut Self::PaintState,
|
2021-05-28 22:25:15 +00:00
|
|
|
cx: &mut EventContext,
|
2021-04-27 03:52:18 +00:00
|
|
|
) -> bool {
|
2021-08-27 17:01:49 +00:00
|
|
|
let cursor_style = self.cursor_style;
|
2021-04-28 16:00:45 +00:00
|
|
|
let click_handler = self.click_handler.as_mut();
|
2021-08-27 12:30:08 +00:00
|
|
|
let drag_handler = self.drag_handler.as_mut();
|
2021-04-28 16:00:45 +00:00
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
let handled_in_child = self.child.dispatch_event(event, cx);
|
2021-04-27 17:37:14 +00:00
|
|
|
|
2021-07-23 16:52:08 +00:00
|
|
|
self.state.update(cx, |state, cx| 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-08-27 17:01:49 +00:00
|
|
|
if let Some(cursor_style) = cursor_style {
|
|
|
|
if !state.clicked {
|
|
|
|
if state.hovered {
|
|
|
|
state.cursor_style_handle = Some(cx.set_cursor_style(cursor_style));
|
|
|
|
} else {
|
|
|
|
state.cursor_style_handle = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-28 22:25:15 +00:00
|
|
|
cx.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-08-27 12:30:08 +00:00
|
|
|
state.prev_drag_position = Some(*position);
|
2021-05-28 22:25:15 +00:00
|
|
|
cx.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-08-27 12:30:08 +00:00
|
|
|
state.prev_drag_position = None;
|
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-08-27 17:01:49 +00:00
|
|
|
if !state.hovered {
|
|
|
|
state.cursor_style_handle = None;
|
|
|
|
}
|
2021-05-28 22:25:15 +00:00
|
|
|
cx.notify();
|
2021-04-28 16:00:45 +00:00
|
|
|
if let Some(handler) = click_handler {
|
|
|
|
if bounds.contains_point(*position) {
|
2021-05-28 22:25:15 +00:00
|
|
|
handler(cx);
|
2021-04-28 16:00:45 +00:00
|
|
|
}
|
|
|
|
}
|
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-08-27 12:30:08 +00:00
|
|
|
Event::LeftMouseDragged { position, .. } => {
|
|
|
|
if !handled_in_child && state.clicked {
|
|
|
|
let prev_drag_position = state.prev_drag_position.replace(*position);
|
|
|
|
if let Some((handler, prev_position)) = drag_handler.zip(prev_drag_position) {
|
|
|
|
let delta = *position - prev_position;
|
|
|
|
if !delta.is_zero() {
|
|
|
|
(handler)(delta, cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
handled_in_child
|
|
|
|
}
|
|
|
|
}
|
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,
|
2021-05-28 22:25:15 +00:00
|
|
|
cx: &DebugContext,
|
2021-04-27 03:52:18 +00:00
|
|
|
) -> serde_json::Value {
|
|
|
|
json!({
|
|
|
|
"type": "MouseEventHandler",
|
2021-05-28 22:25:15 +00:00
|
|
|
"child": self.child.debug(cx),
|
2021-04-27 03:52:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|