From ded16f468dce93777a8d0d981bc4045c4a4a4090 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Wed, 24 Jul 2024 15:55:31 +1200 Subject: [PATCH] Fix cursor line --- parley/src/editor/layout.rs | 7 ++++--- parley/src/editor/selection.rs | 11 ++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/parley/src/editor/layout.rs b/parley/src/editor/layout.rs index 06a88a8..478b57b 100644 --- a/parley/src/editor/layout.rs +++ b/parley/src/editor/layout.rs @@ -387,10 +387,11 @@ impl TextLayout { /// This is not meaningful until [`Self::rebuild`] has been called. // TODO: This is too simplistic. See https://raphlinus.github.io/text/2020/10/26/text-layout.html#shaping-cluster // for example. This would break in a `fi` ligature - pub fn cursor_line_for_text_position(&self, text_pos: usize) -> Line { + pub fn cursor_line_for_text_position(&self, text_pos: usize) -> Option { let from_position = self.cursor_for_text_position(text_pos); - let line = from_position.path.line(&self.layout).unwrap(); + // TODO: fix in case there is no text + let line = from_position.path.line(&self.layout)?; let line_metrics = line.metrics(); let baseline = line_metrics.baseline + line_metrics.descent; @@ -399,7 +400,7 @@ impl TextLayout { from_position.offset as f64, (baseline - line_metrics.size()) as f64, ); - Line::new(p1, p2) + Some(Line::new(p1, p2)) } /// Returns the [`Link`] at the provided point (relative to the layout's origin) if one exists. diff --git a/parley/src/editor/selection.rs b/parley/src/editor/selection.rs index 95e033a..4d3f60a 100644 --- a/parley/src/editor/selection.rs +++ b/parley/src/editor/selection.rs @@ -30,7 +30,7 @@ pub struct TextWithSelection { needs_selection_update: bool, selecting_with_mouse: bool, // TODO: Cache cursor line, selection boxes - cursor_line: Option, + // cursor_line: Option, } impl TextWithSelection { @@ -40,7 +40,7 @@ impl TextWithSelection { selection: None, needs_selection_update: false, selecting_with_mouse: false, - cursor_line: None, + // cursor_line: None, highlight_brush: TextBrush::Highlight { text: Color::WHITE.into(), fill: Color::LIGHT_BLUE.into(), @@ -59,7 +59,12 @@ impl TextWithSelection { } pub fn get_cursor_line(&self) -> Option { - self.cursor_line + // self.cursor_line + if let Some(selection) = self.selection { + self.layout.cursor_line_for_text_position(selection.active) + } else { + None + } } pub fn pointer_down(