diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/builtins.zig | 15 | ||||
| -rw-r--r-- | src/config.zig | 1 | ||||
| -rw-r--r-- | src/dump.zig | 17 | ||||
| -rw-r--r-- | src/exec.zig | 6 | ||||
| -rw-r--r-- | src/layout.zig | 369 | ||||
| -rw-r--r-- | src/panes.zig | 7 | ||||
| -rw-r--r-- | src/pardes.zig | 13 |
7 files changed, 395 insertions, 33 deletions
diff --git a/src/builtins.zig b/src/builtins.zig index 64710554..1ec33d88 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1283,9 +1283,12 @@ pub const Del = struct { // acme's Del asks winclean first (exec.c del): unsaved text is warned // about once, and the same Del again, nothing edited since, closes. if (warnModifiedIn(c, .Del, .{ .pane = c.id })) return; + // An attachment's rows go back to its owner: nothing to ask. + if (layout.ownerOf(c.p, c.id)) |owner| return c.p.removePane(c.id, owner) catch |err| c.p.reportError(c.id, "close", err); const at = layout.findPane(c.p, c.id); const above = if (at) |f| layout.expandedNeighbor(c.p, f.col, f.idx, .above) else null; - const below = if (at) |f| layout.expandedNeighbor(c.p, f.col, f.idx, .below) else null; + // An owner's attachments close with it: below is past them. + const below = if (at) |f| layout.expandedNeighbor(c.p, f.col, layout.groupEnd(c.p, f.col, f.idx) - 1, .below) else null; const to: ?usize = if (std.mem.eql(u8, side, "k")) above orelse below else if (std.mem.eql(u8, side, "j")) @@ -1394,6 +1397,16 @@ pub const Collapse = struct { } }; +/// Unattach a pane from the pane it answers for: it stays where it is, a +/// pane of its own, and the next search from that pane opens another +/// under it. +pub const Unattach = struct { + pub fn run(c: Ctx) void { + if (c.pane.attached_to == null) return c.p.reportFailure(c.id, "Unattach: not attached to a pane"); + c.pane.attached_to = null; + } +}; + /// Close this column and its panes, refusing once over unsaved text. pub const Delcol = struct { pub fn run(c: Ctx) void { diff --git a/src/config.zig b/src/config.zig index 1fbf0d1b..06eec2d9 100644 --- a/src/config.zig +++ b/src/config.zig @@ -58,6 +58,7 @@ pub const leader_path = paths: { .Mode = null, .Togglettymode = null, .Collapse = null, + .Unattach = null, .Undo = null, .Edit = null, .Redo = null, diff --git a/src/dump.zig b/src/dump.zig index c8c302ab..0ce44c40 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -264,6 +264,9 @@ pub const Pane = struct { /// Exact editable tail when the user touched it. Null retains the live /// default; a present empty slice deliberately restores an empty tail. tag_tail: ?[]const u8 = null, + /// The index in `panes` of the pane this one is attached to (layout.zig, + /// "Attached panes"); null when it is attached to none, and in older dumps. + attached_to: ?usize = null, terminal: ?Terminal = null, file: ?File = null, image: ?Image = null, @@ -744,6 +747,13 @@ pub fn dumpState(p: *Pardes) !void { dump_panes[panes_len] = dp; panes_len += 1; } + // An attachment names its owner by its place in the dump: serials are + // handed out again on a Restore. + for (0..p.panes.len) |id| { + const i = slot_to_pane[id] orelse continue; + const owner = layout.ownerOf(p, id) orelse continue; + dump_panes[i].attached_to = slot_to_pane[owner]; + } // Zerox's twins: the text once, in the first; each other names it. for (p.panes, 0..) |slot, id| { const f = (slot orelse continue).file orelse continue; @@ -1020,6 +1030,13 @@ fn initDump(gpa: std.mem.Allocator, opts: Options, zon_bytes: []const u8, previo p.col_n[c] = @min(col.panes.len, MAX_PANES); for (col.panes[0..p.col_n[c]], 0..) |pid, k| p.col_panes[c][k] = pid; } + // A dump's pane i is restored into slot i: its owner's serial now. + for (st.panes[0..@min(st.panes.len, MAX_PANES)], 0..) |src, i| { + const owner = src.attached_to orelse continue; + if (owner >= MAX_PANES or owner == i) continue; + const pane = p.panes[i] orelse continue; + pane.attached_to = (p.panes[owner] orelse continue).serial; + } p.active = @min(st.active, MAX_PANES - 1); p.sync(); exec.applyStartupConfig(p); diff --git a/src/exec.zig b/src/exec.zig index eb15dbc9..a9c11049 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -1290,7 +1290,7 @@ fn pardesPlace(p: *Pardes, from: usize, id: usize, kind: NewPane) void { const nt = p.panes[id].?; switch (kind) { .scratch, .fs => if (layout.findPane(p, from)) |sf| { - layout.insert(p, sf.col, sf.idx + 1, id); + layout.insert(p, sf.col, layout.groupEnd(p, sf.col, sf.idx), id); layout.splitBelow(p, from, nt); } else atFoot(p, id), .shell => belowParent(p, from, id), @@ -1314,7 +1314,7 @@ fn atFoot(p: *Pardes, id: usize) void { fn belowParent(p: *Pardes, from: usize, id: usize) void { const parent = layout.splitParent(p, from); const f = layout.findPane(p, parent) orelse return atFoot(p, id); - layout.insert(p, f.col, f.idx + 1, id); + layout.insert(p, f.col, layout.groupEnd(p, f.col, f.idx), id); layout.splitBelow(p, parent, p.panes[id].?); } @@ -1344,7 +1344,7 @@ fn pardesDoc(p: *Pardes, from_id: usize, free: usize) void { }; }; if (!doc) if (layout.findPane(p, from_id)) |sf| { - layout.insert(p, sf.col, sf.idx + 1, free); + layout.insert(p, sf.col, layout.groupEnd(p, sf.col, sf.idx), free); layout.splitBelow(p, from_id, nt); // NOT splitParent: no bystander pays return; }; 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); +} diff --git a/src/panes.zig b/src/panes.zig index b535993e..14df07e9 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -101,6 +101,13 @@ pub const Pane = struct { vweight: f32 = 1, /// A folded pane keeps its expansion weight, but occupies only its tag row. collapsed: bool = false, + /// The serial of the pane this one is attached to, its owner: a query + /// pane or its +Preview sits right under its owner in the owner's + /// column, takes its rows from the owner alone, and moves, folds and + /// closes with it (layout.zig, "Attached panes"). An owner that is + /// gone, or a pane no longer right under its owner's group, leaves it + /// an ordinary pane (layout.keepGroups). + attached_to: ?u32 = null, cols: u16, rows: u16, greet: bool = false, diff --git a/src/pardes.zig b/src/pardes.zig index 80ff3495..c58452ee 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -7146,13 +7146,22 @@ pub const Pardes = struct { /// closing ends the session. Every route that closes a pane comes /// through here or removeColumn, so none picks focus for itself, and /// none by recency. - pub fn removePane(p: *Pardes, id: usize, to: ?usize) !void { + pub fn removePane(p: *Pardes, id: usize, to: ?usize) error{OutOfMemory}!void { const pane = p.panes[id] orelse return; + // What is attached to it closes with it, first, each giving its rows + // back to it: then its rows, theirs among them, go where a pane's go. + for (0..p.panes.len) |k| if (p.panes[k]) |other| if (k != id and other.attached_to == pane.serial) { + if (layout.ownerOf(p, k) != id) continue; + try p.removePane(k, id); + layout.compute(p); + }; + // An attachment's rows go back to its owner and nobody else. + const owner = layout.ownerOf(p, id); const place = layout.findPane(p, id); const emptied = if (place) |f| (if (p.col_n[f.col] == 1) f.col else null) else null; // Before anything is torn down, so a failure here changes nothing. try p.deinitPane(pane); - const gainer = layout.absorbVWeight(p, id, to); + const gainer = layout.absorbVWeight(p, id, owner orelse to); const beside = if (emptied) |c| p.besideColumn(c, p.rects[id]) else null; layout.removePane(p, id); p.panes[id] = null; |
