ratatui_toolkit/widgets/markdown_widget/state/selection_state/methods/
get_selection.rs

1//! Get the current selection bounds.
2
3use crate::widgets::markdown_widget::foundation::types::SelectionPos;
4use crate::widgets::markdown_widget::state::selection_state::SelectionState;
5
6impl SelectionState {
7    /// Get the normalized selection bounds (start, end) where start <= end.
8    ///
9    /// # Returns
10    ///
11    /// `Some((start, end))` if there's an active selection, `None` otherwise.
12    pub fn get_selection(&self) -> Option<(SelectionPos, SelectionPos)> {
13        if !self.active {
14            return None;
15        }
16
17        let anchor = self.anchor?;
18        let cursor = self.cursor?;
19
20        // Normalize so start is before end
21        Some(normalize_selection(anchor, cursor))
22    }
23
24    /// Check if there's an active selection (anchor and cursor set).
25    pub fn has_selection(&self) -> bool {
26        self.active && self.anchor.is_some() && self.cursor.is_some()
27    }
28}
29
30/// Normalize selection bounds so start <= end.
31fn normalize_selection(a: SelectionPos, b: SelectionPos) -> (SelectionPos, SelectionPos) {
32    if a.y < b.y || (a.y == b.y && a.x <= b.x) {
33        (a, b)
34    } else {
35        (b, a)
36    }
37}