summaryrefslogtreecommitdiff
path: root/dev/glide.zig
diff options
context:
space:
mode:
Diffstat (limited to 'dev/glide.zig')
-rw-r--r--dev/glide.zig43
1 files changed, 25 insertions, 18 deletions
diff --git a/dev/glide.zig b/dev/glide.zig
index 62e44bb7..9d622b3d 100644
--- a/dev/glide.zig
+++ b/dev/glide.zig
@@ -2,8 +2,9 @@
//! scroll, on its own clock, from the frames it draws alone. Whatever
//! scrolls a text (a wheel's lines, Ctrl-d, a page, G, n, a Look) scrolls
//! the core at once, so every key and click acts on where the text is; the
-//! frontend sees a body's first line move between two frames (Region.line)
-//! and its picture follows there, critically damped (text never passes its
+//! frontend sees a body's top move between two frames (Region.line, and
+//! Region.row: a row inside a wrapped line, File.Top) and its picture
+//! follows there, critically damped (text never passes its
//! mark), every distance on the same law. The GUI draws it at the pixel;
//! the tty at whole rows (tui.zig's glideRows). Nothing here reaches back
//! into the core.
@@ -60,9 +61,9 @@ pub const Glide = struct {
caret_row: ?u16 = null,
caret_rides: bool = false,
/// Each body as the last frame showed it, by its pane: its serial,
- /// 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(.{}),
+ /// top (a line and a row of it), 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, row: u32 = 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,
@@ -121,7 +122,9 @@ pub const Glide = struct {
/// The frame about to be drawn, at `now_ns` on the frontend's clock:
/// the glide moves on to now, and a body whose first line moved since
/// the last frame (the same text in the same place) is measured against
- /// that frame and kicks it. A terminal's view glides only under the
+ /// that frame and kicks it: a move of lines guessed at as many rows (as
+ /// unwrapped), one inside a line's wrapped rows known in rows. A
+ /// terminal's view glides only under the
/// wheel (`wheeled`: its output scrolls it at every line, and that is no
/// glide); not one the frontend moves by the pixel (`direct`). One glide
/// at a time: the focused body's, else the one that moved most.
@@ -139,24 +142,26 @@ pub const Glide = struct {
}
var pick: ?pardes.Region = null;
var delta: i64 = 0;
+ var lines: i64 = 0;
var shown: [pardes.MAX_PANES]bool = @splat(false);
for (surface.regionList()) |region| {
if (region.kind != .body or region.owner >= glide.seen.len) continue;
const id: usize = region.owner;
shown[id] = true;
const was = glide.seen[id];
- glide.seen[id] = .{ .serial = region.serial, .line = region.line, .doc = region.doc, .rect = region.rect };
- if (was.serial != region.serial or was.doc != region.doc or was.line == region.line) continue;
+ glide.seen[id] = .{ .serial = region.serial, .line = region.line, .row = region.row, .doc = region.doc, .rect = region.rect };
+ if (was.serial != region.serial or was.doc != region.doc or (was.line == region.line and was.row == region.row)) continue;
// The last frame drew this body where it is now: a pane that
// moved or was resized in the same step has no picture to glide
// from.
if (!std.meta.eql(was.rect, region.rect)) continue;
if (region.doc == 0 and wheeled != id) continue;
if (direct == id) continue;
- const moved = @as(i64, region.line) - was.line;
+ const moved = if (region.line != was.line) @as(i64, region.line) - was.line else @as(i64, region.row) - was.row;
if (pick) |best| if (best.active or (!region.active and @abs(moved) <= @abs(delta))) continue;
pick = region;
delta = moved;
+ lines = @as(i64, region.line) - was.line;
}
for (&glide.seen, shown) |*seen, here| if (!here) {
seen.* = .{};
@@ -179,8 +184,8 @@ pub const Glide = struct {
if (glide.before.len < n) return glide.stop();
for (0..r.h) |row| @memcpy(glide.before[row * w ..][0..w], glide.last[(@as(usize, r.y) + row) * glide.last_cols + r.x ..][0..w]);
glide.dir = @intCast(std.math.sign(delta));
- glide.pending_lines += @floatFromInt(@abs(delta));
- glide.measure(gpa, surface);
+ glide.pending_lines += @floatFromInt(@abs(lines));
+ glide.measure(gpa, surface, @floatFromInt(@abs(delta)));
}
/// This frame's cells and caret, for the next frame's measure.
@@ -202,8 +207,9 @@ pub const Glide = struct {
}
/// Finds how far the pane's body rows moved, `dir` rows at a time, by
- /// the rows of `surface` against `before`; keeps the rows that left.
- fn measure(glide: *Glide, gpa: std.mem.Allocator, surface: *const pardes.Surface) void {
+ /// the rows of `surface` against `before` (`guess` rows tried first);
+ /// keeps the rows that left.
+ fn measure(glide: *Glide, gpa: std.mem.Allocator, surface: *const pardes.Surface, guess_rows: f32) void {
const w = glide.bw();
const x0 = glide.rect.x + config.GUTTER;
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();
@@ -218,9 +224,10 @@ pub const Glide = struct {
};
// The least shift under which the rows agree, but for a few: the
// cursor's line and its number's weight move with the scroll. The
- // lines the core moved are tried first: unwrapped, they are the
- // rows, and one pass over the body settles it (the search over
- // every shift was 6 ms of the first frame of a page in Debug).
+ // rows guessed are tried first: the lines the core moved, which are
+ // the rows unwrapped, or the rows inside a wrapped line; one pass over the
+ // body settles it (the search over every shift was 6 ms of the
+ // first frame of a page in Debug).
const agrees = struct {
fn at(gl: *const Glide, surf: *const pardes.Surface, x: u16, width: u16, height: u16, kk: u16) bool {
if (kk == 0 or kk + 3 > height) return false;
@@ -239,7 +246,7 @@ pub const Glide = struct {
return true;
}
};
- const guess: u16 = @intFromFloat(@min(glide.pending_lines, @as(f32, @floatFromInt(h))));
+ const guess: u16 = @intFromFloat(@min(guess_rows, @as(f32, @floatFromInt(h))));
// A body's lines or more: every row moved off, none in common.
var shift: u16 = if (guess >= h) 0 else if (agrees.at(glide, surface, x0, w, h, guess)) guess else found: for (1..h) |k| {
if (agrees.at(glide, surface, x0, w, h, @intCast(k))) break :found @intCast(k);
@@ -252,7 +259,7 @@ pub const Glide = struct {
// None in common: a body's move from the last frame's body, if the
// lines moved are that many (rows that merely changed, a small
// scroll the rows cannot show, land at once).
- if (shift == 0 and glide.pending_lines + 3 < @as(f32, @floatFromInt(h))) return glide.stop();
+ if (shift == 0 and guess_rows + 3 < @as(f32, @floatFromInt(h))) return glide.stop();
const far = shift == 0;
if (far) shift = h;
glide.caret_rides = !far and if (glide.caret_row) |old| if (surface.cursor) |c|