diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-24 23:16:11 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-25 01:30:09 -0300 |
| commit | 706b9d98fdce29a58ffe5a8a5417d6b2810e6d2c (patch) | |
| tree | 1367ad33c41891dc89a7507b156660e0694e5a29 /src/gui/gui.zig | |
| parent | 6e8a6a63d45b2caef7b649d358f836e6cc4134d6 (diff) | |
| download | pardes-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/gui/gui.zig')
| -rw-r--r-- | src/gui/gui.zig | 279 |
1 files changed, 7 insertions, 272 deletions
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) { |
