mouse_event_handler.rs

  1use crate::{
  2    geometry::{rect::RectF, vector::Vector2F},
  3    AfterLayoutContext, AppContext, DebugContext, Element, ElementBox, Event, EventContext,
  4    LayoutContext, PaintContext, SizeConstraint, ValueHandle,
  5};
  6use serde_json::json;
  7
  8pub struct MouseEventHandler {
  9    state: ValueHandle<MouseState>,
 10    child: ElementBox,
 11}
 12
 13#[derive(Clone, Copy, Debug, Default)]
 14pub struct MouseState {
 15    pub hovered: bool,
 16    pub clicked: bool,
 17}
 18
 19impl MouseEventHandler {
 20    pub fn new<Tag, F>(id: usize, ctx: &AppContext, render_child: F) -> Self
 21    where
 22        Tag: 'static,
 23        F: FnOnce(MouseState) -> ElementBox,
 24    {
 25        let state_handle = ctx.value::<Tag, _>(id);
 26        let state = state_handle.read(ctx, |state| *state);
 27        let child = render_child(state);
 28        Self {
 29            state: state_handle,
 30            child,
 31        }
 32    }
 33}
 34
 35impl Element for MouseEventHandler {
 36    type LayoutState = ();
 37    type PaintState = ();
 38
 39    fn layout(
 40        &mut self,
 41        constraint: SizeConstraint,
 42        ctx: &mut LayoutContext,
 43    ) -> (Vector2F, Self::LayoutState) {
 44        (self.child.layout(constraint, ctx), ())
 45    }
 46
 47    fn after_layout(
 48        &mut self,
 49        _: Vector2F,
 50        _: &mut Self::LayoutState,
 51        ctx: &mut AfterLayoutContext,
 52    ) {
 53        self.child.after_layout(ctx);
 54    }
 55
 56    fn paint(
 57        &mut self,
 58        bounds: RectF,
 59        _: &mut Self::LayoutState,
 60        ctx: &mut PaintContext,
 61    ) -> Self::PaintState {
 62        self.child.paint(bounds.origin(), ctx);
 63    }
 64
 65    fn dispatch_event(
 66        &mut self,
 67        event: &Event,
 68        bounds: RectF,
 69        _: &mut Self::LayoutState,
 70        _: &mut Self::PaintState,
 71        ctx: &mut EventContext,
 72    ) -> bool {
 73        let handled_in_child = self.child.dispatch_event(event, ctx);
 74
 75        self.state.update(ctx.app, |state| match event {
 76            Event::MouseMoved { position } => {
 77                let mouse_in = bounds.contains_point(*position);
 78                if state.hovered != mouse_in {
 79                    state.hovered = mouse_in;
 80                    ctx.notify();
 81                    true
 82                } else {
 83                    handled_in_child
 84                }
 85            }
 86            Event::LeftMouseDown { position, .. } => {
 87                if !handled_in_child && bounds.contains_point(*position) {
 88                    state.clicked = true;
 89                    ctx.notify();
 90                    true
 91                } else {
 92                    handled_in_child
 93                }
 94            }
 95            Event::LeftMouseUp { .. } => {
 96                if !handled_in_child && state.clicked {
 97                    state.clicked = false;
 98                    ctx.notify();
 99                    true
100                } else {
101                    handled_in_child
102                }
103            }
104            _ => handled_in_child,
105        })
106    }
107
108    fn debug(
109        &self,
110        _: RectF,
111        _: &Self::LayoutState,
112        _: &Self::PaintState,
113        ctx: &DebugContext,
114    ) -> serde_json::Value {
115        json!({
116            "type": "MouseEventHandler",
117            "child": self.child.debug(ctx),
118        })
119    }
120}