diff options
Diffstat (limited to 'src/selection.rs')
| -rw-r--r-- | src/selection.rs | 64 |
1 files changed, 24 insertions, 40 deletions
diff --git a/src/selection.rs b/src/selection.rs index 08af95f5..7290b8f0 100644 --- a/src/selection.rs +++ b/src/selection.rs @@ -32,9 +32,9 @@ enum Phase { /// A text selection within a terminal pane. #[derive(Debug, Clone)] -pub struct Selection { +pub struct Selection<P = PaneId> { /// Which pane the selection belongs to. - pub pane_id: PaneId, + pub pane_id: P, /// Anchor position in screen-buffer coordinates (row, col). anchor: (u32, u16), /// Current/final position in screen-buffer coordinates (row, col). @@ -43,15 +43,10 @@ pub struct Selection { phase: Phase, } -impl Selection { +impl<P> Selection<P> { /// Start a potential selection. This records the anchor but doesn't /// make anything visible yet — the user might just be clicking. - pub fn anchor( - pane_id: PaneId, - viewport_row: u16, - col: u16, - metrics: Option<ScrollMetrics>, - ) -> Self { + pub fn anchor(pane_id: P, viewport_row: u16, col: u16, metrics: Option<ScrollMetrics>) -> Self { let anchor = (absolute_row_for_viewport_row(viewport_row, metrics), col); Self { pane_id, @@ -61,29 +56,25 @@ impl Selection { } } - /// Create an active selection from an explicit viewport-row range. - pub(crate) fn range( - pane_id: PaneId, - viewport_row: u16, - start_col: u16, - end_col: u16, - metrics: Option<ScrollMetrics>, - ) -> Self { - let row = absolute_row_for_viewport_row(viewport_row, metrics); + pub(crate) fn absolute_anchor(pane_id: P, anchor: (u32, u16)) -> Self { + Self { + pane_id, + anchor, + cursor: anchor, + phase: Phase::Anchored, + } + } + + pub(crate) fn absolute_range(pane_id: P, anchor: (u32, u16), cursor: (u32, u16)) -> Self { Self { pane_id, - anchor: (row, start_col), - cursor: (row, end_col), + anchor, + cursor, phase: Phase::Dragging, } } - pub(crate) fn line_range( - pane_id: PaneId, - anchor_row: u32, - cursor_row: u32, - end_col: u16, - ) -> Self { + pub(crate) fn line_range(pane_id: P, anchor_row: u32, cursor_row: u32, end_col: u16) -> Self { let (anchor_col, cursor_col) = if anchor_row <= cursor_row { (0, end_col) } else { @@ -97,13 +88,6 @@ impl Selection { } } - pub(crate) fn absolute_row_for_viewport( - viewport_row: u16, - metrics: Option<ScrollMetrics>, - ) -> u32 { - absolute_row_for_viewport_row(viewport_row, metrics) - } - /// Convert the anchor's absolute row and pane-relative column back to /// screen coordinates. Adds the pane origin before clamping so the /// returned (screen_row, screen_col) can be compared directly against @@ -160,16 +144,12 @@ impl Selection { } /// Whether this selection was already finalized. + #[cfg(test)] pub fn is_finalized(&self) -> bool { self.phase == Phase::Done } /// Whether the user just clicked without dragging (not a selection). - pub fn was_just_click(&self) -> bool { - self.phase == Phase::Anchored - } - - /// Whether the user just clicked without dragging (not a selection). pub fn is_just_click(&self) -> bool { self.phase == Phase::Anchored } @@ -240,6 +220,10 @@ fn viewport_top_row(metrics: Option<ScrollMetrics>) -> u32 { .unwrap_or(0) as u32 } +pub(crate) fn absolute_row_for_viewport(viewport_row: u16, metrics: Option<ScrollMetrics>) -> u32 { + absolute_row_for_viewport_row(viewport_row, metrics) +} + fn absolute_row_for_viewport_row(viewport_row: u16, metrics: Option<ScrollMetrics>) -> u32 { viewport_top_row(metrics) + u32::from(viewport_row) } @@ -485,7 +469,7 @@ mod tests { #[test] fn click_without_drag() { let mut sel = Selection::anchor(PaneId::from_raw(0), 5, 10, None); - assert!(sel.was_just_click()); + assert!(sel.is_just_click()); let copied = sel.finish(); assert!(!copied); } @@ -495,7 +479,7 @@ mod tests { let mut sel = Selection::anchor(PaneId::from_raw(0), 5, 10, None); sel.drag(20, 7, Rect::new(10, 5, 80, 24), None); assert!(sel.is_visible()); - assert!(!sel.was_just_click()); + assert!(!sel.is_just_click()); let copied = sel.finish(); assert!(copied); } |
