summaryrefslogtreecommitdiff
path: root/src/glide.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/glide.zig')
-rw-r--r--src/glide.zig51
1 files changed, 46 insertions, 5 deletions
diff --git a/src/glide.zig b/src/glide.zig
index 6f8fe9a3..1c49d8e8 100644
--- a/src/glide.zig
+++ b/src/glide.zig
@@ -59,6 +59,11 @@ pub const Glide = struct {
/// first line, text and place, so a view that moved since is found
/// whatever moved it.
seen: [pardes.MAX_PANES]struct { serial: u32 = 0, line: i32 = 0, doc: u64 = 0, rect: pardes.Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 } } = @splat(.{}),
+ /// Each body's rows held at its top while it scrolls (a source's
+ /// sticky context rows, TreeContext), its frontend's to say: they do
+ /// not move, so a move is measured, and the rows that left are kept,
+ /// below them.
+ held: [pardes.MAX_PANES]u16 = @splat(0),
/// The last frame's cells, which a move is measured against.
last: []pardes.Cell = &.{},
last_cols: u16 = 0,
@@ -189,9 +194,12 @@ pub const Glide = struct {
/// the rows of `surface` against `before`; keeps the rows that left.
fn measure(glide: *Glide, gpa: std.mem.Allocator, surface: *const pardes.Surface) void {
const w = glide.bw();
- const h = glide.body_h;
const x0 = glide.rect.x + config.GUTTER;
- if (w == 0 or h == 0 or x0 + w > surface.cols or glide.body_y + h > surface.rows or glide.before.len < @as(usize, w) * h) return glide.stop();
+ if (w == 0 or glide.body_h == 0 or x0 + w > surface.cols or glide.body_y + glide.body_h > surface.rows or glide.before.len < @as(usize, w) * glide.body_h) return glide.stop();
+ // The rows under the held ones (`held`) are what scrolls: `before`
+ // and the frame are compared, and kept, from there.
+ const top = @min(glide.held[glide.pane], glide.body_h -| 1);
+ const h = glide.body_h - top;
const now = struct {
fn row(s: *const pardes.Surface, x: u16, y: u16, n: u16) []const pardes.Cell {
return s.cells[@as(usize, y) * s.cols + x ..][0..n];
@@ -209,8 +217,9 @@ pub const Glide = struct {
var misses: usize = 0;
for (0..height - kk) |r| {
const rr: u16 = @intCast(r);
- const old_row = gl.before[@as(usize, if (gl.dir > 0) rr + kk else rr) * width ..][0..width];
- const new_row = now.row(surf, x, gl.body_y + if (gl.dir > 0) rr else rr + kk, width);
+ const held = @min(gl.held[gl.pane], gl.body_h -| 1);
+ const old_row = gl.before[@as(usize, held + if (gl.dir > 0) rr + kk else rr) * width ..][0..width];
+ const new_row = now.row(surf, x, gl.body_y + held + if (gl.dir > 0) rr else rr + kk, width);
if (!sameRow(old_row, new_row)) {
misses += 1;
if (misses > allowed) return false;
@@ -249,7 +258,7 @@ pub const Glide = struct {
const kept = @min(count.*, h * reach - shift);
std.mem.copyBackwards(pardes.Cell, keep.*[@as(usize, shift) * w ..][0 .. @as(usize, kept) * w], keep.*[0 .. @as(usize, kept) * w]);
for (0..shift) |i| {
- const src_row: usize = if (glide.dir > 0) shift - 1 - i else h - shift + i;
+ const src_row: usize = top + if (glide.dir > 0) shift - 1 - i else h - shift + i;
@memcpy(keep.*[i * w ..][0..w], glide.before[src_row * w ..][0..w]);
}
count.* = kept + shift;
@@ -423,3 +432,35 @@ test "a follow comes to rest without a snap: past its fastest frame each step is
try std.testing.expect(last <= 0.1);
}
}
+
+test "a body scrolling under held rows (sticky context) is measured, and keeps the rows that left, below them" {
+ const gpa = std.testing.allocator;
+ // A body 10 rows high whose first 3 hold still (a source's context
+ // rows) while the lines under them scroll one.
+ var cells: [2][24 * 12]pardes.Cell = undefined;
+ var frames: [2]pardes.Surface = undefined;
+ for (&frames, &cells, [_]u16{ 10, 11 }) |*s, *c, first| {
+ @memset(c, .{});
+ s.* = .{ .cols = 24, .rows = 12, .cells = c };
+ s.addRegion(.{ .kind = .body, .owner = 0, .serial = 1, .rect = .{ .x = config.GUTTER, .y = 1, .w = 24 - config.GUTTER, .h = 10 }, .active = true, .line = first, .doc = 7 });
+ for (0..3) |k| {
+ var buf: [8]u8 = undefined;
+ _ = s.print(config.GUTTER, @intCast(1 + k), 20, std.fmt.bufPrint(&buf, "ctx {d}", .{k}) catch unreachable, .{});
+ }
+ for (3..10) |k| {
+ var buf: [8]u8 = undefined;
+ _ = s.print(config.GUTTER, @intCast(1 + k), 20, std.fmt.bufPrint(&buf, "L{d}", .{first + k - 3}) catch unreachable, .{});
+ }
+ }
+ var glide: Glide = .{};
+ defer glide.deinit(gpa);
+ glide.held[0] = 3;
+ glide.frame(gpa, &frames[0], std.time.ns_per_s, null, null);
+ glide.frame(gpa, &frames[1], std.time.ns_per_s + 16 * std.time.ns_per_ms, null, null);
+ // A row down, and the row that left is the first under the held ones,
+ // L10, not a held row.
+ try std.testing.expect(glide.follow.offset < -0.9);
+ try std.testing.expectEqual(@as(u16, 1), glide.nabove);
+ const w = glide.bw();
+ try std.testing.expect(sameRow(glide.above[0..w], frames[0].cells[@as(usize, 1 + 3) * 24 + config.GUTTER ..][0..w]));
+}