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    hovered: bool,
 16    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 = ctx.value::<Tag, _>(id);
 26        let child = state.map(ctx, |state| render_child(*state));
 27        Self { state, child }
 28    }
 29}
 30
 31impl Element for MouseEventHandler {
 32    type LayoutState = ();
 33    type PaintState = ();
 34
 35    fn layout(
 36        &mut self,
 37        constraint: SizeConstraint,
 38        ctx: &mut LayoutContext,
 39    ) -> (Vector2F, Self::LayoutState) {
 40        (self.child.layout(constraint, ctx), ())
 41    }
 42
 43    fn after_layout(
 44        &mut self,
 45        _: Vector2F,
 46        _: &mut Self::LayoutState,
 47        ctx: &mut AfterLayoutContext,
 48    ) {
 49        self.child.after_layout(ctx);
 50    }
 51
 52    fn paint(
 53        &mut self,
 54        bounds: RectF,
 55        _: &mut Self::LayoutState,
 56        ctx: &mut PaintContext,
 57    ) -> Self::PaintState {
 58        self.child.paint(bounds.origin(), ctx);
 59    }
 60
 61    fn dispatch_event(
 62        &mut self,
 63        event: &Event,
 64        bounds: RectF,
 65        _: &mut Self::LayoutState,
 66        _: &mut Self::PaintState,
 67        ctx: &mut EventContext,
 68    ) -> bool {
 69        let handled_in_child = self.child.dispatch_event(event, ctx);
 70
 71        self.state.map(ctx.app, |state| match event {
 72            Event::MouseMoved { position } => {
 73                let mouse_in = bounds.contains_point(*position);
 74                if state.hovered != mouse_in {
 75                    state.hovered = mouse_in;
 76                    ctx.notify();
 77                    true
 78                } else {
 79                    handled_in_child
 80                }
 81            }
 82            Event::LeftMouseDown { position, .. } => {
 83                if !handled_in_child && bounds.contains_point(*position) {
 84                    state.clicked = true;
 85                    ctx.notify();
 86                    true
 87                } else {
 88                    handled_in_child
 89                }
 90            }
 91            Event::LeftMouseUp { .. } => {
 92                if !handled_in_child && state.clicked {
 93                    state.clicked = false;
 94                    ctx.notify();
 95                    true
 96                } else {
 97                    handled_in_child
 98                }
 99            }
100            _ => handled_in_child,
101        })
102    }
103
104    fn debug(
105        &self,
106        _: RectF,
107        _: &Self::LayoutState,
108        _: &Self::PaintState,
109        ctx: &DebugContext,
110    ) -> serde_json::Value {
111        json!({
112            "type": "MouseEventHandler",
113            "child": self.child.debug(ctx),
114        })
115    }
116}