Skip to main content

rofd_dom/
selection.rs

1//! Annotation selection model (object-level).
2//!
3//! Pure data type over [`AnnotationId`](crate::AnnotationId). Lives in dom
4//! (not editor) so that `rofd-render` can consume it for handle drawing and
5//! hit-testing without depending on `rofd-editor` (which would be a reverse
6//! dependency edge: render -> editor is forbidden).
7//!
8//! v1 UI produces only `None` and `Single`; `Multi` is reserved for future
9//! Shift+click multi-select.
10
11use crate::AnnotationId;
12
13/// Which annotation(s) are currently selected.
14#[derive(Debug, Clone, PartialEq)]
15pub enum AnnotationSelection {
16    /// No selection.
17    None,
18    /// A single annotation is selected (handles drawn, draggable).
19    Single(AnnotationId),
20    /// Multiple annotations selected (v1: not produced by UI).
21    Multi(Vec<AnnotationId>),
22}
23
24impl AnnotationSelection {
25    /// Returns `true` if `id` is among the selected annotations.
26    pub fn contains(&self, id: &AnnotationId) -> bool {
27        match self {
28            AnnotationSelection::None => false,
29            AnnotationSelection::Single(s) => s == id,
30            AnnotationSelection::Multi(ids) => ids.iter().any(|i| i == id),
31        }
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn none_contains_nothing() {
41        assert!(!AnnotationSelection::None.contains(&AnnotationId::from_int(1)));
42    }
43
44    #[test]
45    fn single_contains_its_id() {
46        let id = AnnotationId::from_int(1);
47        assert!(AnnotationSelection::Single(id.clone()).contains(&id));
48        assert!(!AnnotationSelection::Single(id.clone()).contains(&AnnotationId::from_int(2)));
49    }
50
51    #[test]
52    fn multi_contains_any_listed() {
53        let a = AnnotationId::from_int(1);
54        let b = AnnotationId::from_int(2);
55        assert!(AnnotationSelection::Multi(vec![a.clone(), b.clone()]).contains(&a));
56    }
57}