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