Skip to main content

xrack_runtime/
hover.rs

1use crate::{Length, Property};
2use std::{fmt, rc::Rc};
3
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub enum CursorShape {
6    #[default]
7    Auto,
8    Default,
9    Pointer,
10    Text,
11    Crosshair,
12    Move,
13    Grab,
14    Grabbing,
15    NotAllowed,
16    Wait,
17    Help,
18    ResizeHorizontal,
19    ResizeVertical,
20    ZoomIn,
21    ZoomOut,
22}
23
24#[derive(Default)]
25struct HoverSignals {
26    hovered: Property<bool>,
27    x: Property<Length>,
28    y: Property<Length>,
29}
30
31/// Reactive hover state for one instance of a handler.
32#[doc(hidden)]
33#[derive(Clone, Default)]
34pub struct HoverHandle(Rc<HoverSignals>);
35
36impl fmt::Debug for HoverHandle {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        f.debug_struct("HoverHandle").finish_non_exhaustive()
39    }
40}
41
42impl PartialEq for HoverHandle {
43    fn eq(&self, other: &Self) -> bool {
44        Rc::ptr_eq(&self.0, &other.0)
45    }
46}
47
48impl HoverHandle {
49    pub fn hovered(&self) -> bool {
50        self.0.hovered.get(|| false)
51    }
52
53    pub fn hover_x(&self) -> Length {
54        self.0.x.get(Length::default)
55    }
56
57    pub fn hover_y(&self) -> Length {
58        self.0.y.get(Length::default)
59    }
60
61    pub fn publish(&self, position: Option<(Length, Length)>) {
62        let (x, y) = position.unwrap_or_default();
63        self.0.x.set_if_changed(x);
64        self.0.y.set_if_changed(y);
65        self.0.hovered.set_if_changed(position.is_some());
66    }
67}