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