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_mouse_down_out(
 92        mut self,
 93        handler: impl Fn(Vector2F, &mut EventContext) + 'static,
 94    ) -> Self {
 95        self.mouse_down_out = Some(Rc::new(handler));
 96        self
 97    }
 98
 99    pub fn on_right_mouse_down_out(
100        mut self,
101        handler: impl Fn(Vector2F, &mut EventContext) + 'static,
102    ) -> Self {
103        self.right_mouse_down_out = Some(Rc::new(handler));
104        self
105    }
106
107    pub fn on_drag(mut self, handler: impl Fn(Vector2F, &mut EventContext) + 'static) -> Self {
108        self.drag = Some(Rc::new(handler));
109        self
110    }
111
112    pub fn with_padding(mut self, padding: Padding) -> Self {
113        self.padding = padding;
114        self
115    }
116
117    fn hit_bounds(&self, bounds: RectF) -> RectF {
118        RectF::from_points(
119            bounds.origin() - vec2f(self.padding.left, self.padding.top),
120            bounds.lower_right() + vec2f(self.padding.right, self.padding.bottom),
121        )
122        .round_out()
123    }
124}
125
126impl Element for MouseEventHandler {
127    type LayoutState = ();
128    type PaintState = ();
129
130    fn layout(
131        &mut self,
132        constraint: SizeConstraint,
133        cx: &mut LayoutContext,
134    ) -> (Vector2F, Self::LayoutState) {
135        (self.child.layout(constraint, cx), ())
136    }
137
138    fn paint(
139        &mut self,
140        bounds: RectF,
141        visible_bounds: RectF,
142        _: &mut Self::LayoutState,
143        cx: &mut PaintContext,
144    ) -> Self::PaintState {
145        if let Some(style) = self.cursor_style {
146            cx.scene.push_cursor_region(CursorRegion {
147                bounds: self.hit_bounds(bounds),
148                style,
149            });
150        }
151
152        cx.scene.push_mouse_region(MouseRegion {
153            view_id: cx.current_view_id(),
154            discriminant: Some((self.tag, self.id)),
155            bounds: self.hit_bounds(bounds),
156            hover: None,
157            click: self.click.clone(),
158            mouse_down: self.mouse_down.clone(),
159            right_click: self.right_click.clone(),
160            right_mouse_down: self.right_mouse_down.clone(),
161            mouse_down_out: self.mouse_down_out.clone(),
162            right_mouse_down_out: self.right_mouse_down_out.clone(),
163            drag: self.drag.clone(),
164        });
165
166        self.child.paint(bounds.origin(), visible_bounds, cx);
167    }
168
169    fn dispatch_event(
170        &mut self,
171        event: &Event,
172        _: RectF,
173        _: RectF,
174        _: &mut Self::LayoutState,
175        _: &mut Self::PaintState,
176        cx: &mut EventContext,
177    ) -> bool {
178        self.child.dispatch_event(event, cx)
179    }
180
181    fn debug(
182        &self,
183        _: RectF,
184        _: &Self::LayoutState,
185        _: &Self::PaintState,
186        cx: &DebugContext,
187    ) -> serde_json::Value {
188        json!({
189            "type": "MouseEventHandler",
190            "child": self.child.debug(cx),
191        })
192    }
193}