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 {
98 position,
99 left_mouse_down,
100 } => {
101 if !left_mouse_down {
102 let mouse_in = bounds.contains_point(*position);
103 if state.hovered != mouse_in {
104 state.hovered = mouse_in;
105 if let Some(cursor_style) = cursor_style {
106 if !state.clicked {
107 if state.hovered {
108 state.cursor_style_handle =
109 Some(cx.set_cursor_style(cursor_style));
110 } else {
111 state.cursor_style_handle = None;
112 }
113 }
114 }
115 cx.notify();
116 return true;
117 }
118 }
119 handled_in_child
120 }
121 Event::LeftMouseDown { position, .. } => {
122 if !handled_in_child && bounds.contains_point(*position) {
123 state.clicked = true;
124 state.prev_drag_position = Some(*position);
125 cx.notify();
126 true
127 } else {
128 handled_in_child
129 }
130 }
131 Event::LeftMouseUp { position, .. } => {
132 state.prev_drag_position = None;
133 if !handled_in_child && state.clicked {
134 state.clicked = false;
135 if !state.hovered {
136 state.cursor_style_handle = None;
137 }
138 cx.notify();
139 if let Some(handler) = click_handler {
140 if bounds.contains_point(*position) {
141 handler(cx);
142 }
143 }
144 true
145 } else {
146 handled_in_child
147 }
148 }
149 Event::LeftMouseDragged { position, .. } => {
150 if !handled_in_child && state.clicked {
151 let prev_drag_position = state.prev_drag_position.replace(*position);
152 if let Some((handler, prev_position)) = drag_handler.zip(prev_drag_position) {
153 let delta = *position - prev_position;
154 if !delta.is_zero() {
155 (handler)(delta, cx);
156 }
157 }
158 true
159 } else {
160 handled_in_child
161 }
162 }
163 _ => handled_in_child,
164 })
165 }
166
167 fn debug(
168 &self,
169 _: RectF,
170 _: &Self::LayoutState,
171 _: &Self::PaintState,
172 cx: &DebugContext,
173 ) -> serde_json::Value {
174 json!({
175 "type": "MouseEventHandler",
176 "child": self.child.debug(cx),
177 })
178 }
179}