1use std::ops::DerefMut;
2
3use crate::{
4 geometry::{rect::RectF, vector::Vector2F},
5 platform::CursorStyle,
6 CursorStyleHandle, DebugContext, Element, ElementBox, ElementStateHandle, Event, EventContext,
7 LayoutContext, MutableAppContext, PaintContext, SizeConstraint,
8};
9use serde_json::json;
10
11pub struct MouseEventHandler {
12 state: ElementStateHandle<MouseState>,
13 child: ElementBox,
14 cursor_style: Option<CursorStyle>,
15 click_handler: Option<Box<dyn FnMut(&mut EventContext)>>,
16 drag_handler: Option<Box<dyn FnMut(Vector2F, &mut EventContext)>>,
17}
18
19#[derive(Default)]
20pub struct MouseState {
21 pub hovered: bool,
22 pub clicked: bool,
23 prev_drag_position: Option<Vector2F>,
24 cursor_style_handle: Option<CursorStyleHandle>,
25}
26
27impl MouseEventHandler {
28 pub fn new<Tag, F, C>(id: usize, cx: &mut C, render_child: F) -> Self
29 where
30 Tag: 'static,
31 F: FnOnce(&MouseState, &mut C) -> ElementBox,
32 C: DerefMut<Target = MutableAppContext>,
33 {
34 let state_handle = cx.element_state::<Tag, _>(id);
35 let child = state_handle.update(cx, |state, cx| render_child(state, cx));
36 Self {
37 state: state_handle,
38 child,
39 cursor_style: None,
40 click_handler: None,
41 drag_handler: None,
42 }
43 }
44
45 pub fn with_cursor_style(mut self, cursor: CursorStyle) -> Self {
46 self.cursor_style = Some(cursor);
47 self
48 }
49
50 pub fn on_click(mut self, handler: impl FnMut(&mut EventContext) + 'static) -> Self {
51 self.click_handler = Some(Box::new(handler));
52 self
53 }
54
55 pub fn on_drag(mut self, handler: impl FnMut(Vector2F, &mut EventContext) + 'static) -> Self {
56 self.drag_handler = Some(Box::new(handler));
57 self
58 }
59}
60
61impl Element for MouseEventHandler {
62 type LayoutState = ();
63 type PaintState = ();
64
65 fn layout(
66 &mut self,
67 constraint: SizeConstraint,
68 cx: &mut LayoutContext,
69 ) -> (Vector2F, Self::LayoutState) {
70 (self.child.layout(constraint, cx), ())
71 }
72
73 fn paint(
74 &mut self,
75 bounds: RectF,
76 _: &mut Self::LayoutState,
77 cx: &mut PaintContext,
78 ) -> Self::PaintState {
79 self.child.paint(bounds.origin(), cx);
80 }
81
82 fn dispatch_event(
83 &mut self,
84 event: &Event,
85 bounds: RectF,
86 _: &mut Self::LayoutState,
87 _: &mut Self::PaintState,
88 cx: &mut EventContext,
89 ) -> bool {
90 let cursor_style = self.cursor_style;
91 let click_handler = self.click_handler.as_mut();
92 let drag_handler = self.drag_handler.as_mut();
93
94 let handled_in_child = self.child.dispatch_event(event, cx);
95
96 self.state.update(cx, |state, cx| match event {
97 Event::MouseMoved { position } => {
98 let mouse_in = bounds.contains_point(*position);
99 if state.hovered != mouse_in {
100 state.hovered = mouse_in;
101 if let Some(cursor_style) = cursor_style {
102 if !state.clicked {
103 if state.hovered {
104 state.cursor_style_handle = Some(cx.set_cursor_style(cursor_style));
105 } else {
106 state.cursor_style_handle = None;
107 }
108 }
109 }
110 cx.notify();
111 true
112 } else {
113 handled_in_child
114 }
115 }
116 Event::LeftMouseDown { position, .. } => {
117 if !handled_in_child && bounds.contains_point(*position) {
118 state.clicked = true;
119 state.prev_drag_position = Some(*position);
120 cx.notify();
121 true
122 } else {
123 handled_in_child
124 }
125 }
126 Event::LeftMouseUp { position, .. } => {
127 state.prev_drag_position = None;
128 if !handled_in_child && state.clicked {
129 state.clicked = false;
130 if !state.hovered {
131 state.cursor_style_handle = None;
132 }
133 cx.notify();
134 if let Some(handler) = click_handler {
135 if bounds.contains_point(*position) {
136 handler(cx);
137 }
138 }
139 true
140 } else {
141 handled_in_child
142 }
143 }
144 Event::LeftMouseDragged { position, .. } => {
145 if !handled_in_child && state.clicked {
146 let prev_drag_position = state.prev_drag_position.replace(*position);
147 if let Some((handler, prev_position)) = drag_handler.zip(prev_drag_position) {
148 let delta = *position - prev_position;
149 if !delta.is_zero() {
150 (handler)(delta, cx);
151 }
152 }
153 true
154 } else {
155 handled_in_child
156 }
157 }
158 _ => handled_in_child,
159 })
160 }
161
162 fn debug(
163 &self,
164 _: RectF,
165 _: &Self::LayoutState,
166 _: &Self::PaintState,
167 cx: &DebugContext,
168 ) -> serde_json::Value {
169 json!({
170 "type": "MouseEventHandler",
171 "child": self.child.debug(cx),
172 })
173 }
174}