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