Skip to content

Commit

Permalink
Track horizontal scroll (which must be implemented by renderers)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed May 30, 2024
1 parent 2f5f2c6 commit 5709998
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,25 @@ 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
pub line: usize,
/// 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,
}
}
}

0 comments on commit 5709998

Please sign in to comment.