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