Skip to main content

tenui_core/
selection.rs

1//! A first-class, direction-aware selection over a linear index space.
2//!
3//! Selection previously lived only as `tenui-text::Cursor`; promoting it to core lets
4//! the mouse dispatcher (drag-select), the compositor, and the text widgets share one type.
5//! Indices are opaque `usize`s (byte offsets, cell offsets, or item indices, per caller).
6
7/// An anchored selection: `anchor` is where it started, `head` is the moving end.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
9pub struct Selection {
10    pub anchor: usize,
11    pub head: usize,
12}
13
14impl Selection {
15    /// A collapsed (empty) selection at `pos`.
16    pub fn point(pos: usize) -> Self {
17        Self { anchor: pos, head: pos }
18    }
19
20    /// A selection spanning `[anchor, head]` (order-independent for range queries).
21    pub fn new(anchor: usize, head: usize) -> Self {
22        Self { anchor, head }
23    }
24
25    /// The lower bound of the selection.
26    pub fn start(&self) -> usize {
27        self.anchor.min(self.head)
28    }
29
30    /// The upper bound of the selection.
31    pub fn end(&self) -> usize {
32        self.anchor.max(self.head)
33    }
34
35    /// `(start, end)` as an ordered range.
36    pub fn range(&self) -> (usize, usize) {
37        (self.start(), self.end())
38    }
39
40    pub fn is_empty(&self) -> bool {
41        self.anchor == self.head
42    }
43
44    /// Number of indices covered.
45    pub fn len(&self) -> usize {
46        self.end() - self.start()
47    }
48
49    /// Whether the selection is anchored before the head (forward) vs after (backward).
50    pub fn is_forward(&self) -> bool {
51        self.head >= self.anchor
52    }
53
54    /// Whether `index` falls within `[start, end)`.
55    pub fn contains(&self, index: usize) -> bool {
56        index >= self.start() && index < self.end()
57    }
58
59    /// Moves the head, keeping the anchor (extends the selection).
60    pub fn extend_to(&mut self, head: usize) {
61        self.head = head;
62    }
63
64    /// Collapses the selection to a point at `pos`.
65    pub fn collapse_to(&mut self, pos: usize) {
66        self.anchor = pos;
67        self.head = pos;
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn test_range_and_direction() {
77        let fwd = Selection::new(2, 6);
78        assert_eq!(fwd.range(), (2, 6));
79        assert!(fwd.is_forward());
80        assert_eq!(fwd.len(), 4);
81
82        let back = Selection::new(6, 2);
83        assert_eq!(back.range(), (2, 6)); // order-independent
84        assert!(!back.is_forward());
85    }
86
87    #[test]
88    fn test_empty_and_contains() {
89        let p = Selection::point(3);
90        assert!(p.is_empty());
91        assert!(!p.contains(3));
92
93        let s = Selection::new(1, 4);
94        assert!(s.contains(1));
95        assert!(s.contains(3));
96        assert!(!s.contains(4)); // half-open
97    }
98
99    #[test]
100    fn test_extend_and_collapse() {
101        let mut s = Selection::point(5);
102        s.extend_to(9);
103        assert_eq!(s.range(), (5, 9));
104        s.collapse_to(2);
105        assert!(s.is_empty());
106        assert_eq!(s.head, 2);
107    }
108}