Track horizontal scroll (which must be implemented by renderers)

This commit is contained in:
Jeremy Soller 2024-05-30 14:26:38 -06:00
parent 2f5f2c63da
commit 570999809c
No known key found for this signature in database
GPG key ID: D02FD439211AF56F
2 changed files with 33 additions and 4 deletions

View file

@ -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

View file

@ -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,
}
}
}