mouse_event_handler.rs

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