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, MouseClickOut, MouseDown, MouseDownOut, MouseDrag,
 11        MouseHover, MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut,
 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    pub fn on_down_out(
149        mut self,
150        button: MouseButton,
151        handler: impl Fn(MouseDownOut, &mut V, &mut EventContext<V>) + 'static,
152    ) -> Self {
153        self.handlers = self.handlers.on_down_out(button, handler);
154        self
155    }
156
157    pub fn on_up_out(
158        mut self,
159        button: MouseButton,
160        handler: impl Fn(MouseUpOut, &mut V, &mut EventContext<V>) + 'static,
161    ) -> Self {
162        self.handlers = self.handlers.on_up_out(button, handler);
163        self
164    }
165
166    pub fn on_drag(
167        mut self,
168        button: MouseButton,
169        handler: impl Fn(MouseDrag, &mut V, &mut EventContext<V>) + 'static,
170    ) -> Self {
171        self.handlers = self.handlers.on_drag(button, handler);
172        self
173    }
174
175    pub fn on_hover(
176        mut self,
177        handler: impl Fn(MouseHover, &mut V, &mut EventContext<V>) + 'static,
178    ) -> Self {
179        self.handlers = self.handlers.on_hover(handler);
180        self
181    }
182
183    pub fn on_scroll(
184        mut self,
185        handler: impl Fn(MouseScrollWheel, &mut V, &mut EventContext<V>) + 'static,
186    ) -> Self {
187        self.handlers = self.handlers.on_scroll(handler);
188        self
189    }
190
191    pub fn with_hoverable(mut self, is_hoverable: bool) -> Self {
192        self.hoverable = is_hoverable;
193        self
194    }
195
196    pub fn with_padding(mut self, padding: Padding) -> Self {
197        self.padding = padding;
198        self
199    }
200
201    fn hit_bounds(&self, bounds: RectF) -> RectF {
202        RectF::from_points(
203            bounds.origin() - vec2f(self.padding.left, self.padding.top),
204            bounds.lower_right() + vec2f(self.padding.right, self.padding.bottom),
205        )
206        .round_out()
207    }
208
209    fn paint_regions(
210        &self,
211        scene: &mut SceneBuilder,
212        bounds: RectF,
213        visible_bounds: RectF,
214        cx: &mut ViewContext<V>,
215    ) {
216        let visible_bounds = visible_bounds.intersection(bounds).unwrap_or_default();
217        let hit_bounds = self.hit_bounds(visible_bounds);
218
219        if let Some(style) = self.cursor_style {
220            scene.push_cursor_region(CursorRegion {
221                bounds: hit_bounds,
222                style,
223            });
224        }
225        scene.push_mouse_region(
226            MouseRegion::from_handlers::<Tag>(
227                cx.view_id(),
228                self.region_id,
229                hit_bounds,
230                self.handlers.clone(),
231            )
232            .with_hoverable(self.hoverable)
233            .with_notify_on_hover(self.notify_on_hover)
234            .with_notify_on_click(self.notify_on_click),
235        );
236    }
237}
238
239impl<Tag, V: View> Element<V> for MouseEventHandler<Tag, V> {
240    type LayoutState = ();
241    type PaintState = ();
242
243    fn layout(
244        &mut self,
245        constraint: SizeConstraint,
246        view: &mut V,
247        cx: &mut LayoutContext<V>,
248    ) -> (Vector2F, Self::LayoutState) {
249        (self.child.layout(constraint, view, cx), ())
250    }
251
252    fn paint(
253        &mut self,
254        scene: &mut SceneBuilder,
255        bounds: RectF,
256        visible_bounds: RectF,
257        _: &mut Self::LayoutState,
258        view: &mut V,
259        cx: &mut ViewContext<V>,
260    ) -> Self::PaintState {
261        if self.above {
262            self.child
263                .paint(scene, bounds.origin(), visible_bounds, view, cx);
264
265            scene.paint_layer(None, |scene| {
266                self.paint_regions(scene, bounds, visible_bounds, cx);
267            });
268        } else {
269            self.paint_regions(scene, bounds, visible_bounds, cx);
270            self.child
271                .paint(scene, bounds.origin(), visible_bounds, view, cx);
272        }
273    }
274
275    fn rect_for_text_range(
276        &self,
277        range_utf16: Range<usize>,
278        _: RectF,
279        _: RectF,
280        _: &Self::LayoutState,
281        _: &Self::PaintState,
282        view: &V,
283        cx: &ViewContext<V>,
284    ) -> Option<RectF> {
285        self.child.rect_for_text_range(range_utf16, view, cx)
286    }
287
288    fn debug(
289        &self,
290        _: RectF,
291        _: &Self::LayoutState,
292        _: &Self::PaintState,
293        view: &V,
294        cx: &ViewContext<V>,
295    ) -> serde_json::Value {
296        json!({
297            "type": "MouseEventHandler",
298            "child": self.child.debug(view, cx),
299        })
300    }
301}