From 570999809cab5a26f7feac7401eef8393b8cdbc4 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Thu, 30 May 2024 14:26:38 -0600 Subject: [PATCH] Track horizontal scroll (which must be implemented by renderers) --- src/buffer.rs | 23 +++++++++++++++++++++++ src/cursor.rs | 14 ++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index 8dae63f..d5d43c1 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -373,6 +373,29 @@ impl Buffer { } self.shape_until_scroll(font_system, prune); + + // Adjust horizontal scroll to include cursor + if let Some(layout_cursor) = self.layout_cursor(font_system, cursor) { + if let Some(layout_lines) = self.line_layout(font_system, layout_cursor.line) { + if let Some(layout_line) = layout_lines.get(layout_cursor.layout) { + if let Some(glyph) = layout_line.glyphs.get(layout_cursor.glyph) { + //TODO: use code from cursor_glyph_opt? + let x_a = glyph.x; + let x_b = glyph.x + glyph.w; + let x_min = x_a.min(x_b); + let x_max = x_a.max(x_b); + if x_min < self.scroll.horizontal { + self.scroll.horizontal = x_min; + self.redraw = true; + } + if x_max > self.scroll.horizontal + self.width { + self.scroll.horizontal = x_max - self.width; + self.redraw = true; + } + } + } + } + } } /// Shape lines until scroll diff --git a/src/cursor.rs b/src/cursor.rs index 1bff075..a104deb 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -132,7 +132,7 @@ pub enum Motion { } /// Scroll position in [`Buffer`] -#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)] pub struct Scroll { /// Index of [`BufferLine`] in [`Buffer::lines`]. This will be adjusted as needed if layout is /// out of bounds @@ -140,11 +140,17 @@ pub struct Scroll { /// Index of [`LayoutLine`] in [`BufferLine::layout`]. This will be adjusted as needed /// if it is negative or exceeds the number of layout lines pub layout: i32, + /// The horizontal position of scroll in fractional pixels + pub horizontal: f32, } impl Scroll { - /// Create a new cursor - pub const fn new(line: usize, layout: i32) -> Self { - Self { line, layout } + /// Create a new scroll + pub const fn new(line: usize, layout: i32, horizontal: f32) -> Self { + Self { + line, + layout, + horizontal, + } } }