mouse_event_handler.rs

  1use super::Padding;
  2use crate::{
  3    geometry::{
  4        rect::RectF,
  5        vector::{vec2f, Vector2F},
  6    },
  7    platform::CursorStyle,
  8    platform::MouseButton,
  9    scene::{
 10        CursorRegion, HandlerSet, MouseClick, MouseDown, MouseDownOut, MouseDrag, MouseHover,
 11        MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut, MouseClickOut,
 12    },
 13    AnyElement, Element, EventContext, LayoutContext, MouseRegion, MouseState, SceneBuilder,
 14    SizeConstraint, View, ViewContext,
 15};
 16use serde_json::json;
 17use std::{marker::PhantomData, ops::Range};
 18
 19pub struct MouseEventHandler<Tag: 'static, V: View> {
 20    child: AnyElement<V>,
 21    region_id: usize,
 22    cursor_style: Option<CursorStyle>,
 23    handlers: HandlerSet,
 24    hoverable: bool,
 25    notify_on_hover: bool,
 26    notify_on_click: bool,
 27    above: bool,
 28    padding: Padding,
 29    _tag: PhantomData<Tag>,
 30}
 31
 32/// Element which provides a render_child callback with a MouseState and paints a mouse
 33/// region under (or above) it for easy mouse event handling.
 34impl<Tag, V: View> MouseEventHandler<Tag, V> {
 35    pub fn for_child(child: impl Element<V>, region_id: usize) -> Self {
 36        Self {
 37            child: child.into_any(),
 38            region_id,
 39            cursor_style: None,
 40            handlers: Default::default(),
 41            notify_on_hover: false,
 42            notify_on_click: false,
 43            hoverable: false,
 44            above: false,
 45            padding: Default::default(),
 46            _tag: PhantomData,
 47        }
 48    }
 49
 50    pub fn new<E, F>(region_id: usize, cx: &mut ViewContext<V>, render_child: F) -> Self
 51    where
 52        E: Element<V>,
 53        F: FnOnce(&mut MouseState, &mut ViewContext<V>) -> E,
 54    {
 55        let mut mouse_state = cx.mouse_state::<Tag>(region_id);
 56        let child = render_child(&mut mouse_state, cx).into_any();
 57        let notify_on_hover = mouse_state.accessed_hovered();
 58        let notify_on_click = mouse_state.accessed_clicked();
 59        Self {
 60            child,
 61            region_id,
 62            cursor_style: None,
 63            handlers: Default::default(),
 64            notify_on_hover,
 65            notify_on_click,
 66            hoverable: true,
 67            above: false,
 68            padding: Default::default(),
 69            _tag: PhantomData,
 70        }
 71    }
 72
 73    /// Modifies the MouseEventHandler to render the MouseRegion above the child element. Useful
 74    /// for drag and drop handling and similar events which should be captured before the child
 75    /// gets the opportunity
 76    pub fn above<D, F>(region_id: usize, cx: &mut ViewContext<V>, render_child: F) -> Self
 77    where
 78        D: Element<V>,
 79        F: FnOnce(&mut MouseState, &mut ViewContext<V>) -> D,
 80    {
 81        let mut handler = Self::new(region_id, cx, render_child);
 82        handler.above = true;
 83        handler
 84    }
 85
 86    pub fn with_cursor_style(mut self, cursor: CursorStyle) -> Self {
 87        self.cursor_style = Some(cursor);
 88        self
 89    }
 90
 91    pub fn capture_all(mut self) -> Self {
 92        self.handlers = HandlerSet::capture_all();
 93        self
 94    }
 95
 96    pub fn on_move(
 97        mut self,
 98        handler: impl Fn(MouseMove, &mut V, &mut EventContext<V>) + 'static,
 99    ) -> Self {
100        self.handlers = self.handlers.on_move(handler);
101        self
102    }
103
104    pub fn on_move_out(
105        mut self,
106        handler: impl Fn(MouseMoveOut, &mut V, &mut EventContext<V>) + 'static,
107    ) -> Self {
108        self.handlers = self.handlers.on_move_out(handler);
109        self
110    }
111
112    pub fn on_down(
113        mut self,
114        button: MouseButton,
115        handler: impl Fn(MouseDown, &mut V, &mut EventContext<V>) + 'static,
116    ) -> Self {
117        self.handlers = self.handlers.on_down(button, handler);
118        self
119    }
120
121    pub fn on_up(
122        mut self,
123        button: MouseButton,
124        handler: impl Fn(MouseUp, &mut V, &mut EventContext<V>) + 'static,
125    ) -> Self {
126        self.handlers = self.handlers.on_up(button, handler);
127        self
128    }
129
130    pub fn on_click(
131        mut self,
132        button: MouseButton,
133        handler: impl Fn(MouseClick, &mut V, &mut EventContext<V>) + 'static,
134    ) -> Self {
135        self.handlers = self.handlers.on_click(button, handler);
136        self
137    }
138
139    pub fn on_click_out(
140        mut self,
141        button: MouseButton,
142        handler: impl Fn(MouseClickOut, &mut V, &mut EventContext<V>) + 'static,
143    ) -> Self {
144        self.handlers = self.handlers.on_click_out(button, handler);
145        self
146    }
147
148
149    pub fn on_down_out(
150        mut self,
151        button: MouseButton,
152        handler: impl Fn(MouseDownOut, &mut V, &mut EventContext<V>) + 'static,
153    ) -> Self {
154        self.handlers = self.handlers.on_down_out(button, handler);
155        self
156    }
157
158    pub fn on_up_out(
159        mut self,
160        button: MouseButton,
161        handler: impl Fn(MouseUpOut, &mut V, &mut EventContext<V>) + 'static,
162    ) -> Self {
163        self.handlers = self.handlers.on_up_out(button, handler);
164        self
165    }
166
167    pub fn on_drag(
168        mut self,
169        button: MouseButton,
170        handler: impl Fn(MouseDrag, &mut V, &mut EventContext<V>) + 'static,
171    ) -> Self {
172        self.handlers = self.handlers.on_drag(button, handler);
173        self
174    }
175
176    pub fn on_hover(
177        mut self,
178        handler: impl Fn(MouseHover, &mut V, &mut EventContext<V>) + 'static,
179    ) -> Self {
180        self.handlers = self.handlers.on_hover(handler);
181        self
182    }
183
184    pub fn on_scroll(
185        mut self,
186        handler: impl Fn(MouseScrollWheel, &mut V, &mut EventContext<V>) + 'static,
187    ) -> Self {
188        self.handlers = self.handlers.on_scroll(handler);
189        self
190    }
191
192    pub fn with_hoverable(mut self, is_hoverable: bool) -> Self {
193        self.hoverable = is_hoverable;
194        self
195    }
196
197    pub fn with_padding(mut self, padding: Padding) -> Self {
198        self.padding = padding;
199        self
200    }
201
202    fn hit_bounds(&self, bounds: RectF) -> RectF {
203        RectF::from_points(
204            bounds.origin() - vec2f(self.padding.left, self.padding.top),
205            bounds.lower_right() + vec2f(self.padding.right, self.padding.bottom),
206        )
207        .round_out()
208    }
209
210    fn paint_regions(
211        &self,
212        scene: &mut SceneBuilder,
213        bounds: RectF,
214        visible_bounds: RectF,
215        cx: &mut ViewContext<V>,
216    ) {
217        let visible_bounds = visible_bounds.intersection(bounds).unwrap_or_default();
218        let hit_bounds = self.hit_bounds(visible_bounds);
219
220        if let Some(style) = self.cursor_style {
221            scene.push_cursor_region(CursorRegion {
222                bounds: hit_bounds,
223                style,
224            });
225        }
226        scene.push_mouse_region(
227            MouseRegion::from_handlers::<Tag>(
228                cx.view_id(),
229                self.region_id,
230                hit_bounds,
231                self.handlers.clone(),
232            )
233            .with_hoverable(self.hoverable)
234            .with_notify_on_hover(self.notify_on_hover)
235            .with_notify_on_click(self.notify_on_click),
236        );
237    }
238}
239
240impl<Tag, V: View> Element<V> for MouseEventHandler<Tag, V> {
241    type LayoutState = ();
242    type PaintState = ();
243
244    fn layout(
245        &mut self,
246        constraint: SizeConstraint,
247        view: &mut V,
248        cx: &mut LayoutContext<V>,
249    ) -> (Vector2F, Self::LayoutState) {
250        (self.child.layout(constraint, view, cx), ())
251    }
252
253    fn paint(
254        &mut self,
255        scene: &mut SceneBuilder,
256        bounds: RectF,
257        visible_bounds: RectF,
258        _: &mut Self::LayoutState,
259        view: &mut V,
260        cx: &mut ViewContext<V>,
261    ) -> Self::PaintState {
262        if self.above {
263            self.child
264                .paint(scene, bounds.origin(), visible_bounds, view, cx);
265
266            scene.paint_layer(None, |scene| {
267                self.paint_regions(scene, bounds, visible_bounds, cx);
268            });
269        } else {
270            self.paint_regions(scene, bounds, visible_bounds, cx);
271            self.child
272                .paint(scene, bounds.origin(), visible_bounds, view, cx);
273        }
274    }
275
276    fn rect_for_text_range(
277        &self,
278        range_utf16: Range<usize>,
279        _: RectF,
280        _: RectF,
281        _: &Self::LayoutState,
282        _: &Self::PaintState,
283        view: &V,
284        cx: &ViewContext<V>,
285    ) -> Option<RectF> {
286        self.child.rect_for_text_range(range_utf16, view, cx)
287    }
288
289    fn debug(
290        &self,
291        _: RectF,
292        _: &Self::LayoutState,
293        _: &Self::PaintState,
294        view: &V,
295        cx: &ViewContext<V>,
296    ) -> serde_json::Value {
297        json!({
298            "type": "MouseEventHandler",
299            "child": self.child.debug(view, cx),
300        })
301    }
302}