Nacre/vendor/epaint/src/text/cursor.rs
astar76 a97abc1711 S003: first launch on Xvfb, smoke harness, epaint glyph panic and quit segfault fixed
- tools/smoke.sh + tests/smoke/{launch,https,restore}.txt: hidden Xvfb run with a throwaway home
- vendor/epaint: render glyphs single-threaded (Servo enables vello_cpu/multithreading), D-006
- App::quit drops every Servo handle before the GL contexts, so Servo shuts down cleanly
- D-005: smoke runs use Xvfb

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-11 18:01:15 -06:00

124 lines
3.5 KiB
Rust

//! Different types of text cursors, i.e. ways to point into a [`super::Galley`].
use super::index::CharIndex;
/// Character cursor.
///
/// The default cursor is zero.
#[derive(Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct CCursor {
/// Character offset (NOT byte offset!).
pub index: CharIndex,
/// If this cursors sits right at the border of a wrapped row break (NOT paragraph break)
/// do we prefer the next row?
/// This is *almost* always what you want, *except* for when
/// explicitly clicking the end of a row or pressing the end key.
pub prefer_next_row: bool,
}
impl CCursor {
/// A cursor at the given character offset.
///
/// NOTE: `index` is a *character* offset (i.e. counting [`char`]s), NOT a byte offset!
/// For instance, the cursor after `"ö"` is at `CCursor::new(1)`, even though `"ö".len() == 2`.
///
/// To put the cursor after the last character of a string, use [`Self::end_of_str`].
#[inline]
pub fn new(index: impl Into<CharIndex>) -> Self {
Self {
index: index.into(),
prefer_next_row: false,
}
}
/// A cursor placed after the last character of `text`,
/// i.e. at `text.chars().count()`.
#[inline]
pub fn end_of_str(text: &str) -> Self {
Self::new(text.chars().count())
}
}
/// Two `CCursor`s are considered equal if they refer to the same character boundary,
/// even if one prefers the start of the next row.
impl PartialEq for CCursor {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.index == other.index
}
}
impl core::ops::Add<usize> for CCursor {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self {
index: self.index.saturating_add(rhs),
prefer_next_row: self.prefer_next_row,
}
}
}
impl core::ops::Add<CharIndex> for CCursor {
type Output = Self;
fn add(self, rhs: CharIndex) -> Self::Output {
Self {
index: self.index + rhs,
prefer_next_row: self.prefer_next_row,
}
}
}
impl core::ops::Sub<usize> for CCursor {
type Output = Self;
fn sub(self, rhs: usize) -> Self::Output {
Self {
index: self.index.saturating_sub(rhs),
prefer_next_row: self.prefer_next_row,
}
}
}
impl core::ops::Sub<CharIndex> for CCursor {
type Output = Self;
fn sub(self, rhs: CharIndex) -> Self::Output {
Self {
index: self.index - rhs,
prefer_next_row: self.prefer_next_row,
}
}
}
impl core::ops::AddAssign<usize> for CCursor {
fn add_assign(&mut self, rhs: usize) {
self.index = self.index.saturating_add(rhs);
}
}
impl core::ops::SubAssign<usize> for CCursor {
fn sub_assign(&mut self, rhs: usize) {
self.index = self.index.saturating_sub(rhs);
}
}
/// Row/column cursor.
///
/// This refers to rows and columns in layout terms--text wrapping creates multiple rows.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct LayoutCursor {
/// 0 is first row, and so on.
/// Note that a single paragraph can span multiple rows.
/// (a paragraph is text separated by `\n`).
pub row: usize,
/// Character based (NOT bytes).
/// It is fine if this points to something beyond the end of the current row.
/// When moving up/down it may again be within the next row.
pub column: CharIndex,
}