summaryrefslogtreecommitdiff
path: root/src/layout.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/layout.zig')
-rw-r--r--src/layout.zig2686
1 files changed, 1113 insertions, 1573 deletions
diff --git a/src/layout.zig b/src/layout.zig
index c6cb7503..f545f60d 100644
--- a/src/layout.zig
+++ b/src/layout.zig
@@ -1,679 +1,45 @@
const std = @import("std");
const pardes = @import("pardes.zig");
+const tagline = @import("tagline.zig");
+const dump = @import("dump.zig");
const config = @import("config.zig");
const panes = @import("panes.zig");
+const animation = @import("animation.zig");
+const Box = animation.Box;
const Pardes = pardes.Pardes;
const Pane = pardes.Pane;
pub const Rect = struct { x: u16 = 0, y: u16 = 0, w: u16 = 0, h: u16 = 0 };
-pub const Snapshot = struct { serial: u32, box: Box };
const MAX_COLS = pardes.MAX_COLS;
const MAX_PANES = pardes.MAX_PANES;
const BOX_H = pardes.BOX_H;
-pub const Presentation = struct {
- // Only acknowledged draws advance shown geometry; committed and submitted may be ahead.
- committed: [MAX_PANES]?Snapshot = @splat(null),
- initialized: bool = false,
- enabled: bool = false,
- snap_once: bool = false,
- tracks: [MAX_PANES]?Track = @splat(null),
- closing: [MAX_PANES]Track = undefined,
- closing_len: usize = 0,
- submitted: [MAX_PANES]?Snapshot = @splat(null),
- submitted_ready: bool = false,
- shown: [MAX_PANES]?Snapshot = @splat(null),
- shown_tracks: [MAX_PANES]?Track = @splat(null),
- shown_closing: [MAX_PANES]Track = undefined,
- shown_closing_len: usize = 0,
- acknowledged: bool = false,
- pending: bool = false,
- previous_cells: []pardes.Cell = &.{},
- previous_body_layers: [MAX_PANES]pardes.BodyLayer = @splat(.{}),
- previous_tag_layers: [pardes.MAX_TAG_LAYERS]pardes.TagLayer = @splat(.{}),
- previous_cols: u16 = 0,
- previous_rows: u16 = 0,
- previous_valid: bool = false,
- previous_layout: [MAX_PANES]?Snapshot = @splat(null),
- diffs: []pardes.PanelCellDiff = &.{},
- diff_state: enum { none, requested, ready } = .none,
-
- pub const CellPosition = struct { col: u16, row: u16 };
-
- pub fn deinit(self: *Presentation, gpa: std.mem.Allocator) void {
- if (self.previous_cells.len > 0) gpa.free(self.previous_cells);
- for (&self.previous_body_layers) |*layer| gpa.free(layer.cells);
- for (&self.previous_tag_layers) |*layer| gpa.free(layer.cells);
- if (self.diffs.len > 0) gpa.free(self.diffs);
- }
-
- pub fn sync(self: *Presentation, p: *Pardes) void {
- const initializing = !self.initialized or !self.enabled;
- if (initializing or self.snap_once) {
- const had_layout = self.initialized;
- self.committed = @splat(null);
- self.tracks = @splat(null);
- self.closing_len = 0;
- self.diff_state = .none;
- if (initializing) {
- self.shown_tracks = @splat(null);
- self.shown_closing_len = 0;
- }
- for (p.panes, 0..) |slot, id| {
- const pane = slot orelse continue;
- self.committed[id] = .{ .serial = pane.serial, .box = panelBox(p.rects[id]) };
- }
- self.initialized = true;
- self.snap_once = false;
- if (had_layout and self.acknowledged)
- self.pending = true;
- return;
- }
-
- const effect = p.settings.panel_transition;
- if (effect.needsPreviousGrid() and self.diff_state != .none) {
- var second_change = false;
- for (p.panes, self.committed, 0..) |slot, snapshot, id| {
- const pane = slot orelse {
- second_change = second_change or snapshot != null;
- continue;
- };
- const target = panelBox(p.rects[id]);
- second_change = second_change or snapshot == null or
- snapshot.?.serial != pane.serial or !snapshot.?.box.eql(target);
- }
- if (second_change) {
- p.abandonPanelAnimations();
- self.committed = @splat(null);
- for (p.panes, 0..) |slot, id| {
- const pane = slot orelse continue;
- self.committed[id] = .{ .serial = pane.serial, .box = panelBox(p.rects[id]) };
- }
- return;
- }
- }
- var changed = false;
- var animated_change = false;
- for (p.panes, 0..) |slot, id| {
- const pane = slot orelse {
- if (self.committed[id]) |old| {
- changed = true;
- if (effect.lifecycleOnly()) {
- if (self.appendClosing(.{
- .serial = old.serial,
- .pane = @intCast(id),
- .phase = .closing,
- .effect = effect,
- .from = old.box,
- .to = closingBox(effect, old.box),
- })) animated_change = true;
- }
- }
- self.committed[id] = null;
- self.tracks[id] = null;
- // Never let pixels from a dead pane address a reused slot.
- self.shown_tracks[id] = null;
- continue;
- };
- const target = panelBox(p.rects[id]);
- const previous = self.committed[id];
- self.committed[id] = .{ .serial = pane.serial, .box = target };
-
- if (effect == .off) {
- changed = changed or previous == null or previous.?.serial != pane.serial or
- !previous.?.box.eql(target);
- self.tracks[id] = null;
- continue;
- }
- if (previous) |old| {
- if (old.serial == pane.serial and old.box.eql(target)) continue;
- const same_lifetime = old.serial == pane.serial;
- const prior = if (self.tracks[id]) |track|
- if (track.serial == pane.serial and track.active()) track else null
- else
- null;
- if (effect.lifecycleOnly() and same_lifetime) {
- if (prior) |active| if (active.phase == .opening) {
- var next = active;
- next.from = openingBox(effect, target, p.screen_w);
- next.to = target;
- self.tracks[id] = next;
- changed = true;
- animated_change = true;
- continue;
- };
- self.tracks[id] = null;
- changed = true;
- continue;
- }
- if (effect.lifecycleOnly() and !same_lifetime) {
- _ = self.appendClosing(.{
- .serial = old.serial,
- .pane = @intCast(id),
- .phase = .closing,
- .effect = effect,
- .from = old.box,
- .to = closingBox(effect, old.box),
- });
- }
- const shown = if (self.shown[id]) |snapshot|
- if (snapshot.serial == pane.serial) snapshot.box else null
- else
- null;
- const from = if (!same_lifetime)
- openingBox(effect, target, p.screen_w)
- else if (shown) |box|
- box
- else if (self.acknowledged and prior != null)
- prior.?.from
- else
- old.box;
- const next: Track = .{
- .serial = pane.serial,
- .pane = @intCast(id),
- .phase = if (!same_lifetime or
- (prior != null and prior.?.phase == .opening)) .opening else .moving,
- .effect = effect,
- .from = from,
- .to = target,
- };
- self.tracks[id] = next;
- changed = true;
- animated_change = true;
- } else {
- const next: Track = .{
- .serial = pane.serial,
- .pane = @intCast(id),
- .phase = .opening,
- .effect = effect,
- .from = openingBox(effect, target, p.screen_w),
- .to = target,
- };
- self.tracks[id] = next;
- changed = true;
- animated_change = true;
- }
- }
- if (animated_change and effect.needsPreviousGrid()) {
- self.diff_state = .requested;
- }
- if (changed and self.acknowledged) self.pending = true;
- }
-
- fn appendClosing(self: *Presentation, track: Track) bool {
- std.debug.assert(track.phase == .closing);
- const baseline = self.previous_layout[track.pane] orelse return false;
- if (!self.previous_valid or baseline.serial != track.serial or
- !baseline.box.eql(track.from)) return false;
- if (self.closing_len == self.closing.len) {
- std.mem.copyForwards(
- Track,
- self.closing[0 .. self.closing.len - 1],
- self.closing[1..],
- );
- self.closing_len -= 1;
- }
- self.closing[self.closing_len] = track;
- self.closing_len += 1;
- return true;
- }
-
- pub fn advance(self: *Presentation) void {
- for (&self.tracks) |*slot| {
- const track = if (slot.*) |*track| track else continue;
- track.frame +|= 1;
- if (!track.active()) slot.* = null;
- }
- var out: usize = 0;
- for (self.closing[0..self.closing_len]) |value| {
- var track = value;
- track.frame +|= 1;
- if (!track.active()) continue;
- self.closing[out] = track;
- out += 1;
- }
- self.closing_len = out;
- }
-
- pub fn acknowledge(self: *Presentation, p: *Pardes, tracks: []const Track) void {
- var presented: [MAX_PANES]?Track = @splat(null);
- var presented_closing: [MAX_PANES]Track = undefined;
- var nclosing: usize = 0;
- var presented_layout: [MAX_PANES]?Snapshot = if (self.submitted_ready)
- self.submitted
- else
- @splat(null);
- if (!self.submitted_ready) for (p.panes, 0..) |slot, id| {
- const pane = slot orelse continue;
- presented_layout[id] = .{ .serial = pane.serial, .box = panelBox(p.rects[id]) };
- };
- for (&presented_layout, 0..) |*snapshot, id| if (snapshot.*) |saved| {
- const pane = p.panes[id] orelse {
- snapshot.* = null;
- continue;
- };
- if (pane.serial != saved.serial) snapshot.* = null;
- };
- for (tracks) |track| {
- if (track.phase == .closing) {
- const current = for (self.closing[0..self.closing_len]) |candidate| {
- if (candidate.serial == track.serial and candidate.pane == track.pane and
- candidate.effect == track.effect) break true;
- } else false;
- if (!current or !track.active() or nclosing == presented_closing.len) continue;
- presented_closing[nclosing] = track;
- nclosing += 1;
- continue;
- }
- const id: usize = track.pane;
- if (id >= p.panes.len or !track.active()) continue;
- const pane = p.panes[id] orelse continue;
- if (pane.serial != track.serial) continue;
- presented[id] = track;
- presented_layout[id] = .{ .serial = track.serial, .box = track.visualBox() };
- }
- self.shown_tracks = presented;
- self.shown_closing = presented_closing;
- self.shown_closing_len = nclosing;
- self.shown = presented_layout;
- self.acknowledged = true;
- self.pending = false;
- if (tracks.len == 0) {
- self.tracks = @splat(null);
- self.closing_len = 0;
- self.diff_state = .none;
- self.capturePrevious(p.gpa, &p.surface);
- }
- }
-
- fn capturePrevious(self: *Presentation, gpa: std.mem.Allocator, surface: *const pardes.Surface) void {
- self.diff_state = .none;
- const cells = surface.cells;
- if (cells.len == 0) {
- self.previous_valid = false;
- self.previous_layout = @splat(null);
- return;
- }
- if (self.previous_cells.len != cells.len) {
- const next = gpa.alloc(pardes.Cell, cells.len) catch {
- self.previous_valid = false;
- self.previous_layout = @splat(null);
- return;
- };
- if (self.previous_cells.len > 0) gpa.free(self.previous_cells);
- self.previous_cells = next;
- }
- for (&self.previous_body_layers, surface.bodyLayers()) |*old, *current| {
- const count = if (current.rows > 0) current.cells.len else 0;
- const storage = if (old.cells.len == count) old.cells else gpa.realloc(old.cells, count) catch {
- self.previous_valid = false;
- self.previous_layout = @splat(null);
- return;
- };
- if (count > 0) @memcpy(storage, current.cells);
- old.* = current.*;
- old.cells = storage;
- }
- for (&self.previous_tag_layers, surface.tagLayers()) |*old, *current| {
- const count = if (current.cols > 0) current.cells.len else 0;
- const storage = if (old.cells.len == count) old.cells else gpa.realloc(old.cells, count) catch {
- self.previous_valid = false;
- self.previous_layout = @splat(null);
- return;
- };
- if (count > 0) @memcpy(storage, current.cells);
- old.* = current.*;
- old.cells = storage;
- }
- @memcpy(self.previous_cells, cells);
- self.previous_cols = surface.cols;
- self.previous_rows = surface.rows;
- self.previous_valid = true;
- self.previous_layout = if (self.submitted_ready)
- self.submitted
- else
- @splat(null);
- }
-
- pub fn cellDiff(self: *const Presentation, cols: u16, rows: u16, col: u16, row: u16) pardes.PanelCellDiff {
- if (self.diff_state != .ready or col >= cols or row >= rows or
- self.diffs.len != @as(usize, cols) * rows) return .unchanged;
- return self.diffs[@as(usize, row) * cols + col];
- }
-
- pub const Point = struct { x: f32, y: f32 };
-
- /// Coordinates stay fractional until the host resolves its body font grid.
- pub fn pointerFractional(self: *const Presentation, cols: u16, rows: u16, x: f32, y: f32) ?Point {
- if (self.pending or !std.math.isFinite(x) or !std.math.isFinite(y) or
- x < 0 or y < 0 or x >= @as(f32, @floatFromInt(cols)) or y >= @as(f32, @floatFromInt(rows))) return null;
- var closing = self.shown_closing_len;
- while (closing > 0) {
- closing -= 1;
- const track = self.shown_closing[closing];
- if (track.active() and boxContainsPoint(track.contentBox(), x, y) and
- boxContainsPoint(track.presented(), x, y)) return null;
- }
- for ([_]Phase{ .opening, .moving }) |phase| {
- var index = self.shown_tracks.len;
- while (index > 0) {
- index -= 1;
- const track = self.shown_tracks[index] orelse continue;
- if (!track.active() or track.phase != phase) continue;
- switch (track.effect) {
- .slide, .zoom, .vertical => {
- const target = track.contentBox();
- if (track.effect == .vertical and !boxContainsPoint(target, x, y)) continue;
- const shown = track.presented();
- if (!boxContainsPoint(shown, x, y)) {
- if (track.effect == .vertical) return null;
- continue;
- }
- return .{
- .x = target.x + (x - shown.x) / shown.w * target.w,
- .y = target.y + (y - shown.y) / shown.h * target.h,
- };
- },
- else => {
- if (!boxContainsPoint(track.to, x, y)) continue;
- _ = self.pointer(cols, rows, @intFromFloat(@floor(x)), @intFromFloat(@floor(y))) orelse return null;
- return .{ .x = x, .y = y };
- },
- }
- }
- }
- _ = self.pointer(cols, rows, @intFromFloat(@floor(x)), @intFromFloat(@floor(y))) orelse return null;
- return .{ .x = x, .y = y };
- }
-
- fn boxContainsPoint(box: Box, x: f32, y: f32) bool {
- return box.w > 0 and box.h > 0 and x >= box.x and y >= box.y and x < box.x + box.w and y < box.y + box.h;
- }
-
- pub fn pointer(self: *const Presentation, cols: u16, rows: u16, col: u16, row: u16) ?CellPosition {
- if (self.pending) return null;
- var closing = self.shown_closing_len;
- while (closing > 0) {
- closing -= 1;
- const track = self.shown_closing[closing];
- if (!track.active()) continue;
- if (boxContainsCell(track.contentBox(), col, row) and
- boxContainsCell(track.presented(), col, row)) return null;
- }
- const phases = [_]Phase{ .opening, .moving };
- for (phases) |phase| {
- // Backends paint pane slots forward within a phase. Probe them in
- // reverse so overlapping transition quads address the top pixel.
- var index = self.shown_tracks.len;
- while (index > 0) {
- index -= 1;
- const track = self.shown_tracks[index] orelse continue;
- if (!track.active() or track.phase != phase) continue;
- switch (track.effect) {
- .slide, .zoom => {
- const shown = track.presented();
- if (shown.w <= 0 or shown.h <= 0 or !boxContainsCell(shown, col, row)) continue;
- const x = @as(f32, @floatFromInt(col)) + 0.5;
- const y = @as(f32, @floatFromInt(row)) + 0.5;
- const u = std.math.clamp((x - shown.x) / shown.w, 0, 0.999_999);
- const v = std.math.clamp((y - shown.y) / shown.h, 0, 0.999_999);
- const logical_x: i32 = @intFromFloat(@floor(track.to.x + u * track.to.w));
- const logical_y: i32 = @intFromFloat(@floor(track.to.y + v * track.to.h));
- return .{
- .col = @intCast(std.math.clamp(logical_x, 0, @as(i32, cols -| 1))),
- .row = @intCast(std.math.clamp(logical_y, 0, @as(i32, rows -| 1))),
- };
- },
- .vertical => {
- const clip = track.contentBox();
- if (!boxContainsCell(clip, col, row)) continue;
- const shown = track.presented();
- if (shown.w <= 0 or shown.h <= 0 or !boxContainsCell(shown, col, row))
- return null;
- const x = @as(f32, @floatFromInt(col)) + 0.5;
- const y = @as(f32, @floatFromInt(row)) + 0.5;
- const u = std.math.clamp((x - shown.x) / shown.w, 0, 0.999_999);
- const v = std.math.clamp((y - shown.y) / shown.h, 0, 0.999_999);
- return .{
- .col = @intFromFloat(@floor(clip.x + u * clip.w)),
- .row = @intFromFloat(@floor(clip.y + v * clip.h)),
- };
- },
- .dissolve, .ascii => {
- if (!boxContainsCell(track.to, col, row)) continue;
- const cell_diff = self.cellDiff(cols, rows, col, row);
- if (!cell_diff.changed())
- return .{ .col = col, .row = row };
- const relative_col: u16 = @intFromFloat(@floor(
- @as(f32, @floatFromInt(col)) + 0.5 - track.to.x,
- ));
- const relative_row: u16 = @intFromFloat(@floor(
- @as(f32, @floatFromInt(row)) + 0.5 - track.to.y,
- ));
- const visible = switch (track.effect) {
- .dissolve => dissolveRevealed(
- track.serial,
- relative_col,
- relative_row,
- track.amount(),
- ),
- .ascii => switch (cell_diff) {
- .ascii => |diff| diff.complete(track.frame),
- .unchanged, .visual => true,
- },
- else => unreachable,
- };
- return if (visible) .{ .col = col, .row = row } else null;
- },
- .edges, .fall, .wave, .curtain, .scramble, .typewriter => {
- if (!boxContainsCell(track.to, col, row)) continue;
- const area = CellArea.of(track.to);
- const settled = std.meta.eql(
- charSource(
- track,
- col -| area.x0,
- row -| area.y0,
- area,
- ),
- CharSource.settled,
- );
- return if (settled) .{ .col = col, .row = row } else null;
- },
- .off => {},
- }
- }
- }
- for (self.shown_tracks) |maybe| {
- const track = maybe orelse continue;
- if (!track.active() or (track.effect != .slide and track.effect != .zoom and
- track.effect != .vertical)) continue;
- if (boxContainsCell(track.to, col, row)) return null;
- }
- return .{ .col = col, .row = row };
- }
-
- fn boxContainsCell(box: Box, col: u16, row: u16) bool {
- const x = @as(f32, @floatFromInt(col)) + 0.5;
- const y = @as(f32, @floatFromInt(row)) + 0.5;
- return x >= box.x and x < box.x + box.w and y >= box.y and y < box.y + box.h;
- }
-
- pub fn cancel(self: *Presentation) void {
- self.tracks = @splat(null);
- self.closing_len = 0;
- self.shown_tracks = @splat(null);
- self.shown_closing_len = 0;
- self.diff_state = .none;
- self.pending = self.acknowledged;
- }
-
- fn prepareDiff(self: *Presentation, gpa: std.mem.Allocator, surface: *const pardes.Surface) !bool {
- const count = surface.cells.len;
- if (!self.previous_valid or
- self.previous_cols != surface.cols or
- self.previous_rows != surface.rows or
- self.previous_cells.len != count) return false;
- if (self.diffs.len != count) {
- const next = try gpa.alloc(pardes.PanelCellDiff, count);
- if (self.diffs.len > 0) gpa.free(self.diffs);
- self.diffs = next;
- }
- for (self.diffs, self.previous_cells, surface.cells) |*diff, *old, *new|
- diff.* = pardes.PanelCellDiff.between(old, new);
- for (&self.tracks) |*slot| {
- const track = if (slot.*) |*track| track else continue;
- if (track.effect != .ascii) continue;
- var longest: u16 = 1;
- var row: u16 = 0;
- while (row < surface.rows) : (row += 1) {
- var col: u16 = 0;
- while (col < surface.cols) : (col += 1) {
- if (!boxContainsCell(track.to, col, row)) continue;
- const index = @as(usize, row) * surface.cols + col;
- switch (self.diffs[index]) {
- .ascii => |diff| longest = @max(longest, diff.frameCount()),
- .unchanged, .visual => {},
- }
- }
- }
- track.frame_count = @max(track.frame_count, longest);
- }
- self.diff_state = .ready;
- return true;
- }
-
- pub fn submit(self: *Presentation, p: *Pardes, s: *pardes.Surface) !void {
- self.submitted = @splat(null);
- for (p.panes, 0..) |slot, id| {
- const pane = slot orelse continue;
- self.submitted[id] = .{
- .serial = pane.serial,
- .box = panelBox(p.rects[id]),
- };
- }
- self.submitted_ready = true;
- if (self.diff_state != .none and !try self.prepareDiff(p.gpa, s)) {
- for (&self.tracks) |*slot| {
- const track = slot.* orelse continue;
- if (track.effect.needsPreviousGrid()) slot.* = null;
- }
- self.closing_len = 0;
- self.diff_state = .none;
- }
- if (self.diff_state == .ready) {
- s.previous_cells = self.previous_cells;
- s.previous_body_layers = &self.previous_body_layers;
- s.previous_tag_layers = &self.previous_tag_layers;
- s.cell_diffs = self.diffs;
- }
- s.npanel_tracks = paintOrder(
- &self.tracks,
- self.closing[0..self.closing_len],
- &s.panel_tracks,
- );
- }
-
- pub fn animating(self: *const Presentation) bool {
- for (self.tracks) |track| if (track != null and track.?.active()) return true;
- for (self.closing[0..self.closing_len]) |track|
- if (track.active()) return true;
- return false;
- }
-
- test "presentation cache allocation failures retain ownership and allow retry" {
- var allocator: std.testing.FailingAllocator = .init(std.testing.allocator, .{});
- const gpa = allocator.allocator();
- var state: Presentation = .{};
- defer state.deinit(gpa);
- var cells: [3]pardes.Cell = @splat(.{});
- cells[0].default = false;
- cells[0].text[0] = 'a';
- var surface: pardes.Surface = .{ .cols = 2, .rows = 1, .cells = cells[0..2] };
- state.submitted[0] = .{ .serial = 7, .box = .{ .x = 0, .y = 0, .w = 2, .h = 1 } };
- state.submitted_ready = true;
- state.capturePrevious(gpa, &surface);
- try std.testing.expect(state.previous_valid);
- try std.testing.expectEqual(@as(u32, 7), state.previous_layout[0].?.serial);
- const previous = state.previous_cells.ptr;
- const allocated = allocator.allocations;
- allocator.fail_index = allocator.alloc_index;
- state.capturePrevious(gpa, &surface);
- try std.testing.expect(state.previous_valid);
- try std.testing.expectEqual(allocated, allocator.allocations);
- try std.testing.expect(!allocator.has_induced_failure);
-
- surface.cols = 3;
- surface.cells = &cells;
- state.submitted[0].?.box.w = 3;
- state.diff_state = .ready;
- state.capturePrevious(gpa, &surface);
- try std.testing.expect(allocator.has_induced_failure);
- try std.testing.expect(!state.previous_valid);
- try std.testing.expectEqual(previous, state.previous_cells.ptr);
- try std.testing.expectEqual(@as(usize, 2), state.previous_cells.len);
- try std.testing.expectEqual(@as(u8, 'a'), state.previous_cells[0].text[0]);
- try std.testing.expect(state.previous_layout[0] == null);
- try std.testing.expectEqual(.none, state.diff_state);
-
- allocator.fail_index = std.math.maxInt(usize);
- state.capturePrevious(gpa, &surface);
- try std.testing.expect(state.previous_valid);
- try std.testing.expectEqual(@as(usize, 3), state.previous_cells.len);
- try std.testing.expectEqual(@as(f32, 3), state.previous_layout[0].?.box.w);
- state.diff_state = .requested;
- allocator.fail_index = allocator.alloc_index;
- try std.testing.expectError(error.OutOfMemory, state.prepareDiff(gpa, &surface));
- try std.testing.expectEqual(.requested, state.diff_state);
- try std.testing.expectEqual(@as(usize, 0), state.diffs.len);
- allocator.fail_index = std.math.maxInt(usize);
- try std.testing.expect(try state.prepareDiff(gpa, &surface));
- try std.testing.expectEqual(.ready, state.diff_state);
- try std.testing.expectEqual(@as(usize, 3), state.diffs.len);
- for (state.diffs) |diff| try std.testing.expectEqual(.unchanged, diff);
- }
-
- test "presentation submission failure preserves shown geometry until retry is acknowledged" {
- var allocator: std.testing.FailingAllocator = .init(std.testing.allocator, .{});
- const p = try Pardes.init(allocator.allocator(), .{ .tty_only = true, .cols = 40, .rows = 12 });
- defer p.deinit();
- var frame: std.heap.ArenaAllocator = .init(std.testing.allocator);
- defer frame.deinit();
- _ = try p.render(frame.allocator());
- p.acknowledgePanelPresentation(&.{});
- const shown = p.presentation.shown;
- const previous = p.presentation.previous_cells.ptr;
- p.presentation.diff_state = .requested;
- p.presentation.pending = true;
- allocator.fail_index = allocator.alloc_index;
- try std.testing.expectError(error.OutOfMemory, p.presentation.submit(p, &p.surface));
- try std.testing.expectEqualDeep(shown, p.presentation.shown);
- try std.testing.expectEqual(previous, p.presentation.previous_cells.ptr);
- try std.testing.expectEqual(.requested, p.presentation.diff_state);
- try std.testing.expect(p.presentation.pending);
- try std.testing.expect(p.presentation.pointer(p.screen_w, p.screen_h, 5, 3) == null);
-
- allocator.fail_index = std.math.maxInt(usize);
- try p.presentation.submit(p, &p.surface);
- try std.testing.expectEqualDeep(shown, p.presentation.shown);
- try std.testing.expect(p.presentation.pending);
- p.acknowledgePanelPresentation(&.{});
- try std.testing.expect(!p.presentation.pending);
- try std.testing.expectEqual(CellPosition{ .col = 5, .row = 3 }, p.presentation.pointer(p.screen_w, p.screen_h, 5, 3).?);
- }
-};
-
pub const column_weight_unit: u64 = 1 << 32;
pub const max_column_weight: u64 = std.math.maxInt(u64) / MAX_COLS;
pub const MovePlacement = struct {
preview_col: usize,
- above_id: usize,
+ /// The pane the moved one lands below; null when the column is empty
+ /// and the moved pane takes it whole.
+ above_id: ?usize,
row: u16,
above_y: u16,
above_h: u16,
};
-pub fn focusDir(p: *Pardes, from: usize, dir: enum { left, right, up, down }) void {
+pub const Dir = enum { left, right, up, down };
+
+/// Move the keyboard to the nearest pane in `dir`; false when there is none.
+pub fn focusDir(p: *Pardes, from: usize, dir: Dir) bool {
+ const b = nearest(p, from, dir) orelse return false;
+ p.active = b;
+ // a count typed before the hop was meant for the pane you left
+ p.panes[b].?.body.normal.clear();
+ return true;
+}
+
+/// The pane next to `from` in `dir`, by the screen.
+pub fn nearest(p: *const Pardes, from: usize, dir: Dir) ?usize {
const a = p.rects[from];
var best: ?usize = null;
var best_d: i32 = 0;
@@ -700,11 +66,83 @@ pub fn focusDir(p: *Pardes, from: usize, dir: enum { left, right, up, down }) vo
best_d = d;
}
}
- if (best) |b| {
- p.active = b;
- // a count typed before the hop was meant for the pane you left
- p.panes[b].?.normal.clear();
+ return best;
+}
+
+/// Window mode's labels, one a pane in reading order (columns left to
+/// right, each top to bottom); h, j, k and l move instead, d deletes and c
+/// collapses. ponytail: a pane past the 20th has no label and is reached
+/// by moving.
+pub const window_labels = "asfgqwertyuiopzxvbnm";
+
+/// The letter window mode shows on pane `id`.
+pub fn windowLabel(p: *const Pardes, id: usize) ?u8 {
+ var n: usize = 0;
+ for (0..p.ncol) |c| for (p.col_panes[c][0..p.col_n[c]]) |pane| {
+ if (n == window_labels.len) return null;
+ if (pane == id) return window_labels[n];
+ n += 1;
+ };
+ return null;
+}
+
+/// Window mode (SPC w, Ctrl-w): h/j/k/l (or the arrows) move an outline
+/// from pane to pane and nothing else gets the keys; Enter or Esc focuses
+/// the outlined pane, `:` its tag, and a pane's label letter that one. Up past a
+/// column's top pane goes on to its tag, as the Up word does. `d` is Del
+/// on the outlined pane and `c` Collapse, and the mode stays: a Del that
+/// closes it moves the outline to the pane its rows went to (after the k/j
+/// question, when Del asks one; Pardes.answerQuestion). Any other key
+/// changes nothing.
+pub fn windowKey(p: *Pardes, key: pardes.Key) void {
+ const at = p.window_pick orelse return;
+ if (p.panes[at] == null) {
+ p.window_pick = null;
+ return;
+ }
+ if (key.ctrl or key.alt) return;
+ for (config.window_keys) |wk| if (pardes.hit(key, &.{ wk.letter, wk.arrow })) {
+ const dir: Dir = switch (wk.cmd) {
+ .Left => .left,
+ .Down => .down,
+ .Up => .up,
+ else => .right,
+ };
+ if (nearest(p, at, dir)) |next| {
+ p.window_pick = next;
+ } else if (dir == .up) {
+ p.window_pick = null;
+ p.active = at;
+ pardes.exec.runBuiltin(p, .Up, at, "", null);
+ }
+ return;
+ };
+ if (pardes.hit(key, config.window_collapse)) return pardes.exec.runBuiltin(p, .Collapse, at, "", null);
+ if (pardes.hit(key, config.window_delete)) {
+ // Del's focus follows the rows from the pane it closes, so the
+ // outlined pane is the active one while it runs.
+ p.active = at;
+ pardes.exec.runBuiltin(p, .Del, at, "", null);
+ if (p.panes[at] == null) p.window_pick = if (p.header_focus) null else p.active;
+ return;
}
+ const chosen = if (key.cp == pardes.Key.enter or key.cp == pardes.Key.escape or key.cp == ':') at else for (0..p.panes.len) |id| {
+ if (p.panes[id] != null and key.cp < 128 and windowLabel(p, id) == @as(u8, @intCast(key.cp))) break id;
+ } else return;
+ p.window_pick = null;
+ p.active = chosen;
+ p.panes[chosen].?.body.normal.clear();
+ if (key.cp == ':') tagline.enterTag(p, p.panes[chosen].?);
+}
+
+/// Column `c`'s serial, handed out the first time it is asked for.
+pub fn columnSerial(p: *Pardes, c: usize) u32 {
+ if (p.col_serial[c] == 0) {
+ p.col_serial[c] = p.next_column_serial;
+ p.next_column_serial +%= 1;
+ if (p.next_column_serial == 0) p.next_column_serial = 1;
+ }
+ return p.col_serial[c];
}
pub fn columnBySerial(p: *const Pardes, serial: u32) ?usize {
@@ -713,14 +151,48 @@ pub fn columnBySerial(p: *const Pardes, serial: u32) ?usize {
return null;
}
-pub fn columnMoveTarget(p: *const Pardes, source: usize, x: u16) usize {
- if (source >= p.ncol) return source;
+/// What releasing column `source`'s grip at `x` does: past a neighbour's
+/// middle it takes that column's place; short of it, as in acme, the
+/// column's left edge moves to `x - grab`, clamped as setColumnPairWidth
+/// clamps it. Still on the grip, or at the leftmost column, nothing moves.
+/// The drag preview and the release both ask here, so they cannot disagree.
+pub const ColumnDrop = union(enum) { stay, reorder: usize, edge: u16 };
+
+pub fn columnDrop(p: *const Pardes, source: usize, x: u16, grab: u16) ColumnDrop {
+ if (source >= p.ncol) return .stay;
var insertion: usize = 0;
for (0..p.ncol) |column| {
if (column == source) continue;
if (x >= p.col_x[column] + p.col_w[column] / 2) insertion += 1;
}
- return insertion;
+ if (insertion != source) return .{ .reorder = insertion };
+ if (source == 0 or (x >= p.col_x[source] and x < p.col_x[source] + config.GUTTER)) return .stay;
+ if (p.col_w[source - 1] + p.col_w[source] < 2 * config.MINW) return .stay;
+ const lo = p.col_x[source - 1] + config.MINW;
+ const hi = (p.col_x[source] + p.col_w[source]) -| config.MINW;
+ return .{ .edge = std.math.clamp(x -| grab, lo, @max(lo, hi)) };
+}
+
+/// Share out the width of columns `left` and `left + 1` so the left one is
+/// `left_width` cells, neither below MINW. A border drag and a column grip
+/// dropped inside its own place both end here.
+pub fn setColumnPairWidth(p: *Pardes, left: usize, left_width: u64) void {
+ if (left + 1 >= p.ncol) return;
+ const combined: u64 = p.col_w[left] + p.col_w[left + 1];
+ // The finder's edge sets its share (Finder.dragWidth).
+ if (p.finder.column(p)) |fc| {
+ if (fc == left + 1) return p.finder.dragWidth(p, left, combined -| left_width);
+ if (fc == left) return p.finder.dragWidth(p, left + 1, left_width);
+ }
+ // Too narrow for both to keep MINW: any split would squeeze one away.
+ if (combined < 2 * @as(u64, config.MINW)) return;
+ const width = std.math.clamp(left_width, config.MINW, @max(@as(u64, config.MINW), combined -| config.MINW));
+ const pair = p.col_weight[left] + p.col_weight[left + 1];
+ if (pair <= 1 or combined == 0) return;
+ const numerator = @as(u128, pair) * width + combined / 2;
+ const share = std.math.clamp(@as(u64, @intCast(numerator / combined)), 1, pair - 1);
+ p.col_weight[left] = share;
+ p.col_weight[left + 1] = pair - share;
}
pub fn reorderColumn(p: *Pardes, from: usize, to: usize) void {
@@ -768,21 +240,40 @@ pub fn targetColumn(p: *Pardes, cur_x: u16) usize {
}
pub fn splitRowForExtent(y: u16, h: u16, cur_y: u16) ?u16 {
+ return splitRowFor(y, h, cur_y, null);
+}
+
+/// As `splitRowForExtent`, each pane keeping at least `least` rows when
+/// given: a pane dragged by its grip within its own column is how a
+/// terminal resizes, and acme lets either window keep only its tag.
+fn splitRowFor(y: u16, h: u16, cur_y: u16, least: ?u16) ?u16 {
if (h < 2) return null;
- const min_each: u16 = if (h >= config.MINH * 2) config.MINH else 1;
+ const min_each: u16 = least orelse if (h >= config.MINH * 2) config.MINH else 1;
const lo = y +| min_each;
const hi = y + h - min_each;
if (lo > hi) return y + h / 2;
return std.math.clamp(cur_y, lo, hi);
}
+/// Where pane `id` dragged by its grip to (`cur_x`, `cur_y`) lands. An
+/// owner's grip drags its group; an attachment's drags it alone, which
+/// within its group resizes it and anywhere else detaches it (movePane).
+/// Another group is one place: a pane dropped on it lands under all of it.
pub fn movePlacement(p: *Pardes, id: usize, cur_x: u16, cur_y: u16) ?MovePlacement {
const src = findPane(p, id) orelse return null;
+ const span_end = if (ownerOf(p, id) == null) groupEnd(p, src.col, src.idx) else src.idx + 1;
+ const own_root = groupRoot(p, id);
const tc = targetColumn(p, cur_x);
- if (tc == src.col and p.col_n[src.col] == 1) return null;
+ if (tc == src.col and p.col_n[src.col] == span_end - src.idx) return null;
+ var span_h: u16 = 0;
+ for (p.col_panes[src.col][src.idx..span_end]) |k| span_h +|= p.rects[k].h;
if (tc == src.col) {
- const sr = p.rects[id];
- if (cur_y >= sr.y and cur_y < sr.y + sr.h) return null;
+ const top = p.rects[id].y;
+ // Dropped on itself it stays, unless dragged down from its top with
+ // a pane above: its top moves down there, the pane above taking the
+ // rows, as acme's window dragged by its button does. With no rule
+ // between panes, that is how a terminal resizes them.
+ if (cur_y >= top and cur_y < top + span_h and (src.idx == 0 or cur_y <= top)) return null;
}
var heights: [MAX_PANES]u16 = @splat(0);
for (0..p.ncol) |c| {
@@ -791,23 +282,36 @@ pub fn movePlacement(p: *Pardes, id: usize, cur_x: u16, cur_y: u16) ?MovePlaceme
heights[pid] = p.rects[pid].h;
}
}
- if (p.col_n[src.col] > 1) {
- const sib = if (src.idx > 0) p.col_panes[src.col][src.idx - 1] else p.col_panes[src.col][src.idx + 1];
- heights[sib] +|= p.rects[id].h;
+ if (p.col_n[src.col] > span_end - src.idx) {
+ const sib = if (src.idx > 0) p.col_panes[src.col][src.idx - 1] else p.col_panes[src.col][span_end];
+ heights[sib] +|= span_h;
}
var y: u16 = p.topBarHeight() + p.columnBarHeight();
+ if (p.col_n[tc] == 0) return .{ .preview_col = tc, .above_id = null, .row = y, .above_y = y, .above_h = 0 };
var last: ?MovePlacement = null;
- for (0..p.col_n[tc]) |k| {
- const pid = p.col_panes[tc][k];
- if (pid == id) continue;
- const h = heights[pid];
- const row = splitRowForExtent(y, h, cur_y) orelse {
+ var k: usize = 0;
+ while (k < p.col_n[tc]) {
+ if (tc == src.col and k >= src.idx and k < span_end) {
+ k += 1;
+ continue;
+ }
+ // Its own group's panes are places of their own: that is how an
+ // attachment's grip resizes it against its owner.
+ const unit_end = if (groupRoot(p, p.col_panes[tc][k]) == own_root) k + 1 else groupEnd(p, tc, k);
+ var h: u16 = 0;
+ for (k..unit_end) |j| {
+ if (tc == src.col and j >= src.idx and j < span_end) continue;
+ h +|= heights[p.col_panes[tc][j]];
+ }
+ const above = p.col_panes[tc][unit_end - 1];
+ k = unit_end;
+ const row = splitRowFor(y, h, cur_y, if (tc == src.col) BOX_H else null) orelse {
y +|= h;
continue;
};
const placement: MovePlacement = .{
.preview_col = tc,
- .above_id = pid,
+ .above_id = above,
.row = row,
.above_y = y,
.above_h = h,
@@ -819,10 +323,18 @@ pub fn movePlacement(p: *Pardes, id: usize, cur_x: u16, cur_y: u16) ?MovePlaceme
return last;
}
+/// Move pane `id`, and the group it owns, where movePlacement says. An
+/// attachment moved anywhere but within its own group is detached there.
pub fn movePane(p: *Pardes, id: usize, cur_x: u16, cur_y: u16) void {
const placement = movePlacement(p, id, cur_x, cur_y) orelse return;
const src = findPane(p, id) orelse return;
- const source_multi = p.col_n[src.col] > 1;
+ const owner = ownerOf(p, id);
+ const own_root = groupRoot(p, id);
+ const span_end = if (owner == null) groupEnd(p, src.col, src.idx) else src.idx + 1;
+ const count = span_end - src.idx;
+ var moved: [MAX_PANES]usize = undefined;
+ @memcpy(moved[0..count], p.col_panes[src.col][src.idx..span_end]);
+ const source_multi = p.col_n[src.col] > count;
var heights: [MAX_PANES]u16 = @splat(0);
for (0..p.ncol) |c| {
for (0..p.col_n[c]) |k| {
@@ -830,19 +342,47 @@ pub fn movePane(p: *Pardes, id: usize, cur_x: u16, cur_y: u16) void {
heights[pid] = p.rects[pid].h;
}
}
- removePane(p, id);
- if (source_multi and src.col < p.ncol and p.col_n[src.col] > 0) {
+ var span_h: u16 = 0;
+ for (moved[0..count]) |m| span_h +|= heights[m];
+ for (moved[0..count]) |m| removePane(p, m);
+ if (source_multi) {
const sib = if (src.idx > 0) p.col_panes[src.col][src.idx - 1] else p.col_panes[src.col][src.idx];
- heights[sib] +|= p.rects[id].h;
+ heights[sib] +|= span_h;
}
- const af = findPane(p, placement.above_id) orelse return;
+ const above = placement.above_id orelse {
+ for (moved[0..count], 0..) |m, j| insert(p, placement.preview_col, j, m);
+ if (count > 1) setColumnWeights(p, placement.preview_col, &heights);
+ if (source_multi) setColumnWeights(p, src.col, &heights);
+ if (owner != null) p.panes[id].?.attached_to = null;
+ return;
+ };
+ const af = findPane(p, above) orelse return;
const upper_h = @max(1, placement.row -| placement.above_y);
- const lower_h = @max(1, placement.above_h -| upper_h);
- heights[placement.above_id] = upper_h;
- heights[id] = lower_h;
- insert(p, af.col, af.idx + 1, id);
+ var lower_h = @max(1, placement.above_h -| upper_h);
+ const above_root = groupRoot(p, above);
+ if (above_root != above and above_root != own_root) {
+ // Dropped on another group: its owner gives the rows, keeping its tag.
+ const pay = @min(placement.above_h -| upper_h, heights[above_root] -| BOX_H);
+ heights[above_root] -= pay;
+ lower_h = @max(1, pay);
+ } else heights[above] = upper_h;
+ // The group shares what it lands in as it shared what it had.
+ var left = lower_h;
+ for (moved[1..count]) |m| {
+ const share: u16 = @intCast(@max(1, @as(u32, heights[m]) * lower_h / @max(1, span_h)));
+ heights[m] = share;
+ left -|= share;
+ }
+ heights[id] = @max(1, left);
+ for (moved[0..count], 0..) |m, j| insert(p, af.col, af.idx + 1 + j, m);
setColumnWeights(p, af.col, &heights);
- if (source_multi and src.col < p.ncol and src.col != af.col) setColumnWeights(p, src.col, &heights);
+ if (source_multi and src.col != af.col) setColumnWeights(p, src.col, &heights);
+ if (owner) |o| {
+ const pane = p.panes[id].?;
+ // Still right under its owner's group: resized there. Anywhere
+ // else it is a pane of its own.
+ if (af.col != (findPane(p, o) orelse return).col or !descends(p, above, o)) pane.attached_to = null;
+ }
}
pub fn setColumnWeights(p: *Pardes, col: usize, heights: *const [MAX_PANES]u16) void {
@@ -855,6 +395,28 @@ pub fn setColumnWeights(p: *Pardes, col: usize, heights: *const [MAX_PANES]u16)
}
}
+/// Give a newly opened report enough rows to read, borrowing only space
+/// above the other panes' usable minima. Keep their tags and bodies visible.
+pub fn givePaneRows(p: *Pardes, id: usize, wanted: u16, context: usize) void {
+ const at = findPane(p, id) orelse return;
+ compute(p);
+ var heights: [MAX_PANES]u16 = @splat(0);
+ for (p.col_panes[at.col][0..p.col_n[at.col]]) |other| heights[other] = p.rects[other].h;
+ var missing = wanted -| heights[id];
+ for (p.col_panes[at.col][0..p.col_n[at.col]]) |other| {
+ if (other == id or p.panes[other].?.collapsed) continue;
+ const body_rows: u16 = if (other == context) 6 else 3;
+ const minimum = @max(minRows(p, other), p.panes[other].?.tag_rows + body_rows);
+ const take = @min(missing, heights[other] -| minimum);
+ heights[other] -= take;
+ heights[id] += take;
+ missing -= take;
+ if (missing == 0) break;
+ }
+ setColumnWeights(p, at.col, &heights);
+ p.sync();
+}
+
pub fn applyRowSplit(p: *Pardes, cc: usize, k: usize, cur_y: u16) void {
if (k + 1 >= p.col_n[cc]) return;
const a = p.panes[p.col_panes[cc][k]] orelse return;
@@ -863,7 +425,11 @@ pub fn applyRowSplit(p: *Pardes, cc: usize, k: usize, cur_y: u16) void {
const rb = p.rects[p.col_panes[cc][k + 1]];
const combined: f32 = @floatFromInt(ra.h + rb.h);
var nt: f32 = @floatFromInt(if (p.settings.tag_bottom) cur_y -| ra.y else (cur_y + 1) -| ra.y);
- nt = std.math.clamp(nt, @as(f32, BOX_H), @max(@as(f32, BOX_H), combined - BOX_H));
+ // A terminal keeps its tag and two body rows (floorRows); a text pane
+ // gives up all but its tag, as acme's do.
+ const lo: f32 = @floatFromInt(@max(BOX_H, floorRows(p, p.col_panes[cc][k])));
+ const hi = combined - @as(f32, @floatFromInt(@max(BOX_H, floorRows(p, p.col_panes[cc][k + 1]))));
+ nt = std.math.clamp(nt, @min(lo, @max(@as(f32, BOX_H), hi)), @max(@as(f32, BOX_H), hi));
if (combined <= 0) return;
// Dragging a folded tag open is an explicit resize, so switch this pair
// back to visible-height weights without consuming other folded weights.
@@ -880,7 +446,7 @@ pub fn applyRowSplit(p: *Pardes, cc: usize, k: usize, cur_y: u16) void {
b.vweight = pair - a.vweight;
}
-pub fn findPane(p: *Pardes, id: usize) ?struct { col: usize, idx: usize } {
+pub fn findPane(p: *const Pardes, id: usize) ?struct { col: usize, idx: usize } {
for (0..p.ncol) |c| {
for (0..p.col_n[c]) |k| {
if (p.col_panes[c][k] == id) return .{ .col = c, .idx = k };
@@ -896,38 +462,36 @@ pub fn insert(p: *Pardes, c: usize, idx: usize, id: usize) void {
p.col_n[c] += 1;
}
+/// Take a pane out of its column. The column stays, empty if this was its
+/// last pane, as acme's colclose leaves it (cols.c:186): only Delcol and
+/// Joincol take a column away.
pub fn removePane(p: *Pardes, id: usize) void {
const f = findPane(p, id) orelse return;
const c = f.col;
var k = f.idx;
while (k + 1 < p.col_n[c]) : (k += 1) p.col_panes[c][k] = p.col_panes[c][k + 1];
p.col_n[c] -= 1;
- if (p.col_n[c] == 0) {
- p.exitHeader();
- if (p.ncol > 1) p.col_weight[if (c > 0) c - 1 else c + 1] +|= p.col_weight[c];
- var j = c;
- while (j + 1 < p.ncol) : (j += 1) {
- p.col_panes[j] = p.col_panes[j + 1];
- p.col_n[j] = p.col_n[j + 1];
- p.col_weight[j] = p.col_weight[j + 1];
- p.col_tags[j] = p.col_tags[j + 1];
- p.col_serial[j] = p.col_serial[j + 1];
- }
- p.ncol -= 1;
- p.col_tags[p.ncol] = .{};
- p.col_serial[p.ncol] = 0;
- }
}
-pub fn joinCol(p: *Pardes) void {
- const f = findPane(p, p.active) orelse return;
- const c = f.col;
- if (c + 1 >= p.ncol) return;
- p.exitHeader();
- const dst = c + 1;
- p.col_weight[dst] +|= p.col_weight[c];
- for (0..p.col_n[c]) |k| p.col_panes[dst][p.col_n[dst] + k] = p.col_panes[c][k];
- p.col_n[dst] += p.col_n[c];
+/// Close column `c` once its panes are gone, its width to the one left of
+/// it (the first column's to its right), as acme's rowclose gives it.
+pub fn dropColumn(p: *Pardes, c: usize) void {
+ const serial = columnSerial(p, c);
+ dropColumnUnannounced(p, c);
+ noteColumn(p, "delcol", serial);
+}
+
+/// dropColumn with no `delcol`, for a column never announced.
+pub fn dropColumnUnannounced(p: *Pardes, c: usize) void {
+ std.debug.assert(p.col_n[c] == 0);
+ tagline.exitHeader(p);
+ // The finder's width was its share, no weight: nobody gets one, so the
+ // others are as wide as before it came. A column whose weight would go
+ // to the finder gives it to the column on the finder's other side.
+ const finder = p.finder.column(p);
+ const heir: ?usize = if (p.ncol <= 1 or finder == c) null else if (c > 0 and finder != c - 1) c - 1 else if (c + 1 < p.ncol and finder != c + 1) c + 1 else if (c > 1) c - 2 else if (c + 2 < p.ncol) c + 2 else null;
+ if (heir) |h| p.col_weight[h] +|= p.col_weight[c];
+ p.col_tags[c].deinit(p.gpa);
var j = c;
while (j + 1 < p.ncol) : (j += 1) {
p.col_panes[j] = p.col_panes[j + 1];
@@ -937,47 +501,429 @@ pub fn joinCol(p: *Pardes) void {
p.col_serial[j] = p.col_serial[j + 1];
}
p.ncol -= 1;
- p.col_tags[p.ncol] = .{};
+ p.col_tags[p.ncol] = .{ .what = .column, .gpa = p.gpa };
p.col_serial[p.ncol] = 0;
}
+/// `newcol <serial>` or `delcol <serial>` in the log: the column's serial,
+/// as /layout and /col name it.
+pub fn noteColumn(p: *Pardes, what: []const u8, serial: u32) void {
+ var buf: [16]u8 = undefined;
+ pardes.ctlfs.events.notePath(p, what, std.fmt.bufPrint(&buf, "{d}", .{serial}) catch return);
+}
+
+/// Fold column `c` into the one on its right, its panes below theirs.
+pub fn joinCol(p: *Pardes, c: usize) void {
+ if (c + 1 >= p.ncol) return;
+ const dst = c + 1;
+ joinPanes(p, c, dst);
+ dropColumn(p, c);
+}
+
+/// Whether column `c` joined into the one on its right leaves every pane
+/// its tag and 2 rows (minimaFit), tried in place and undone: nothing is
+/// dropped or logged for a join that does not fit.
+pub fn joinFits(p: *Pardes, c: usize) bool {
+ if (c + 1 >= p.ncol) return true;
+ const dst = c + 1;
+ const n = p.col_n[c];
+ const had = p.col_n[dst];
+ const weights = .{ p.col_weight[c], p.col_weight[dst] };
+ const panes_c = p.col_panes[c];
+ joinPanes(p, c, dst);
+ const fits = minimaFit(p);
+ p.col_n[dst] = had;
+ p.col_n[c] = n;
+ p.col_panes[c] = panes_c;
+ p.col_weight[c] = weights[0];
+ p.col_weight[dst] = weights[1];
+ compute(p);
+ return fits;
+}
+
+fn joinPanes(p: *Pardes, c: usize, dst: usize) void {
+ for (0..p.col_n[c]) |k| p.col_panes[dst][p.col_n[dst] + k] = p.col_panes[c][k];
+ p.col_n[dst] += p.col_n[c];
+ p.col_n[c] = 0;
+ // The width goes right, where the panes went.
+ p.col_weight[dst] +|= p.col_weight[c];
+ p.col_weight[c] = 0;
+}
+
+/// Put new pane `id` in column `c` as acme's makenewwindow does
+/// (util.c:449-495): an empty column it takes whole; asked from a tag or
+/// from 9P (`origin` null, acme's t->w == nil) it takes the bottom half of
+/// the column's last pane (coladd with y < 0, cols.c:62-65). Asked from a
+/// pane's text, it goes under the text of the pane with the most blank rows
+/// when there are many, else it halves the biggest pane, or `origin` when
+/// that is in the column and not much smaller.
+pub fn acmePlace(p: *Pardes, c: usize, origin: ?usize, id: usize) void {
+ compute(p);
+ const n = p.col_n[c];
+ if (n == 0) {
+ insert(p, c, 0, id);
+ p.panes[id].?.vweight = 1;
+ return;
+ }
+ var big: ?usize = null;
+ var empty: ?usize = null;
+ for (p.col_panes[c][0..n]) |k| {
+ const pane = p.panes[k] orelse continue;
+ // An attachment is its owner's: never halved for another pane.
+ if (pane.collapsed or ownerOf(p, k) != null) continue;
+ // `>=`: at a tie the one nearer the bottom, as acme picks.
+ if (big == null or bodyRows(p, k) >= bodyRows(p, big.?)) big = k;
+ if (empty == null or blankRows(p, k) >= blankRows(p, empty.?)) empty = k;
+ }
+ const v0 = big orelse {
+ // Every pane folded: the new one goes last.
+ insert(p, c, n, id);
+ return;
+ };
+ const y: u16 = y: {
+ const o = origin orelse {
+ var last = v0;
+ for (p.col_panes[c][0..n]) |k| if (!p.panes[k].?.collapsed and ownerOf(p, k) == null) {
+ last = k;
+ };
+ break :y bodyTop(p, last) + bodyRows(p, last) / 2;
+ };
+ const e = empty.?;
+ const el = blankRows(p, e);
+ if (el > 15 or (el > 3 and el > (bodyRows(p, v0) -| 1) / 2))
+ break :y bodyTop(p, e) + bodyRows(p, e) - el;
+ // Asked from a results pane, it is its owner that is split.
+ const root = groupRoot(p, o);
+ const mine = if (findPane(p, root)) |f| f.col == c and !p.panes[root].?.collapsed else false;
+ const v = if (mine and @as(u32, p.rects[root].h) * 3 > @as(u32, p.rects[v0].h) * 2) root else v0;
+ break :y p.rects[v].y + p.rects[v].h / 2;
+ };
+ splitAt(p, c, id, y);
+}
+
+/// The body rows a placement leaves every pane it makes or shrinks, under
+/// its tag (acme's minht keeps one).
+pub const min_body_rows = 2;
+
+/// The rows a placement, and a `size`, leave a pane: its tag at its width
+/// and `min_body_rows`. The one minimum both ask.
+pub fn minRows(p: *Pardes, id: usize) u16 {
+ return p.wantedTagRows(id) + min_body_rows;
+}
+
+/// Whether every column has the rows for each of its unfolded panes'
+/// `minRows` (a folded one its tag row): the panes' own minima, not how
+/// the weights happen to share the rows, so a size that fits keeps fitting
+/// when it grows. One pane alone in its column always fits.
+pub fn minimaFit(p: *Pardes) bool {
+ compute(p);
+ const avail = p.screen_h -| (p.topBarHeight() + p.columnBarHeight());
+ for (0..p.ncol) |c| {
+ if (p.col_n[c] < 2) continue;
+ var need: u32 = 0;
+ for (p.col_panes[c][0..p.col_n[c]]) |k| {
+ const pane = p.panes[k] orelse continue;
+ need += if (pane.collapsed) BOX_H else minRows(p, k);
+ }
+ if (need > avail) return false;
+ }
+ return true;
+}
+
+/// Whether every unfolded pane of column `c` has its `minRows` (a pane
+/// alone in its column always does).
+pub fn columnAtMinimums(p: *Pardes, c: usize) bool {
+ compute(p);
+ if (p.col_n[c] < 2) return true;
+ for (p.col_panes[c][0..p.col_n[c]]) |k| {
+ const pane = p.panes[k] orelse continue;
+ if (!pane.collapsed and p.rects[k].h < minRows(p, k)) return false;
+ }
+ return true;
+}
+
+/// Shares column `c`'s rows out again so every unfolded pane has its
+/// `minRows`, as evenly as that allows; false, changing nothing, when the
+/// column has not the rows for all of them (or holds a folded pane).
+pub fn shareColumn(p: *Pardes, c: usize) bool {
+ compute(p);
+ const n = p.col_n[c];
+ if (n == 0) return false;
+ const avail = p.screen_h -| (p.topBarHeight() + p.columnBarHeight());
+ var mins: [MAX_PANES]u16 = undefined;
+ var need: u32 = 0;
+ for (p.col_panes[c][0..n], 0..) |k, i| {
+ const pane = p.panes[k] orelse return false;
+ if (pane.collapsed) return false;
+ p.rects[k].w = p.col_w[c];
+ mins[i] = minRows(p, k);
+ need += mins[i];
+ }
+ if (need > avail) return false;
+ // Each its minimum, then what is left a row at a time to the smallest.
+ var hs: [MAX_PANES]u16 = undefined;
+ @memcpy(hs[0..n], mins[0..n]);
+ var left = avail - need;
+ while (left > 0) : (left -= 1) {
+ var low: usize = 0;
+ for (1..n) |i| if (hs[i] < hs[low]) {
+ low = i;
+ };
+ hs[low] += 1;
+ }
+ for (p.col_panes[c][0..n], 0..) |k, i| p.panes[k].?.vweight = @floatFromInt(hs[i]);
+ compute(p);
+ return true;
+}
+
+/// After a resize the minima fit: each unfolded pane the resize took below
+/// its `minRows` (it had them in `before`) gets them back, a row at a time
+/// from whichever pane of its column has the most above its own. A pane
+/// dragged short before keeps what it had.
+pub fn restoreMinima(p: *Pardes, before: *const [MAX_PANES]Rect) void {
+ restoreMinimaFrom(p, before, null);
+}
+
+/// Each pane's `minRows` now: what `restoreMinimaFrom` measures a step that
+/// grows the minima (a narrower column wrapping tags) against.
+pub fn minima(p: *Pardes) [MAX_PANES]u16 {
+ compute(p);
+ var mins: [MAX_PANES]u16 = @splat(0);
+ for (0..p.ncol) |c| for (p.col_panes[c][0..p.col_n[c]]) |k| {
+ mins[k] = minRows(p, k);
+ };
+ return mins;
+}
+
+/// restoreMinima, a pane's minimum before the step `before_min` (its tag
+/// may wrap onto more rows since): one that had that minimum is lifted to
+/// its minimum now, one dragged under it before keeps its rows.
+pub fn restoreMinimaFrom(p: *Pardes, before: *const [MAX_PANES]Rect, before_min: ?*const [MAX_PANES]u16) void {
+ compute(p);
+ for (0..p.ncol) |c| {
+ const n = p.col_n[c];
+ if (n < 2) continue;
+ var hs: [MAX_PANES]u16 = undefined;
+ var mins: [MAX_PANES]u16 = undefined;
+ var short = false;
+ for (p.col_panes[c][0..n], 0..) |k, i| {
+ const pane = p.panes[k] orelse return;
+ if (pane.collapsed) break;
+ hs[i] = p.rects[k].h;
+ mins[i] = minRows(p, k);
+ if (hs[i] < mins[i] and before[k].h >= if (before_min) |bm| bm[k] else mins[i]) short = true;
+ } else {
+ if (!short) continue;
+ for (p.col_panes[c][0..n], 0..) |k, i| {
+ if (hs[i] >= mins[i] or before[k].h < if (before_min) |bm| bm[k] else mins[i]) continue;
+ while (hs[i] < mins[i]) {
+ var giver: ?usize = null;
+ var spare: u16 = 0;
+ for (0..n) |j| if (j != i and hs[j] -| mins[j] > spare) {
+ spare = hs[j] - mins[j];
+ giver = j;
+ };
+ const g = giver orelse break;
+ hs[g] -= 1;
+ hs[i] += 1;
+ }
+ }
+ for (p.col_panes[c][0..n], 0..) |k, i| p.panes[k].?.vweight = @floatFromInt(@max(1, hs[i]));
+ }
+ }
+ compute(p);
+}
+
+/// Whether a pane of column `c` is under its minimum now that had its
+/// minimum then (`before`, `before_min`): one a step squeezed, not one
+/// dragged short.
+pub fn squeezed(p: *Pardes, c: usize, before: *const [MAX_PANES]Rect, before_min: *const [MAX_PANES]u16) bool {
+ compute(p);
+ if (p.col_n[c] < 2) return false;
+ for (p.col_panes[c][0..p.col_n[c]]) |k| {
+ const pane = p.panes[k] orelse continue;
+ if (!pane.collapsed and p.rects[k].h < minRows(p, k) and before[k].h >= before_min[k]) return true;
+ }
+ return false;
+}
+
+/// Column `c`'s rows for `id`, last in it, at its minimum: a row at a
+/// time from whichever pane has the most above its own minimum, so a pane
+/// dragged short keeps its rows and the others give only what they must.
+/// False, changing nothing, when the column has not the rows.
+pub fn takeRoom(p: *Pardes, c: usize, id: usize) bool {
+ compute(p);
+ const n = p.col_n[c];
+ p.rects[id].w = p.col_w[c];
+ var need = minRows(p, id);
+ var hs: [MAX_PANES]u16 = undefined;
+ var mins: [MAX_PANES]u16 = undefined;
+ var spare: u32 = 0;
+ for (p.col_panes[c][0..n], 0..) |k, i| {
+ const pane = p.panes[k] orelse return false;
+ if (pane.collapsed) return false;
+ hs[i] = p.rects[k].h;
+ mins[i] = minRows(p, k);
+ spare += hs[i] -| mins[i];
+ }
+ if (spare < need) return false;
+ while (need > 0) : (need -= 1) {
+ var giver: usize = 0;
+ for (1..n) |i| if (hs[i] -| mins[i] > hs[giver] -| mins[giver]) {
+ giver = i;
+ };
+ hs[giver] -= 1;
+ }
+ for (p.col_panes[c][0..n], 0..) |k, i| p.panes[k].?.vweight = @floatFromInt(@max(1, hs[i]));
+ insert(p, c, n, id);
+ p.panes[id].?.vweight = @floatFromInt(minRows(p, id));
+ compute(p);
+ return true;
+}
+
+/// After placing `id`: whether it, and every pane of its column that the
+/// placement shrank, still has its tag and `min_body_rows` (from `before`,
+/// the rects as they were). A pane alone in its column always has room: the
+/// window can be no taller.
+pub fn placedRoomy(p: *Pardes, id: usize, before: *const [MAX_PANES]Rect) bool {
+ compute(p);
+ const f = findPane(p, id) orelse return true;
+ if (p.col_n[f.col] == 1) return true;
+ for (p.col_panes[f.col][0..p.col_n[f.col]]) |k| {
+ const pane = p.panes[k] orelse continue;
+ if (pane.collapsed) continue;
+ if (k != id and p.rects[k].h >= before[k].h) continue;
+ if (p.rects[k].h < minRows(p, k)) return false;
+ }
+ return true;
+}
+
+/// Places `id` last in column `c`, where acme's coladd puts a new window,
+/// with half the rows of the column's tallest unfolded pane, when that
+/// leaves both their rows; false, and `id` in no column, when not.
+pub fn splitTallest(p: *Pardes, c: usize, id: usize) bool {
+ compute(p);
+ var tall: ?usize = null;
+ for (p.col_panes[c][0..p.col_n[c]]) |k| {
+ if (k == id or p.panes[k].?.collapsed or ownerOf(p, k) != null) continue;
+ if (tall == null or p.rects[k].h > p.rects[tall.?].h) tall = k;
+ }
+ const v = tall orelse return false;
+ const h = p.rects[v].h;
+ // Its tag wraps at the column's width: measured there, not at none.
+ p.rects[id].w = p.col_w[c];
+ if (h < minRows(p, v) + minRows(p, id)) return false;
+ snapColWeights(p, c);
+ insert(p, c, p.col_n[c], id);
+ const given = std.math.clamp(h / 2, minRows(p, id), h - minRows(p, v));
+ p.panes[v].?.vweight = @floatFromInt(h - given);
+ p.panes[id].?.vweight = @floatFromInt(given);
+ return true;
+}
+
+/// acme's coladd at row `y` (cols.c:66-126): the new pane goes under the
+/// pane that `y` falls in, which keeps its rows above `y`.
+///
+/// `y` in a group: the new pane goes under all of it, its rows from the
+/// group's owner, so nothing lands between a pane and its results.
+fn splitAt(p: *Pardes, c: usize, id: usize, y: u16) void {
+ const n = p.col_n[c];
+ var k: usize = 0;
+ while (k + 1 < n and y >= p.rects[p.col_panes[c][k]].y + p.rects[p.col_panes[c][k]].h) k += 1;
+ const v = groupRoot(p, p.col_panes[c][k]);
+ const r = p.rects[v];
+ snapColWeights(p, c);
+ insert(p, c, groupEnd(p, c, k), id);
+ const pane = p.panes[id].?;
+ // Each keeps a tag and min_body_rows, where acme's minht keeps one.
+ const lo = r.y +| minRows(p, v);
+ const hi = (r.y + r.h) -| minRows(p, id);
+ const row = splitRowForExtent(r.y, r.h, if (lo <= hi) std.math.clamp(y, lo, hi) else r.y + r.h / 2) orelse {
+ pane.vweight = BOX_H + 1;
+ return;
+ };
+ // `y` fell in an unfolded pane: acmePlace picks from those.
+ p.panes[v].?.vweight = @floatFromInt(@max(1, row - r.y));
+ pane.vweight = @floatFromInt(@max(1, (r.y + r.h) - row));
+}
+
+fn bodyTop(p: *const Pardes, id: usize) u16 {
+ return p.rects[id].y + p.panes[id].?.tag_rows;
+}
+
+fn bodyRows(p: *const Pardes, id: usize) u16 {
+ return p.rects[id].h -| p.panes[id].?.tag_rows;
+}
+
+/// The rows at the bottom of a pane's body that show nothing: acme's
+/// maxlines - nlines. A picture or a PDF page fills its body.
+fn blankRows(p: *const Pardes, id: usize) u16 {
+ const pane = p.panes[id].?;
+ const rows = bodyRows(p, id);
+ const used: usize = if (pane.file) |*f|
+ // The empty line after a final newline shows nothing.
+ (panes.File.nlines(p.gpa, f) -| @intFromBool(std.mem.endsWith(u8, f.content, "\n"))) -| f.scroll
+ else if (pane.isTerminal())
+ @as(usize, panes.terminal.gridCursor(pane).y) + 1
+ else
+ rows;
+ return rows -| @as(u16, @intCast(@min(used, rows)));
+}
+
pub fn canSplitColumn(p: *Pardes, source_id: usize) bool {
- if (p.ncol >= MAX_COLS or source_id >= MAX_PANES or p.panes[source_id] == null) return false;
+ if (source_id >= MAX_PANES or p.panes[source_id] == null) return false;
const source = findPane(p, source_id) orelse return false;
+ return canSplitCol(p, source.col);
+}
+
+fn canSplitCol(p: *Pardes, col: usize) bool {
+ if (p.ncol >= MAX_COLS or col >= p.ncol) return false;
// Refresh derived widths: public layout surgery may be chained between
// syncs, and a cached width must never admit a now-too-narrow split.
compute(p);
- if (p.col_w[source.col] < config.MINW * 2) return false;
+ if (p.col_w[col] < config.MINW * 2) return false;
- const weight = p.col_weight[source.col];
+ const weight = p.col_weight[col];
if (weight >= 2 and weight % 2 == 0) return true;
for (0..p.ncol) |column| if (p.col_weight[column] > std.math.maxInt(u64) / 2)
return false;
return weight > 0;
}
-pub fn splitColumn(p: *Pardes, source_id: usize, id: usize, before: bool) bool {
- if (id >= MAX_PANES or p.panes[id] == null) return false;
- if (!canSplitColumn(p, source_id)) return false;
- const source = findPane(p, source_id) orelse return false;
- const source_col = source.col;
- var old_weight = p.col_weight[source_col];
- const needs_rebase = old_weight < 2 or old_weight % 2 != 0;
- if (needs_rebase) old_weight *= 2;
- if (id == source_id) {
- if (p.col_n[source_col] <= 1) return false;
- absorbVWeight(p, id);
- removePane(p, id);
- } else if (findPane(p, id) != null) return false;
+/// An empty column beside column `col`, with half its width: acme's Newcol
+/// (rowadd, rows.c:51). The new column's index, or null with no room.
+/// A window with no column left (Delcol took the last, as acme lets it):
+/// its first one, empty; null when it has columns.
+pub fn firstColumn(p: *Pardes) ?usize {
+ if (p.ncol != 0) return null;
+ p.col_weight[0] = 2;
+ p.col_tags[0] = .{ .what = .column, .gpa = p.gpa };
+ p.col_serial[0] = 0;
+ p.col_n[0] = 0;
+ p.ncol = 1;
+ noteColumn(p, "newcol", columnSerial(p, 0));
+ return 0;
+}
+
+pub fn insertColumn(p: *Pardes, col: usize, before: bool) ?usize {
+ const c = insertColumnUnannounced(p, col, before) orelse return null;
+ noteColumn(p, "newcol", columnSerial(p, c));
+ return c;
+}
- if (needs_rebase) {
+/// insertColumn with no serial given and no `newcol`: a column that may
+/// yet be taken back (Newcol's trial), which then leaves no trace.
+pub fn insertColumnUnannounced(p: *Pardes, col: usize, before: bool) ?usize {
+ if (!canSplitCol(p, col)) return null;
+ var old_weight = p.col_weight[col];
+ if (old_weight < 2 or old_weight % 2 != 0) {
+ old_weight *= 2;
for (0..p.ncol) |column| p.col_weight[column] *= 2;
}
const source_weight = old_weight / 2;
- const new_weight = old_weight - source_weight;
- p.col_weight[source_col] = source_weight;
- const c = source_col + @intFromBool(!before);
- p.exitHeader();
+ p.col_weight[col] = source_weight;
+ const c = col + @intFromBool(!before);
+ tagline.exitHeader(p);
var j = p.ncol;
while (j > c) : (j -= 1) {
p.col_panes[j] = p.col_panes[j - 1];
@@ -986,12 +932,27 @@ pub fn splitColumn(p: *Pardes, source_id: usize, id: usize, before: bool) bool {
p.col_tags[j] = p.col_tags[j - 1];
p.col_serial[j] = p.col_serial[j - 1];
}
- p.col_weight[c] = new_weight;
- p.col_tags[c] = .{};
+ p.col_weight[c] = old_weight - source_weight;
+ p.col_tags[c] = .{ .what = .column, .gpa = p.gpa };
p.col_serial[c] = 0;
- p.col_panes[c][0] = id;
- p.col_n[c] = 1;
+ p.col_n[c] = 0;
p.ncol += 1;
+ return c;
+}
+
+/// Pane `id` alone in a new column beside `source_id`'s; `id` may be the
+/// source itself when its column keeps another pane.
+pub fn splitColumn(p: *Pardes, source_id: usize, id: usize, before: bool) bool {
+ if (id >= MAX_PANES or p.panes[id] == null) return false;
+ if (!canSplitColumn(p, source_id)) return false;
+ const source = findPane(p, source_id) orelse return false;
+ if (id == source_id) {
+ if (p.col_n[source.col] <= 1) return false;
+ _ = absorbVWeight(p, id, null);
+ removePane(p, id);
+ } else if (findPane(p, id) != null) return false;
+ const c = insertColumn(p, source.col, before).?;
+ insert(p, c, 0, id);
return true;
}
@@ -1004,21 +965,32 @@ pub fn snapColWeights(p: *Pardes, c: usize) void {
}
}
-pub fn absorbVWeight(p: *Pardes, id: usize) void {
- const f = findPane(p, id) orelse return;
- if (p.col_n[f.col] <= 1) return;
+/// Hand a closing pane's rows to `to`. With none named they go up: past any
+/// results listing to the nearest pane above that is not one, or to the
+/// pane below when the closing one is first in its column.
+/// Give a closing pane's rows to `to`, or to the nearest document above it
+/// (the pane below for the top one); the pane that got them is returned.
+pub fn absorbVWeight(p: *Pardes, id: usize, to: ?usize) ?usize {
+ const f = findPane(p, id) orelse return null;
+ if (p.col_n[f.col] <= 1) return null;
+ // A refused pane held no weight: the rows the others' rounding lent it
+ // go back to them as they were, nobody's weight snapped.
+ if (p.panes[id]) |pane| if (!pane.collapsed and pane.vweight == 0) return null;
snapColWeights(p, f.col);
- var sib = if (f.idx > 0) p.col_panes[f.col][f.idx - 1] else p.col_panes[f.col][f.idx + 1];
+ var sib = to orelse if (f.idx > 0) p.col_panes[f.col][f.idx - 1] else p.col_panes[f.col][f.idx + 1];
var k = f.idx;
- while (k > 0) : (k -= 1) {
+ while (to == null and k > 0) : (k -= 1) {
sib = p.col_panes[f.col][k - 1];
- if (p.panes[sib]) |pp| if (if (pp.file) |ff| panes.Output.fileTraits(ff.output).doc else true) break;
+ // nor to a pane attached to another: rows go to owners
+ if (p.panes[sib]) |pp| if (if (pp.file) |ff| panes.Output.fileTraits(ff.output).doc else true) if (ownerOf(p, sib) == null) break;
}
- if (p.panes[sib]) |s| s.vweight += @as(f32, @floatFromInt(@max(1, p.rects[id].h)));
+ const s = p.panes[sib] orelse return null;
+ s.vweight += @as(f32, @floatFromInt(@max(1, p.rects[id].h)));
+ return sib;
}
pub fn splitParent(p: *Pardes, want: usize) usize {
- const need = 2 * BOX_H + 3;
+ const need = 2 * (BOX_H + min_body_rows);
if (p.rects[want].h >= need) return want;
if (findPane(p, want)) |f| for (0..p.col_n[f.col]) |k| {
if (p.rects[p.col_panes[f.col][k]].h >= need) return p.col_panes[f.col][k];
@@ -1035,23 +1007,28 @@ pub fn splitParent(p: *Pardes, want: usize) usize {
pub fn splitBelow(p: *Pardes, src_id: usize, nw: *Pane) void {
const src = p.panes[src_id] orelse return;
const src_h = p.rects[src_id].h;
- const body: u16 = if (src_h > BOX_H) src_h - BOX_H else 1;
- const cur: u16 = if (!src.isTerminal()) body / 2 else panes.Terminal.gridCursor(src).y + 1;
+ // Its tag may wrap to more than one row: the rows under it are its body.
+ const tag_h = @max(BOX_H, src.tag_rows);
+ const body: u16 = if (src_h > tag_h) src_h - tag_h else 1;
+ const cur: u16 = if (!src.isTerminal()) body / 2 else panes.terminal.gridCursor(src).y + 1;
// cap keep so a content-full source still leaves the new pane a tag +
// a few body rows (an Alt-n from a full shell was born 0 rows tall)
- const keep = std.math.clamp(cur, 1, @max(1, body -| (BOX_H + 3)));
+ // ...and the source min_body_rows of its own (exec.placeNew), when it
+ // has them: a boot layout splits before any row is laid out.
+ const least = @min(min_body_rows, body);
+ const keep = std.math.clamp(cur, least, @max(least, body -| (BOX_H + 3)));
if (findPane(p, src_id)) |f| for (0..p.col_n[f.col]) |k| {
const pid = p.col_panes[f.col][k];
if (p.panes[pid]) |pp| if (pp != nw and !pp.collapsed) {
pp.vweight = @floatFromInt(@max(1, p.rects[pid].h));
};
};
- if (!src.collapsed) src.vweight = @floatFromInt(BOX_H + keep);
- nw.vweight = @floatFromInt(@max(1, src_h -| (BOX_H + keep)));
+ if (!src.collapsed) src.vweight = @floatFromInt(tag_h + keep);
+ nw.vweight = @floatFromInt(@max(1, src_h -| (tag_h + keep)));
if (nw.file) |f| if (!panes.Output.fileTraits(f.output).doc) {
// trimmed: every row ends in a newline, and the empty line after
// the last one is not a result
- const want: f32 = @floatFromInt(BOX_H + panes.File.lineCount(std.mem.trimEnd(u8, f.content, "\n")));
+ const want: f32 = @floatFromInt(BOX_H + @max(min_body_rows, panes.File.lineCount(std.mem.trimEnd(u8, f.content, "\n"))));
if (want < nw.vweight) {
src.vweight += nw.vweight - want;
nw.vweight = want;
@@ -1059,6 +1036,166 @@ pub fn splitBelow(p: *Pardes, src_id: usize, nw: *Pane) void {
};
}
+// ---- Attached panes ----
+//
+// A results pane (+Search, +Callstack, +Diff, +Errors) is ATTACHED to the
+// pane it answers for, its owner (Pane.attached_to). The owner and what is
+// attached to it, and what is attached to those (a preview under a
+// +Search), are a GROUP: contiguous in one column, the owner first. A group
+// is one place to everything else in the layout: an unrelated pane is never
+// put inside it (acmePlace, splitAt, splitTallest), a pane dragged onto it
+// lands under all of it (movePlacement), and its owner's grip, Collapse and
+// Del take all of it (movePane, toggleCollapse, Pardes.removePane). An
+// attachment's rows come from its owner and go back to it, nobody else's.
+
+/// The slot of `id`'s owner, when `id` is attached to a live pane.
+pub fn ownerOf(p: *const Pardes, id: usize) ?usize {
+ const serial = (p.panes[id] orelse return null).attached_to orelse return null;
+ for (p.panes, 0..) |slot, i| if (slot) |pane| if (pane.serial == serial and i != id) return i;
+ return null;
+}
+
+/// The owner at the top of `id`'s group: `id` itself when it is attached to
+/// nothing. Bounded: a dump could name a ring of owners.
+pub fn groupRoot(p: *const Pardes, id: usize) usize {
+ var at = id;
+ for (0..MAX_PANES) |_| at = ownerOf(p, at) orelse return at;
+ return at;
+}
+
+/// Whether `id` is `root` or attached to it through any number of owners.
+fn descends(p: *const Pardes, id: usize, root: usize) bool {
+ var at = id;
+ for (0..MAX_PANES) |_| {
+ if (at == root) return true;
+ at = ownerOf(p, at) orelse return false;
+ }
+ return false;
+}
+
+/// One past the last pane of the group that pane `idx` of column `c` is
+/// in: `idx + 1` for a pane with nothing attached.
+pub fn groupEnd(p: *const Pardes, c: usize, idx: usize) usize {
+ const root = groupRoot(p, p.col_panes[c][idx]);
+ var k = idx + 1;
+ while (k < p.col_n[c] and descends(p, p.col_panes[c][k], root) and p.col_panes[c][k] != root) k += 1;
+ return k;
+}
+
+/// The attachments kept where they are attached: one whose owner is gone,
+/// or that something (a 9P move, a pane split into a column of its own, an
+/// owner moved by a route that does not carry its group) left anywhere but
+/// right under its owner's group, is an ordinary pane from then on, where
+/// it stands. Nothing moves. Top to bottom, so a nested attachment is
+/// judged after the one it hangs from.
+pub fn keepGroups(p: *Pardes) void {
+ for (0..p.ncol) |c| for (p.col_panes[c][0..p.col_n[c]], 0..) |id, k| {
+ const pane = p.panes[id] orelse continue;
+ if (pane.attached_to == null) continue;
+ const kept = if (ownerOf(p, id)) |owner| k > 0 and descends(p, p.col_panes[c][k - 1], owner) else false;
+ if (kept) continue;
+ pane.attached_to = null;
+ };
+}
+
+/// Attach new pane `id`, in no column yet, to `owner`: under the owner's
+/// group, `rows` of the owner's rows (tag included), from the owner and
+/// from no other pane. The owner keeps its own minimum, else its tag (acme
+/// lets a window shrink to it), so the attachment may get fewer. A folded
+/// owner is unfolded first. The rows are the attachment's from then on:
+/// nothing here ever resizes it again; the seam is the user's to drag.
+/// False, `id` in no column, when the owner has not the rows for its tag
+/// and the attachment's minimum.
+pub fn attach(p: *Pardes, owner: usize, id: usize, rows: u16) bool {
+ return attachFrom(p, owner, id, rows, owner);
+}
+
+/// attach, the rows given by `giver`: the owner itself or a pane of its
+/// group (a +Preview added under a query pane later takes the text's rows,
+/// so the query pane, typed into, keeps its own).
+pub fn attachFrom(p: *Pardes, owner: usize, id: usize, rows: u16, giver: usize) bool {
+ const o = p.panes[owner] orelse return false;
+ const g = p.panes[giver] orelse return false;
+ const nw = p.panes[id] orelse return false;
+ if (o.collapsed) toggleCollapse(p, owner);
+ if (g.collapsed) toggleCollapse(p, giver);
+ compute(p);
+ const f = findPane(p, owner) orelse return false;
+ if (giver != owner and groupRoot(p, owner) != groupRoot(p, giver)) return false;
+ // Its tag wraps at the column's width: measured there.
+ p.rects[id].w = p.col_w[f.col];
+ const oh = p.rects[giver].h;
+ const least = minRows(p, id);
+ if (oh < least + BOX_H) return false;
+ const giver_keeps = if (oh >= least + minRows(p, giver)) minRows(p, giver) else BOX_H;
+ const give = std.math.clamp(rows, least, oh - giver_keeps);
+ snapColWeights(p, f.col);
+ insert(p, f.col, groupEnd(p, f.col, f.idx), id);
+ g.vweight = @floatFromInt(oh - give);
+ nw.vweight = @floatFromInt(give);
+ nw.attached_to = o.serial;
+ compute(p);
+ return true;
+}
+
+/// A closed pane brought back (Store.reopen) where it was: next to
+/// `gainer`, the pane its rows went to when it closed, below it (under its
+/// group) or above it, `rows` tall, those rows taken from `gainer` alone,
+/// so no other pane moves. False when the gainer is folded or too short
+/// to give a pane its minimum and keep its own.
+pub fn giveBack(p: *Pardes, id: usize, gainer: usize, below: bool, rows: u16) bool {
+ const g = p.panes[gainer] orelse return false;
+ const nw = p.panes[id] orelse return false;
+ if (g.collapsed) return false;
+ compute(p);
+ const f = findPane(p, gainer) orelse return false;
+ // Its tag wraps at the column's width: measured there.
+ p.rects[id].w = p.col_w[f.col];
+ const gh = p.rects[gainer].h;
+ const least = minRows(p, id);
+ const keeps = minRows(p, gainer);
+ if (gh < least + keeps) return false;
+ const give = std.math.clamp(rows, least, gh - keeps);
+ snapColWeights(p, f.col);
+ insert(p, f.col, if (below) groupEnd(p, f.col, f.idx) else f.idx, id);
+ g.vweight = @floatFromInt(gh - give);
+ nw.vweight = @floatFromInt(give);
+ compute(p);
+ return true;
+}
+
+/// Collapse on an owner with attachments: the whole group folds to its
+/// tags, every row it gave up to the one unfolded neighbour outside it,
+/// and unfolds again from that neighbour, as one pane's Collapse does.
+/// ponytail: unfolding opens every attachment, one folded alone before
+/// included; remembering which were is a bit a pane.
+fn collapseGroup(p: *Pardes, c: usize, start: usize, end: usize) void {
+ const root = p.panes[p.col_panes[c][start]].?;
+ const folding = !root.collapsed;
+ snapColWeights(p, c);
+ const neighbor = expandedNeighbor(p, c, start, .above) orelse expandedNeighbor(p, c, end - 1, .below);
+ var spare: u16 = if (neighbor) |other| p.rects[other].h -| BOX_H else 0;
+ for (p.col_panes[c][start..end]) |k| {
+ const pane = p.panes[k] orelse continue;
+ if (folding) {
+ if (pane.collapsed) continue;
+ const h = p.rects[k].h;
+ pane.vweight = @floatFromInt(@max(1, h));
+ pane.collapsed = true;
+ if (neighbor) |other| p.panes[other].?.vweight += @floatFromInt(h -| BOX_H);
+ } else {
+ if (!pane.collapsed) continue;
+ const wanted: u16 = @intFromFloat(@min(@as(f32, @floatFromInt(std.math.maxInt(u16))), @max(1, @round(pane.vweight))));
+ const give = if (neighbor != null) @min(wanted -| BOX_H, spare) else 0;
+ spare -= give;
+ if (neighbor) |other| p.panes[other].?.vweight -= @floatFromInt(give);
+ pane.vweight = @floatFromInt(@max(1, BOX_H + give));
+ pane.collapsed = false;
+ }
+ }
+ compute(p);
+}
+
pub fn columnFitsHalves(p: *Pardes, source_id: usize, min_cells: u16) bool {
const f = findPane(p, source_id) orelse return false;
compute(p);
@@ -1084,13 +1221,18 @@ pub fn toggleCollapse(p: *Pardes, id: usize) void {
const pane = p.panes[id] orelse return;
const at = findPane(p, id) orelse return;
compute(p);
+ // An owner folds and unfolds with what is attached to it.
+ if (ownerOf(p, id) == null) {
+ const end = groupEnd(p, at.col, at.idx);
+ if (end > at.idx + 1) return collapseGroup(p, at.col, at.idx, end);
+ }
const was_collapsed = pane.collapsed;
const old_height = p.rects[id].h;
const remembered_height = pane.vweight;
// Convert only visible panes to row weights. The toggle then changes one
// pair, rather than letting the column redistribute the released space.
snapColWeights(p, at.col);
- const neighbor = collapseNeighbor(p, at.col, at.idx);
+ const neighbor = expandedNeighbor(p, at.col, at.idx, .above) orelse expandedNeighbor(p, at.col, at.idx, .below);
if (was_collapsed) {
if (neighbor) |other| {
const wanted: u16 = @intFromFloat(@min(@as(f32, @floatFromInt(std.math.maxInt(u16))), @max(1, @round(remembered_height))));
@@ -1110,15 +1252,17 @@ pub fn toggleCollapse(p: *Pardes, id: usize) void {
compute(p);
}
-fn collapseNeighbor(p: *Pardes, col: usize, index: usize) ?usize {
- var above = index;
- while (above > 0) {
- above -= 1;
- const id = p.col_panes[col][above];
+/// The nearest pane on one side of `index` in its column that is not folded
+/// to its tag: the one Collapse lends rows to and Del k / Del j gives them
+/// to. A folded pane only keeps its weight for later, so rows handed to it
+/// would spread over the whole column instead.
+pub fn expandedNeighbor(p: *const Pardes, col: usize, index: usize, side: enum { above, below }) ?usize {
+ var k = index;
+ while (if (side == .above) k > 0 else k + 1 < p.col_n[col]) {
+ k = if (side == .above) k - 1 else k + 1;
+ const id = p.col_panes[col][k];
if (p.panes[id]) |pane| if (!pane.collapsed) return id;
}
- for (p.col_panes[col][index + 1 .. p.col_n[col]]) |id|
- if (p.panes[id]) |pane| if (!pane.collapsed) return id;
return null;
}
@@ -1158,25 +1302,34 @@ fn computeCollapsedColumn(p: *Pardes, c: usize, x: u16, width: u16, folded: usiz
pub fn compute(p: *Pardes) void {
p.rects = @splat(.{});
if (p.ncol == 0) return;
- for (0..p.ncol) |column| if (p.col_serial[column] == 0) {
- p.col_serial[column] = p.next_column_serial;
- p.next_column_serial +%= 1;
- if (p.next_column_serial == 0) p.next_column_serial = 1;
- };
+ keepGroups(p);
+ for (0..p.ncol) |column| _ = columnSerial(p, column);
+ // The finder column is as wide as its share says (Finder.width), not
+ // by weight: the others share what it leaves by theirs, so it comes
+ // and goes and each of them keeps its proportion, none rounded off.
+ const finder = p.finder.column(p);
+ const finder_w: u16 = if (finder != null) p.finder.width(p) else 0;
+ const rest = p.screen_w -| finder_w;
var wsum: u128 = 0;
- for (0..p.ncol) |c| wsum += p.col_weight[c];
+ var last_plain: usize = p.ncol;
+ for (0..p.ncol) |c| if (finder != c) {
+ wsum += p.col_weight[c];
+ last_plain = c;
+ };
if (wsum == 0) wsum = 1;
// Round cumulative boundaries so widths still sum to the available screen.
var prefix: u128 = 0;
+ var offset: u16 = 0;
for (0..p.ncol) |c| {
- const last = c + 1 == p.ncol;
- const x = columnBoundary(p.screen_w, prefix, wsum);
- prefix += p.col_weight[c];
- const end: u16 = if (last)
- p.screen_w
- else
- columnBoundary(p.screen_w, prefix, wsum);
+ const x = offset + columnBoundary(rest, prefix, wsum);
+ const end: u16 = if (finder == c) end: {
+ offset = finder_w;
+ break :end x + finder_w;
+ } else end: {
+ prefix += p.col_weight[c];
+ break :end if (c == last_plain) offset + rest else offset + columnBoundary(rest, prefix, wsum);
+ };
const cw = end -| x;
p.col_x[c] = x;
p.col_w[c] = cw;
@@ -1197,21 +1350,65 @@ pub fn compute(p: *Pardes) void {
if (vsum <= 0) vsum = 1;
var y: u16 = p.topBarHeight() + p.columnBarHeight();
+ const top = y;
const avail_h = p.screen_h -| y;
- for (0..p.col_n[c]) |k| {
+ var hs: [MAX_PANES]u16 = undefined;
+ const n = p.col_n[c];
+ for (0..n) |k| {
const id = p.col_panes[c][k];
- const pane = p.panes[id] orelse continue;
- const lastk = k + 1 == p.col_n[c];
+ const pane = p.panes[id] orelse {
+ hs[k] = 0;
+ continue;
+ };
+ const lastk = k + 1 == n;
const fh = @as(f32, @floatFromInt(avail_h)) * pane.vweight / vsum;
const room = p.screen_h -| y;
- const ch: u16 = if (lastk) room else @min(room, @max(1, @as(u16, @intFromFloat(@round(fh)))));
- p.rects[id] = .{ .x = x, .y = y, .w = cw, .h = ch };
- y +|= ch;
+ hs[k] = if (lastk) room else @min(room, @max(1, @as(u16, @intFromFloat(@round(fh)))));
+ y +|= hs[k];
+ }
+ keepTerminalFloors(p, c, hs[0..n]);
+ y = top;
+ for (0..n) |k| {
+ const id = p.col_panes[c][k];
+ if (p.panes[id] == null) continue;
+ p.rects[id] = .{ .x = x, .y = y, .w = cw, .h = hs[k] };
+ y +|= hs[k];
}
}
}
-pub const ascii_max_movement_frames: u16 = 12;
+/// The fewest rows a pane is laid out with: a terminal keeps its tag and
+/// `min_body_rows` (a shorter one loses its prompt and reads busy for
+/// ever), a text pane may shrink to its tag, as acme's windows do.
+fn floorRows(p: *Pardes, id: usize) u16 {
+ const pane = p.panes[id] orelse return 0;
+ return if (pane.isTerminal()) @max(1, pane.tag_rows) + min_body_rows else 1;
+}
+
+/// Raises each terminal in column `c` below its floor to it, a row at a
+/// time from whichever pane has the most rows above its own floor. Where
+/// the column has not the rows, the terminal keeps what it could get.
+// ponytail: a row per step, O(panes x rows); a column holds few of both.
+fn keepTerminalFloors(p: *Pardes, c: usize, hs: []u16) void {
+ for (hs, 0..) |*h, k| {
+ const floor = floorRows(p, p.col_panes[c][k]);
+ while (h.* < floor) {
+ var giver: ?usize = null;
+ var spare: u16 = 0;
+ for (hs, 0..) |other, j| {
+ if (j == k) continue;
+ const extra = other -| floorRows(p, p.col_panes[c][j]);
+ if (extra > spare) {
+ spare = extra;
+ giver = j;
+ }
+ }
+ const g = giver orelse break;
+ hs[g] -= 1;
+ h.* += 1;
+ }
+ }
+}
test "collapse transfers height upward and restores the original layout" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 32 });
@@ -1297,7 +1494,6 @@ test "collapse local transfers remain bounded through repeated tiny window resiz
}
for (0..180) |step| {
p.screen_h = @intCast((step * 17) % 81);
- p.settings.column_tags = step % 2 == 0;
p.settings.tag_bottom = step % 3 == 0;
compute(p);
toggleCollapse(p, step % 5);
@@ -1317,9 +1513,10 @@ test "collapse local transfers remain bounded through repeated tiny window resiz
test "collapse expansion is limited to space available in one neighbor" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 42 });
defer p.deinit();
+ // Text panes: a terminal would keep its tag and two rows (floorRows).
for (1..4) |id| {
- _ = try p.newShell(id, "");
- insert(p, 0, id, id);
+ p.newScratchBelow(0);
+ try std.testing.expectEqual(id, p.col_panes[0][id]);
}
compute(p);
toggleCollapse(p, 2);
@@ -1352,7 +1549,7 @@ test "collapse single and all panes leave unused column space" {
try std.testing.expectEqual(p.screen_h - 1, p.rects[1].y);
}
-test "collapse survives tiny resizes column tags and TagBottom" {
+test "collapse survives tiny resizes and TagBottom" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 32 });
defer p.deinit();
_ = try p.newShell(1, "");
@@ -1362,8 +1559,7 @@ test "collapse survives tiny resizes column tags and TagBottom" {
p.panes[1].?.collapsed = true;
for ([_]bool{ false, true }) |bottom| {
p.settings.tag_bottom = bottom;
- for ([_]bool{ false, true }) |column_tags| {
- p.settings.column_tags = column_tags;
+ {
for (0..50) |height| {
p.screen_h = @intCast(height);
compute(p);
@@ -1408,8 +1604,8 @@ test "collapse dump restores folded state and retained expansion weight" {
compute(p);
const original_height = p.rects[0].h;
toggleCollapse(p, 0);
- try p.dumpState();
- const restored = try Pardes.initFromDump(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 32 }, p.dump_out.?);
+ try dump.dumpState(p);
+ const restored = try dump.initFromDump(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 32 }, p.dump_out.?);
defer restored.deinit();
try std.testing.expect(restored.panes[0].?.collapsed);
try std.testing.expectEqual(@as(f32, @floatFromInt(original_height)), restored.panes[0].?.vweight);
@@ -1418,789 +1614,6 @@ test "collapse dump restores folded state and retained expansion weight" {
try std.testing.expectEqual(restored.screen_h - restored.rects[0].y, restored.rects[0].h);
}
-pub const Easing = enum(u8) {
- linear,
- smooth,
- /// Quintic ease-in-out. It creeps at both ends and crosses the middle of
- /// the distance fast, inside the same frame count a linear walk would use.
- smoother,
- in_cubic,
- out_cubic,
- out_back,
-};
-
-pub const Transition = enum(u8) {
- // Numeric values are shared with the GUI shader ABI.
- off = 0,
- slide = 1,
- zoom = 2,
- dissolve = 3,
- ascii = 4,
- vertical = 5,
- edges = 6,
- fall = 7,
- wave = 8,
- curtain = 9,
- scramble = 10,
- typewriter = 11,
-
- pub fn easing(effect: Transition) Easing {
- return switch (effect) {
- .off, .dissolve, .wave => .smooth,
- .slide, .vertical, .edges => .out_cubic,
- .zoom => .out_back,
- // Character walks and per-cell locks read best with a slow start,
- // a fast middle, and a slow settle over their fixed frame count.
- .ascii, .fall, .scramble => .smoother,
- // A sweep and a typewriter are constant-rate by definition: easing
- // their head would make the pass visibly hesitate mid-pane.
- .curtain, .typewriter => .linear,
- };
- }
-
- pub fn frames(effect: Transition) u16 {
- return switch (effect) {
- .off => 0,
- .slide => 12,
- .zoom => 14,
- .dissolve => 10,
- .ascii => ascii_max_movement_frames + 1,
- .vertical => 12,
- .edges, .curtain, .scramble => 12,
- // Travelling motion needs a couple more samples than a lock or a
- // rigid slide before it stops reading as a jump.
- .fall, .wave, .typewriter => 14,
- };
- }
-
- pub fn composedByCore(effect: Transition) bool {
- return switch (effect) {
- .ascii, .edges, .fall, .wave, .curtain, .scramble, .typewriter => true,
- .off, .slide, .zoom, .dissolve, .vertical => false,
- };
- }
-
- pub fn needsPreviousGrid(effect: Transition) bool {
- return effect == .dissolve or effect == .vertical or effect.composedByCore();
- }
-
- pub fn lifecycleOnly(effect: Transition) bool {
- return effect == .vertical;
- }
-};
-
-pub const SceneEffect = struct {
- crt: bool = false,
- ripple: bool = false,
- glitch: bool = false,
-};
-
-pub const Phase = enum(u8) {
- opening = 0,
- moving = 1,
- /// Presentation-only content whose pane lifetime has already ended.
- /// It is never a valid input target.
- closing = 2,
-};
-
-pub const Box = extern struct {
- x: f32 = 0,
- y: f32 = 0,
- w: f32 = 0,
- h: f32 = 0,
-
- pub fn eql(a: Box, b: Box) bool {
- return a.x == b.x and a.y == b.y and a.w == b.w and a.h == b.h;
- }
-
- // A cell belongs to a fractional box when its center lies inside.
- pub fn contains(box: Box, col: u16, row: u16) bool {
- const x: f32 = @floatFromInt(col);
- const y: f32 = @floatFromInt(row);
- return x >= box.x and x < box.x + box.w and y >= box.y and y < box.y + box.h;
- }
-};
-
-// Drawing and hit testing share this moving/opening/closing order.
-pub fn paintOrder(live: []const ?Track, closing: []const Track, out: []Track) usize {
- var len: usize = 0;
- for ([_]Phase{ .moving, .opening }) |phase| for (live) |maybe| {
- const track = maybe orelse continue;
- if (!track.active() or track.phase != phase) continue;
- if (len == out.len) return len;
- out[len] = track;
- len += 1;
- };
- for (closing) |track| {
- if (!track.active()) continue;
- if (len == out.len) return len;
- out[len] = track;
- len += 1;
- }
- return len;
-}
-
-/// One POD record is enough for every backend. `from` and `to` are logical
-/// cell boxes; frontends convert them to pixels only at their render edge.
-pub const Track = extern struct {
- serial: u32 = 0,
- pane: u8 = 0,
- phase: Phase = .moving,
- effect: Transition = .off,
- _padding: u8 = 0,
- frame: u16 = 0,
- frame_count: u16 = 0,
- from: Box = .{},
- to: Box = .{},
-
- pub fn active(track: Track) bool {
- return track.effect != .off and track.frame < track.frames();
- }
-
- pub fn frames(track: Track) u16 {
- return if (track.frame_count != 0) track.frame_count else track.effect.frames();
- }
-
- pub fn amount(track: Track) f32 {
- // Opening rises quickly and settles; closing reverses that motion and
- // accelerates down out of the fixed clip.
- if (track.phase == .closing and track.effect == .vertical)
- return progressEased(.in_cubic, track.frames(), track.frame);
- return progressEased(track.effect.easing(), track.frames(), track.frame);
- }
-
- pub fn presented(track: Track) Box {
- return lerpBox(track.from, track.to, track.amount());
- }
-
- pub fn visualBox(track: Track) Box {
- return switch (track.effect) {
- .slide, .zoom, .vertical => track.presented(),
- .off, .dissolve => track.to,
- // Every character effect stays inside the pane's final rectangle.
- .ascii, .edges, .fall, .wave, .curtain, .scramble, .typewriter => track.to,
- };
- }
-
- pub fn contentBox(track: Track) Box {
- return if (track.phase == .closing) track.from else track.to;
- }
-};
-
-pub fn openingBox(effect: Transition, target: Box, screen_width: u16) Box {
- return switch (effect) {
- .slide => blk: {
- var from = target;
- const middle = target.x + target.w * 0.5;
- from.x = if (middle < @as(f32, @floatFromInt(screen_width)) * 0.5)
- -target.w
- else
- @floatFromInt(screen_width);
- break :blk from;
- },
- .zoom => .{
- .x = target.x + target.w * 0.5,
- .y = target.y + target.h * 0.5,
- .w = 0,
- .h = 0,
- },
- .vertical => blk: {
- var from = target;
- from.y += target.h;
- break :blk from;
- },
- .off, .dissolve => target,
- // Character effects own the glyphs inside a fixed rectangle, so their
- // panel opens at exactly its final geometry.
- .ascii, .edges, .fall, .wave, .curtain, .scramble, .typewriter => target,
- };
-}
-
-pub fn closingBox(effect: Transition, source: Box) Box {
- return switch (effect) {
- .vertical => blk: {
- var to = source;
- to.y += source.h;
- break :blk to;
- },
- else => source,
- };
-}
-
-pub fn sample(easing: Easing, raw: f32) f32 {
- const t = std.math.clamp(raw, 0.0, 1.0);
- return switch (easing) {
- .linear => t,
- .smooth => t * t * (3.0 - 2.0 * t),
- .smoother => t * t * t * (t * (6.0 * t - 15.0) + 10.0),
- .in_cubic => t * t * t,
- .out_cubic => 1.0 - (1.0 - t) * (1.0 - t) * (1.0 - t),
- // Robert Penner's ease-out-back polynomial. It intentionally travels
- // a little past one before settling exactly on the endpoint.
- .out_back => blk: {
- const c1: f32 = 1.70158;
- const c3 = c1 + 1.0;
- const u = t - 1.0;
- break :blk 1.0 + c3 * u * u * u + c1 * u * u;
- },
- };
-}
-
-pub fn progress(effect: Transition, frame: u16) f32 {
- return progressEased(effect.easing(), effect.frames(), frame);
-}
-
-fn progressEased(easing: Easing, frames: u16, frame: u16) f32 {
- if (frames <= 1 or frame >= frames - 1) return 1.0;
- return sample(easing, @as(f32, @floatFromInt(frame)) / @as(f32, @floatFromInt(frames - 1)));
-}
-
-pub fn lerpBox(from: Box, to: Box, t: f32) Box {
- const u = @max(0.0, t);
- return .{
- .x = from.x + (to.x - from.x) * u,
- .y = from.y + (to.y - from.y) * u,
- .w = @max(0.0, from.w + (to.w - from.w) * u),
- .h = @max(0.0, from.h + (to.h - from.h) * u),
- };
-}
-
-/// Stable cell noise shared by the TTY reveal and shader ports. Integer-only
-/// hashing means resizing or repainting a frame does not make cells flicker.
-pub fn cellNoise(serial: u32, col: u16, row: u16) f32 {
- var x = serial ^ (@as(u32, col) *% 0x9e37_79b9) ^ (@as(u32, row) *% 0x85eb_ca6b);
- x ^= x >> 16;
- x *%= 0x7feb_352d;
- x ^= x >> 15;
- x *%= 0x846c_a68b;
- x ^= x >> 16;
- return @as(f32, @floatFromInt(x & 0xffff)) / 65535.0;
-}
-
-/// Whether a changed dissolve cell has crossed from the frozen old grid to
-/// the new one. Exact endpoints are part of the presentation contract.
-pub fn dissolveRevealed(serial: u32, col: u16, row: u16, raw_progress: f32) bool {
- const t = std.math.clamp(raw_progress, 0.0, 1.0);
- if (t <= 0) return false;
- if (t >= 1) return true;
- return cellNoise(serial, col, row) < t;
-}
-
-pub const CellArea = struct {
- x0: u16 = 0,
- y0: u16 = 0,
- cols: u16 = 1,
- rows: u16 = 1,
-
- pub fn of(box: Box) CellArea {
- return .{
- .x0 = floorCell(box.x),
- .y0 = floorCell(box.y),
- .cols = ceilCell(box.w),
- .rows = ceilCell(box.h),
- };
- }
-};
-
-fn floorCell(value: f32) u16 {
- return @intFromFloat(std.math.clamp(@floor(value), 0.0, @as(f32, std.math.maxInt(u16))));
-}
-
-fn ceilCell(value: f32) u16 {
- return @intFromFloat(std.math.clamp(@ceil(value), 1.0, @as(f32, std.math.maxInt(u16))));
-}
-
-pub const CharSource = union(enum) {
- /// Nothing has arrived here yet: keep the frozen old cell.
- old,
- at: Offset,
- /// Paint this printable byte in the destination cell's own style, whatever
- /// that cell holds — a caret marching over empty space is still a caret.
- byte: u8,
- churn: u8,
-
- pub const Offset = struct { cols: i32 = 0, rows: i32 = 0 };
-
- pub const settled: CharSource = .{ .at = .{} };
-};
-
-pub fn charSource(track: Track, col: u16, row: u16, area: CellArea) CharSource {
- const t = track.amount();
- if (t >= 1.0) return .settled;
- const w: f32 = @floatFromInt(area.cols);
- const h: f32 = @floatFromInt(area.rows);
- const c: f32 = @floatFromInt(col);
- const r: f32 = @floatFromInt(row);
- const remaining = 1.0 - t;
- return switch (track.effect) {
- .edges => blk: {
- const travel = cellsOf(remaining * (w + 1.0));
- break :blk .{ .at = .{ .cols = if (row % 2 == 0) travel else -travel } };
- },
- // Columns rain down, each with its own stable head start, so the pane
- // fills from the top and the last glyphs land at the bottom.
- .fall => blk: {
- const local = staggered(t, cellNoise(track.serial, col, 0) * 0.4);
- if (local <= 0.0) break :blk .old;
- break :blk .{ .at = .{ .rows = cellsOf((1.0 - local) * (h + 1.0)) } };
- },
- // A vertical ripple travels left to right and its amplitude decays, so
- // the pane settles out of a wave instead of a fade.
- .wave => .{ .at = .{
- .rows = cellsOf(remaining * @min(4.0, h) * @sin(c * 0.55 - t * 9.0)),
- } },
- // A curtain of glyphs marches in from the right, column by column, left
- // to right; each column still has a short slide of its own.
- .curtain => blk: {
- const lead = t * (w + 1.0) - c;
- if (lead <= 0.0) break :blk .old;
- break :blk .{ .at = .{ .cols = -cellsOf(@max(0.0, 3.0 - lead)) } };
- },
- // Every cell churns through printable ASCII and locks onto its final
- // glyph at its own stable threshold: the pane resolves out of noise.
- .scramble => blk: {
- if (t >= cellNoise(track.serial, col, row) * 0.8) break :blk .settled;
- const churn = cellNoise(
- track.serial ^ (@as(u32, track.frame) *% 0x27d4_eb2f),
- col,
- row,
- );
- break :blk .{ .churn = @intCast(33 + @min(93, @as(u32, @intFromFloat(churn * 94.0)))) };
- },
- // Reading-order reveal with a caret sitting on the write head.
- .typewriter => blk: {
- const head = t * w * h;
- const index = r * w + c;
- if (index + 1.0 <= head) break :blk .settled;
- if (index <= head) break :blk .{ .byte = '_' };
- break :blk .old;
- },
- // PanelAscii walks its own byte distance per cell, and the geometry
- // effects never reach this path at all.
- .off, .slide, .zoom, .dissolve, .vertical, .ascii => .settled,
- };
-}
-
-fn cellsOf(distance: f32) i32 {
- return @intFromFloat(@round(std.math.clamp(distance, -65535.0, 65535.0)));
-}
-
-/// Remap track progress into one cell's own window. A stagger delays a glyph
-/// without making the effect as a whole end after its last frame.
-fn staggered(t: f32, delay: f32) f32 {
- if (delay >= 1.0) return t;
- return (t - delay) / (1.0 - delay);
-}
-
-test "easing presets have exact endpoints and intended shapes" {
- inline for (std.enums.values(Easing)) |easing| {
- try std.testing.expectEqual(@as(f32, 0), sample(easing, 0));
- try std.testing.expectEqual(@as(f32, 1), sample(easing, 1));
- }
- try std.testing.expectEqual(@as(f32, 0.5), sample(.linear, 0.5));
- try std.testing.expect(sample(.in_cubic, 0.5) < sample(.linear, 0.5));
- try std.testing.expect(sample(.out_cubic, 0.5) > sample(.linear, 0.5));
- try std.testing.expect(sample(.out_back, 0.8) > 1.0);
- // Slow at both ends, fast through the middle, and symmetric about the
- // halfway point: the same curve the integer byte walk reproduces.
- try std.testing.expectEqual(@as(f32, 0.5), sample(.smoother, 0.5));
- try std.testing.expect(sample(.smoother, 0.15) < sample(.smooth, 0.15));
- try std.testing.expect(sample(.smoother, 0.85) > sample(.smooth, 0.85));
- try std.testing.expect(sample(.smoother, 0.6) - sample(.smoother, 0.4) >
- sample(.linear, 0.6) - sample(.linear, 0.4));
-}
-
-test "transition progress completes exactly" {
- inline for (std.enums.values(Transition)) |effect| {
- try std.testing.expectEqual(@as(f32, 1), progress(effect, effect.frames()));
- if (effect.frames() > 0)
- try std.testing.expectEqual(@as(f32, 1), progress(effect, effect.frames() - 1));
- try std.testing.expectEqual(@as(f32, 1), progress(effect, std.math.maxInt(u16)));
- }
- try std.testing.expectEqual(@as(f32, 1), progress(.off, 0));
-
- const shrinking = lerpBox(.{ .w = 100, .h = 40 }, .{}, sample(.out_back, 0.8));
- try std.testing.expectEqual(@as(f32, 0), shrinking.w);
- try std.testing.expectEqual(@as(f32, 0), shrinking.h);
- const opening = lerpBox(.{}, .{ .w = 100, .h = 40 }, sample(.out_back, 0.8));
- try std.testing.expect(opening.w > 100);
- try std.testing.expect(opening.h > 40);
-}
-
-test "character effects are core-composed and settle on the canonical glyph" {
- const box: Box = .{ .x = 4, .y = 2, .w = 20, .h = 6 };
- const area: CellArea = .of(box);
- try std.testing.expectEqual(@as(u16, 4), area.x0);
- try std.testing.expectEqual(@as(u16, 2), area.y0);
- try std.testing.expectEqual(@as(u16, 20), area.cols);
- try std.testing.expectEqual(@as(u16, 6), area.rows);
-
- inline for (std.enums.values(Transition)) |effect| {
- if (comptime !effect.composedByCore()) continue;
- // Core composition needs the frozen old grid for every glyph which has
- // not arrived, so no character effect may animate without it.
- try std.testing.expect(effect.needsPreviousGrid());
- if (comptime effect == .ascii) continue; // owns its own per-cell byte walk
-
- const last: Track = .{ .effect = effect, .frame = effect.frames() - 1, .to = box };
- const first: Track = .{ .effect = effect, .frame = 0, .to = box };
- var moving = false;
- var row: u16 = 0;
- while (row < area.rows) : (row += 1) {
- var col: u16 = 0;
- while (col < area.cols) : (col += 1) {
- // The last active sample is the exact canonical grid: no cell
- // is displaced, churning, or still frozen.
- try std.testing.expectEqual(CharSource.settled, charSource(last, col, row, area));
- if (!std.meta.eql(CharSource.settled, charSource(first, col, row, area)))
- moving = true;
- }
- }
- try std.testing.expect(moving);
- }
-}
-
-test "each character effect moves glyphs along its own axis" {
- const box: Box = .{ .w = 30, .h = 8 };
- const area: CellArea = .of(box);
-
- // Rows alternate which screen edge they come from, and every glyph in a row
- // travels as one rigid slide: one offset, no vertical component.
- var edges: Track = .{ .effect = .edges, .frame = 2, .to = box };
- const even = charSource(edges, 5, 0, area).at;
- const odd = charSource(edges, 5, 1, area).at;
- try std.testing.expect(even.cols > 0);
- try std.testing.expectEqual(-even.cols, odd.cols);
- try std.testing.expectEqual(@as(i32, 0), even.rows);
- try std.testing.expectEqual(even, charSource(edges, 17, 0, area).at);
- edges.frame = 5;
- try std.testing.expect(charSource(edges, 5, 0, area).at.cols < even.cols);
-
- // Falling columns are vertical only, staggered, and sample from below the
- // destination because the new text is still above the pane.
- const fall: Track = .{ .effect = .fall, .frame = 4, .to = box };
- var falling = false;
- var col: u16 = 0;
- while (col < area.cols) : (col += 1) switch (charSource(fall, col, 0, area)) {
- .old => {},
- .byte, .churn => return error.FallShouldNotChurn,
- .at => |offset| {
- try std.testing.expectEqual(@as(i32, 0), offset.cols);
- try std.testing.expect(offset.rows >= 0);
- if (offset.rows > 0) falling = true;
- },
- };
- try std.testing.expect(falling);
-
- // The wave displaces rows both ways as it travels, and only rows.
- const wave: Track = .{ .effect = .wave, .frame = 1, .to = box };
- var above = false;
- var below = false;
- col = 0;
- while (col < area.cols) : (col += 1) {
- const offset = charSource(wave, col, 3, area).at;
- try std.testing.expectEqual(@as(i32, 0), offset.cols);
- if (offset.rows < 0) above = true;
- if (offset.rows > 0) below = true;
- }
- try std.testing.expect(above and below);
-
- // The curtain has a head: columns behind it hold the old grid, columns the
- // head has passed are settled, and the head itself is still sliding.
- const curtain: Track = .{ .effect = .curtain, .frame = 5, .to = box };
- try std.testing.expectEqual(CharSource.settled, charSource(curtain, 0, 0, area));
- try std.testing.expectEqual(CharSource{ .old = {} }, charSource(curtain, 29, 0, area));
- var sliding = false;
- col = 0;
- while (col < area.cols) : (col += 1) switch (charSource(curtain, col, 0, area)) {
- .at => |offset| if (offset.cols < 0) {
- sliding = true;
- },
- .old, .byte, .churn => {},
- };
- try std.testing.expect(sliding);
-
- var scramble: Track = .{ .effect = .scramble, .frame = 3, .to = box };
- var churning: usize = 0;
- var locked: usize = 0;
- var changed = false;
- col = 0;
- while (col < area.cols) : (col += 1) switch (charSource(scramble, col, 0, area)) {
- .churn => |byte| {
- try std.testing.expect(byte >= ' ' and byte <= '~');
- churning += 1;
- scramble.frame = 4;
- switch (charSource(scramble, col, 0, area)) {
- .churn => |next| changed = changed or next != byte,
- .old, .at, .byte => {},
- }
- scramble.frame = 3;
- },
- .at => locked += 1,
- .old, .byte => return error.ScrambleShouldNotFreeze,
- };
- try std.testing.expect(churning > 0 and locked > 0 and changed);
-
- // The typewriter writes in reading order with a caret on its head.
- const typewriter: Track = .{ .effect = .typewriter, .frame = 7, .to = box };
- try std.testing.expectEqual(CharSource.settled, charSource(typewriter, 0, 0, area));
- try std.testing.expectEqual(
- CharSource{ .old = {} },
- charSource(typewriter, area.cols - 1, area.rows - 1, area),
- );
- var carets: usize = 0;
- var row: u16 = 0;
- while (row < area.rows) : (row += 1) {
- col = 0;
- while (col < area.cols) : (col += 1) switch (charSource(typewriter, col, row, area)) {
- .byte => |byte| {
- try std.testing.expectEqual(@as(u8, '_'), byte);
- carets += 1;
- },
- .old, .at, .churn => {},
- };
- }
- try std.testing.expectEqual(@as(usize, 1), carets);
-}
-
-test "opening presets separate geometry and content transitions" {
- const target: Box = .{ .x = 30, .y = 2, .w = 20, .h = 8 };
- try std.testing.expectEqual(target, openingBox(.ascii, target, 80));
- try std.testing.expectEqual(@as(f32, 0), openingBox(.zoom, target, 80).w);
- try std.testing.expectEqual(@as(f32, 80), openingBox(.slide, target, 80).x);
- try std.testing.expectEqual(@as(f32, target.y + target.h), openingBox(.vertical, target, 80).y);
- try std.testing.expectEqual(@as(f32, target.y + target.h), closingBox(.vertical, target).y);
-
- var track: Track = .{ .effect = .slide, .from = target, .to = target };
- try std.testing.expect(track.active());
- track.frame = track.effect.frames();
- try std.testing.expect(!track.active());
-
- track = .{ .effect = .dissolve, .frame = 3, .from = .{}, .to = target };
- try std.testing.expectEqual(target, track.visualBox());
-
- var closing: Track = .{
- .phase = .closing,
- .effect = .vertical,
- .from = target,
- .to = closingBox(.vertical, target),
- };
- try std.testing.expectEqual(target, closing.contentBox());
- closing.frame = 2;
- try std.testing.expect(closing.amount() < progress(.vertical, closing.frame));
-}
-
-test "dissolve has exact stable endpoints" {
- for (0..64) |col| {
- const x: u16 = @intCast(col);
- try std.testing.expect(!dissolveRevealed(42, x, 7, 0));
- try std.testing.expect(dissolveRevealed(42, x, 7, 1));
- if (dissolveRevealed(42, x, 7, 0.25))
- try std.testing.expect(dissolveRevealed(42, x, 7, 0.75));
- }
-}
-
-pub const Animation = struct {
- pub const frame_ms: u32 = 16;
- pub const frame_ns: u64 = frame_ms * std.time.ns_per_ms;
- pub const transition_steps: u16 = 10;
-
- pub fn Transition(comptime Value: type) type {
- return struct {
- const Self = @This();
-
- from: Value,
- to: Value,
- displayed: Value,
- step: u16 = transition_steps,
-
- pub fn init(value: Value) Self {
- return .{ .from = value, .to = value, .displayed = value };
- }
-
- pub fn isActive(a: *const Self) bool {
- return a.step < transition_steps;
- }
-
- pub fn retarget(a: *Self, target: Value) void {
- a.from = a.displayed;
- a.to = target;
- a.step = if (std.meta.eql(a.from, target)) transition_steps else 0;
- if (a.step == transition_steps) a.displayed = target;
- }
-
- pub fn advance(a: *Self) void {
- if (!a.isActive()) return;
- a.step += 1;
- a.displayed = if (a.step == transition_steps)
- a.to
- else
- Value.interpolate(a.from, a.to, a.step, transition_steps);
- }
-
- pub fn snap(a: *Self, value: Value) void {
- a.* = init(value);
- }
- };
- }
-
- pub fn Immediate(comptime Value: type) type {
- return struct {
- const Self = @This();
-
- displayed: Value,
-
- pub fn init(value: Value) Self {
- return .{ .displayed = value };
- }
-
- pub fn isActive(_: *const Self) bool {
- return false;
- }
-
- pub fn retarget(a: *Self, target: Value) void {
- a.displayed = target;
- }
-
- pub fn advance(_: *Self) void {}
-
- pub fn snap(a: *Self, value: Value) void {
- a.displayed = value;
- }
- };
- }
-
- pub fn interpolateRgb(from: [3]u8, to: [3]u8, step: u16, steps: u16) [3]u8 {
- if (step == 0) return from;
- if (step >= steps) return to;
- var out: [3]u8 = undefined;
- for (&out, from, to) |*dst, a, b| {
- const numerator = @as(u32, a) * (steps - step) + @as(u32, b) * step;
- dst.* = @intCast((numerator + steps / 2) / steps);
- }
- return out;
- }
-
- const TestColor = struct {
- rgb: [3]u8,
-
- pub fn interpolate(from: TestColor, to: TestColor, step: u16, steps: u16) TestColor {
- return .{ .rgb = interpolateRgb(from.rgb, to.rgb, step, steps) };
- }
- };
-
- test "Immediate lands where a completed Transition lands" {
- const from: TestColor = .{ .rgb = .{ 240, 3, 90 } };
- const to: TestColor = .{ .rgb = .{ 5, 222, 90 } };
-
- var faded = Animation.Transition(TestColor).init(from);
- faded.retarget(to);
- for (0..transition_steps) |_| faded.advance();
-
- var instant = Immediate(TestColor).init(from);
- try std.testing.expect(!instant.isActive());
- instant.retarget(to);
- try std.testing.expectEqual(faded.displayed, instant.displayed);
-
- // Never active, so a frontend that renders only while something is animating stops immediately
- // rather than spending ten frames discovering there is nothing to draw.
- try std.testing.expect(!instant.isActive());
- instant.advance();
- try std.testing.expectEqual(to, instant.displayed);
-
- instant.snap(from);
- try std.testing.expectEqual(from, instant.displayed);
- }
-
- test "fixed-step interpolation has exact monotonic endpoints" {
- const Tween = Animation.Transition(TestColor);
- const from: TestColor = .{ .rgb = .{ 240, 3, 90 } };
- const to: TestColor = .{ .rgb = .{ 5, 222, 90 } };
- var tween = Tween.init(from);
- tween.retarget(to);
- try std.testing.expectEqual(from, tween.displayed);
-
- var previous = tween.displayed;
- for (0..transition_steps) |_| {
- tween.advance();
- try std.testing.expect(tween.displayed.rgb[0] <= previous.rgb[0]);
- try std.testing.expect(tween.displayed.rgb[1] >= previous.rgb[1]);
- try std.testing.expectEqual(@as(u8, 90), tween.displayed.rgb[2]);
- previous = tween.displayed;
- }
- try std.testing.expect(!tween.isActive());
- try std.testing.expectEqual(to, tween.displayed);
- tween.advance();
- try std.testing.expectEqual(to, tween.displayed);
- }
-
- test "retarget starts at the currently displayed value" {
- const Tween = Animation.Transition(TestColor);
- const first: TestColor = .{ .rgb = .{ 0, 40, 200 } };
- const second: TestColor = .{ .rgb = .{ 200, 140, 0 } };
- const third: TestColor = .{ .rgb = .{ 20, 10, 250 } };
- var tween = Tween.init(first);
- tween.retarget(second);
- tween.advance();
- tween.advance();
- tween.advance();
- const on_screen = tween.displayed;
-
- tween.retarget(third);
- try std.testing.expectEqual(on_screen, tween.from);
- try std.testing.expectEqual(on_screen, tween.displayed);
- try std.testing.expect(tween.isActive());
- for (0..transition_steps) |_| tween.advance();
- try std.testing.expectEqual(third, tween.displayed);
- }
-};
-
-test "fractional panel inverse preserves compact header rows through slide and zoom" {
- const layer: pardes.BodyLayer = .{ .serial = 7, .viewport = .{ .x = 4, .y = 3, .w = 30, .h = 10 }, .cols = 30, .rows = 15, .context_rows = 4 };
- for ([_]Transition{ .slide, .zoom, .vertical }) |effect| {
- var presentation: Presentation = .{};
- const track: Track = .{ .serial = 7, .effect = effect, .frame = 4, .frame_count = 10, .from = .{ .x = 2, .y = 2, .w = 20, .h = 8 }, .to = .{ .x = 3, .y = 2, .w = 32, .h = 12 } };
- presentation.shown_tracks[0] = track;
- const shown = track.presented();
- for (0..4) |index| {
- const row: u16 = @intCast(index);
- const source_x: f32 = 5.3;
- const source_y = (layer.rowTop(row, 20, 10) + 5) / 20;
- const x = shown.x + (source_x - track.to.x) / track.to.w * shown.w;
- const y = shown.y + (source_y - track.to.y) / track.to.h * shown.h;
- const point = presentation.pointerFractional(80, 30, x, y).?;
- try std.testing.expectApproxEqAbs(source_x, point.x, 0.0001);
- try std.testing.expectApproxEqAbs(source_y, point.y, 0.0001);
- const body_hit = layer.hitAt(point.x * 10, point.y * 20, 10, 20, 8, 10).?;
- try std.testing.expectEqual(row, body_hit.row);
- }
- }
-}
-
-test "presentation snapshots own compact body and tag rows across later edits" {
- var state: Presentation = .{};
- defer state.deinit(std.testing.allocator);
- var cells: [1]pardes.Cell = .{.{}};
- var body: [2]pardes.Cell = .{ .{}, .{} };
- body[0].text[0] = 'A';
- var surface: pardes.Surface = .{ .cols = 1, .rows = 1, .cells = &cells };
- surface.body_layers[0] = .{ .serial = 9, .cols = 1, .rows = 2, .context_rows = 1, .cells = &body };
- var tag: [1]pardes.Cell = .{.{}};
- tag[0].text[0] = 'T';
- surface.tag_layers[0] = .{ .kind = .pane, .serial = 9, .cols = 1, .cells = &tag };
- state.capturePrevious(std.testing.allocator, &surface);
- try std.testing.expect(state.previous_valid);
- tag[0].text[0] = 'U';
- try std.testing.expectEqual(@as(u8, 'T'), state.previous_tag_layers[0].cells[0].text[0]);
- body[0].text[0] = 'B';
- surface.body_layers[0].context_rows = 0;
- try std.testing.expectEqual(@as(u8, 'A'), state.previous_body_layers[0].cells[0].text[0]);
- try std.testing.expectEqual(@as(u16, 1), state.previous_body_layers[0].context_rows);
- state.capturePrevious(std.testing.allocator, &surface);
- try std.testing.expectEqual(@as(u8, 'B'), state.previous_body_layers[0].cells[0].text[0]);
- try std.testing.expectEqual(@as(u8, 'U'), state.previous_tag_layers[0].cells[0].text[0]);
-}
-
test "column reorder moves only crossed state and preserves weights" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 173, .rows = 20 });
defer p.deinit();
@@ -2210,25 +1623,152 @@ test "column reorder moves only crossed state and preserves weights" {
p.col_weight[c] = c * 13 + 7;
p.col_serial[c] = @intCast(c + 10);
p.col_panes[c][0] = c;
- p.col_tags[c].saved_col = @intCast(c + 20);
+ p.col_tags[c].cur_col = @intCast(c + 20);
}
compute(p);
const original_weights = p.col_weight;
const outer_x = p.col_x[0];
const outer_w = p.col_w[0];
+ p.header_focus = true;
p.header_column = 1;
- p.topbar_col = 23;
p.header_scroll = 9;
reorderColumn(p, 1, 3);
try std.testing.expectEqual(original_weights[1], p.col_weight[3]);
try std.testing.expectEqual(original_weights[2], p.col_weight[1]);
try std.testing.expectEqual(@as(u32, 11), p.col_serial[3]);
try std.testing.expectEqual(@as(?usize, 3), p.header_column);
- try std.testing.expectEqual(@as(?u16, 23), p.topbar_col);
+ try std.testing.expectEqual(@as(i32, 21), tagline.focusedHeader(p).?.cur_col);
try std.testing.expectEqual(@as(usize, 9), p.header_scroll);
try std.testing.expectEqual(outer_x, p.col_x[0]);
try std.testing.expectEqual(outer_w, p.col_w[0]);
reorderColumn(p, 3, 1);
try std.testing.expectEqualSlices(u64, original_weights[0..4], p.col_weight[0..4]);
- try std.testing.expectEqual(@as(?u16, 21), p.col_tags[1].saved_col);
+ try std.testing.expectEqual(@as(i32, 21), p.col_tags[1].cur_col);
+}
+
+/// A test's attachment: an empty +Search in a new slot, in no column.
+fn testAttachment(p: *Pardes) !usize {
+ const free = p.freeSlot().?;
+ _ = try panes.Output.open(p, free, "/tmp", .search, "", try p.gpa.dupe(u8, "q\n"));
+ return free;
+}
+
+test "an attachment takes its rows from its owner alone, keeps them, and moves, folds and closes with it" {
+ const exec = pardes.exec;
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 48 });
+ defer p.deinit();
+ _ = try p.setTestFile("alpha one\nbeta\nalphabet\ngamma\nalpha two\ndelta\nepsilon\nzeta\n");
+ p.newScratchBelow(0);
+ p.newScratchBelow(0);
+ const other_col = p.col_panes[0][2];
+ try std.testing.expect(splitColumn(p, other_col, other_col, false));
+ const below = p.col_panes[0][1];
+ p.active = 0;
+ compute(p);
+ const before = p.rects;
+ const unrelated = [_]usize{ below, other_col };
+
+ // Under its owner, the rows asked for, the owner's and nobody else's.
+ const s = try testAttachment(p);
+ try std.testing.expect(attach(p, 0, s, 9));
+ try std.testing.expectEqual(@as(?usize, 0), ownerOf(p, s));
+ try std.testing.expectEqual(s, p.col_panes[0][1]);
+ for (unrelated) |k| try std.testing.expectEqualDeep(before[k], p.rects[k]);
+ try std.testing.expectEqual(before[0].h, p.rects[0].h + p.rects[s].h);
+ try std.testing.expectEqual(before[0].y, p.rects[0].y);
+ try std.testing.expectEqual(@as(u16, 9), p.rects[s].h);
+ // A second level (a preview under a query pane): out of the first's rows.
+ const pv = try testAttachment(p);
+ try std.testing.expect(attach(p, s, pv, 4));
+ try std.testing.expectEqual(pv, p.col_panes[0][2]);
+ try std.testing.expectEqual(@as(usize, 0), groupRoot(p, pv));
+ try std.testing.expectEqual(@as(usize, 3), groupEnd(p, 0, 0));
+ for (unrelated) |k| try std.testing.expectEqualDeep(before[k], p.rects[k]);
+ try std.testing.expectEqual(@as(u16, 5), p.rects[s].h);
+ // Its text changing never resizes it: nothing here fits it to content.
+ const sf = &p.panes[s].?.file.?;
+ var many: [2 + 4 * 30]u8 = @splat('\n');
+ for (0..30) |i| @memcpy(many[2 + 4 * i ..][0..3], "row");
+ many[0] = 'q';
+ panes.File.setContent(p, sf, try p.gpa.dupe(u8, &many));
+ compute(p);
+ try std.testing.expectEqual(@as(u16, 5), p.rects[s].h);
+
+ // An unrelated New, from the owner or from its attachment, never lands
+ // inside the group.
+ exec.runBuiltin(p, .New, 0, "", null);
+ exec.runBuiltin(p, .New, s, "", null);
+ var at = findPane(p, 0).?;
+ try std.testing.expectEqual(s, p.col_panes[at.col][at.idx + 1]);
+ try std.testing.expectEqual(pv, p.col_panes[at.col][at.idx + 2]);
+
+ // The seam between owner and attachment is the user's: the pair alone.
+ compute(p);
+ const pre_drag = p.rects;
+ applyRowSplit(p, at.col, at.idx, p.rects[s].y - 3);
+ compute(p);
+ try std.testing.expectEqual(pre_drag[0].h + pre_drag[s].h, p.rects[0].h + p.rects[s].h);
+ for (p.col_panes[at.col][0..p.col_n[at.col]]) |k| if (k != 0 and k != s) try std.testing.expectEqualDeep(pre_drag[k], p.rects[k]);
+
+ // Collapse folds the group and unfolds it.
+ compute(p);
+ const open = p.rects;
+ exec.runBuiltin(p, .Collapse, 0, "", null);
+ try std.testing.expect(p.panes[0].?.collapsed and p.panes[s].?.collapsed and p.panes[pv].?.collapsed);
+ exec.runBuiltin(p, .Collapse, 0, "", null);
+ try std.testing.expect(!p.panes[0].?.collapsed and !p.panes[s].?.collapsed);
+ try std.testing.expectEqual(open[0].h + open[s].h + open[pv].h, p.rects[0].h + p.rects[s].h + p.rects[pv].h);
+
+ // The owner's grip dragged to the other column takes its group along.
+ movePane(p, 0, p.col_x[1] + 4, p.rects[other_col].y + p.rects[other_col].h / 2);
+ at = findPane(p, 0).?;
+ try std.testing.expectEqual(@as(usize, 1), at.col);
+ try std.testing.expectEqual(s, p.col_panes[1][at.idx + 1]);
+ try std.testing.expectEqual(pv, p.col_panes[1][at.idx + 2]);
+ try std.testing.expectEqual(@as(?usize, 0), ownerOf(p, s));
+
+ // Del of the preview gives its rows to its owner, the query pane.
+ compute(p);
+ var pair = p.rects;
+ exec.runBuiltin(p, .Del, pv, "", null);
+ try std.testing.expect(p.panes[pv] == null);
+ compute(p);
+ try std.testing.expectEqual(pair[s].h + pair[pv].h, p.rects[s].h);
+ // Del of the attachment gives its rows back to the owner, nobody else.
+ pair = p.rects;
+ exec.runBuiltin(p, .Del, s, "", null);
+ try std.testing.expect(p.panes[s] == null);
+ compute(p);
+ try std.testing.expectEqual(pair[0].h + pair[s].h, p.rects[0].h);
+ for (p.col_panes[1][0..p.col_n[1]]) |k| if (k != 0) try std.testing.expectEqualDeep(pair[k], p.rects[k]);
+
+ // Del of the owner closes what is attached to it.
+ const again = try testAttachment(p);
+ try std.testing.expect(attach(p, 0, again, 6));
+ exec.runBuiltin(p, .Del, 0, "", null);
+ try std.testing.expect(p.panes[0] == null and p.panes[again] == null);
+}
+
+test "an attachment survives Dump and Restore, and one left away from its owner is a pane of its own" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 });
+ defer p.deinit();
+ _ = try p.setTestFile("alpha\nbeta\nalphabet\n");
+ const s = try testAttachment(p);
+ try std.testing.expect(attach(p, 0, s, 8));
+ try std.testing.expectEqual(@as(?usize, 0), ownerOf(p, s));
+ try dump.dumpState(p);
+ const restored = try dump.initFromDump(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }, p.dump_out.?);
+ defer restored.deinit();
+ try std.testing.expectEqual(@as(?usize, 0), ownerOf(restored, s));
+ // Unattach: a pane of its own where it stands.
+ pardes.exec.runBuiltin(p, .Unattach, s, "", null);
+ try std.testing.expect(p.panes[s].?.attached_to == null);
+ // Put above its owner by a route that does not carry groups: detached.
+ const t = try testAttachment(p);
+ try std.testing.expect(attach(p, 0, t, 8));
+ try std.testing.expectEqual(@as(?usize, 0), ownerOf(p, t));
+ removePane(p, t);
+ insert(p, 0, 0, t);
+ compute(p);
+ try std.testing.expect(p.panes[t].?.attached_to == null);
}