1use super::Padding;
2use crate::{
3 geometry::{
4 rect::RectF,
5 vector::{vec2f, Vector2F},
6 },
7 platform::CursorStyle,
8 scene::{CursorRegion, HandlerSet},
9 DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MeasurementContext,
10 MouseButton, MouseButtonEvent, MouseMovedEvent, MouseRegion, MouseState, PaintContext,
11 RenderContext, SizeConstraint, View,
12};
13use serde_json::json;
14use std::{any::TypeId, ops::Range};
15
16pub struct MouseEventHandler {
17 child: ElementBox,
18 discriminant: (TypeId, usize),
19 cursor_style: Option<CursorStyle>,
20 handlers: HandlerSet,
21 padding: Padding,
22}
23
24impl MouseEventHandler {
25 pub fn new<Tag, V, F>(id: usize, cx: &mut RenderContext<V>, render_child: F) -> Self
26 where
27 Tag: 'static,
28 V: View,
29 F: FnOnce(MouseState, &mut RenderContext<V>) -> ElementBox,
30 {
31 Self {
32 child: render_child(cx.mouse_state::<Tag>(id), cx),
33 cursor_style: None,
34 discriminant: (TypeId::of::<Tag>(), id),
35 handlers: Default::default(),
36 padding: Default::default(),
37 }
38 }
39
40 pub fn with_cursor_style(mut self, cursor: CursorStyle) -> Self {
41 self.cursor_style = Some(cursor);
42 self
43 }
44
45 pub fn on_down(
46 mut self,
47 button: MouseButton,
48 handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
49 ) -> Self {
50 self.handlers = self.handlers.on_down(button, handler);
51 self
52 }
53
54 pub fn on_click(
55 mut self,
56 button: MouseButton,
57 handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
58 ) -> Self {
59 self.handlers = self.handlers.on_click(button, handler);
60 self
61 }
62
63 pub fn on_down_out(
64 mut self,
65 button: MouseButton,
66 handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
67 ) -> Self {
68 self.handlers = self.handlers.on_down_out(button, handler);
69 self
70 }
71
72 pub fn on_drag(
73 mut self,
74 button: MouseButton,
75 handler: impl Fn(Vector2F, MouseMovedEvent, &mut EventContext) + 'static,
76 ) -> Self {
77 self.handlers = self.handlers.on_drag(button, handler);
78 self
79 }
80
81 pub fn on_hover(
82 mut self,
83 handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
84 ) -> Self {
85 self.handlers = self.handlers.on_hover(handler);
86 self
87 }
88
89 pub fn with_padding(mut self, padding: Padding) -> Self {
90 self.padding = padding;
91 self
92 }
93
94 fn hit_bounds(&self, bounds: RectF) -> RectF {
95 RectF::from_points(
96 bounds.origin() - vec2f(self.padding.left, self.padding.top),
97 bounds.lower_right() + vec2f(self.padding.right, self.padding.bottom),
98 )
99 .round_out()
100 }
101}
102
103impl Element for MouseEventHandler {
104 type LayoutState = ();
105 type PaintState = ();
106
107 fn layout(
108 &mut self,
109 constraint: SizeConstraint,
110 cx: &mut LayoutContext,
111 ) -> (Vector2F, Self::LayoutState) {
112 (self.child.layout(constraint, cx), ())
113 }
114
115 fn paint(
116 &mut self,
117 bounds: RectF,
118 visible_bounds: RectF,
119 _: &mut Self::LayoutState,
120 cx: &mut PaintContext,
121 ) -> Self::PaintState {
122 let hit_bounds = self.hit_bounds(visible_bounds);
123 if let Some(style) = self.cursor_style {
124 cx.scene.push_cursor_region(CursorRegion {
125 bounds: hit_bounds,
126 style,
127 });
128 }
129
130 cx.scene.push_mouse_region(MouseRegion::from_handlers(
131 cx.current_view_id(),
132 Some(self.discriminant),
133 hit_bounds,
134 self.handlers.clone(),
135 ));
136
137 self.child.paint(bounds.origin(), visible_bounds, cx);
138 }
139
140 fn dispatch_event(
141 &mut self,
142 event: &Event,
143 _: RectF,
144 _: RectF,
145 _: &mut Self::LayoutState,
146 _: &mut Self::PaintState,
147 cx: &mut EventContext,
148 ) -> bool {
149 self.child.dispatch_event(event, cx)
150 }
151
152 fn rect_for_text_range(
153 &self,
154 range_utf16: Range<usize>,
155 _: RectF,
156 _: RectF,
157 _: &Self::LayoutState,
158 _: &Self::PaintState,
159 cx: &MeasurementContext,
160 ) -> Option<RectF> {
161 self.child.rect_for_text_range(range_utf16, cx)
162 }
163
164 fn debug(
165 &self,
166 _: RectF,
167 _: &Self::LayoutState,
168 _: &Self::PaintState,
169 cx: &DebugContext,
170 ) -> serde_json::Value {
171 json!({
172 "type": "MouseEventHandler",
173 "child": self.child.debug(cx),
174 })
175 }
176}