Skip to main content

xrack_layout/
hover_observers.rs

1use crate::{Interaction, LayoutNode, Point};
2use xrack_runtime::{Component, CursorShape, HoverHandle, Length, Node, PointerHandle, RowKey};
3
4pub(crate) struct HoverEntry {
5    pub path: Vec<usize>,
6    pub row_key: Option<RowKey>,
7    pub handle: HoverHandle,
8    pub position: (Length, Length),
9    pub cursor_shape: CursorShape,
10}
11
12pub struct HoverDelivery {
13    path: Vec<usize>,
14    row_key: Option<RowKey>,
15    handle: HoverHandle,
16    event: &'static str,
17}
18
19impl HoverDelivery {
20    fn new(entry: &HoverEntry, event: &'static str) -> Self {
21        Self {
22            path: entry.path.clone(),
23            row_key: entry.row_key,
24            handle: entry.handle.clone(),
25            event,
26        }
27    }
28
29    pub fn apply(self, component: &mut impl Component, scene: &Node) -> bool {
30        scene.row_key_at(&self.path) == self.row_key
31            && scene
32                .at_path(&self.path)
33                .is_some_and(|node| node.hover_handle.as_ref() == Some(&self.handle))
34            && component.dispatch_at(scene, &self.path, self.event)
35    }
36}
37
38/// Resolve hover in a prepared frame and publish all state before delivering events.
39#[derive(Default)]
40pub struct HoverObservers {
41    entries: Vec<HoverEntry>,
42    touch_area: Option<PointerHandle>,
43    touch_area_cursor: CursorShape,
44}
45
46impl HoverObservers {
47    pub fn cursor_shape(&self) -> CursorShape {
48        std::iter::once(self.touch_area_cursor)
49            .chain(self.entries.iter().map(|entry| entry.cursor_shape))
50            .find(|shape| *shape != CursorShape::Auto)
51            .unwrap_or_default()
52    }
53
54    pub fn sync(
55        &mut self,
56        input: &Interaction,
57        scene: &Node,
58        layout: &LayoutNode,
59        pointer: Option<Point>,
60    ) -> Vec<HoverDelivery> {
61        let touch_area = pointer.and_then(|point| input.touch_area_hover(scene, layout, point));
62        self.touch_area_cursor =
63            touch_area.map_or(CursorShape::Auto, |(node, _)| node.cursor_shape());
64        let touch_area =
65            touch_area.and_then(|(node, position)| Some((node.pointer_handle.clone()?, position)));
66        let previous = self.touch_area.take();
67        if let Some(previous) = previous
68            && touch_area
69                .as_ref()
70                .is_none_or(|(handle, _)| handle != &previous)
71        {
72            previous.publish_hover(None);
73        }
74        self.touch_area = touch_area.map(|(handle, position)| {
75            handle.publish_hover(Some(position));
76            handle
77        });
78        let next = pointer.map_or_else(Vec::new, |point| input.hover_entries(scene, layout, point));
79        let previous = std::mem::replace(&mut self.entries, next);
80        let mut events = Vec::new();
81        for old in &previous {
82            if !self.entries.iter().any(|entry| entry.handle == old.handle) {
83                old.handle.publish(None);
84                events.push(HoverDelivery::new(old, "exited"));
85            }
86        }
87        for entry in &self.entries {
88            let old = previous.iter().find(|old| old.handle == entry.handle);
89            entry.handle.publish(Some(entry.position));
90            if old.is_none() {
91                events.push(HoverDelivery::new(entry, "entered"));
92            } else if old.is_some_and(|old| old.position != entry.position) {
93                events.push(HoverDelivery::new(entry, "moved"));
94            }
95        }
96        events
97    }
98
99    pub fn clear(&mut self) {
100        self.touch_area_cursor = CursorShape::Auto;
101        if let Some(handle) = self.touch_area.take() {
102            handle.publish_hover(None);
103        }
104        for entry in self.entries.drain(..) {
105            entry.handle.publish(None);
106        }
107    }
108}