mouse_event_handler.rs

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