Skip to main content

egui_pop_core/kit/
drag.rs

1//! The drag cursors a kit gives (`drag-cursor.<kind>.hover`, `.drag`,
2//! `.policy`): per kind of drag, its cursors as CSS names and when the
3//! hover one shows, over the kind's own ([`DragKind::cursors`]). The
4//! pointer profile in force and the widget itself can still change them
5//! (`docs/pointer-gestures.md`).
6//! Compiled with the kit, read as plain fields.
7
8use std::collections::BTreeMap;
9
10use egui_pop_model::pointer::{CURSOR_NAMES, DragCursor, DragKind, HoverPolicy};
11
12use super::{Tokens, string};
13
14/// The drag cursors of a kit, by kind.
15#[derive(Clone, Debug, Default, PartialEq)]
16pub struct DragCursorKit(BTreeMap<DragKind, DragCursor>);
17
18impl DragCursorKit {
19    /// A kind's cursors in the kit: what it gives over the kind's own.
20    pub fn get(&self, kind: DragKind) -> DragCursor {
21        self.0.get(&kind).cloned().unwrap_or_else(|| kind.cursors())
22    }
23
24    pub(super) fn compile(t: &Tokens, problems: &mut Vec<String>) -> DragCursorKit {
25        let mut kinds = BTreeMap::new();
26        for kind in DragKind::ALL {
27            let path = |term: &str| format!("drag-cursor.{}.{term}", kind.as_str());
28            let mut given = DragCursor::default();
29            for (term, slot) in [("hover", &mut given.hover), ("drag", &mut given.drag)] {
30                let Some(name) = string(t, &path(term)) else {
31                    continue;
32                };
33                if CURSOR_NAMES.contains(&name.as_str()) {
34                    *slot = name;
35                } else {
36                    problems.push(format!(
37                        "{}: `{name}` is not a cursor name (CSS's: `grab`, `col-resize`...)",
38                        path(term)
39                    ));
40                }
41            }
42            if let Some(asked) = string(t, &path("policy")) {
43                match HoverPolicy::parse(&asked) {
44                    Some(p) => given.policy = Some(p),
45                    None => problems.push(format!(
46                        "{}: `{asked}` is not only-action, always or never",
47                        path("policy")
48                    )),
49                }
50            }
51            let c = kind.cursors().over(&given);
52            if c != kind.cursors() {
53                kinds.insert(kind, c);
54            }
55        }
56        DragCursorKit(kinds)
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use crate::kit::{Kit, Shapes};
64
65    fn kit(json: &str) -> Kit {
66        let mut themes = egui_pop_tokens::Themes::default();
67        themes
68            .add_text("drag-test", json)
69            .expect("test theme reads");
70        let tokens = Tokens::new(themes.resolve(&egui_pop_tokens::ThemeSpec::new("drag-test")));
71        Kit::compile(&tokens, &Shapes::default())
72    }
73
74    #[test]
75    fn the_default_kit_gives_each_kind_its_own() {
76        let k = kit(r#"$extends: "pop-dark""#);
77        assert!(k.problems.is_empty(), "{:?}", k.problems);
78        for kind in DragKind::ALL {
79            assert_eq!(k.drag_cursors.get(kind), kind.cursors(), "{kind:?}");
80        }
81    }
82
83    #[test]
84    fn a_kit_changes_a_kind_and_reports_what_it_cannot_read() {
85        let k = kit(r#"
86            $extends: "pop-dark"
87            drag-cursor:
88              pan:
89                hover: {$type: "string", $value: "all-scroll"}
90                policy: {$type: "string", $value: "always"}
91              move:
92                drag: {$type: "string", $value: "spinning"}
93                policy: {$type: "string", $value: "sometimes"}
94            "#);
95        let pan = k.drag_cursors.get(DragKind::Pan);
96        assert_eq!(
97            (pan.hover.as_str(), pan.drag.as_str(), pan.policy),
98            ("all-scroll", "grabbing", Some(HoverPolicy::Always))
99        );
100        assert_eq!(k.drag_cursors.get(DragKind::Move), DragKind::Move.cursors());
101        assert_eq!(
102            k.problems,
103            [
104                "drag-cursor.move.drag: `spinning` is not a cursor name (CSS's: `grab`, `col-resize`...)",
105                "drag-cursor.move.policy: `sometimes` is not only-action, always or never",
106            ]
107        );
108    }
109}