From ecdb0996147f319a4427273bf276e1fc5bf89156 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 11:50:43 -0300 Subject: effects out, the panel transitions: the eleven Panel words in both tracks go, with the core's Presentation (tracks, the frozen previous grid, cell diffs, the acknowledged-geometry pending guard, and the pointer's mapping through a moving pane, so a pointer's cell is the cell it is over), draw.zig's core-composed character effects, animation.zig's transition half, the Surface's tracks and previous grid, the tty's compositor (src/tui/panel_compositor.zig: the glide's rows move into tui.zig) and the window's track groups, frozen old layers, last-frame image snapshot, transition instance fields and shader paths (ui.vert, ui.frag's crossfade, image.vert and image.frag, the transition-.ppm capture); acknowledgePanelPresentation becomes `presented`, which a frontend calls after drawing a frame (a drag's moving end and a look-hover follow what is under the pointer now), and its tape site with it; the pointer tape site goes; GUI golden 18-mid-transition goes, ttyhelp's golden loses the `SPC a` rows (its wait is on `SPC Y` now), the window monkey loses PanelSlide, and the settings behaviour test's floor of on/off words is ten Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/tui/panel_compositor.zig | 785 ------------------------------------------- src/tui/tui.zig | 132 +++++++- 2 files changed, 121 insertions(+), 796 deletions(-) delete mode 100644 src/tui/panel_compositor.zig (limited to 'src/tui') diff --git a/src/tui/panel_compositor.zig b/src/tui/panel_compositor.zig deleted file mode 100644 index 142a6fae..00000000 --- a/src/tui/panel_compositor.zig +++ /dev/null @@ -1,785 +0,0 @@ -//! TTY implementation of panel transitions. -//! -//! The core publishes presentation cells plus POD tracks. It has already -//! composed PanelAscii because byte stepping is semantic, while this backend -//! copies the grid for geometry and dissolve operations. No terminal escape -//! trick or pixel assumption leaks into the core. - -const std = @import("std"); -const pardes = @import("../pardes.zig"); -const layout = @import("../layout.zig"); -const animation = @import("../animation.zig"); - -const Box = animation.Box; -const Track = animation.Track; -const Glide = pardes.glide.Glide; - -/// What the tty draws this frame: the canonical surface until a pass -/// changes a cell or the cursor, then the one copy of it the first such -/// pass makes (`own`), which every later pass changes in place. -pub const Frame = struct { - arena: std.mem.Allocator, - canonical: *const pardes.Surface, - copy: ?*pardes.Surface = null, - - pub fn shown(f: *const Frame) *const pardes.Surface { - return f.copy orelse f.canonical; - } - - /// The copy, made at the first call. - pub fn own(f: *Frame) !*pardes.Surface { - if (f.copy) |c| return c; - const c = try f.arena.create(pardes.Surface); - c.* = f.canonical.*; - c.cells = try f.arena.dupe(pardes.Cell, f.canonical.cells); - f.copy = c; - return c; - } -}; - -/// Kitty placements cannot be resampled through the character-grid transform. -/// Keep their transmitted pixels cached, but omit the placement while its pane -/// geometry moves. Data effects animate changed grid cells in place and have -/// no honest old Kitty bitmap, so leaving an unchanged attachment canonical is -/// better than making it disappear for the whole reveal and pop back. -pub fn hidesAttachment(tracks: []const Track, serial: u32) bool { - for (tracks) |track| { - if (track.serial != serial or !track.active()) continue; - if (track.effect != .slide and track.effect != .zoom and track.effect != .vertical) continue; - // The last active sample is already the exact canonical frame. Place - // the image there too, rather than making it pop in one tick later. - if (track.frame + 1 < track.frames()) return true; - } - return false; -} - -/// This frame's TTY presentation of the panel tracks, drawn on the frame's -/// copy (the canonical surface is left as it is when idle). Geometry exposed -/// by a moving panel uses the theme's page background when it has one; null -/// deliberately leaves the host terminal's default background showing. -pub fn compose(frame: *Frame, tracks: []const Track, theme_bg: ?[3]u8) !void { - const source = frame.canonical; - var active = false; - var noncanonical = false; - for (tracks) |track| { - if (!drawable(source, track)) continue; - active = true; - noncanonical = noncanonical or track.amount() < 1.0; - } - if (!active) return; - // Every effect's last active sample is its exact canonical endpoint. Keep - // the real cursor and attachments in that sample instead of suppressing - // them for one extra frame that is otherwise pixel-identical. - if (!noncanonical) return; - - const out = try frame.own(); - // A cursor has no pane/phase record of its own. All native backends hide - // it for the short transition rather than disagreeing about whether it - // should move, stay static, or cover an overlapping opening pane. - out.cursor = null; - - // Clear every final geometry before drawing any presented geometry. If - // clear and draw are interleaved, a new panel can erase the old full-width - // panel that was deliberately drawn underneath it at frame zero. - for (tracks) |track| - if (drawable(source, track) and (track.effect == .slide or track.effect == .zoom)) - clearBox(out, track.to, theme_bg); - - // A vertical opener starts over the exact old pixels in its fixed target - // clip. Its new content then rises into that clip from below. Restore all - // bases before painting anything so an opener cannot erase an earlier - // moving panel merely because its pane slot sorts later. - for (tracks) |track| - if (drawable(source, track) and track.effect == .vertical and track.phase == .opening) - restorePrevious(out, source, track.contentBox()); - - // Stable layout motion first, newly opening panels above it, and closing - // tombstones last. A closing pane no longer owns input or canonical cells, - // but its frozen old content remains the top visual until it slides out. - // A slide or a zoom is clipped by nothing but the screen. - const screen: Box = .{ .w = @floatFromInt(out.cols), .h = @floatFromInt(out.rows) }; - for ([_]animation.Phase{ .moving, .opening, .closing }) |phase| { - for (tracks) |track| { - if (!drawable(source, track) or track.phase != phase) continue; - switch (track.effect) { - .off => {}, - .slide, .zoom => transformClipped(out, source.cols, source.rows, source.cells, track, screen), - .dissolve => dissolve(out, source, track), - // Pardes core has already composed every character effect - // into source.cells — byte walks, glyph motion, and per-cell - // locks are semantic. TTY only owns cursor suppression here. - .ascii, .edges, .fall, .wave, .curtain, .scramble, .typewriter => {}, - .vertical => vertical(out, source, track), - } - } - } -} - -/// The tty's picture of the glide (glide.zig, 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. In place: the rows are walked from the side they move toward, -/// so each is read before a move writes over it. -pub fn glideRows(frame: *Frame, glide: *const Glide) !void { - const surface = frame.shown(); - for (surface.panelTracks()) |track| - if (track.active() and track.pane == glide.pane) return; - const k: i32 = @intFromFloat(@round(glide.follow.offset)); - const w = glide.bw(); - const h = glide.body_h; - const x0 = glide.rect.x + pardes.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; - const out = try frame.own(); - const cols: usize = out.cols; - for (0..h) |i| { - const r = if (k > 0) i else h - 1 - i; - 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) - out.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; - }; -} - -/// Content effects have no honest frame without the frozen old grid. In -/// production the core drops such tracks before publishing Surface; retain -/// this guard at the backend boundary so a partial/custom caller also snaps -/// to canonical content instead of manufacturing blanks. -fn drawable(source: *const pardes.Surface, track: Track) bool { - return track.active() and - (!track.effect.needsPreviousGrid() or source.hasPanelDiff()); -} - -fn dissolve(out: *pardes.Surface, source: *const pardes.Surface, track: Track) void { - const area = clipped(track.to, out.cols, out.rows) orelse return; - const amount = track.amount(); - var y = area.y0; - while (y < area.y1) : (y += 1) { - var x = area.x0; - while (x < area.x1) : (x += 1) { - if (!source.panelCellChanged(x, y)) continue; - if (animation.dissolveRevealed( - track.serial, - x - area.x0, - y - area.y0, - amount, - )) continue; - out.at(x, y).* = previousCell(source, x, y).*; - } - } -} - -fn previousCell(source: *const pardes.Surface, x: u16, y: u16) *const pardes.Cell { - std.debug.assert(source.hasPanelDiff()); - return &source.previous_cells[@as(usize, y) * source.cols + x]; -} - -fn restorePrevious(out: *pardes.Surface, source: *const pardes.Surface, box: Box) void { - const area = clipped(box, out.cols, out.rows) orelse return; - var y = area.y0; - while (y < area.y1) : (y += 1) { - var x = area.x0; - while (x < area.x1) : (x += 1) - out.at(x, y).* = previousCell(source, x, y).*; - } -} - -fn vertical(out: *pardes.Surface, source: *const pardes.Surface, track: Track) void { - const cells = if (track.phase == .closing) source.previous_cells else source.cells; - transformClipped(out, source.cols, source.rows, cells, track, track.contentBox()); -} - -/// Translate one old/new canonical panel through a clip: a vertical -/// panel's fixed lifecycle box, or the screen for a slide or a zoom. -/// Sampling is always relative to contentBox: an opener and a moving panel -/// read their new target, a closer the old box retained after that pane -/// was destroyed (only a vertical one closes). -fn transformClipped( - out: *pardes.Surface, - source_cols: u16, - source_rows: u16, - cells: []const pardes.Cell, - track: Track, - clip: Box, -) void { - const shown = track.presented(); - if (shown.w <= 0 or shown.h <= 0) return; - const dst = clipped(shown, out.cols, out.rows) orelse return; - const content = track.contentBox(); - var y = dst.y0; - while (y < dst.y1) : (y += 1) { - var x = dst.x0; - while (x < dst.x1) : (x += 1) { - // The core hit-tests cell CENTRES against this same half-open box. - // floor/ceil clipping includes boundary cells for iteration, but - // they are not visible unless their centre is actually inside. - // Test that containment before division: at large u16 grids f32 - // can round an inside right-edge ratio to exactly 1.0. - const cx = @as(f32, @floatFromInt(x)) + 0.5; - const cy = @as(f32, @floatFromInt(y)) + 0.5; - if (cx < shown.x or cx >= shown.x + shown.w or - cy < shown.y or cy >= shown.y + shown.h or - cx < clip.x or cx >= clip.x + clip.w or - cy < clip.y or cy >= clip.y + clip.h) continue; - const u = std.math.clamp((cx - shown.x) / shown.w, 0, 0.999_999); - const v = std.math.clamp((cy - shown.y) / shown.h, 0, 0.999_999); - const sx: i32 = @intFromFloat(@floor(content.x + u * content.w)); - const sy: i32 = @intFromFloat(@floor(content.y + v * content.h)); - if (sx < 0 or sy < 0 or sx >= source_cols or sy >= source_rows) continue; - out.at(x, y).* = cells[@as(usize, @intCast(sy)) * source_cols + @as(usize, @intCast(sx))]; - } - } -} - -fn clearBox(surface: *pardes.Surface, box: Box, theme_bg: ?[3]u8) void { - const area = clipped(box, surface.cols, surface.rows) orelse return; - var blank: pardes.Cell = .{}; - if (theme_bg) |rgb| { - blank.default = false; - blank.style.bg = .{ .rgb = rgb }; - } - var y = area.y0; - while (y < area.y1) : (y += 1) { - var x = area.x0; - while (x < area.x1) : (x += 1) surface.at(x, y).* = blank; - } -} - -const GridBox = struct { x0: u16, y0: u16, x1: u16, y1: u16 }; - -fn clipped(box: Box, cols: u16, rows: u16) ?GridBox { - const x0: i32 = @intFromFloat(@floor(box.x)); - const y0: i32 = @intFromFloat(@floor(box.y)); - const x1: i32 = @intFromFloat(@ceil(box.x + box.w)); - const y1: i32 = @intFromFloat(@ceil(box.y + box.h)); - const area: GridBox = .{ - .x0 = @intCast(std.math.clamp(x0, 0, @as(i32, cols))), - .y0 = @intCast(std.math.clamp(y0, 0, @as(i32, rows))), - .x1 = @intCast(std.math.clamp(x1, 0, @as(i32, cols))), - .y1 = @intCast(std.math.clamp(y1, 0, @as(i32, rows))), - }; - return if (area.x0 < area.x1 and area.y0 < area.y1) area else null; -} - -/// `compose` over a frame of `surface` (the tests): what the frame shows, -/// `surface` itself when nothing was drawn (read through `at`). -fn composed(arena: std.mem.Allocator, surface: *pardes.Surface, tracks: []const Track, theme_bg: ?[3]u8) !*pardes.Surface { - var frame: Frame = .{ .arena = arena, .canonical = surface }; - try compose(&frame, tracks, theme_bg); - return frame.copy orelse surface; -} - -/// Each of `cells` a one-byte glyph of `glyphs`, in order. -fn testGlyphs(cells: []pardes.Cell, glyphs: []const u8) void { - for (cells, glyphs) |*cell, glyph| { - cell.text[0] = glyph; - cell.len = 1; - cell.default = false; - } -} - -test "TTY consumes core-composed ASCII cells without reinterpreting glyphs" { - var cells: [8]pardes.Cell = @splat(.{}); - var previous: [8]pardes.Cell = @splat(.{}); - const diffs: [8]pardes.PanelCellDiff = @splat(.visual); - testGlyphs(&cells, "XXXXXXXX"); - testGlyphs(&previous, "OOOOOOOO"); - var surface: pardes.Surface = .{ - .cols = 4, - .rows = 2, - .cells = &cells, - .previous_cells = &previous, - .cell_diffs = &diffs, - }; - var arena = std.heap.ArenaAllocator.init(std.testing.allocator); - defer arena.deinit(); - const track: Track = .{ - .serial = 9, - .effect = .ascii, - .frame = 0, - .from = .{ .w = 4, .h = 2 }, - .to = .{ .w = 4, .h = 2 }, - }; - const animated = try composed(arena.allocator(), &surface, &.{track}, null); - try std.testing.expect(animated != &surface); - try std.testing.expectEqualStrings("X", surface.at(0, 0).grapheme()); - for (animated.cells) |cell| try std.testing.expectEqualStrings("X", cell.grapheme()); -} - -test "dissolve changes only diff cells from old to new" { - var cells: [2]pardes.Cell = @splat(.{}); - var previous: [2]pardes.Cell = @splat(.{}); - testGlyphs(&cells, "NU"); - testGlyphs(&previous, "OU"); - const diffs = [_]pardes.PanelCellDiff{ .{ .ascii = .{ .from = 'O', .to = 'N' } }, .unchanged }; - var surface: pardes.Surface = .{ - .cols = 2, - .rows = 1, - .cells = &cells, - .previous_cells = &previous, - .cell_diffs = &diffs, - }; - var arena = std.heap.ArenaAllocator.init(std.testing.allocator); - defer arena.deinit(); - var track: Track = .{ - .serial = 1, - .effect = .dissolve, - .from = .{ .w = 2, .h = 1 }, - .to = .{ .w = 2, .h = 1 }, - }; - - const first = try composed(arena.allocator(), &surface, &.{track}, null); - try std.testing.expectEqualStrings("O", first.at(0, 0).grapheme()); - try std.testing.expectEqual(cells[1], first.at(1, 0).*); - - _ = arena.reset(.retain_capacity); - track.frame = track.effect.frames() - 1; - const final = try composed(arena.allocator(), &surface, &.{track}, null); - try std.testing.expectEqual(cells[0], final.at(0, 0).*); - try std.testing.expectEqual(cells[1], final.at(1, 0).*); -} - -test "TTY content effects never touch cells outside the published diff" { - var cells: [4]pardes.Cell = @splat(.{}); - var previous: [4]pardes.Cell = @splat(.{}); - testGlyphs(&cells, "NUQV"); - // Deliberately disagree even where the mask says unchanged. This makes - // the test prove that the compositor obeys the core cell diff rather than - // merely appearing correct because old and new happened to match there. - testGlyphs(&previous, "OXRY"); - const diffs = [_]pardes.PanelCellDiff{ .visual, .unchanged, .visual, .unchanged }; - var surface: pardes.Surface = .{ - .cols = 4, - .rows = 1, - .cells = &cells, - .previous_cells = &previous, - .cell_diffs = &diffs, - }; - var arena = std.heap.ArenaAllocator.init(std.testing.allocator); - defer arena.deinit(); - - for ([_]animation.Transition{ .ascii, .dissolve }) |effect| { - const track: Track = .{ - .serial = 17, - .effect = effect, - .from = .{ .w = 4, .h = 1 }, - .to = .{ .w = 4, .h = 1 }, - }; - const animated = try composed(arena.allocator(), &surface, &.{track}, null); - try std.testing.expectEqual(cells[1], animated.at(1, 0).*); - try std.testing.expectEqual(cells[3], animated.at(3, 0).*); - _ = arena.reset(.retain_capacity); - } -} - -test "content transition without a diff snaps to canonical surface" { - var cells = [_]pardes.Cell{.{}}; - testGlyphs(&cells, "N"); - var surface: pardes.Surface = .{ - .cols = 1, - .rows = 1, - .cells = &cells, - .cursor = .{ .x = 0, .y = 0 }, - }; - var arena = std.heap.ArenaAllocator.init(std.testing.allocator); - defer arena.deinit(); - - for ([_]animation.Transition{ .dissolve, .ascii, .vertical }) |effect| { - const track: Track = .{ - .effect = effect, - .phase = .opening, - .from = .{ .y = 1, .w = 1, .h = 1 }, - .to = .{ .w = 1, .h = 1 }, - }; - try std.testing.expectEqual(&surface, try composed(arena.allocator(), &surface, &.{track}, null)); - } -} - -test "exact transition endpoint preserves the canonical cursor" { - var cells = [_]pardes.Cell{.{}}; - var previous = cells; - const diffs = [_]pardes.PanelCellDiff{.visual}; - var surface: pardes.Surface = .{ - .cols = 1, - .rows = 1, - .cells = &cells, - .cursor = .{ .x = 0, .y = 0 }, - .previous_cells = &previous, - .cell_diffs = &diffs, - }; - const track: Track = .{ - .effect = .ascii, - .frame = animation.Transition.ascii.frames() - 1, - .from = .{ .w = 1, .h = 1 }, - .to = .{ .w = 1, .h = 1 }, - }; - var arena = std.heap.ArenaAllocator.init(std.testing.allocator); - defer arena.deinit(); - - const result = try composed(arena.allocator(), &surface, &.{track}, null); - try std.testing.expectEqual(&surface, result); - try std.testing.expect(result.cursor != null); -} - -test "vertical opening rises through a fixed old-grid clip" { - var cells: [12]pardes.Cell = @splat(.{}); - var previous: [12]pardes.Cell = @splat(.{}); - testGlyphs(&cells, "aaabbbcccddd"); - testGlyphs(&previous, "000111222333"); - const diffs: [12]pardes.PanelCellDiff = @splat(.visual); - var surface: pardes.Surface = .{ - .cols = 3, - .rows = 4, - .cells = &cells, - .previous_cells = &previous, - .cell_diffs = &diffs, - }; - var arena = std.heap.ArenaAllocator.init(std.testing.allocator); - defer arena.deinit(); - const target: Box = .{ .y = 1, .w = 3, .h = 2 }; - var track: Track = .{ - .serial = 5, - .phase = .opening, - .effect = .vertical, - .from = animation.openingBox(.vertical, target, 3), - .to = target, - }; - - const first = try composed(arena.allocator(), &surface, &.{track}, null); - try std.testing.expectEqualStrings("1", first.at(0, 1).grapheme()); - try std.testing.expectEqualStrings("2", first.at(0, 2).grapheme()); - try std.testing.expectEqualStrings("a", first.at(0, 0).grapheme()); - try std.testing.expectEqualStrings("d", first.at(0, 3).grapheme()); - - _ = arena.reset(.retain_capacity); - track.frame = 2; - const entering = try composed(arena.allocator(), &surface, &.{track}, null); - try std.testing.expectEqualStrings("1", entering.at(0, 1).grapheme()); - try std.testing.expectEqualStrings("b", entering.at(0, 2).grapheme()); - try std.testing.expectEqualStrings("a", entering.at(0, 0).grapheme()); - try std.testing.expectEqualStrings("d", entering.at(0, 3).grapheme()); - - _ = arena.reset(.retain_capacity); - track.frame = track.effect.frames() - 1; - const final = try composed(arena.allocator(), &surface, &.{track}, null); - try std.testing.expectEqualStrings("b", final.at(0, 1).grapheme()); - try std.testing.expectEqualStrings("c", final.at(0, 2).grapheme()); -} - -test "vertical closing drops frozen content over canonical cells" { - var cells: [12]pardes.Cell = @splat(.{}); - var previous: [12]pardes.Cell = @splat(.{}); - testGlyphs(&cells, "aaabbbcccddd"); - testGlyphs(&previous, "000111222333"); - const diffs: [12]pardes.PanelCellDiff = @splat(.visual); - var surface: pardes.Surface = .{ - .cols = 3, - .rows = 4, - .cells = &cells, - .previous_cells = &previous, - .cell_diffs = &diffs, - }; - var arena = std.heap.ArenaAllocator.init(std.testing.allocator); - defer arena.deinit(); - const old_box: Box = .{ .y = 1, .w = 3, .h = 2 }; - var track: Track = .{ - .serial = 5, - .phase = .closing, - .effect = .vertical, - .from = old_box, - .to = animation.closingBox(.vertical, old_box), - }; - - const first = try composed(arena.allocator(), &surface, &.{track}, null); - try std.testing.expectEqualStrings("1", first.at(0, 1).grapheme()); - try std.testing.expectEqualStrings("2", first.at(0, 2).grapheme()); - - _ = arena.reset(.retain_capacity); - track.frame = 8; - const leaving = try composed(arena.allocator(), &surface, &.{track}, null); - try std.testing.expectEqualStrings("b", leaving.at(0, 1).grapheme()); - try std.testing.expectEqualStrings("1", leaving.at(0, 2).grapheme()); - try std.testing.expectEqualStrings("a", leaving.at(0, 0).grapheme()); - try std.testing.expectEqualStrings("d", leaving.at(0, 3).grapheme()); - - _ = arena.reset(.retain_capacity); - track.frame = track.effect.frames() - 1; - const final = try composed(arena.allocator(), &surface, &.{track}, null); - try std.testing.expectEqual(cells[3], final.at(0, 1).*); - try std.testing.expectEqual(cells[6], final.at(0, 2).*); -} - -test "closing content paints after opening content regardless of track order" { - var cells = [_]pardes.Cell{.{}}; - var previous = [_]pardes.Cell{.{}}; - testGlyphs(&cells, "N"); - testGlyphs(&previous, "O"); - const diffs = [_]pardes.PanelCellDiff{.visual}; - var surface: pardes.Surface = .{ - .cols = 1, - .rows = 1, - .cells = &cells, - .previous_cells = &previous, - .cell_diffs = &diffs, - }; - var arena = std.heap.ArenaAllocator.init(std.testing.allocator); - defer arena.deinit(); - const box: Box = .{ .w = 1, .h = 1 }; - const closing: Track = .{ - .serial = 1, - .phase = .closing, - .effect = .vertical, - .from = box, - .to = animation.closingBox(.vertical, box), - }; - const opening: Track = .{ - .serial = 2, - .phase = .opening, - .effect = .ascii, - .frame = animation.Transition.ascii.frames() - 1, - .from = box, - .to = box, - }; - - const animated = try composed(arena.allocator(), &surface, &.{ closing, opening }, null); - try std.testing.expectEqualStrings("O", animated.at(0, 0).grapheme()); -} - -test "opening panel cannot erase an existing panel drawn underneath" { - var cells: [8]pardes.Cell = @splat(.{}); - testGlyphs(&cells, "AAAABBBB"); - var surface: pardes.Surface = .{ .cols = 8, .rows = 1, .cells = &cells }; - var arena = std.heap.ArenaAllocator.init(std.testing.allocator); - defer arena.deinit(); - const tracks = [_]Track{ - .{ - .serial = 1, - .phase = .moving, - .effect = .slide, - .from = .{ .w = 8, .h = 1 }, - .to = .{ .w = 4, .h = 1 }, - }, - .{ - .serial = 2, - .phase = .opening, - .effect = .slide, - .from = .{ .x = 8, .w = 4, .h = 1 }, - .to = .{ .x = 4, .w = 4, .h = 1 }, - }, - }; - const animated = try composed(arena.allocator(), &surface, &tracks, null); - try std.testing.expectEqualStrings("A", animated.at(7, 0).grapheme()); -} - -test "geometry gap is a blank painted in an explicit theme background" { - var cells: [4]pardes.Cell = @splat(.{}); - testGlyphs(&cells, "ABCD"); - for (&cells) |*cell| { - cell.style = .{ - .fg = .{ .rgb = .{ 220, 210, 200 } }, - .bg = .{ .rgb = .{ 30, 20, 10 } }, - .bold = true, - .font_role = .tagline, - }; - } - var surface: pardes.Surface = .{ .cols = 4, .rows = 1, .cells = &cells }; - var arena = std.heap.ArenaAllocator.init(std.testing.allocator); - defer arena.deinit(); - const track: Track = .{ - .serial = 1, - .effect = .slide, - .from = .{ .x = 2, .w = 2, .h = 1 }, - .to = .{ .w = 4, .h = 1 }, - }; - const page_bg = [3]u8{ 248, 244, 232 }; - const animated = try composed(arena.allocator(), &surface, &.{track}, page_bg); - - const gap = animated.at(0, 0); - try std.testing.expect(!gap.default); - try std.testing.expectEqualStrings(" ", gap.grapheme()); - try std.testing.expectEqual(pardes.Color{ .rgb = page_bg }, gap.style.bg); - try std.testing.expectEqual(pardes.Color.default, gap.style.fg); - try std.testing.expect(!gap.style.bold); - try std.testing.expectEqual(pardes.FontRole.body, gap.style.font_role); - // The clear supplies only exposed geometry. Cells sampled from the moving - // panel retain all canonical content and style fields byte-for-byte. - try std.testing.expectEqual(cells[1], animated.at(2, 0).*); -} - -test "geometry gap stays terminal-default for a transparent theme" { - var cells: [2]pardes.Cell = @splat(.{}); - testGlyphs(&cells, "AB"); - var surface: pardes.Surface = .{ .cols = 2, .rows = 1, .cells = &cells }; - var arena = std.heap.ArenaAllocator.init(std.testing.allocator); - defer arena.deinit(); - const track: Track = .{ - .serial = 1, - .effect = .slide, - .from = .{ .x = 1, .w = 1, .h = 1 }, - .to = .{ .w = 2, .h = 1 }, - }; - const animated = try composed(arena.allocator(), &surface, &.{track}, null); - - const gap = animated.at(0, 0); - try std.testing.expect(gap.default); - try std.testing.expectEqualStrings(" ", gap.grapheme()); - try std.testing.expectEqual(pardes.Color.default, gap.style.bg); -} - -test "fractional geometry draws exactly the cell centres core can hit" { - var cells: [3]pardes.Cell = @splat(.{}); - testGlyphs(&cells, "ABC"); - var surface: pardes.Surface = .{ .cols = 3, .rows = 1, .cells = &cells }; - var arena = std.heap.ArenaAllocator.init(std.testing.allocator); - defer arena.deinit(); - const track: Track = .{ - .serial = 1, - .effect = .slide, - .from = .{ .x = 0.9, .w = 1, .h = 1 }, - .to = .{ .x = 0, .w = 1, .h = 1 }, - }; - const animated = try composed(arena.allocator(), &surface, &.{track}, null); - // Cell 0 lies in floor/ceil iteration bounds, but its centre (0.5) is - // outside [0.9, 1.9); cell 1 is the only presented/hit-testable cell. - try std.testing.expectEqualStrings(" ", animated.at(0, 0).grapheme()); - try std.testing.expectEqualStrings("A", animated.at(1, 0).grapheme()); -} - -test "large-grid right edge uses containment before rounded ratio" { - const cols = std.math.maxInt(u16); - const source_cells = try std.testing.allocator.alloc(pardes.Cell, cols); - defer std.testing.allocator.free(source_cells); - const output_cells = try std.testing.allocator.alloc(pardes.Cell, cols); - defer std.testing.allocator.free(output_cells); - @memset(source_cells, .{}); - @memset(output_cells, .{}); - for (source_cells) |*cell| { - cell.text[0] = 'X'; - cell.len = 1; - cell.default = false; - } - const source: pardes.Surface = .{ .cols = cols, .rows = 1, .cells = source_cells }; - var output: pardes.Surface = .{ .cols = cols, .rows = 1, .cells = output_cells }; - const track: Track = .{ - .serial = 1, - .effect = .zoom, - .frame = 8, - .from = .{ .x = 56_796, .w = 8_299, .h = 1 }, - .to = .{ .x = 556, .w = 63_483, .h = 1 }, - }; - const shown = track.presented(); - try std.testing.expect(@as(f32, 63_935.5) < shown.x + shown.w); - transformClipped(&output, source.cols, source.rows, source.cells, track, .{ .w = @floatFromInt(output.cols), .h = @floatFromInt(output.rows) }); - try std.testing.expectEqualStrings("X", output.at(63_935, 0).grapheme()); -} - -test "active panel transition hides only its own native attachment" { - const tracks = [_]Track{ - .{ .serial = 41, .effect = .slide, .frame = 0 }, - .{ .serial = 42, .effect = .ascii, .frame = 0 }, - .{ .serial = 43, .effect = .zoom, .frame = animation.Transition.zoom.frames() - 1 }, - .{ .serial = 44, .effect = .zoom, .frame = animation.Transition.zoom.frames() }, - .{ .serial = 45, .phase = .opening, .effect = .vertical, .frame = 0 }, - }; - - try std.testing.expect(hidesAttachment(&tracks, 41)); - try std.testing.expect(!hidesAttachment(&tracks, 42)); - try std.testing.expect(!hidesAttachment(&tracks, 43)); - try std.testing.expect(!hidesAttachment(&tracks, 44)); - try std.testing.expect(hidesAttachment(&tracks, 45)); - try std.testing.expect(!hidesAttachment(&tracks, 46)); -} - -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); - var frame: Frame = .{ .arena = a.allocator(), .canonical = s }; - try glideRows(&frame, g); - 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); - var frame: Frame = .{ .arena = arena.allocator(), .canonical = &core.surface }; - try glideRows(&frame, &glide); - try std.testing.expectEqual(row, (frame.shown().cursor orelse return error.NoCursor).y); - } -} diff --git a/src/tui/tui.zig b/src/tui/tui.zig index fae55141..b2a198c7 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -12,7 +12,6 @@ const message = pardes.Messages.Message; const file_watch = @import("../file_watch.zig"); const selection_pipe = @import("../selection_pipe.zig"); const ninep_io = @import("../9p_io.zig"); -const panel_compositor = @import("panel_compositor.zig"); const daemon = @import("../daemon.zig"); const host_io = @import("../host_io.zig"); const upgrade = @import("../upgrade.zig"); @@ -1408,7 +1407,6 @@ const Screen = struct { caps_pending: bool = true, /// Its size as last taken (`Shell.resized`). size: ?vaxis.Winsize = null, - tracks: []const pardes.animation.Track = &.{}, /// The view's glide, the GUI's motion drawn at whole rows (anim 12). glide: pardes.glide.Glide = .{}, @@ -2601,7 +2599,6 @@ const Shell = struct { const s = of(ctx); const sc = s.term() orelse return; const vx = &sc.vx; - sc.tracks = canonical.panelTracks(); // The last frame is not answered yet: this one stays owed, and the // answer's wake draws the state as it is then. if (sc.feed.ack.holds(@intCast(std.Io.Clock.awake.now(s.io).nanoseconds), s.io)) { @@ -2609,10 +2606,6 @@ const Shell = struct { return; } _ = s.frame.reset(.retain_capacity); - // Each pass below draws on the frame's one copy of the canonical - // surface, made by the first that changes anything. - var frame: panel_compositor.Frame = .{ .arena = s.frame.allocator(), .canonical = canonical }; - panel_compositor.compose(&frame, sc.tracks, s.core.theme().bg) catch return; const tz_cells = tracy.zone(@src(), "surface->vaxis"); const win = vx.window(); // Each of the shell's own motions below asks for frames while it runs. @@ -2620,13 +2613,11 @@ const Shell = struct { if (sc.glide.moving()) sc.glide.advance(s.core.now_ns, sc.glide.motion); s.core.shell_continuous = sc.glide.moving(); s.core.frames.scroll = @round(sc.glide.follow.offset); - panel_compositor.glideRows(&frame, &sc.glide) catch {}; - const shown = frame.shown(); + const shown = glideRows(s.frame.allocator(), canonical, &sc.glide) catch canonical; paintCells(win, shown.cells, shown.cols, shown.rows); if (s.remote) |c| if (c.link.notice_len > 0) noticeLine(win, c.link.notice(), shown.cols, shown.rows); tz_cells.end(); for (canonical.images.items) |place| { - if (panel_compositor.hidesAttachment(canonical.panelTracks(), place.serial)) continue; if (comptime pardes.pdf_enabled) { if (!kittyImageRepresentable(place)) continue; } @@ -2726,7 +2717,7 @@ const Shell = struct { fn postPresent(ctx: ?*anyopaque) void { const s = of(ctx); - if (s.term()) |sc| s.core.acknowledgePanelPresentation(sc.tracks); + if (s.term() != null) s.core.presented(); tracy.frameMark(); } @@ -3352,6 +3343,45 @@ fn noticeLine(win: vaxis.Window, text: []const u8, cols: u16, rows: u16) void { /// (pardes's patch to Vaxis.zig in build.zig), since a blank's ink shows /// nowhere. Deciding that here cost every cell of every frame a look at the /// frame before; vaxis already makes that comparison. +/// The tty's picture of the glide (glide.zig, 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; `canonical` itself while the picture is where the core is, else +/// a copy of it in `arena`. The rows are walked from the side they move +/// toward, so each is read before a move writes over it. +fn glideRows(arena: std.mem.Allocator, canonical: *const pardes.Surface, glide: *const pardes.glide.Glide) !*const pardes.Surface { + const k: i32 = @intFromFloat(@round(glide.follow.offset)); + const w = glide.bw(); + const h = glide.body_h; + const x0 = glide.rect.x + pardes.config.GUTTER; + const y0 = glide.body_y; + if (k == 0 or w == 0 or h == 0 or x0 + w > canonical.cols or y0 + h > canonical.rows) return canonical; + const out = try arena.create(pardes.Surface); + out.* = canonical.*; + out.cells = try arena.dupe(pardes.Cell, canonical.cells); + const cols: usize = out.cols; + for (0..h) |i| { + const r = if (k > 0) i else h - 1 - i; + 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) + out.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; +} + fn paintCells(win: vaxis.Window, cells: []const pardes.Cell, cols: u16, rows: u16) void { win.clear(); var y: u16 = 0; @@ -3399,3 +3429,83 @@ test "a fade sends the text it dims, not the blanks beside it" { try std.testing.expect(std.mem.indexOf(u8, sent[1], "90:90:90") != null); try std.testing.expect(std.mem.indexOfScalar(u8, sent[1], 'a') != null); } + +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: pardes.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: *pardes.glide.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 glideRows(a.allocator(), s, g); + 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 shown = try glideRows(arena.allocator(), &core.surface, &glide); + try std.testing.expectEqual(row, (shown.cursor orelse return error.NoCursor).y); + } +} -- cgit v1.3