返回 CodeWhale
selection.rs
根目录 / crates / tui / src / tui / selection.rs
1 //! Text selection state for the transcript view.
2
3 use std::time::Instant;
4
5 // === Types ===
6
7 /// A selection endpoint in the transcript (line/column).
8 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
9 pub struct TranscriptSelectionPoint {
10 pub line_index: usize,
11 pub column: usize,
12 }
13
14 /// Current selection state in the transcript view.
15 #[derive(Debug, Clone, Copy, Default)]
16 pub struct TranscriptSelection {
17 pub anchor: Option<TranscriptSelectionPoint>,
18 pub head: Option<TranscriptSelectionPoint>,
19 pub dragging: bool,
20 pub(crate) owner: Option<crate::tui::history::TranscriptActionOwner>,
21 }
22
23 /// Drag-past-edge auto-scroll state. While the user holds the left button
24 /// and the cursor is above or below the transcript rect, the main loop
25 /// advances `pending_scroll_delta` and extends the selection head on a
26 /// fixed cadence so a long passage can be selected in one drag (#1163).
27 #[derive(Debug, Clone, Copy)]
28 pub struct SelectionAutoscroll {
29 /// `-1` scrolls up, `+1` scrolls down. Never `0`.
30 pub direction: i32,
31 /// Last in-bounds mouse column, in absolute terminal coordinates.
32 pub column: u16,
33 /// When the next tick is allowed to fire.
34 pub next_tick: Instant,
35 }
36
37 impl TranscriptSelection {
38 /// Clear any active selection.
39 pub fn clear(&mut self) {
40 self.anchor = None;
41 self.head = None;
42 self.dragging = false;
43 self.owner = None;
44 }
45
46 /// Whether a full selection is active.
47 #[must_use]
48 pub fn is_active(&self) -> bool {
49 self.anchor.is_some() && self.head.is_some()
50 }
51
52 /// Return selection endpoints ordered from start to end.
53 #[must_use]
54 pub fn ordered_endpoints(
55 &self,
56 ) -> Option<(TranscriptSelectionPoint, TranscriptSelectionPoint)> {
57 let anchor = self.anchor?;
58 let head = self.head?;
59 if (head.line_index, head.column) < (anchor.line_index, anchor.column) {
60 Some((head, anchor))
61 } else {
62 Some((anchor, head))
63 }
64 }
65 }
66
66 lines RUST