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, 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        self.state.map(ctx.app, |state| match event {
 70            Event::MouseMoved { position } => {
 71                let mouse_in = bounds.contains_point(*position);
 72                if state.hovered != mouse_in {
 73                    state.hovered = mouse_in;
 74                    // ctx.notify();
 75                    true
 76                } else {
 77                    false
 78                }
 79            }
 80            Event::LeftMouseDown { position, .. } => {
 81                if bounds.contains_point(*position) {
 82                    state.clicked = true;
 83                    // ctx.notify();
 84                    true
 85                } else {
 86                    false
 87                }
 88            }
 89            Event::LeftMouseUp { .. } => {
 90                if state.clicked {
 91                    state.clicked = false;
 92                    // ctx.notify();
 93                    true
 94                } else {
 95                    false
 96                }
 97            }
 98            _ => false,
 99        })
100    }
101
102    fn debug(
103        &self,
104        _: RectF,
105        _: &Self::LayoutState,
106        _: &Self::PaintState,
107        ctx: &DebugContext,
108    ) -> serde_json::Value {
109        json!({
110            "type": "MouseEventHandler",
111            "child": self.child.debug(ctx),
112        })
113    }
114}