mouse_event_handler.rs

  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_up(
 55        mut self,
 56        button: MouseButton,
 57        handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
 58    ) -> Self {
 59        self.handlers = self.handlers.on_up(button, handler);
 60        self
 61    }
 62
 63    pub fn on_click(
 64        mut self,
 65        button: MouseButton,
 66        handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
 67    ) -> Self {
 68        self.handlers = self.handlers.on_click(button, handler);
 69        self
 70    }
 71
 72    pub fn on_down_out(
 73        mut self,
 74        button: MouseButton,
 75        handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
 76    ) -> Self {
 77        self.handlers = self.handlers.on_down_out(button, handler);
 78        self
 79    }
 80
 81    pub fn on_up_out(
 82        mut self,
 83        button: MouseButton,
 84        handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
 85    ) -> Self {
 86        self.handlers = self.handlers.on_up(button, handler);
 87        self
 88    }
 89
 90    pub fn on_drag(
 91        mut self,
 92        button: MouseButton,
 93        handler: impl Fn(Vector2F, MouseMovedEvent, &mut EventContext) + 'static,
 94    ) -> Self {
 95        self.handlers = self.handlers.on_drag(button, handler);
 96        self
 97    }
 98
 99    pub fn on_drag_over(
100        mut self,
101        button: MouseButton,
102        handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
103    ) -> Self {
104        self.handlers = self.handlers.on_drag_over(button, handler);
105        self
106    }
107
108    pub fn on_hover(
109        mut self,
110        handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
111    ) -> Self {
112        self.handlers = self.handlers.on_hover(handler);
113        self
114    }
115
116    pub fn with_padding(mut self, padding: Padding) -> Self {
117        self.padding = padding;
118        self
119    }
120
121    fn hit_bounds(&self, bounds: RectF) -> RectF {
122        RectF::from_points(
123            bounds.origin() - vec2f(self.padding.left, self.padding.top),
124            bounds.lower_right() + vec2f(self.padding.right, self.padding.bottom),
125        )
126        .round_out()
127    }
128}
129
130impl Element for MouseEventHandler {
131    type LayoutState = ();
132    type PaintState = ();
133
134    fn layout(
135        &mut self,
136        constraint: SizeConstraint,
137        cx: &mut LayoutContext,
138    ) -> (Vector2F, Self::LayoutState) {
139        (self.child.layout(constraint, cx), ())
140    }
141
142    fn paint(
143        &mut self,
144        bounds: RectF,
145        visible_bounds: RectF,
146        _: &mut Self::LayoutState,
147        cx: &mut PaintContext,
148    ) -> Self::PaintState {
149        let hit_bounds = self.hit_bounds(visible_bounds);
150        if let Some(style) = self.cursor_style {
151            cx.scene.push_cursor_region(CursorRegion {
152                bounds: hit_bounds,
153                style,
154            });
155        }
156
157        cx.scene.push_mouse_region(MouseRegion::from_handlers(
158            cx.current_view_id(),
159            Some(self.discriminant),
160            hit_bounds,
161            self.handlers.clone(),
162        ));
163
164        self.child.paint(bounds.origin(), visible_bounds, cx);
165    }
166
167    fn dispatch_event(
168        &mut self,
169        event: &Event,
170        _: RectF,
171        _: RectF,
172        _: &mut Self::LayoutState,
173        _: &mut Self::PaintState,
174        cx: &mut EventContext,
175    ) -> bool {
176        self.child.dispatch_event(event, cx)
177    }
178
179    fn rect_for_text_range(
180        &self,
181        range_utf16: Range<usize>,
182        _: RectF,
183        _: RectF,
184        _: &Self::LayoutState,
185        _: &Self::PaintState,
186        cx: &MeasurementContext,
187    ) -> Option<RectF> {
188        self.child.rect_for_text_range(range_utf16, cx)
189    }
190
191    fn debug(
192        &self,
193        _: RectF,
194        _: &Self::LayoutState,
195        _: &Self::PaintState,
196        cx: &DebugContext,
197    ) -> serde_json::Value {
198        json!({
199            "type": "MouseEventHandler",
200            "child": self.child.debug(cx),
201        })
202    }
203}