Skip to main content

epaint/text/
cursor.rs

1//! Different types of text cursors, i.e. ways to point into a [`super::Galley`].
2
3use super::index::CharIndex;
4
5/// Character cursor.
6///
7/// The default cursor is zero.
8#[derive(Clone, Copy, Debug, Default)]
9#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
10pub struct CCursor {
11    /// Character offset (NOT byte offset!).
12    pub index: CharIndex,
13
14    /// If this cursors sits right at the border of a wrapped row break (NOT paragraph break)
15    /// do we prefer the next row?
16    /// This is *almost* always what you want, *except* for when
17    /// explicitly clicking the end of a row or pressing the end key.
18    pub prefer_next_row: bool,
19}
20
21impl CCursor {
22    #[inline]
23    pub fn new(index: impl Into<CharIndex>) -> Self {
24        Self {
25            index: index.into(),
26            prefer_next_row: false,
27        }
28    }
29}
30
31/// Two `CCursor`s are considered equal if they refer to the same character boundary,
32/// even if one prefers the start of the next row.
33impl PartialEq for CCursor {
34    #[inline]
35    fn eq(&self, other: &Self) -> bool {
36        self.index == other.index
37    }
38}
39
40impl std::ops::Add<usize> for CCursor {
41    type Output = Self;
42
43    fn add(self, rhs: usize) -> Self::Output {
44        Self {
45            index: self.index.saturating_add(rhs),
46            prefer_next_row: self.prefer_next_row,
47        }
48    }
49}
50
51impl std::ops::Add<CharIndex> for CCursor {
52    type Output = Self;
53
54    fn add(self, rhs: CharIndex) -> Self::Output {
55        Self {
56            index: self.index + rhs,
57            prefer_next_row: self.prefer_next_row,
58        }
59    }
60}
61
62impl std::ops::Sub<usize> for CCursor {
63    type Output = Self;
64
65    fn sub(self, rhs: usize) -> Self::Output {
66        Self {
67            index: self.index.saturating_sub(rhs),
68            prefer_next_row: self.prefer_next_row,
69        }
70    }
71}
72
73impl std::ops::Sub<CharIndex> for CCursor {
74    type Output = Self;
75
76    fn sub(self, rhs: CharIndex) -> Self::Output {
77        Self {
78            index: self.index - rhs,
79            prefer_next_row: self.prefer_next_row,
80        }
81    }
82}
83
84impl std::ops::AddAssign<usize> for CCursor {
85    fn add_assign(&mut self, rhs: usize) {
86        self.index = self.index.saturating_add(rhs);
87    }
88}
89
90impl std::ops::SubAssign<usize> for CCursor {
91    fn sub_assign(&mut self, rhs: usize) {
92        self.index = self.index.saturating_sub(rhs);
93    }
94}
95
96/// Row/column cursor.
97///
98/// This refers to rows and columns in layout terms--text wrapping creates multiple rows.
99#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
100#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
101pub struct LayoutCursor {
102    /// 0 is first row, and so on.
103    /// Note that a single paragraph can span multiple rows.
104    /// (a paragraph is text separated by `\n`).
105    pub row: usize,
106
107    /// Character based (NOT bytes).
108    /// It is fine if this points to something beyond the end of the current row.
109    /// When moving up/down it may again be within the next row.
110    pub column: CharIndex,
111}