diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-09 00:52:52 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-09 01:28:28 -0300 |
| commit | 5157579ff7f9ae0cfc3c7629e953da05bce340fc (patch) | |
| tree | 02147789efdcea998981efcfe05d4bcb3e9bb7b0 /src/layout.zig | |
| parent | 4c4b9bf54d77dbc0f9f4e2619746908e501c6001 (diff) | |
| download | pardes-5157579ff7f9ae0cfc3c7629e953da05bce340fc.tar.gz pardes-5157579ff7f9ae0cfc3c7629e953da05bce340fc.zip | |
tests: at the end of their files: every top-level test block of the 46 files that had one mid-file (the locality analysis's 45 and test/hxdiff.zig; gui.zig, macos.zig, look, host_io, 9p_io, terminal, tui, mouse, fs, replica, layout among them, 11,429 test lines), with the functions only tests call, moved after the file's code in their order, a mechanical move (.scratch/suites/tests_to_end.py), zig fmt'd; the unit suites pass as before (tui 1486/1487, gui 1536/1537, core 956/956 on both), macos.zig only parse-checked here
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/layout.zig')
| -rw-r--r-- | src/layout.zig | 366 |
1 files changed, 183 insertions, 183 deletions
diff --git a/src/layout.zig b/src/layout.zig index 188ac2b8..f21eace1 100644 --- a/src/layout.zig +++ b/src/layout.zig @@ -1350,6 +1350,189 @@ fn keepTerminalFloors(p: *Pardes, c: usize, hs: []u16) void { } } +// ---- the session's columns and the rows its chrome takes ---- + +/// The workspace tag's height: one row while the setting keeps it, as +/// acme's row tag is, zero once a native shell takes its commands over. +/// Layout and hit testing read this rather than the constant, the way +/// they read `columnBarHeight`. +pub fn topBarHeight(p: *const Pardes) u16 { + if (!p.settings.workspace_tag) return 0; + return TOPBAR_H; +} + +/// The column tags' height: one row, as acme's (cols.c:244, colresize, +/// gives a column tag one font height); a newline in one is a space +/// (tagline.flattenHeaders). +pub fn columnBarHeight(p: *const Pardes) u16 { + if (p.view().screen_h < 3) return 0; + return COLBAR_H; +} + +/// Newcol: an empty column right of the calling pane's, the keyboard +/// on its tag, for the next pane made to fill. +pub fn newColumn(p: *Pardes, from_id: usize) void { + // An empty window (Delcol took the last column): its first column. + if (firstColumn(p)) |c| return tagline.enterHeader(p, c); + const col = keyboardColumn(p, from_id) orelse return; + // A trial: laying it out gives it a serial (layout.compute), which a + // refusal takes back. Every other column has its own already, so + // the counter as it is now is the trial's to return to. + for (0..p.view().ncol) |k| _ = columnSerial(p, k); + const unspent = p.next_column_serial; + const before_min = minima(p); + const before = p.view().rects; + // Said, not silently nothing: "no space" is 9ns's word for ENOSPC. + const c = insertColumnUnannounced(p, col, false) orelse { + return p.reportFailure(from_id, if (p.view().ncol >= MAX_COLS) + std.fmt.comptimePrint("Newcol: no space for a column: {d} max", .{MAX_COLS}) + else + "Newcol: no space for a column: this one is too narrow to split"); + }; + if (!keepColumn(p, c, &before, &before_min)) { + p.next_column_serial = unspent; + compute(p); + return p.reportFailure(from_id, "Newcol: no space for a column: the panes' tags would not fit"); + } + // Kept: now it has a serial, and the log hears of it. + noteColumn(p, "newcol", columnSerial(p, c)); + tagline.enterHeader(p, c); +} + +pub fn setActiveColumn(p: *Pardes, c: usize) void { + if (c < p.view().ncol) p.view().active_column = columnSerial(p, c); +} + +/// The active column, while it is still there. +pub fn activeColumn(p: *const Pardes) ?usize { + return columnBySerial(p, p.view().active_column); +} + +pub fn noteTyped(p: *Pardes) void { + if (p.view().header != null) { + if (p.headerColumn()) |c| p.setActiveColumn(c); + } else if (findPane(p, p.view().active)) |f| p.setActiveColumn(f.col); +} + +/// New, run from column `c`'s tag. +pub fn newScratchIn(p: *Pardes, from_id: usize, c: usize) void { + p.exec_column = c; + defer p.exec_column = null; + p.newScratchBelow(from_id); +} + +/// The column a command acts on: the one whose tag it came from, else +/// the one whose tag has the keyboard, else `id`'s. +pub fn keyboardColumn(p: *Pardes, id: usize) ?usize { + if (p.exec_column) |c| return c; + if (p.headerColumn()) |c| return c; + return if (findPane(p, id)) |f| f.col else null; +} + +/// Delcol: close column `col` and every pane in it. +pub fn removeColumn(p: *Pardes, col: usize) !void { + const v = p.view(); + if (col >= v.ncol) return; + const had_focus = keyboardColumn(p, v.active) == col; + const beside = if (had_focus) besideColumn(p, col, if (v.header != null) null else v.rects[v.active]) else null; + var ids: [MAX_PANES]usize = undefined; + var parents: [MAX_PANES]*Pane = undefined; + const count = v.cols[col].n; + for (0..count) |i| { + ids[i] = v.cols[col].panes[i]; + parents[i] = p.panes[ids[i]].?; + } + try panes.detachCwds(p, parents[0..count]); + for (ids[0..count], parents[0..count]) |closed, pane| { + // Kept for Panes; its column goes, so it comes back as a new pane. + if (p.store.keep(p, closed)) |k| p.store.close(p, k, null); + panes.retirePane(p, pane); + removePane(p, closed); + p.panes[closed] = null; + pardes.views.gone(p, closed); + } + dropColumn(p, col); + // The last column gone, the window stays, empty but for the + // workspace tag, as acme's does; Newcol starts again from there. + if (p.panes[v.active] == null) v.active = beside orelse panes.anyPane(p) orelse return tagline.enterHeader(p, null); +} + +/// The pane that takes over the keyboard from column `col`: in the +/// column layout.dropColumn gives the width to, the one level with the +/// middle of `r` (of the screen without one). Asked while rects hold. +pub fn besideColumn(p: *const Pardes, col: usize, r: ?Rect) ?usize { + const v = p.view(); + if (v.ncol < 2) return null; + const next = if (col > 0) col - 1 else col + 1; + const mid = if (r) |at| at.y + at.h / 2 else v.screen_h / 2; + var pick: ?usize = null; + for (v.cols[next].panes[0..p.view().cols[next].n]) |k| { + if (p.panes[k] == null) continue; + pick = k; + if (v.rects[k].y +| v.rects[k].h > mid) break; + } + return pick; +} + +/// The rows pane `id`'s tag takes at its width, wrapped as `sync` wraps +/// it: what a placement must leave it above its body rows. +pub fn wantedTagRows(p: *Pardes, id: usize) u16 { + const pane = p.panes[id] orelse return BOX_H; + if (pane.collapsed or !pane.tag_expanded) return BOX_H; + if (p.view().rects[id].w == 0) return pane.tag_rows; // never laid out + var wrap_room: [32 * 1024]u8 = undefined; + var wrap_fba = std.heap.FixedBufferAllocator.init(&wrap_room); + const wa = wrap_fba.allocator(); + const prefix = tagline.prefixIn(p, wa, pane) catch return pane.tag_rows; + const whole = std.mem.concat(wa, u8, &.{ prefix, tagline.curTail(pane) }) catch return pane.tag_rows; + const shown = tagline.wrapTag(wa, whole, tagline.tagWidth(p, id)) catch return pane.tag_rows; + return @intCast(@max(BOX_H, @min(shown.rows(), MAX_TAG_ROWS))); +} + +/// The pane's first body row, under its tag. Notice bands do NOT move it +/// -- they are an overlay, drawn over the top rows rather than taking them. +pub fn bodyTop(_: *const Pardes, pane: *const Pane, r: Rect) u16 { + return r.y + pane.tag_rows; +} + +/// The tag's first row: the pane's top. +pub fn tagTop(_: *const Pardes, _: *const Pane, r: Rect) u16 { + return r.y; +} + +pub fn tagCapacity(p: *const Pardes, physical_width: u16) u16 { + const layer: Layer = .{ .kind = .pane, .viewport = .{ .x = 0, .y = 0, .w = physical_width, .h = 1 } }; + return layer.capacity(p.view().row_metrics); +} + +// ---- render: build the canonical surface (draw.zig) ---- + +/// A frontend's tag hit, if what it names is still there: resolved on +/// the place it carries (Layer.Place), not on this process's frame. +pub fn reprojectTagHit(p: *const Pardes, tag_hit: TagHit) ?TagHit { + if (tag_hit.kind == .pane) { + if (tag_hit.id >= p.panes.len) return null; + const pane = p.panes[tag_hit.id] orelse return null; + if (pane.serial != tag_hit.serial) return null; + } + if (tag_hit.kind == .column and (tag_hit.id >= p.view().ncol or p.view().cols[tag_hit.id].serial != tag_hit.serial)) return null; + return Layer.tagAgain(tag_hit); +} + +/// 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; +} + +/// Each column's weight, as a test compares them. +fn weightsOf(p: *const Pardes) [MAX_COLS]u64 { + var out: [MAX_COLS]u64 = undefined; + for (&out, p.view().cols) |*w, c| w.* = c.weight; + return out; +} + 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 }); defer p.deinit(); @@ -1578,13 +1761,6 @@ test "column reorder moves only crossed state and preserves weights" { try std.testing.expectEqual(@as(i32, 21), p.view().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 }); @@ -1705,175 +1881,6 @@ test "an attachment survives Dump and Restore, and one left away from its owner try std.testing.expect(p.panes[t].?.attached_to == null); } -// ---- the session's columns and the rows its chrome takes ---- - -/// The workspace tag's height: one row while the setting keeps it, as -/// acme's row tag is, zero once a native shell takes its commands over. -/// Layout and hit testing read this rather than the constant, the way -/// they read `columnBarHeight`. -pub fn topBarHeight(p: *const Pardes) u16 { - if (!p.settings.workspace_tag) return 0; - return TOPBAR_H; -} - -/// The column tags' height: one row, as acme's (cols.c:244, colresize, -/// gives a column tag one font height); a newline in one is a space -/// (tagline.flattenHeaders). -pub fn columnBarHeight(p: *const Pardes) u16 { - if (p.view().screen_h < 3) return 0; - return COLBAR_H; -} - -/// Newcol: an empty column right of the calling pane's, the keyboard -/// on its tag, for the next pane made to fill. -pub fn newColumn(p: *Pardes, from_id: usize) void { - // An empty window (Delcol took the last column): its first column. - if (firstColumn(p)) |c| return tagline.enterHeader(p, c); - const col = keyboardColumn(p, from_id) orelse return; - // A trial: laying it out gives it a serial (layout.compute), which a - // refusal takes back. Every other column has its own already, so - // the counter as it is now is the trial's to return to. - for (0..p.view().ncol) |k| _ = columnSerial(p, k); - const unspent = p.next_column_serial; - const before_min = minima(p); - const before = p.view().rects; - // Said, not silently nothing: "no space" is 9ns's word for ENOSPC. - const c = insertColumnUnannounced(p, col, false) orelse { - return p.reportFailure(from_id, if (p.view().ncol >= MAX_COLS) - std.fmt.comptimePrint("Newcol: no space for a column: {d} max", .{MAX_COLS}) - else - "Newcol: no space for a column: this one is too narrow to split"); - }; - if (!keepColumn(p, c, &before, &before_min)) { - p.next_column_serial = unspent; - compute(p); - return p.reportFailure(from_id, "Newcol: no space for a column: the panes' tags would not fit"); - } - // Kept: now it has a serial, and the log hears of it. - noteColumn(p, "newcol", columnSerial(p, c)); - tagline.enterHeader(p, c); -} - -pub fn setActiveColumn(p: *Pardes, c: usize) void { - if (c < p.view().ncol) p.view().active_column = columnSerial(p, c); -} - -/// The active column, while it is still there. -pub fn activeColumn(p: *const Pardes) ?usize { - return columnBySerial(p, p.view().active_column); -} - -pub fn noteTyped(p: *Pardes) void { - if (p.view().header != null) { - if (p.headerColumn()) |c| p.setActiveColumn(c); - } else if (findPane(p, p.view().active)) |f| p.setActiveColumn(f.col); -} - -/// New, run from column `c`'s tag. -pub fn newScratchIn(p: *Pardes, from_id: usize, c: usize) void { - p.exec_column = c; - defer p.exec_column = null; - p.newScratchBelow(from_id); -} - -/// The column a command acts on: the one whose tag it came from, else -/// the one whose tag has the keyboard, else `id`'s. -pub fn keyboardColumn(p: *Pardes, id: usize) ?usize { - if (p.exec_column) |c| return c; - if (p.headerColumn()) |c| return c; - return if (findPane(p, id)) |f| f.col else null; -} - -/// Delcol: close column `col` and every pane in it. -pub fn removeColumn(p: *Pardes, col: usize) !void { - const v = p.view(); - if (col >= v.ncol) return; - const had_focus = keyboardColumn(p, v.active) == col; - const beside = if (had_focus) besideColumn(p, col, if (v.header != null) null else v.rects[v.active]) else null; - var ids: [MAX_PANES]usize = undefined; - var parents: [MAX_PANES]*Pane = undefined; - const count = v.cols[col].n; - for (0..count) |i| { - ids[i] = v.cols[col].panes[i]; - parents[i] = p.panes[ids[i]].?; - } - try panes.detachCwds(p, parents[0..count]); - for (ids[0..count], parents[0..count]) |closed, pane| { - // Kept for Panes; its column goes, so it comes back as a new pane. - if (p.store.keep(p, closed)) |k| p.store.close(p, k, null); - panes.retirePane(p, pane); - removePane(p, closed); - p.panes[closed] = null; - pardes.views.gone(p, closed); - } - dropColumn(p, col); - // The last column gone, the window stays, empty but for the - // workspace tag, as acme's does; Newcol starts again from there. - if (p.panes[v.active] == null) v.active = beside orelse panes.anyPane(p) orelse return tagline.enterHeader(p, null); -} - -/// The pane that takes over the keyboard from column `col`: in the -/// column layout.dropColumn gives the width to, the one level with the -/// middle of `r` (of the screen without one). Asked while rects hold. -pub fn besideColumn(p: *const Pardes, col: usize, r: ?Rect) ?usize { - const v = p.view(); - if (v.ncol < 2) return null; - const next = if (col > 0) col - 1 else col + 1; - const mid = if (r) |at| at.y + at.h / 2 else v.screen_h / 2; - var pick: ?usize = null; - for (v.cols[next].panes[0..p.view().cols[next].n]) |k| { - if (p.panes[k] == null) continue; - pick = k; - if (v.rects[k].y +| v.rects[k].h > mid) break; - } - return pick; -} - -/// The rows pane `id`'s tag takes at its width, wrapped as `sync` wraps -/// it: what a placement must leave it above its body rows. -pub fn wantedTagRows(p: *Pardes, id: usize) u16 { - const pane = p.panes[id] orelse return BOX_H; - if (pane.collapsed or !pane.tag_expanded) return BOX_H; - if (p.view().rects[id].w == 0) return pane.tag_rows; // never laid out - var wrap_room: [32 * 1024]u8 = undefined; - var wrap_fba = std.heap.FixedBufferAllocator.init(&wrap_room); - const wa = wrap_fba.allocator(); - const prefix = tagline.prefixIn(p, wa, pane) catch return pane.tag_rows; - const whole = std.mem.concat(wa, u8, &.{ prefix, tagline.curTail(pane) }) catch return pane.tag_rows; - const shown = tagline.wrapTag(wa, whole, tagline.tagWidth(p, id)) catch return pane.tag_rows; - return @intCast(@max(BOX_H, @min(shown.rows(), MAX_TAG_ROWS))); -} - -/// The pane's first body row, under its tag. Notice bands do NOT move it -/// -- they are an overlay, drawn over the top rows rather than taking them. -pub fn bodyTop(_: *const Pardes, pane: *const Pane, r: Rect) u16 { - return r.y + pane.tag_rows; -} - -/// The tag's first row: the pane's top. -pub fn tagTop(_: *const Pardes, _: *const Pane, r: Rect) u16 { - return r.y; -} - -pub fn tagCapacity(p: *const Pardes, physical_width: u16) u16 { - const layer: Layer = .{ .kind = .pane, .viewport = .{ .x = 0, .y = 0, .w = physical_width, .h = 1 } }; - return layer.capacity(p.view().row_metrics); -} - -// ---- render: build the canonical surface (draw.zig) ---- - -/// A frontend's tag hit, if what it names is still there: resolved on -/// the place it carries (Layer.Place), not on this process's frame. -pub fn reprojectTagHit(p: *const Pardes, tag_hit: TagHit) ?TagHit { - if (tag_hit.kind == .pane) { - if (tag_hit.id >= p.panes.len) return null; - const pane = p.panes[tag_hit.id] orelse return null; - if (pane.serial != tag_hit.serial) return null; - } - if (tag_hit.kind == .column and (tag_hit.id >= p.view().ncol or p.view().cols[tag_hit.id].serial != tag_hit.serial)) return null; - return Layer.tagAgain(tag_hit); -} - test "a new column takes width only from the column that created it" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 124, .rows = 24 }); defer p.deinit(); @@ -2033,10 +2040,3 @@ test "tiny screens keep pane space before adding a column tag" { try std.testing.expectEqual(TOPBAR_H, p.view().rects[0].y); _ = try p.render(p.scratch.allocator()); } - -/// Each column's weight, as a test compares them. -fn weightsOf(p: *const Pardes) [MAX_COLS]u64 { - var out: [MAX_COLS]u64 = undefined; - for (&out, p.view().cols) |*w, c| w.* = c.weight; - return out; -} |
