summaryrefslogtreecommitdiff
path: root/src/layout.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-24 10:05:01 -0300
committerGabriel Schneider <[email protected]>2026-10-24 10:14:38 -0300
commit873f81ccb1449119846de8d4e6e0e8899236100a (patch)
tree66ad8ca2e913e4a954be947c54aa0b31a0c9fdb0 /src/layout.zig
parent19c83c90ebed4817acd7a12d4d225f5af0957bc1 (diff)
downloadpardes-873f81ccb1449119846de8d4e6e0e8899236100a.tar.gz
pardes-873f81ccb1449119846de8d4e6e0e8899236100a.zip
Search converge 1/6: attached panes, a pane under its owner on the owner's rows alone, fixed height, moving, folding and closing with it
Diffstat (limited to 'src/layout.zig')
-rw-r--r--src/layout.zig369
1 files changed, 342 insertions, 27 deletions
diff --git a/src/layout.zig b/src/layout.zig
index 6345c91a..f0689b10 100644
--- a/src/layout.zig
+++ b/src/layout.zig
@@ -237,17 +237,25 @@ fn splitRowFor(y: u16, h: u16, cur_y: u16, least: ?u16) ?u16 {
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];
+ 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 >= sr.y and cur_y < sr.y + sr.h and (src.idx == 0 or cur_y <= sr.y)) return null;
+ 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| {
@@ -256,24 +264,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];
+ 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,
@@ -285,10 +305,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| {
@@ -296,24 +324,47 @@ pub fn movePane(p: *Pardes, id: usize, cur_x: u16, cur_y: u16) void {
heights[pid] = p.rects[pid].h;
}
}
- removePane(p, id);
+ 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 above = placement.above_id orelse {
- insert(p, placement.preview_col, 0, id);
+ 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[above] = 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 != 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 {
@@ -495,7 +546,8 @@ pub fn acmePlace(p: *Pardes, c: usize, origin: ?usize, id: usize) void {
var empty: ?usize = null;
for (p.col_panes[c][0..n]) |k| {
const pane = p.panes[k] orelse continue;
- if (pane.collapsed) 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;
@@ -508,7 +560,7 @@ pub fn acmePlace(p: *Pardes, c: usize, origin: ?usize, id: usize) void {
const y: u16 = y: {
const o = origin orelse {
var last = v0;
- for (p.col_panes[c][0..n]) |k| if (!p.panes[k].?.collapsed) {
+ 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;
@@ -517,8 +569,10 @@ pub fn acmePlace(p: *Pardes, c: usize, origin: ?usize, id: usize) void {
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;
- const mine = if (findPane(p, o)) |f| f.col == c and !p.panes[o].?.collapsed else false;
- const v = if (mine and @as(u32, p.rects[o].h) * 3 > @as(u32, p.rects[v0].h) * 2) o else v0;
+ // 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);
@@ -663,7 +717,7 @@ 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) continue;
+ 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;
@@ -681,14 +735,17 @@ pub fn splitTallest(p: *Pardes, c: usize, id: usize) bool {
/// 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 = p.col_panes[c][k];
+ const v = groupRoot(p, p.col_panes[c][k]);
const r = p.rects[v];
snapColWeights(p, c);
- insert(p, c, k + 1, id);
+ 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);
@@ -833,7 +890,8 @@ pub fn absorbVWeight(p: *Pardes, id: usize, to: ?usize) ?usize {
var k = f.idx;
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;
}
const s = p.panes[sib] orelse return null;
s.vweight += @as(f32, @floatFromInt(@max(1, p.rects[id].h)));
@@ -887,6 +945,130 @@ 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 {
+ const o = p.panes[owner] orelse return false;
+ const nw = p.panes[id] orelse return false;
+ if (o.collapsed) toggleCollapse(p, owner);
+ compute(p);
+ const f = findPane(p, owner) orelse 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[owner].h;
+ const least = minRows(p, id);
+ if (oh < least + BOX_H) return false;
+ const owner_keeps = if (oh >= least + minRows(p, owner)) minRows(p, owner) else BOX_H;
+ const give = std.math.clamp(rows, least, oh - owner_keeps);
+ snapColWeights(p, f.col);
+ insert(p, f.col, groupEnd(p, f.col, f.idx), id);
+ o.vweight = @floatFromInt(oh - give);
+ nw.vweight = @floatFromInt(give);
+ nw.attached_to = o.serial;
+ 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);
@@ -912,6 +1094,11 @@ 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;
@@ -988,6 +1175,7 @@ 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;
+ keepGroups(p);
for (0..p.ncol) |column| _ = columnSerial(p, column);
var wsum: u128 = 0;
for (0..p.ncol) |c| wsum += p.col_weight[c];
@@ -1318,3 +1506,130 @@ test "column reorder moves only crossed state and preserves weights" {
try std.testing.expectEqualSlices(u64, original_weights[0..4], p.col_weight[0..4]);
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);
+}