summaryrefslogtreecommitdiff
path: root/src/gui/gui.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-24 14:09:45 -0300
committerGabriel Schneider <[email protected]>2026-10-24 20:59:59 -0300
commit114757fa94bd38b7620589709965d5f46e317b12 (patch)
tree10a3936b7277184705c54fa0897511ed3e021b1e /src/gui/gui.zig
parent55dd0c16e0dcc6a7e1c9e7babb31371bfe31119d (diff)
downloadpardes-114757fa94bd38b7620589709965d5f46e317b12.tar.gz
pardes-114757fa94bd38b7620589709965d5f46e317b12.zip
anim 3: every scroll glides, whatever moved the view (the wheel, Ctrl-d, a page, ge, gg, n, a Look), on the same spring; a move past what two frames share slides one body from the last frame's; a move during a glide adds to it; the picture moves at most a body in four frames
Diffstat (limited to 'src/gui/gui.zig')
-rw-r--r--src/gui/gui.zig295
1 files changed, 244 insertions, 51 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 5921d5dc..be92fb18 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -5025,12 +5025,18 @@ 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): a wheel's lines scroll 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). 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.
+/// 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. A move during a glide
+/// adds to it, its speed carried: it retargets, it never queues.
const Glide = struct {
spring: pardes.animation.Spring = .{},
/// The picture's offset from the core's rows now (rows; negative, the
@@ -5060,6 +5066,17 @@ const Glide = struct {
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.
+ /// When the picture was last sampled (sample).
+ shown_ns: u64 = 0,
+ seen: [pardes.MAX_PANES]struct { serial: u32 = 0, line: i64 = 0, file: u64 = 0 } = @splat(.{}),
+
+ /// How far the picture may lag, in bodies: the rows kept past each
+ /// edge, so a jump made while one glides carries on from where it is.
+ const reach = 2;
+ const bodies_per_s: f32 = 15;
fn moving(glide: *const Glide) bool {
return !glide.spring.settled or glide.pending;
@@ -5100,11 +5117,25 @@ const Glide = struct {
var pace = motion;
pace.omega = motion.omega * 1.5;
glide.spring.carry(-rows, now_ns, pace);
- // Never more than a body's rows behind: a flick past that jumps.
- const limit: f32 = @floatFromInt(glide.body_h);
+ // Never more than `reach` bodies behind: a flick past that jumps.
+ const limit: f32 = @floatFromInt(@as(u32, glide.body_h) * reach);
const now = glide.spring.value(now_ns);
if (@abs(now) > limit) glide.spring.carry(std.math.clamp(now, -limit, limit) - now, now_ns, pace);
glide.offset = glide.spring.value(now_ns);
+ glide.shown_ns = now_ns;
+ }
+
+ /// The picture at `now_ns`: the spring's, but never more than
+ /// `bodies_per_s` of the body a second from the last frame's (a body in
+ /// four frames at 60 Hz), so text that moves stays text one can read;
+ /// the spring slows below that before it lands, and the picture is on
+ /// it again by then.
+ fn sample(glide: *Glide, now_ns: u64) void {
+ const want = glide.spring.value(now_ns);
+ const dt: f32 = @floatCast(@as(f64, @floatFromInt(@min(now_ns -| glide.shown_ns, 2 * pardes.animation.frame_ns))) / std.time.ns_per_s);
+ glide.shown_ns = now_ns;
+ const cap = @as(f32, @floatFromInt(glide.body_h)) * bodies_per_s * dt;
+ glide.offset = std.math.clamp(want, glide.offset - cap, glide.offset + cap);
}
/// Finds how far the pane's body rows moved, `dir` rows at a time, by
@@ -5122,7 +5153,7 @@ 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.
- const shift: u16 = found: for (1..h) |k| {
+ var shift: u16 = found: for (1..h) |k| {
const kk: u16 = @intCast(k);
const allowed = @max(2, (h - kk) / 8);
var misses: usize = 0;
@@ -5135,11 +5166,16 @@ const Glide = struct {
if (misses > allowed) continue :found;
}
}
- // Too few rows left to tell: no glide.
+ // Too few rows left to tell: none in common.
if (h - kk < 3) break :found 0;
break :found kk;
} else 0;
- if (shift == 0) return glide.stop();
+ // 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;
@@ -5147,7 +5183,7 @@ const Glide = struct {
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 - shift);
+ 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;
@@ -5157,14 +5193,15 @@ const Glide = struct {
// 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;
- const dropped = @min(other_n.*, shift);
+ // ...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;
+ 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;
@@ -5182,8 +5219,10 @@ fn sameRow(a: []const pardes.Cell, b: []const pardes.Cell) bool {
return true;
}
-/// Feeds a wheel's notches to the core, snapshotting the body first so the
-/// next frame can measure the move (Glide.measure); advances the glide and
+/// 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.
fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void {
const glide = &g.glide;
@@ -5204,51 +5243,87 @@ fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void {
}
}
const notch = g.scroll_notch;
- if (notch == 0) return;
g.scroll_notch = 0;
- const id = g.scroll_pane orelse return;
- const pane = core.panes[id] orelse return resetScroll(g);
+ var wheeled: ?usize = null;
+ if (notch != 0) wheel: {
+ const id = g.scroll_pane orelse break :wheel;
+ const pane = core.panes[id] orelse {
+ resetScroll(g);
+ break :wheel;
+ };
+ const r = core.rects[id];
+ if (r.h <= pane.tag_rows or r.w <= config.GUTTER) {
+ resetScroll(g);
+ break :wheel;
+ }
+ var left = notch;
+ while (left != 0) {
+ const down = left > 0;
+ left += if (down) -1 else 1;
+ const was = pane.scroll();
+ // Shift: pardes's own scroll, never a program's wheel.
+ core.update(.{ .mouse = .{ .button = if (down) .wheel_down else .wheel_up, .kind = .press, .col = g.scroll_col, .row = g.scroll_row, .shift = true } });
+ if (pane.scroll() == was) break;
+ wheeled = id;
+ }
+ 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 was = glide.seen[id];
+ glide.seen[id] = .{ .serial = pane.serial, .line = line, .file = file };
+ if (was.serial != pane.serial or was.file != file or was.line == line or pane.collapsed or pane.hasPdf()) 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 resetScroll(g);
+ 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 s = &core.surface;
- const w = r.w - config.GUTTER;
- const x0 = r.x + config.GUTTER;
- const animate = !glide.motion.instant and x0 + w <= s.cols and body_y + body_h <= s.rows and s.cells.len != 0;
- if (animate) {
- 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) {
- for (0..body_h) |row| @memcpy(glide.before[row * w ..][0..w], s.cells[(@as(usize, body_y) + row) * s.cols + x0 ..][0..w]);
- }
- }
- var left = notch;
- var moved = false;
- const first = pane.scroll();
- defer glide.pending_lines += @floatFromInt(@abs(pane.scroll() - first));
- while (left != 0) {
- const down = left > 0;
- left += if (down) -1 else 1;
- const was = pane.scroll();
- // Shift: pardes's own scroll, never a program's wheel.
- core.update(.{ .mouse = .{ .button = if (down) .wheel_down else .wheel_up, .kind = .press, .col = g.scroll_col, .row = g.scroll_row, .shift = true } });
- if (pane.scroll() == was) break;
- moved = true;
- }
- if (!moved) return;
+ 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;
- if (animate and glide.before.len >= @as(usize, w) * body_h) {
- glide.pending = true;
- glide.dir = @intCast(std.math.sign(notch));
- }
}
/// The glide's picture: the pane's body rows moved by the offset, and in
@@ -5288,7 +5363,7 @@ fn emitGlideRows(g: *Gui, instances: [*]CellInstance, base: u32, surface: *parde
}
// The gap: rows past the top while the picture lags a move down, past
// the bottom for one up; as many as it opens.
- const gap: u16 = @intFromFloat(@min(@ceil(@abs(glide.offset)), @as(f32, @floatFromInt(bh))));
+ const gap: u16 = @intFromFloat(@min(@ceil(@abs(glide.offset)), @as(f32, @floatFromInt(@as(u32, bh) * Glide.reach))));
const nkept = if (glide.offset < 0) glide.nabove else glide.nbelow;
const rows = if (glide.offset < 0) glide.above else glide.below;
for (0..@min(gap, nkept)) |i| {
@@ -5339,6 +5414,8 @@ test "a notch scrolls the core at once and the picture glides after it, a wrappe
const body_y = core.bodyTop(core.panes[core.active].?, r);
const row_before = try gpa.dupe(pardes.Cell, s.cells[@as(usize, body_y + 1) * s.cols + r.x + config.GUTTER ..][0 .. r.w - config.GUTTER]);
defer gpa.free(row_before);
+ // The loop has seen the view once (stepGlide finds a view that moved).
+ stepGlide(&g, core, gpa);
// One notch down: the core is a line on at once.
g.scroll_notch = 1;
const scrolled = core.panes[core.active].?.scroll();
@@ -5374,6 +5451,122 @@ test "a notch scrolls the core at once and the picture glides after it, a wrappe
try std.testing.expect(!g.glide.pending and !g.glide.moving());
}
+/// The scripted scroll sequence of the glide tests: a frame a 60 Hz
+/// refresh, as the paced loop draws them (waitInput, then stepGlide, the
+/// core's render and the frame's measure), an input on the frames `script`
+/// names; each frame's picture offset and the core's first line.
+const GlideRun = struct {
+ const Step = struct { frame: usize, key: pardes.Key };
+ offsets: [600]f32 = undefined,
+ lines: [600]i64 = undefined,
+ frames: usize = 0,
+
+ fn play(seq: *GlideRun, g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator, arena: *std.heap.ArenaAllocator, script: []const Step, frames: usize) !void {
+ var now: u64 = core.now_ns;
+ var next: usize = 0;
+ for (0..frames) |frame| {
+ while (next < script.len and script[next].frame == frame) : (next += 1) core.update(.{ .key = script[next].key });
+ now += std.time.ns_per_s / 60;
+ core.advance(now);
+ stepGlide(g, core, gpa);
+ _ = arena.reset(.retain_capacity);
+ const s = try core.render(arena.allocator());
+ if (g.glide.pending) g.glide.measure(gpa, s);
+ if (g.glide.moving()) g.glide.sample(g.glide.now_ns);
+ seq.offsets[frame] = g.glide.offset;
+ seq.lines[frame] = @intCast(core.panes[core.active].?.file.?.scroll);
+ }
+ seq.frames = frames;
+ }
+
+ /// Frames from `from` until the picture is still: the scroll's glide.
+ fn settle(seq: *const GlideRun, from: usize) usize {
+ var k = from;
+ while (k < seq.frames and seq.offsets[k] != 0) k += 1;
+ return k - from;
+ }
+};
+
+test "every scroll glides on one law: a half page, a page, the far jumps, a run of them retargeting, and Motion off lands at once" {
+ const gpa = std.testing.allocator;
+ const core = try pardes.Pardes.init(gpa, .{ .cols = 100, .rows = 40 });
+ defer core.deinit();
+ var text: std.ArrayList(u8) = .empty;
+ defer text.deinit(gpa);
+ // Numbered lines no two alike, short enough never to wrap.
+ for (0..3000) |i| try text.print(gpa, "{d:0>5} line {d} of the long file\n", .{ i, i * 7 % 13 });
+ _ = try core.setTestFile(text.items);
+ core.settings.motion = .smooth;
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ core.advance(std.time.ns_per_s);
+ _ = try core.render(arena.allocator());
+ var g: Gui = undefined;
+ g.glide = .{};
+ defer g.glide.deinit(gpa);
+ g.scroll_notch = 0;
+ g.scroll_pane = null;
+ g.scroll_delta = 0;
+ g.scroll_lag = 0;
+ g.scroll_edge_len = 0;
+ g.sel_glow = .{};
+ g.hover_glow = .{};
+ const body_h: f32 = @floatFromInt(core.rects[core.active].h - core.panes[core.active].?.tag_rows);
+ const ctrl = struct {
+ fn key(cp: u21) pardes.Key {
+ return .{ .cp = cp, .ctrl = true };
+ }
+ };
+ const script = [_]GlideRun.Step{
+ .{ .frame = 2, .key = ctrl.key('d') },
+ .{ .frame = 40, .key = ctrl.key('f') },
+ .{ .frame = 80, .key = .{ .cp = 'g', .text = "g" } },
+ .{ .frame = 80, .key = .{ .cp = 'e', .text = "e" } },
+ .{ .frame = 120, .key = .{ .cp = 'g', .text = "g" } },
+ .{ .frame = 120, .key = .{ .cp = 'g', .text = "g" } },
+ // Faster than they settle: each adds to the glide under way.
+ .{ .frame = 160, .key = ctrl.key('d') },
+ .{ .frame = 165, .key = ctrl.key('d') },
+ .{ .frame = 170, .key = ctrl.key('d') },
+ .{ .frame = 175, .key = ctrl.key('d') },
+ };
+ var seq: GlideRun = .{};
+ try seq.play(&g, core, gpa, &arena, &script, 240);
+ // Every one moves the picture on the frame it is drawn in, and the
+ // picture lags by what the core moved (a far jump, by one body).
+ var longest: usize = 0;
+ var shortest: usize = std.math.maxInt(usize);
+ for ([_]usize{ 2, 40, 80, 120 }) |at| {
+ const moved: f32 = @floatFromInt(@abs(seq.lines[at] - seq.lines[at - 1]));
+ try std.testing.expect(moved >= 1);
+ const lag = @abs(seq.offsets[at]);
+ try std.testing.expect(lag > 0.5 * @min(moved, body_h));
+ try std.testing.expect(lag <= @min(moved, body_h) + 1e-3);
+ // Settles on the same law whatever the distance: a few frames for
+ // a half page, not many more for a jump of 3000 lines.
+ const frames = seq.settle(at);
+ longest = @max(longest, frames);
+ shortest = @min(shortest, frames);
+ try std.testing.expect(frames >= 8 and frames <= 24);
+ }
+ try std.testing.expect(longest - shortest <= 5);
+ // Readable: never more than a quarter of a body a frame once it moves.
+ for (1..seq.frames) |k| {
+ if (seq.lines[k] != seq.lines[k - 1]) continue;
+ try std.testing.expect(@abs(seq.offsets[k] - seq.offsets[k - 1]) <= body_h / 4 + 1e-3);
+ }
+ // The seq of half pages: one glide, never at rest between them, and
+ // done as soon after the last as one half page alone takes.
+ for (160..180) |k| try std.testing.expect(seq.offsets[k] != 0);
+ try std.testing.expect(seq.settle(175) <= seq.settle(2) + 4);
+ // Motion off: the same scroll lands at once.
+ core.settings.motion = .off;
+ var off: GlideRun = .{};
+ try off.play(&g, core, gpa, &arena, &.{.{ .frame = 1, .key = ctrl.key('u') }}, 10);
+ try std.testing.expect(off.lines[1] != off.lines[0]);
+ for (off.offsets[0..off.frames]) |offset| try std.testing.expectEqual(@as(f32, 0), offset);
+}
+
fn resetScroll(g: *Gui) void {
g.scroll_pane = null;
g.scroll_delta = 0;
@@ -6071,7 +6264,7 @@ fn renderFrame(
if (surface.chrome.fx & 1 == 0 or surface.panelTracks().len != 0) g.sel_glow = .{ .now_ns = g.sel_glow.now_ns };
g.hover_glow.now_ns = g.sel_glow.now_ns;
if (surface.chrome.fx & 2 == 0 or surface.panelTracks().len != 0) g.hover_glow = .{ .now_ns = g.sel_glow.now_ns };
- if (g.glide.moving()) g.glide.offset = g.glide.spring.value(g.glide.now_ns);
+ if (g.glide.moving()) g.glide.sample(g.glide.now_ns);
cell_decor = CellDecor.of(chrome);
plaque_through_len = 0;
// G13: a body's dots follow its text at a quarter of its speed: its