Skip to content

Commit

Permalink
Fix cursor line
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Jul 24, 2024
1 parent 0e2d31b commit f455a75
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 4 additions & 3 deletions parley/src/editor/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,11 @@ impl<T: TextStorage> TextLayout<T> {
/// 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<Line> {
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;
Expand All @@ -399,7 +400,7 @@ impl<T: TextStorage> TextLayout<T> {
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.
Expand Down
11 changes: 8 additions & 3 deletions parley/src/editor/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct TextWithSelection<T: Selectable> {
needs_selection_update: bool,
selecting_with_mouse: bool,
// TODO: Cache cursor line, selection boxes
cursor_line: Option<Line>,
// cursor_line: Option<Line>,
}

impl<T: Selectable> TextWithSelection<T> {
Expand All @@ -40,7 +40,7 @@ impl<T: Selectable> TextWithSelection<T> {
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(),
Expand All @@ -59,7 +59,12 @@ impl<T: Selectable> TextWithSelection<T> {
}

pub fn get_cursor_line(&self) -> Option<Line> {
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(
Expand Down

0 comments on commit f455a75

Please sign in to comment.