diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-05 19:53:53 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 22:03:21 -0300 |
| commit | 12cb0a704075dea2512f77582635376fe967dee6 (patch) | |
| tree | 0f498bf581e3f8ece31f8a5eaa06accba7aa7415 /src/layout.zig | |
| parent | 1a1ce29051ea6aa3be4754074f6a00b905d6b208 (diff) | |
| download | pardes-12cb0a704075dea2512f77582635376fe967dee6.tar.gz pardes-12cb0a704075dea2512f77582635376fe967dee6.zip | |
split pardes.zig by noun, layout: the session's columns and the rows its chrome takes move into src/layout.zig: newColumn, setActiveColumn, activeColumn, noteTyped, newScratchIn, keyboardColumn, removeColumn, besideColumn, topBarHeight, columnBarHeight, wantedTagRows, bodyTop, tagTop, tagCapacity and reprojectTagHit; the session keeps four as declaration aliases (topBarHeight and bodyTop, called from everywhere; tagTop and setActiveColumn, which Store.zig and Handover.zig call, so the persistence agent's files are untouched), the rest are layout.x(p, ...) in their callers (draw.zig, mouse.zig, ninep/cols.zig, tagline.zig and seven more; gui.zig, whose locals are named layout, through pardes.layout); layout's own placement helper bodyTop, a pane's top past its tag as heights go, is bodyFrom now, the name going to the session's; tests moved with them, by name: a new column takes width only from the column that created it; Newcol refuses an unsplittable restored weight before spawning; repeated non-dyadic column splits preserve every unrelated boundary; dragging a column's only pane to another leaves that column in place, empty; tiny screens keep pane space before adding a column tag; a pure move, no field touched, a dump and the wire as they were; gates: tui core-test 960/960, behaviour-test 238/238, pane-test 171/172 (1 skipped), snap all 108 scripts ok, gui unit-test 1518/1519 (1 skipped)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/layout.zig')
| -rw-r--r-- | src/layout.zig | 360 |
1 files changed, 351 insertions, 9 deletions
diff --git a/src/layout.zig b/src/layout.zig index 4a4582b3..bcee3fba 100644 --- a/src/layout.zig +++ b/src/layout.zig @@ -5,6 +5,11 @@ const dump = @import("dump.zig"); const config = @import("config.zig"); const panes = @import("panes.zig"); const Pardes = pardes.Pardes; +const Layer = @import("Layer.zig"); +const TagHit = Layer.TagHit; +const TOPBAR_H = pardes.TOPBAR_H; +const COLBAR_H = pardes.COLBAR_H; +const MAX_TAG_ROWS = pardes.MAX_TAG_ROWS; const Pane = pardes.Pane; pub const Rect = struct { x: u16 = 0, y: u16 = 0, w: u16 = 0, h: u16 = 0 }; const MAX_COLS = pardes.MAX_COLS; @@ -277,7 +282,7 @@ pub fn movePlacement(p: *Pardes, id: usize, cur_x: u16, cur_y: u16) ?MovePlaceme 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(); + var y: u16 = p.topBarHeight() + columnBarHeight(p); 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; var k: usize = 0; @@ -572,12 +577,12 @@ pub fn acmePlace(p: *Pardes, c: usize, origin: ?usize, id: usize) void { 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; + break :y bodyFrom(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; + break :y bodyFrom(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; @@ -594,7 +599,7 @@ 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; + return wantedTagRows(p, id) + min_body_rows; } /// Whether every column has the rows for each of its unfolded panes' @@ -603,7 +608,7 @@ pub fn minRows(p: *Pardes, id: usize) u16 { /// 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()); + const avail = p.screen_h -| (p.topBarHeight() + columnBarHeight(p)); for (0..p.ncol) |c| { if (p.col_n[c] < 2) continue; var need: u32 = 0; @@ -635,7 +640,7 @@ 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()); + const avail = p.screen_h -| (p.topBarHeight() + columnBarHeight(p)); var mins: [MAX_PANES]u16 = undefined; var need: u32 = 0; for (p.col_panes[c][0..n], 0..) |k, i| { @@ -843,7 +848,7 @@ fn splitAt(p: *Pardes, c: usize, id: usize, y: u16) void { /// leaves the pane its tag and that much of its body, whichever end the tag /// is on (Tagbottom's is at its foot, the height the same; behaviour test /// "placement: under Tagbottom..."). -fn bodyTop(p: *const Pardes, id: usize) u16 { +fn bodyFrom(p: *const Pardes, id: usize) u16 { return p.rects[id].y + p.panes[id].?.tag_rows; } @@ -1221,7 +1226,7 @@ pub fn expandedNeighbor(p: *const Pardes, col: usize, index: usize, side: enum { } fn computeCollapsedColumn(p: *Pardes, c: usize, x: u16, width: u16, folded: usize) void { - var y: u16 = p.topBarHeight() + p.columnBarHeight(); + var y: u16 = p.topBarHeight() + columnBarHeight(p); const available = p.screen_h -| y; var expanded_left: usize = 0; var weight: f32 = 0; @@ -1303,7 +1308,7 @@ pub fn compute(p: *Pardes) void { } if (vsum <= 0) vsum = 1; - var y: u16 = p.topBarHeight() + p.columnBarHeight(); + var y: u16 = p.topBarHeight() + columnBarHeight(p); const top = y; const avail_h = p.screen_h -| y; var hs: [MAX_PANES]u16 = undefined; @@ -1726,3 +1731,340 @@ test "an attachment survives Dump and Restore, and one left away from its owner compute(p); 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.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 keyboard_col = keyboardColumn(p, from_id) orelse return; + // Asked from the finder: the new column goes left of it, so the + // finder stays at the edge as wide. + const in_finder = p.finder.column(p) == keyboard_col; + const before_finder = in_finder and keyboard_col == 0; + const col = if (in_finder and keyboard_col > 0) keyboard_col - 1 else keyboard_col; + // 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.ncol) |k| _ = columnSerial(p, k); + const unspent = p.next_column_serial; + const before_min = minima(p); + const before = p.rects; + // Said, not silently nothing: "no space" is 9ns's word for ENOSPC. + const c = insertColumnUnannounced(p, col, before_finder) orelse { + var said: [96]u8 = undefined; + return p.reportFailure(from_id, if (p.ncol >= MAX_COLS) + std.fmt.bufPrint(&said, "Newcol: no space for a column: {d} max", .{MAX_COLS}) catch "Newcol: no space for a column" + 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.ncol) p.active_column = columnSerial(p, c); +} + +/// The active column, while it is still there. +pub fn activeColumn(p: *const Pardes) ?usize { + return columnBySerial(p, p.active_column); +} + +pub fn noteTyped(p: *Pardes) void { + if (p.header_focus) { + if (p.header_column) |c| p.setActiveColumn(c); + } else if (findPane(p, p.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.header_focus) if (p.header_column) |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 { + if (col >= p.ncol) return; + const had_focus = keyboardColumn(p, p.active) == col; + const beside = if (had_focus) besideColumn(p, col, if (p.header_focus) null else p.rects[p.active]) else null; + var ids: [MAX_PANES]usize = undefined; + var parents: [MAX_PANES]*Pane = undefined; + const count = p.col_n[col]; + for (0..count) |i| { + ids[i] = p.col_panes[col][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; + } + 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[p.active] == null) p.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 { + if (p.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 p.screen_h / 2; + var pick: ?usize = null; + for (p.col_panes[next][0..p.col_n[next]]) |k| { + if (p.panes[k] == null) continue; + pick = k; + if (p.rects[k].y +| p.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.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 the tag when it is on top, over it +/// when it is at the bottom. Notice bands do NOT move it -- they are an +/// overlay, drawn over the top rows rather than taking them. +pub fn bodyTop(p: *const Pardes, pane: *const Pane, r: Rect) u16 { + return if (p.settings.tag_bottom) r.y else r.y + pane.tag_rows; +} + +/// The tag's first row: the pane's top, or its last rows with Tagbottom. +pub fn tagTop(p: *const Pardes, pane: *const Pane, r: Rect) u16 { + return if (p.settings.tag_bottom) r.y + r.h -| pane.tag_rows else 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.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.ncol or p.col_serial[tag_hit.id] != 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(); + + _ = try p.newShell(1, ""); + try std.testing.expect(splitColumn(p, 0, 1, false)); + _ = try p.newShell(2, ""); + try std.testing.expect(splitColumn(p, 0, 2, true)); + p.sync(); + + const left_before = p.rects[2]; + const right_before = p.rects[1]; + _ = try p.newShell(3, ""); + try std.testing.expect(splitColumn(p, 0, 3, false)); + p.sync(); + + try std.testing.expectEqual(left_before.x, p.rects[2].x); + try std.testing.expectEqual(left_before.w, p.rects[2].w); + try std.testing.expectEqual(right_before.x, p.rects[1].x); + try std.testing.expectEqual(right_before.w, p.rects[1].w); + try std.testing.expectEqual(@as(u16, 16), p.rects[0].w); + try std.testing.expectEqual(@as(u16, 15), p.rects[3].w); + + const narrow = try Pardes.init(std.testing.allocator, .{ .cols = config.MINW * 2 - 1, .rows = 10 }); + defer narrow.deinit(); + try std.testing.expect(!splitColumn(narrow, 0, 1, false)); + try std.testing.expectEqual(@as(usize, 1), narrow.ncol); + + const sequential = try Pardes.init(std.testing.allocator, .{ .cols = config.MINW * 2, .rows = 10 }); + defer sequential.deinit(); + _ = try sequential.newShell(1, ""); + try std.testing.expect(splitColumn(sequential, 0, 1, false)); + _ = try sequential.newShell(2, ""); + try std.testing.expect(!splitColumn(sequential, 0, 2, false)); + try std.testing.expectEqual(@as(usize, 2), sequential.ncol); + + const coarse = try Pardes.init(std.testing.allocator, .{ .cols = 22, .rows = 10 }); + defer coarse.deinit(); + coarse.col_weight[0] = 3; + _ = try coarse.newShell(1, ""); + try std.testing.expect(splitColumn(coarse, 0, 1, false)); + coarse.sync(); + try std.testing.expectEqual(@as(u16, 11), coarse.col_w[0]); + try std.testing.expectEqual(@as(u16, 11), coarse.col_w[1]); + + const extreme = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 20 }); + defer extreme.deinit(); + _ = try extreme.newShell(1, ""); + try std.testing.expect(splitColumn(extreme, 0, 1, false)); + _ = try extreme.newShell(2, ""); + insert(extreme, 0, 1, 2); + extreme.col_weight[0] = std.math.maxInt(u64) / 2 + 2; // odd and cannot double + extreme.col_weight[1] = 1; + compute(extreme); + const panes_before = extreme.col_panes; + const counts_before = extreme.col_n; + const weights_before = extreme.col_weight; + const source_vweight = extreme.panes[0].?.vweight; + const sibling_vweight = extreme.panes[2].?.vweight; + try std.testing.expect(!splitColumn(extreme, 0, 0, false)); + try std.testing.expectEqual(panes_before, extreme.col_panes); + try std.testing.expectEqual(counts_before, extreme.col_n); + try std.testing.expectEqual(weights_before, extreme.col_weight); + try std.testing.expectEqual(source_vweight, extreme.panes[0].?.vweight); + try std.testing.expectEqual(sibling_vweight, extreme.panes[2].?.vweight); +} + +test "Newcol refuses an unsplittable restored weight before spawning" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 200, .rows = 20 }); + defer p.deinit(); + + for (1..4) |id| { + _ = try p.newShell(id, ""); + try std.testing.expect(splitColumn(p, id - 1, id, false)); + } + const restored_cap = std.math.maxInt(u64) / 6; + for (0..4) |column| p.col_weight[column] = restored_cap; + p.col_weight[3] -= 1; + for ([_]usize{ 3, 2, 1 }) |id| { + const pane = p.panes[id].?; + try panes.deinitPane(p, pane); + removePane(p, id); + dropColumn(p, id); + p.panes[id] = null; + } + while (p.nextEffect()) |_| {} + + try std.testing.expect(p.col_weight[0] > std.math.maxInt(u64) / 2); + try std.testing.expect(p.col_weight[0] % 2 == 1); + try std.testing.expect(!canSplitCol(p, findPane(p, 0).?.col)); + const panes_before = p.panes; + const serial_before = p.next_serial; + try std.testing.expect(p.executeBuiltinLine(0, "Newcol")); + try std.testing.expectEqual(panes_before, p.panes); + try std.testing.expectEqual(serial_before, p.next_serial); + try std.testing.expect(p.nextEffect() == null); +} + +test "repeated non-dyadic column splits preserve every unrelated boundary" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 979, .rows = 20 }); + defer p.deinit(); + + _ = try p.newShell(1, ""); + try std.testing.expect(splitColumn(p, 0, 1, false)); + p.sync(); + + const handle = p.col_x[0] + p.col_w[0] - 1; + const untouched = p.col_weight; + p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = handle, .row = TOPBAR_H + 1 } }); + p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = handle, .row = TOPBAR_H + 1 } }); + try std.testing.expectEqual(untouched, p.col_weight); + + p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = handle, .row = TOPBAR_H + 1 } }); + p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = 417, .row = TOPBAR_H + 1 } }); + p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = 417, .row = TOPBAR_H + 1 } }); + p.sync(); + try std.testing.expectEqual(@as(u16, 418), p.rects[0].w); + + _ = try p.newShell(2, ""); + try std.testing.expect(splitColumn(p, 1, 2, false)); + p.sync(); + const left_before = p.rects[0]; + const middle_before = p.rects[1]; + + _ = try p.newShell(3, ""); + try std.testing.expect(splitColumn(p, 2, 3, false)); + p.sync(); + try std.testing.expectEqual(left_before, p.rects[0]); + try std.testing.expectEqual(middle_before, p.rects[1]); +} + +test "dragging a column's only pane to another leaves that column in place, empty" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 32 }); + defer p.deinit(); + newColumn(p, 0); + newScratchIn(p, 0, activeColumn(p).?); + const moved = p.active; + p.sync(); + const weights = p.col_weight; + const tag_serial = p.col_serial[1]; + movePane(p, moved, p.col_x[0] + 4, p.rects[0].y + p.rects[0].h / 2); + p.sync(); + // acme's coldragwin: colclose then coladd (cols.c:516-517); the column + // left behind keeps its place, width and tag. + try std.testing.expectEqual(@as(usize, 2), p.ncol); + try std.testing.expectEqual(@as(usize, 0), p.col_n[1]); + try std.testing.expectEqual(tag_serial, p.col_serial[1]); + try std.testing.expectEqualDeep(weights, p.col_weight); + try std.testing.expectEqual(@as(usize, 2), p.col_n[0]); + // ...and a pane dragged into the empty column takes it whole. + movePane(p, moved, p.col_x[1] + 4, p.screen_h / 2); + p.sync(); + try std.testing.expectEqualSlices(usize, &.{moved}, p.col_panes[1][0..p.col_n[1]]); + try std.testing.expectEqual(@as(usize, 1), p.col_n[0]); +} + +test "tiny screens keep pane space before adding a column tag" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 8, .rows = 2, .tty_only = true }); + defer p.deinit(); + try std.testing.expectEqual(@as(u16, 0), columnBarHeight(p)); + try std.testing.expectEqual(TOPBAR_H, p.rects[0].y); + _ = try p.render(p.scratch.allocator()); +} |
