summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-24 23:16:11 -0300
committerGabriel Schneider <[email protected]>2026-10-25 01:30:09 -0300
commit706b9d98fdce29a58ffe5a8a5417d6b2810e6d2c (patch)
tree1367ad33c41891dc89a7507b156660e0694e5a29 /src
parent6e8a6a63d45b2caef7b649d358f836e6cc4134d6 (diff)
downloadpardes-706b9d98fdce29a58ffe5a8a5417d6b2810e6d2c.tar.gz
pardes-706b9d98fdce29a58ffe5a8a5417d6b2810e6d2c.zip
anim 12: the tty runs the GUI's motion system: the view's glide moves to src/glide.zig (Glide, its step: find the view that moved, snapshot, measure, kick), the GUI keeps only its wheel notches and its pixel draw; the tty steps it each loop and draws it at whole rows (glide.shifted: the position rounded, never the step, the rows that left in the gap, a caret whose line rode the scroll carried with it, one that kept its row left still); rows that only disagree glide the core's lines instead of snapping; the snapshot harness's virtual clock runs a frame a wake while it moves
Diffstat (limited to 'src')
-rw-r--r--src/glide.zig415
-rw-r--r--src/gui/gui.zig279
-rw-r--r--src/pardes.zig2
-rw-r--r--src/tty/tty.zig27
4 files changed, 447 insertions, 276 deletions
diff --git a/src/glide.zig b/src/glide.zig
new file mode 100644
index 00000000..1d128dd0
--- /dev/null
+++ b/src/glide.zig
@@ -0,0 +1,415 @@
+//! The view's glide (G6, anim 3, 9, 12): the text's picture following
+//! its core's rows, one system on every shell. The GUI draws it at the
+//! pixel; the tty at whole rows (`shifted`).
+
+const std = @import("std");
+const pardes = @import("pardes.zig");
+const config = @import("config.zig");
+
+/// G6 (docs/render-pipeline.md §9.1): 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, and the picture glides there
+/// on the Motion flavour's spring (critically damped: text never passes its
+/// mark), every distance on the same law. How far the rows moved is
+/// measured on the next frame against the last, a line wrapped over several
+/// rows or a terminal's output included, and the rows that left are kept to
+/// draw in the gap. A move past what the two frames share (a page and more,
+/// G) has no rows between them to show: the picture comes from the last
+/// frame's body as if it lay just past the new one, one body away, so a
+/// jump of any length reads as one body's slide.
+///
+/// The picture's motion is one system with its own state, integrated in
+/// one place every frame (`advance`), and every scroll, whatever made it,
+/// is an input into it (`kick`): it moves the target by the rows the core
+/// moved and leaves the motion as it is. What the system was doing decides
+/// how it is absorbed: inputs coming as a stream (each like the last, at a
+/// steady spacing: a held key, a spun wheel) feed it, and it moves at the
+/// stream's own rate, a lag of an input or so behind, never easing toward
+/// each target (that was a bounce a line); an input into a resting view,
+/// or against the way it moves, starts the spring from the speed it has
+/// (an ease, a turn through zero); a jump mid-glide adds its rows with the
+/// speed kept. The spring's settle only comes once the inputs stop.
+pub const Glide = struct {
+ /// The picture's lag behind the core's rows and its motion (rows).
+ follow: pardes.animation.Follow = .{},
+ now_ns: u64 = 0,
+ pane: usize = 0,
+ rect: pardes.Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 },
+ body_y: u16 = 0,
+ body_h: u16 = 0,
+ /// A scroll made and not yet measured: its direction, and the body's
+ /// rows before it.
+ pending: bool = false,
+ dir: i8 = 0,
+ before: []pardes.Cell = &.{},
+ /// The rows past the body's top (`above`) and bottom (`below`), nearest
+ /// first, `bw` cells each: what the gap shows while the picture lags.
+ above: []pardes.Cell = &.{},
+ below: []pardes.Cell = &.{},
+ nabove: u16 = 0,
+ nbelow: u16 = 0,
+ /// The core's lines a scroll moved, not yet measured; and what the
+ /// glide in flight has moved, in lines and in the picture's rows. A
+ /// wrapped line is several rows, and the thumb counts lines.
+ pending_lines: f32 = 0,
+ lines_moved: f32 = 0,
+ rows_moved: f32 = 0,
+ /// The caret's row as the last frame drew it, and whether the move
+ /// measured carried it with its line (a wheel's) or left it on its row
+ /// (a page, a held key's): the tty draws a carried caret with the rows.
+ caret_row: ?u16 = null,
+ caret_rides: bool = false,
+ /// The Motion flavour the last loop step read.
+ motion: pardes.animation.Motion = .{},
+ /// Each pane's view as the last loop step saw it (stepGlide): its
+ /// serial, first line and file, so a view that moved since is found
+ /// whatever moved it.
+ seen: [pardes.MAX_PANES]struct { serial: u32 = 0, line: i64 = 0, file: u64 = 0, revision: u32 = 0 } = @splat(.{}),
+
+ /// The rows kept past each edge, in bodies, so a jump made while one
+ /// glides carries on from where it is: as far as the picture may lag.
+ pub const reach = pardes.animation.Follow.reach;
+
+ pub fn moving(glide: *const Glide) bool {
+ return glide.follow.moving() or glide.pending;
+ }
+
+ pub fn stop(glide: *Glide) void {
+ glide.follow = .{ .at_ns = glide.follow.at_ns };
+ glide.pending = false;
+ glide.nabove = 0;
+ glide.nbelow = 0;
+ glide.pending_lines = 0;
+ glide.lines_moved = 0;
+ glide.rows_moved = 0;
+ }
+
+ /// The glide's lag in the core's lines: its rows, scaled by the lines
+ /// they carried.
+ pub fn lagLines(glide: *const Glide) f32 {
+ if (glide.rows_moved == 0) return glide.follow.offset;
+ return glide.follow.offset * glide.lines_moved / glide.rows_moved;
+ }
+
+ pub fn deinit(glide: *Glide, gpa: std.mem.Allocator) void {
+ gpa.free(glide.before);
+ gpa.free(glide.above);
+ gpa.free(glide.below);
+ }
+
+ pub fn bw(glide: *const Glide) u16 {
+ return glide.rect.w -| config.GUTTER;
+ }
+
+ /// The rules at this body's size: a tenth of a pixel is 5e-3 rows.
+ pub fn pace(glide: *const Glide, motion: pardes.animation.Motion) pardes.animation.Follow.Pace {
+ return pardes.animation.Follow.pace(motion, @floatFromInt(glide.body_h), 5e-3);
+ }
+
+ /// An input: the core's rows moved by `rows` (down positive).
+ pub fn kick(glide: *Glide, rows: f32, now_ns: u64, motion: pardes.animation.Motion) void {
+ glide.follow.kick(rows, now_ns, glide.pace(motion));
+ }
+
+ pub fn advance(glide: *Glide, now_ns: u64, motion: pardes.animation.Motion) void {
+ glide.follow.advance(now_ns, glide.pace(motion));
+ }
+
+ /// 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.
+ pub fn measure(glide: *Glide, gpa: std.mem.Allocator, surface: *const pardes.Surface) void {
+ glide.pending = false;
+ 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();
+ 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];
+ }
+ };
+ // 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).
+ 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;
+ const allowed = @max(2, (height - kk) / 8);
+ 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);
+ if (!sameRow(old_row, new_row)) {
+ misses += 1;
+ if (misses > allowed) return false;
+ }
+ }
+ return true;
+ }
+ };
+ const guess: u16 = @intFromFloat(@min(glide.pending_lines, @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);
+ } else 0;
+ // Rows that disagree under every shift though the core moved a few
+ // lines: their colours changed as they moved (a paint arriving as a
+ // spin stops). Unwrapped, the rows moved are the lines: glide those
+ // (it landed from wherever it was, a snap at a spin's end).
+ if (shift == 0 and guess > 0 and guess < h) shift = guess;
+ // 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();
+ const far = shift == 0;
+ if (far) shift = h;
+ glide.caret_rides = !far and if (glide.caret_row) |old| if (surface.cursor) |c|
+ @as(i32, c.y) == @as(i32, old) - @as(i32, shift) * glide.dir
+ else
+ false else false;
+ glide.rows_moved += @floatFromInt(shift);
+ glide.lines_moved += glide.pending_lines;
+ glide.pending_lines = 0;
+ // The rows that left: past the top going down, the bottom going up.
+ if (!glide.ensure(gpa)) return glide.stop();
+ const keep = if (glide.dir > 0) &glide.above else &glide.below;
+ const count = if (glide.dir > 0) &glide.nabove else &glide.nbelow;
+ 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;
+ @memcpy(keep.*[i * w ..][0..w], glide.before[src_row * w ..][0..w]);
+ }
+ count.* = kept + shift;
+ // What came into view is no longer past the other edge.
+ const other = if (glide.dir > 0) &glide.below else &glide.above;
+ const other_n = if (glide.dir > 0) &glide.nbelow else &glide.nabove;
+ // ...and after a jump, the rows past it are another place's.
+ const dropped = if (far) other_n.* else @min(other_n.*, shift);
+ std.mem.copyForwards(pardes.Cell, other.*[0 .. @as(usize, other_n.* - dropped) * w], other.*[@as(usize, dropped) * w ..][0 .. @as(usize, other_n.* - dropped) * w]);
+ other_n.* -= dropped;
+ glide.kick(@as(f32, @floatFromInt(shift)) * @as(f32, @floatFromInt(glide.dir)), glide.now_ns, glide.motion);
+ }
+
+ /// Finds the text whose view moved since the last loop step, however
+ /// it moved (the wheel, a key, a Look), and snapshots its body as the
+ /// last frame drew it (`core.surface`), so the frame about to be drawn
+ /// can measure the move (`measure`); moves the glide on to now. 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 shell
+ /// moves by the pixel (`direct`).
+ pub fn step(glide: *Glide, core: *pardes.Pardes, gpa: std.mem.Allocator, wheeled: ?usize, direct: ?usize) void {
+ glide.now_ns = core.now_ns;
+ glide.motion = pardes.animation.Motion.of(core.settings.motion);
+ if (glide.follow.moving()) {
+ glide.advance(core.now_ns, glide.motion);
+ core.needs_frame = true;
+ if (glide.follow.offset == 0) {
+ glide.nabove = 0;
+ glide.nbelow = 0;
+ glide.lines_moved = 0;
+ glide.rows_moved = 0;
+ }
+ }
+ // ponytail: one glide at a time, the focused pane's, else the one that
+ // moved most; a second pane scrolled in the same step lands. A glide a
+ // pane (and a scissor each) if two ever need to move together.
+ // The view that moved: the focused pane's, else the one that moved
+ // most; not one showing another file now.
+ const s = &core.surface;
+ var pick: ?usize = null;
+ var delta: i64 = 0;
+ for (core.panes, 0..) |slot, id| {
+ const pane = slot orelse {
+ glide.seen[id] = .{};
+ continue;
+ };
+ const line: i64 = if (pane.file) |f| @intCast(f.scroll) else if (pane.isTerminal()) pardes.panes.terminal.gridOffset(pane) else 0;
+ const file: u64 = if (pane.file) |f| std.hash.Wyhash.hash(0, f.path) else 0;
+ const revision: u32 = if (pane.file) |f| f.revision else 0;
+ const was = glide.seen[id];
+ glide.seen[id] = .{ .serial = pane.serial, .line = line, .file = file, .revision = revision };
+ if (was.serial != pane.serial or was.file != file or was.line == line or pane.collapsed or pane.hasPdf()) continue;
+ // Generated text rewritten as its view moved (a query pane's live
+ // rows, a +Search refilled, Callstack): the rows are another
+ // text's, not this one's moved; it lands.
+ if (pane.file) |f| if (f.output != null and revision != was.revision) continue;
+ if (pane.file == null and wheeled != id) continue;
+ if (direct == id) continue;
+ const moved = line - was.line;
+ if (pick) |best| if (best == core.active or (id != core.active and @abs(moved) <= @abs(delta))) continue;
+ pick = id;
+ delta = moved;
+ }
+ const id = pick orelse return;
+ const pane = core.panes[id].?;
+ const r = core.rects[id];
+ if (r.h <= pane.tag_rows or r.w <= config.GUTTER) return;
+ const body_y = core.bodyTop(pane, r);
+ const body_h = r.h -| pane.tag_rows;
+ const w = r.w - config.GUTTER;
+ const x0 = r.x + config.GUTTER;
+ // 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.
+ const drawn = for (s.regionList()) |region| {
+ if (region.kind == .body and region.owner == id and region.serial == pane.serial) break std.meta.eql(region.rect, pardes.Rect{ .x = x0, .y = body_y, .w = w, .h = body_h });
+ } else false;
+ // A new pane or place: what was kept belongs to another picture.
+ if (glide.pane != id or !std.meta.eql(glide.rect, r) or glide.body_y != body_y or glide.body_h != body_h or glide.pending) glide.stop();
+ glide.pane = id;
+ glide.rect = r;
+ glide.body_y = body_y;
+ glide.body_h = body_h;
+ if (glide.motion.instant or !drawn or x0 + w > s.cols or body_y + body_h > s.rows or s.cells.len == 0) return glide.stop();
+ // The body as the last frame drew it, which the move is measured from.
+ const n = @as(usize, w) * body_h;
+ if (glide.before.len < n) glide.before = gpa.realloc(glide.before, n) catch &.{};
+ if (glide.before.len < n) return glide.stop();
+ for (0..body_h) |row| @memcpy(glide.before[row * w ..][0..w], s.cells[(@as(usize, body_y) + row) * s.cols + x0 ..][0..w]);
+ glide.pending = true;
+ glide.caret_row = if (s.cursor) |c| c.y else null;
+ glide.dir = @intCast(std.math.sign(delta));
+ glide.pending_lines += @floatFromInt(@abs(delta));
+ core.needs_frame = true;
+ }
+
+ pub fn ensure(glide: *Glide, gpa: std.mem.Allocator) bool {
+ const n = @as(usize, glide.bw()) * glide.body_h * reach;
+ inline for (.{ &glide.above, &glide.below }) |buffer| if (buffer.len < n) {
+ const grown = gpa.realloc(buffer.*, n) catch return false;
+ buffer.* = grown;
+ };
+ return true;
+ }
+};
+
+/// The tty's picture of the glide (anim 12): the body's rows moved by the
+/// offset rounded to whole rows (the position rounded, never the step, so
+/// a slowing glide's steps come further apart), the rows that left in the
+/// gap it opens, and a caret whose line rode the scroll carried with it.
+/// `surface` itself while nothing is shifted.
+pub fn shifted(glide: *const Glide, arena: std.mem.Allocator, surface: *pardes.Surface) !*pardes.Surface {
+ for (surface.panelTracks()) |track|
+ if (track.active() and track.pane == glide.pane) return surface;
+ const k: i32 = @intFromFloat(@round(glide.follow.offset));
+ const w = glide.bw();
+ const h = glide.body_h;
+ const x0 = glide.rect.x + config.GUTTER;
+ const y0 = glide.body_y;
+ if (k == 0 or w == 0 or h == 0 or x0 + w > surface.cols or y0 + h > surface.rows) return surface;
+ const out = try arena.create(pardes.Surface);
+ out.* = surface.*;
+ out.cells = try arena.dupe(pardes.Cell, surface.cells);
+ const cols: usize = surface.cols;
+ for (0..h) |r| {
+ const from = @as(i32, @intCast(r)) + k;
+ const to = out.cells[(y0 + r) * cols + x0 ..][0..w];
+ const kept: ?[]const pardes.Cell = if (from >= 0 and from < h)
+ surface.cells[(y0 + @as(usize, @intCast(from))) * cols + x0 ..][0..w]
+ else if (from < 0 and -from - 1 < glide.nabove)
+ glide.above[@as(usize, @intCast(-from - 1)) * w ..][0..w]
+ else if (from >= h and from - h < glide.nbelow)
+ glide.below[@as(usize, @intCast(from - h)) * w ..][0..w]
+ else
+ null;
+ if (kept) |cells| @memcpy(to, cells) else @memset(to, .{});
+ }
+ if (out.cursor) |*c| if (glide.caret_rides and c.x >= x0 and c.x < x0 + w and c.y >= y0 and c.y < y0 + h) {
+ const y = @as(i32, c.y) - k;
+ if (y >= y0 and y < y0 + h) c.y = @intCast(y) else out.cursor = null;
+ };
+ return out;
+}
+
+pub fn sameRow(a: []const pardes.Cell, b: []const pardes.Cell) bool {
+ for (a, b) |x, y| {
+ if (x.default != y.default) return false;
+ if (x.default) continue;
+ if (!std.mem.eql(u8, x.grapheme(), y.grapheme()) or !std.meta.eql(x.style, y.style)) return false;
+ }
+ return true;
+}
+
+
+test "the tty draws a glide at whole rows: once a flick or a spin slows, its steps a tick never grow and its one-row steps come further apart, to where the core is" {
+ const gpa = std.testing.allocator;
+ const core = try pardes.Pardes.init(gpa, .{ .cols = 80, .rows = 30, .tty_only = true });
+ defer core.deinit();
+ var text: std.ArrayList(u8) = .empty;
+ defer text.deinit(gpa);
+ for (0..3000) |i| try text.print(gpa, "{d:0>5} line\n", .{i});
+ _ = try core.setTestFile(text.items);
+ core.settings.motion = .smooth;
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ var glide: Glide = .{};
+ defer glide.deinit(gpa);
+ var now: u64 = std.time.ns_per_s;
+ // One tick of the tty: its loop step, the frame, the picture.
+ const tick = struct {
+ fn f(c: *pardes.Pardes, g: *Glide, a: *std.heap.ArenaAllocator, t: u64) !i32 {
+ c.advance(t);
+ g.step(c, std.testing.allocator, null, null);
+ _ = a.reset(.retain_capacity);
+ const s = try c.render(a.allocator());
+ if (g.pending) g.measure(std.testing.allocator, s);
+ if (g.moving()) g.advance(t, g.motion);
+ _ = try shifted(g, a.allocator(), s);
+ return @intFromFloat(@round(g.follow.offset));
+ }
+ }.f;
+ _ = try tick(core, &glide, &arena, now);
+ for ([_]usize{ 4, 24 }) |notches| {
+ for (0..notches) |i| {
+ core.update(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = 20, .row = 10 } });
+ if (i % 2 == 1) {
+ now += pardes.animation.frame_ns;
+ _ = try tick(core, &glide, &arena, now);
+ }
+ }
+ var drawn = try tick(core, &glide, &arena, now + pardes.animation.frame_ns);
+ now += pardes.animation.frame_ns;
+ var last_step: i32 = std.math.maxInt(i32);
+ var last_gap: usize = 0;
+ var since: usize = 0;
+ var ticks: usize = 0;
+ while (glide.moving()) : (ticks += 1) {
+ try std.testing.expect(ticks < 200);
+ now += pardes.animation.frame_ns;
+ const k = try tick(core, &glide, &arena, now);
+ const step = k - drawn;
+ drawn = k;
+ since += 1;
+ if (step == 0) continue;
+ // Toward the core, never past it, each step no longer than the last.
+ try std.testing.expect(step > 0 and k <= 0);
+ try std.testing.expect(step <= last_step);
+ if (step == 1 and last_step == 1) try std.testing.expect(since >= last_gap);
+ if (step == 1) last_gap = since;
+ last_step = step;
+ since = 0;
+ }
+ try std.testing.expectEqual(@as(i32, 0), drawn);
+ }
+ // j held on the bottom row: the text glides a row a key under a caret
+ // that stays on its cell (it does not ride: its row is kept).
+ const keys = struct {
+ fn j(c: *pardes.Pardes) void {
+ c.update(.{ .key = .{ .cp = 'j', .text = "j" } });
+ }
+ };
+ for (0..40) |_| keys.j(core);
+ now += std.time.ns_per_s;
+ _ = try tick(core, &glide, &arena, now);
+ const row = (core.surface.cursor orelse return error.NoCursor).y;
+ for (0..20) |i| {
+ if (i % 2 == 0) keys.j(core);
+ now += pardes.animation.frame_ns;
+ _ = try tick(core, &glide, &arena, now);
+ const out = try shifted(&glide, arena.allocator(), &core.surface);
+ try std.testing.expectEqual(row, (out.cursor orelse return error.NoCursor).y);
+ }
+}
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 94eabb6c..13fd7905 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -5042,222 +5042,15 @@ test "a wheel over codex in a terminal reaches its pty as the mouse reports it a
try std.testing.expectEqual(@as(i32, -1), g.scroll_notch);
}
-/// G6 (docs/render-pipeline.md §9.1): 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, and the picture glides there
-/// on the Motion flavour's spring (critically damped: text never passes its
-/// mark), every distance on the same law. How far the rows moved is
-/// measured on the next frame against the last, a line wrapped over several
-/// rows or a terminal's output included, and the rows that left are kept to
-/// draw in the gap. A move past what the two frames share (a page and more,
-/// G) has no rows between them to show: the picture comes from the last
-/// frame's body as if it lay just past the new one, one body away, so a
-/// jump of any length reads as one body's slide.
-///
-/// The picture's motion is one system with its own state, integrated in
-/// one place every frame (`advance`), and every scroll, whatever made it,
-/// is an input into it (`kick`): it moves the target by the rows the core
-/// moved and leaves the motion as it is. What the system was doing decides
-/// how it is absorbed: inputs coming as a stream (each like the last, at a
-/// steady spacing: a held key, a spun wheel) feed it, and it moves at the
-/// stream's own rate, a lag of an input or so behind, never easing toward
-/// each target (that was a bounce a line); an input into a resting view,
-/// or against the way it moves, starts the spring from the speed it has
-/// (an ease, a turn through zero); a jump mid-glide adds its rows with the
-/// speed kept. The spring's settle only comes once the inputs stop.
-const Glide = struct {
- /// The picture's lag behind the core's rows and its motion (rows).
- follow: pardes.animation.Follow = .{},
- now_ns: u64 = 0,
- pane: usize = 0,
- rect: pardes.Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 },
- body_y: u16 = 0,
- body_h: u16 = 0,
- /// A scroll made and not yet measured: its direction, and the body's
- /// rows before it.
- pending: bool = false,
- dir: i8 = 0,
- before: []pardes.Cell = &.{},
- /// The rows past the body's top (`above`) and bottom (`below`), nearest
- /// first, `bw` cells each: what the gap shows while the picture lags.
- above: []pardes.Cell = &.{},
- below: []pardes.Cell = &.{},
- nabove: u16 = 0,
- nbelow: u16 = 0,
- /// The core's lines a scroll moved, not yet measured; and what the
- /// glide in flight has moved, in lines and in the picture's rows. A
- /// wrapped line is several rows, and the thumb counts lines.
- pending_lines: f32 = 0,
- lines_moved: f32 = 0,
- rows_moved: f32 = 0,
- /// The Motion flavour the last loop step read.
- motion: pardes.animation.Motion = .{},
- /// Each pane's view as the last loop step saw it (stepGlide): its
- /// serial, first line and file, so a view that moved since is found
- /// whatever moved it.
- seen: [pardes.MAX_PANES]struct { serial: u32 = 0, line: i64 = 0, file: u64 = 0, revision: u32 = 0 } = @splat(.{}),
-
- /// The rows kept past each edge, in bodies, so a jump made while one
- /// glides carries on from where it is: as far as the picture may lag.
- const reach = pardes.animation.Follow.reach;
-
- fn moving(glide: *const Glide) bool {
- return glide.follow.moving() or glide.pending;
- }
-
- fn stop(glide: *Glide) void {
- glide.follow = .{ .at_ns = glide.follow.at_ns };
- glide.pending = false;
- glide.nabove = 0;
- glide.nbelow = 0;
- glide.pending_lines = 0;
- glide.lines_moved = 0;
- glide.rows_moved = 0;
- }
-
- /// The glide's lag in the core's lines: its rows, scaled by the lines
- /// they carried.
- fn lagLines(glide: *const Glide) f32 {
- if (glide.rows_moved == 0) return glide.follow.offset;
- return glide.follow.offset * glide.lines_moved / glide.rows_moved;
- }
-
- fn deinit(glide: *Glide, gpa: std.mem.Allocator) void {
- gpa.free(glide.before);
- gpa.free(glide.above);
- gpa.free(glide.below);
- }
-
- fn bw(glide: *const Glide) u16 {
- return glide.rect.w -| config.GUTTER;
- }
-
- /// The rules at this body's size: a tenth of a pixel is 5e-3 rows.
- fn pace(glide: *const Glide, motion: pardes.animation.Motion) pardes.animation.Follow.Pace {
- return pardes.animation.Follow.pace(motion, @floatFromInt(glide.body_h), 5e-3);
- }
-
- /// An input: the core's rows moved by `rows` (down positive).
- fn kick(glide: *Glide, rows: f32, now_ns: u64, motion: pardes.animation.Motion) void {
- glide.follow.kick(rows, now_ns, glide.pace(motion));
- }
-
- fn advance(glide: *Glide, now_ns: u64, motion: pardes.animation.Motion) void {
- glide.follow.advance(now_ns, glide.pace(motion));
- }
-
- /// 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 {
- glide.pending = false;
- 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();
- 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];
- }
- };
- // 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).
- 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;
- const allowed = @max(2, (height - kk) / 8);
- 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);
- if (!sameRow(old_row, new_row)) {
- misses += 1;
- if (misses > allowed) return false;
- }
- }
- return true;
- }
- };
- const guess: u16 = @intFromFloat(@min(glide.pending_lines, @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);
- } else 0;
- // 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();
- const far = shift == 0;
- if (far) shift = h;
- glide.rows_moved += @floatFromInt(shift);
- glide.lines_moved += glide.pending_lines;
- glide.pending_lines = 0;
- // The rows that left: past the top going down, the bottom going up.
- if (!glide.ensure(gpa)) return glide.stop();
- const keep = if (glide.dir > 0) &glide.above else &glide.below;
- const count = if (glide.dir > 0) &glide.nabove else &glide.nbelow;
- 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;
- @memcpy(keep.*[i * w ..][0..w], glide.before[src_row * w ..][0..w]);
- }
- count.* = kept + shift;
- // What came into view is no longer past the other edge.
- const other = if (glide.dir > 0) &glide.below else &glide.above;
- const other_n = if (glide.dir > 0) &glide.nbelow else &glide.nabove;
- // ...and after a jump, the rows past it are another place's.
- const dropped = if (far) other_n.* else @min(other_n.*, shift);
- std.mem.copyForwards(pardes.Cell, other.*[0 .. @as(usize, other_n.* - dropped) * w], other.*[@as(usize, dropped) * w ..][0 .. @as(usize, other_n.* - dropped) * w]);
- other_n.* -= dropped;
- glide.kick(@as(f32, @floatFromInt(shift)) * @as(f32, @floatFromInt(glide.dir)), glide.now_ns, glide.motion);
- }
-
- fn ensure(glide: *Glide, gpa: std.mem.Allocator) bool {
- const n = @as(usize, glide.bw()) * glide.body_h * reach;
- inline for (.{ &glide.above, &glide.below }) |buffer| if (buffer.len < n) {
- const grown = gpa.realloc(buffer.*, n) catch return false;
- buffer.* = grown;
- };
- return true;
- }
-};
-
-fn sameRow(a: []const pardes.Cell, b: []const pardes.Cell) bool {
- for (a, b) |x, y| {
- if (x.default != y.default) return false;
- if (x.default) continue;
- if (!std.mem.eql(u8, x.grapheme(), y.grapheme()) or !std.meta.eql(x.style, y.style)) return false;
- }
- return true;
-}
+const Glide = pardes.glide.Glide;
-/// Feeds a wheel's notches to the core, then finds the text whose view
-/// moved since the last loop, however it moved (the wheel, a key, a Look),
-/// and snapshots its body as the last frame drew it so the frame about to
-/// be drawn can measure the move (Glide.measure); advances the glide and
-/// asks for frames while it moves.
+/// Feeds a wheel's notches to the core, then steps the glide (Glide.step)
+/// and asks for frames while it moves.
fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void {
const glide = &g.glide;
// The glide runs on the core's continuous path: frames every loop, and
// a virtual clock carried on.
defer core.shell_continuous = glide.moving() or g.sel_glow.fading or g.hover_glow.fading;
- glide.now_ns = core.now_ns;
- glide.motion = pardes.animation.Motion.of(core.settings.motion);
- if (glide.follow.offset != 0 or glide.follow.speed != 0) {
- glide.advance(core.now_ns, glide.motion);
- core.needs_frame = true;
- if (glide.follow.offset == 0) {
- glide.follow.offset = 0;
- glide.nabove = 0;
- glide.nbelow = 0;
- glide.lines_moved = 0;
- glide.rows_moved = 0;
- }
- }
const notch = g.scroll_notch;
g.scroll_notch = 0;
var wheeled: ?usize = null;
@@ -5284,67 +5077,9 @@ fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void {
}
if (wheeled != null) core.needs_frame = true;
}
- // ponytail: one glide at a time, the focused pane's, else the one that
- // moved most; a second pane scrolled in the same step lands. A glide a
- // pane (and a scissor each) if two ever need to move together.
- // The view that moved: the focused pane's, else the one that moved
- // most. A text's whatever moved it; a terminal's only under the wheel
- // (its output scrolls it at every line, and that is no glide); not one
- // a touchpad is moving by the pixel, nor one showing another file now.
- const s = &core.surface;
- var pick: ?usize = null;
- var delta: i64 = 0;
- for (core.panes, 0..) |slot, id| {
- const pane = slot orelse {
- glide.seen[id] = .{};
- continue;
- };
- const line: i64 = if (pane.file) |f| @intCast(f.scroll) else if (pane.isTerminal()) pardes.panes.terminal.gridOffset(pane) else 0;
- const file: u64 = if (pane.file) |f| std.hash.Wyhash.hash(0, f.path) else 0;
- const revision: u32 = if (pane.file) |f| f.revision else 0;
- const was = glide.seen[id];
- glide.seen[id] = .{ .serial = pane.serial, .line = line, .file = file, .revision = revision };
- if (was.serial != pane.serial or was.file != file or was.line == line or pane.collapsed or pane.hasPdf()) continue;
- // Generated text rewritten as its view moved (a query pane's live
- // rows, a +Search refilled, Callstack): the rows are another
- // text's, not this one's moved; it lands.
- if (pane.file) |f| if (f.output != null and revision != was.revision) continue;
- if (pane.file == null and wheeled != id) continue;
- if (g.scroll_pane == id and (g.scroll_lag != 0 or g.scroll_delta != 0)) continue;
- const moved = line - was.line;
- if (pick) |best| if (best == core.active or (id != core.active and @abs(moved) <= @abs(delta))) continue;
- pick = id;
- delta = moved;
- }
- const id = pick orelse return;
- const pane = core.panes[id].?;
- const r = core.rects[id];
- if (r.h <= pane.tag_rows or r.w <= config.GUTTER) return;
- const body_y = core.bodyTop(pane, r);
- const body_h = r.h -| pane.tag_rows;
- const w = r.w - config.GUTTER;
- const x0 = r.x + config.GUTTER;
- // 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.
- const drawn = for (s.regionList()) |region| {
- if (region.kind == .body and region.owner == id and region.serial == pane.serial) break std.meta.eql(region.rect, pardes.Rect{ .x = x0, .y = body_y, .w = w, .h = body_h });
- } else false;
- // A new pane or place: what was kept belongs to another picture.
- if (glide.pane != id or !std.meta.eql(glide.rect, r) or glide.body_y != body_y or glide.body_h != body_h or glide.pending) glide.stop();
- glide.pane = id;
- glide.rect = r;
- glide.body_y = body_y;
- glide.body_h = body_h;
- if (glide.motion.instant or !drawn or x0 + w > s.cols or body_y + body_h > s.rows or s.cells.len == 0) return glide.stop();
- // The body as the last frame drew it, which the move is measured from.
- const n = @as(usize, w) * body_h;
- if (glide.before.len < n) glide.before = gpa.realloc(glide.before, n) catch &.{};
- if (glide.before.len < n) return glide.stop();
- for (0..body_h) |row| @memcpy(glide.before[row * w ..][0..w], s.cells[(@as(usize, body_y) + row) * s.cols + x0 ..][0..w]);
- glide.pending = true;
- glide.dir = @intCast(std.math.sign(delta));
- glide.pending_lines += @floatFromInt(@abs(delta));
- core.needs_frame = true;
+ // Not a view a touchpad is moving by the pixel.
+ const direct: ?usize = if (g.scroll_lag != 0 or g.scroll_delta != 0) g.scroll_pane else null;
+ glide.step(core, gpa, wheeled, direct);
}
/// The glide's picture: the pane's body rows moved by the offset, and in
@@ -5456,7 +5191,7 @@ test "a notch scrolls the core at once and the picture glides after it, a wrappe
// nearest first.
try std.testing.expectApproxEqAbs(@as(f32, -2), g.glide.follow.offset, 1e-4);
try std.testing.expectEqual(@as(u16, 2), g.glide.nabove);
- try std.testing.expect(sameRow(g.glide.above[0 .. r.w - config.GUTTER], row_before));
+ try std.testing.expect(pardes.glide.sameRow(g.glide.above[0 .. r.w - config.GUTTER], row_before));
// Frame by frame it comes back, never past, and settles.
var frames: usize = 0;
while (g.glide.moving()) : (frames += 1) {
diff --git a/src/pardes.zig b/src/pardes.zig
index 829a7bfd..a1d1eef8 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -1,6 +1,7 @@
const std = @import("std");
pub const layout = @import("layout.zig");
pub const animation = @import("animation.zig");
+pub const glide = @import("glide.zig");
pub const FrameStats = @import("FrameStats.zig");
pub const Presentation = @import("Presentation.zig");
const uucode = @import("uucode");
@@ -483,6 +484,7 @@ test {
_ = @import("Pipe.zig");
_ = @import("colors.zig");
_ = @import("surface.zig");
+ _ = @import("glide.zig");
_ = @import("body_layer.zig");
_ = @import("draw.zig");
_ = @import("dump.zig"); // its tests ran nowhere before
diff --git a/src/tty/tty.zig b/src/tty/tty.zig
index b7bb2d54..c97ba4bf 100644
--- a/src/tty/tty.zig
+++ b/src/tty/tty.zig
@@ -1339,6 +1339,7 @@ fn localSession(
};
pardes.lsp.setStatusSink(&sh, lspStatusSink);
host_io.setExitWake(loop, wakeForExit);
+ defer sh.glide.deinit(gpa);
defer {
host_io.setExitWake(null, null);
pardes.lsp.setStatusSink(null, null);
@@ -1516,6 +1517,8 @@ const Shell = struct {
tracks: []const pardes.animation.Track = &.{},
/// T2: the cursor's jump trail (JumpTrail).
trail: JumpTrail = .{},
+ /// The view's glide, the GUI's motion drawn at whole rows (anim 12).
+ glide: pardes.glide.Glide = .{},
/// T7: when each pane's view last moved (ThumbFlash).
flash: ThumbFlash = .{},
/// PARDES_TEST_CLOCK's virtual time (host_io.testClock), and where the
@@ -1540,6 +1543,7 @@ const Shell = struct {
.now = clockNow,
.present = present,
.post_present = postPresent,
+ .poll_frame = stepGlide,
};
const vtable: host_io.Host.VTable = .{
@@ -1611,7 +1615,9 @@ const Shell = struct {
fn waitOnce(s: *Shell, wait: u32) void {
var batch: usize = 0;
if (wait != 0) {
- s.wake_to = s.core.nextWake() orelse 0;
+ // A virtual clock moves a frame a wake while anything moves
+ // continuously (the glide), as the real one would.
+ s.wake_to = s.core.nextWake() orelse if (s.core.continuous()) s.core.now_ns + pardes.animation.frame_ns else 0;
requestWake(s.io, wait);
}
pardes.turn.rest();
@@ -1789,8 +1795,17 @@ const Shell = struct {
s.loop.postEvent(.files_changed) catch {};
}
+ /// ponytail: a terminal's scrollback under the wheel lands here (the
+ /// wheel reaches the core as a mouse event, not counted as the GUI's
+ /// notches are); `wheeled` from the mouse event if it should glide.
+ fn stepGlide(ctx: ?*anyopaque) void {
+ const s = of(ctx);
+ s.glide.step(s.core, s.gpa, null, null);
+ }
+
fn pollFrame(ctx: ?*anyopaque) void {
const s = of(ctx);
+ stepGlide(ctx);
s.core.native_image_patches = kitty_shm.load(.acquire);
if (s.fs) |f| if (f.tick().pending) {
_ = s.loop.tryPostEvent(.fs_ready) catch {};
@@ -1831,8 +1846,12 @@ const Shell = struct {
const win = vx.window();
const truecolor = vx.caps.rgb or colortermTruecolor();
// Each of the shell's own motions below asks for frames while it runs.
- s.core.shell_continuous = false;
- const shadowed = chipShadows(s.frame.allocator(), surface, s.core, truecolor) catch surface;
+ if (s.glide.pending) s.glide.measure(s.gpa, canonical);
+ if (s.glide.moving()) s.glide.advance(s.core.now_ns, s.glide.motion);
+ s.core.shell_continuous = s.glide.moving();
+ s.core.frames.scroll = @round(s.glide.follow.offset);
+ const glided = pardes.glide.shifted(&s.glide, s.frame.allocator(), surface) catch surface;
+ const shadowed = chipShadows(s.frame.allocator(), glided, s.core, truecolor) catch glided;
const flashed = s.flash.apply(s.frame.allocator(), shadowed, s.core, truecolor) catch shadowed;
const trailed = s.trail.apply(s.frame.allocator(), flashed, s.core, truecolor) catch flashed;
paintCells(win, trailed.cells, trailed.cols, trailed.rows);
@@ -1922,7 +1941,7 @@ const Shell = struct {
vx.freeImage(s.tty.writer(), removed.value.id);
if (stale_len < stale.len) break;
}
- if (surface.cursor) |cur| paintCursor(win, cur.x, cur.y, cur.bar);
+ if (glided.cursor) |cur| paintCursor(win, cur.x, cur.y, cur.bar);
const tz_render = tracy.zone(@src(), "vx.render");
const before = s.tty.tty_writer.pos + s.tty.writer().end;
vx.render(s.tty.writer()) catch {};