summaryrefslogtreecommitdiff
path: root/src/layout.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/layout.zig')
-rw-r--r--src/layout.zig779
1 files changed, 407 insertions, 372 deletions
diff --git a/src/layout.zig b/src/layout.zig
index 1515b525..a0d7cbd5 100644
--- a/src/layout.zig
+++ b/src/layout.zig
@@ -35,7 +35,7 @@ pub const Dir = enum { left, right, up, down };
/// Move the keyboard to the nearest pane in `dir`; false when there is none.
pub fn focusDir(p: *Pardes, from: usize, dir: Dir) bool {
const b = nearest(p, from, dir) orelse return false;
- p.active = b;
+ p.view().active = b;
// a count typed before the hop was meant for the pane you left
p.panes[b].?.body.normal.clear();
return true;
@@ -43,12 +43,12 @@ pub fn focusDir(p: *Pardes, from: usize, dir: Dir) bool {
/// The pane next to `from` in `dir`, by the screen.
pub fn nearest(p: *const Pardes, from: usize, dir: Dir) ?usize {
- const a = p.rects[from];
+ const a = p.view().rects[from];
var best: ?usize = null;
var best_d: i32 = 0;
for (p.panes, 0..) |slot, i| {
if (slot == null or i == from) continue;
- const r = p.rects[i];
+ const r = p.view().rects[i];
const vov = a.y < r.y + r.h and r.y < a.y + a.h;
const hov = a.x < r.x + r.w and r.x < a.x + a.w;
const ok = switch (dir) {
@@ -81,7 +81,7 @@ pub const window_labels = "asfgqwertyuiopzxvbnm";
/// The letter window mode shows on pane `id`.
pub fn windowLabel(p: *const Pardes, id: usize) ?u8 {
var n: usize = 0;
- for (0..p.ncol) |c| for (p.cols[c].panes[0..p.cols[c].n]) |pane| {
+ for (0..p.view().ncol) |c| for (p.view().cols[c].panes[0..p.view().cols[c].n]) |pane| {
if (n == window_labels.len) return null;
if (pane == id) return window_labels[n];
n += 1;
@@ -98,9 +98,10 @@ pub fn windowLabel(p: *const Pardes, id: usize) ?u8 {
/// question, when Del asks one; keys.answerQuestion). Any other key
/// changes nothing.
pub fn windowKey(p: *Pardes, key: pardes.Key) void {
- const at = p.window_pick orelse return;
+ const v = p.view();
+ const at = v.window_pick orelse return;
if (p.panes[at] == null) {
- p.window_pick = null;
+ v.window_pick = null;
return;
}
if (key.ctrl or key.alt) return;
@@ -112,10 +113,10 @@ pub fn windowKey(p: *Pardes, key: pardes.Key) void {
else => .right,
};
if (nearest(p, at, dir)) |next| {
- p.window_pick = next;
+ v.window_pick = next;
} else if (dir == .up) {
- p.window_pick = null;
- p.active = at;
+ v.window_pick = null;
+ v.active = at;
pardes.exec.runBuiltin(p, .Up, at, "", null);
}
return;
@@ -124,33 +125,34 @@ pub fn windowKey(p: *Pardes, key: pardes.Key) void {
if (pardes.hit(key, config.window_delete)) {
// Del's focus follows the rows from the pane it closes, so the
// outlined pane is the active one while it runs.
- p.active = at;
+ v.active = at;
pardes.exec.runBuiltin(p, .Del, at, "", null);
- if (p.panes[at] == null) p.window_pick = if (p.header != null) null else p.active;
+ if (p.panes[at] == null) v.window_pick = if (v.header != null) null else v.active;
return;
}
const chosen = if (key.cp == pardes.Key.enter or key.cp == pardes.Key.escape or key.cp == ':') at else for (0..p.panes.len) |id| {
if (p.panes[id] != null and key.cp < 128 and windowLabel(p, id) == @as(u8, @intCast(key.cp))) break id;
} else return;
- p.window_pick = null;
- p.active = chosen;
+ v.window_pick = null;
+ v.active = chosen;
p.panes[chosen].?.body.normal.clear();
if (key.cp == ':') tagline.enterTag(p, p.panes[chosen].?);
}
/// Column `c`'s serial, handed out the first time it is asked for.
pub fn columnSerial(p: *Pardes, c: usize) u32 {
- if (p.cols[c].serial == 0) {
- p.cols[c].serial = p.next_column_serial;
+ const v = p.view();
+ if (v.cols[c].serial == 0) {
+ v.cols[c].serial = p.next_column_serial;
p.next_column_serial +%= 1;
if (p.next_column_serial == 0) p.next_column_serial = 1;
}
- return p.cols[c].serial;
+ return v.cols[c].serial;
}
pub fn columnBySerial(p: *const Pardes, serial: u32) ?usize {
if (serial == 0) return null;
- for (0..p.ncol) |column| if (p.cols[column].serial == serial) return column;
+ for (0..p.view().ncol) |column| if (p.view().cols[column].serial == serial) return column;
return null;
}
@@ -162,17 +164,18 @@ pub fn columnBySerial(p: *const Pardes, serial: u32) ?usize {
pub const ColumnDrop = union(enum) { stay, reorder: usize, edge: u16 };
pub fn columnDrop(p: *const Pardes, source: usize, x: u16, grab: u16) ColumnDrop {
- if (source >= p.ncol) return .stay;
+ const v = p.view();
+ if (source >= v.ncol) return .stay;
var insertion: usize = 0;
- for (0..p.ncol) |column| {
+ for (0..p.view().ncol) |column| {
if (column == source) continue;
- if (x >= p.col_x[column] + p.col_w[column] / 2) insertion += 1;
+ if (x >= v.col_x[column] + v.col_w[column] / 2) insertion += 1;
}
if (insertion != source) return .{ .reorder = insertion };
- if (source == 0 or (x >= p.col_x[source] and x < p.col_x[source] + config.GUTTER)) return .stay;
- if (p.col_w[source - 1] + p.col_w[source] < 2 * config.MINW) return .stay;
- const lo = p.col_x[source - 1] + config.MINW;
- const hi = (p.col_x[source] + p.col_w[source]) -| config.MINW;
+ if (source == 0 or (x >= v.col_x[source] and x < v.col_x[source] + config.GUTTER)) return .stay;
+ if (v.col_w[source - 1] + v.col_w[source] < 2 * config.MINW) return .stay;
+ const lo = v.col_x[source - 1] + config.MINW;
+ const hi = (v.col_x[source] + v.col_w[source]) -| config.MINW;
return .{ .edge = std.math.clamp(x -| grab, lo, @max(lo, hi)) };
}
@@ -180,48 +183,51 @@ pub fn columnDrop(p: *const Pardes, source: usize, x: u16, grab: u16) ColumnDrop
/// `left_width` cells, neither below MINW. A border drag and a column grip
/// dropped inside its own place both end here.
pub fn setColumnPairWidth(p: *Pardes, left: usize, left_width: u64) void {
- if (left + 1 >= p.ncol) return;
- const combined: u64 = p.col_w[left] + p.col_w[left + 1];
+ const v = p.view();
+ if (left + 1 >= v.ncol) return;
+ const combined: u64 = v.col_w[left] + v.col_w[left + 1];
// Too narrow for both to keep MINW: any split would squeeze one away.
if (combined < 2 * @as(u64, config.MINW)) return;
const width = std.math.clamp(left_width, config.MINW, @max(@as(u64, config.MINW), combined -| config.MINW));
- const pair = p.cols[left].weight + p.cols[left + 1].weight;
+ const pair = v.cols[left].weight + v.cols[left + 1].weight;
if (pair <= 1 or combined == 0) return;
const numerator = @as(u128, pair) * width + combined / 2;
const share = std.math.clamp(@as(u64, @intCast(numerator / combined)), 1, pair - 1);
- p.cols[left].weight = share;
- p.cols[left + 1].weight = pair - share;
+ v.cols[left].weight = share;
+ v.cols[left + 1].weight = pair - share;
}
pub fn reorderColumn(p: *Pardes, from: usize, to: usize) void {
- if (from >= p.ncol or to >= p.ncol or from == to) return;
- const moved = p.cols[from];
- const tag = p.col_tags[from];
+ const v = p.view();
+ if (from >= v.ncol or to >= v.ncol or from == to) return;
+ const moved = v.cols[from];
+ const tag = v.col_tags[from];
var at = from;
while (at != to) {
const next = if (at < to) at + 1 else at - 1;
- p.cols[at] = p.cols[next];
- p.col_tags[at] = p.col_tags[next];
+ v.cols[at] = v.cols[next];
+ v.col_tags[at] = v.col_tags[next];
at = next;
}
- p.cols[to] = moved;
- p.col_tags[to] = tag;
- if (p.header) |*h| if (h.* == .column) {
+ v.cols[to] = moved;
+ v.col_tags[to] = tag;
+ if (v.header) |*h| if (h.* == .column) {
const column = h.column;
if (column == from) h.column = to else if (from < to and column > from and column <= to) {
h.column = column - 1;
} else if (from > to and column >= to and column < from) h.column = column + 1;
};
- p.pointer_tag_hit = null;
+ v.pointer_tag_hit = null;
p.look_hover_wait = null;
p.look_hover_preview = null;
compute(p);
}
pub fn targetColumn(p: *Pardes, cur_x: u16) usize {
- var tc: usize = if (p.ncol > 0) p.ncol - 1 else 0;
- for (0..p.ncol) |c| {
- if (cur_x >= p.col_x[c] and cur_x < p.col_x[c] + p.col_w[c]) {
+ const v = p.view();
+ var tc: usize = if (v.ncol > 0) v.ncol - 1 else 0;
+ for (0..p.view().ncol) |c| {
+ if (cur_x >= v.col_x[c] and cur_x < v.col_x[c] + v.col_w[c]) {
tc = c;
break;
}
@@ -247,15 +253,16 @@ fn splitRowFor(y: u16, h: u16, cur_y: u16, least: ?u16) ?u16 {
/// 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 v = p.view();
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.cols[src.col].n == span_end - src.idx) return null;
+ if (tc == src.col and v.cols[src.col].n == span_end - src.idx) return null;
var span_h: u16 = 0;
- for (p.cols[src.col].panes[src.idx..span_end]) |k| span_h +|= p.rects[k].h;
+ for (v.cols[src.col].panes[src.idx..span_end]) |k| span_h +|= v.rects[k].h;
if (tc == src.col) {
- const top = p.rects[id].y;
+ const top = v.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
@@ -264,29 +271,29 @@ pub fn movePlacement(p: *Pardes, id: usize, cur_x: u16, cur_y: u16) ?MovePlaceme
}
// Every pane's rows as laid out; only the placed panes' are read.
var heights: [MAX_PANES]u16 = undefined;
- for (&heights, p.rects) |*h, r| h.* = r.h;
- if (p.cols[src.col].n > span_end - src.idx) {
- const sib = if (src.idx > 0) p.cols[src.col].panes[src.idx - 1] else p.cols[src.col].panes[span_end];
+ for (&heights, v.rects) |*h, r| h.* = r.h;
+ if (v.cols[src.col].n > span_end - src.idx) {
+ const sib = if (src.idx > 0) v.cols[src.col].panes[src.idx - 1] else v.cols[src.col].panes[span_end];
heights[sib] +|= span_h;
}
var y: u16 = p.topBarHeight() + columnBarHeight(p);
- if (p.cols[tc].n == 0) return .{ .preview_col = tc, .above_id = null, .row = y, .above_y = y, .above_h = 0 };
+ if (v.cols[tc].n == 0) return .{ .preview_col = tc, .above_id = null, .row = y, .above_y = y, .above_h = 0 };
var last: ?MovePlacement = null;
var k: usize = 0;
- while (k < p.cols[tc].n) {
+ while (k < v.cols[tc].n) {
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.cols[tc].panes[k]) == own_root) k + 1 else groupEnd(p, tc, k);
+ const unit_end = if (groupRoot(p, v.cols[tc].panes[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.cols[tc].panes[j]];
+ h +|= heights[v.cols[tc].panes[j]];
}
- const above = p.cols[tc].panes[unit_end - 1];
+ const above = v.cols[tc].panes[unit_end - 1];
k = unit_end;
const row = splitRowFor(y, h, cur_y, if (tc == src.col) BOX_H else null) orelse {
y +|= h;
@@ -309,6 +316,7 @@ pub fn movePlacement(p: *Pardes, id: usize, cur_x: u16, cur_y: u16) ?MovePlaceme
/// 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 v = p.view();
const placement = movePlacement(p, id, cur_x, cur_y) orelse return;
const src = findPane(p, id) orelse return;
const owner = ownerOf(p, id);
@@ -316,15 +324,15 @@ pub fn movePane(p: *Pardes, id: usize, cur_x: u16, cur_y: u16) void {
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.cols[src.col].panes[src.idx..span_end]);
- const source_multi = p.cols[src.col].n > count;
+ @memcpy(moved[0..count], v.cols[src.col].panes[src.idx..span_end]);
+ const source_multi = v.cols[src.col].n > count;
var heights: [MAX_PANES]u16 = undefined;
- for (&heights, p.rects) |*h, r| h.* = r.h;
+ for (&heights, v.rects) |*h, r| h.* = r.h;
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.cols[src.col].panes[src.idx - 1] else p.cols[src.col].panes[src.idx];
+ const sib = if (src.idx > 0) v.cols[src.col].panes[src.idx - 1] else v.cols[src.col].panes[src.idx];
heights[sib] +|= span_h;
}
const above = placement.above_id orelse {
@@ -364,9 +372,9 @@ pub fn movePane(p: *Pardes, id: usize, cur_x: u16, cur_y: u16) void {
}
pub fn setColumnWeights(p: *Pardes, col: usize, heights: *const [MAX_PANES]u16) void {
- if (col >= p.ncol) return;
- for (0..p.cols[col].n) |k| {
- const pid = p.cols[col].panes[k];
+ if (col >= p.view().ncol) return;
+ for (0..p.view().cols[col].n) |k| {
+ const pid = p.view().cols[col].panes[k];
if (p.panes[pid]) |pane| if (!pane.collapsed) {
pane.vweight = @floatFromInt(@max(1, heights[pid]));
};
@@ -376,12 +384,13 @@ pub fn setColumnWeights(p: *Pardes, col: usize, heights: *const [MAX_PANES]u16)
/// Give a newly opened report enough rows to read, borrowing only space
/// above the other panes' usable minima. Keep their tags and bodies visible.
pub fn givePaneRows(p: *Pardes, id: usize, wanted: u16, context: usize) void {
+ const v = p.view();
const at = findPane(p, id) orelse return;
compute(p);
var heights: [MAX_PANES]u16 = @splat(0);
- for (p.cols[at.col].panes[0..p.cols[at.col].n]) |other| heights[other] = p.rects[other].h;
+ for (v.cols[at.col].panes[0..p.view().cols[at.col].n]) |other| heights[other] = v.rects[other].h;
var missing = wanted -| heights[id];
- for (p.cols[at.col].panes[0..p.cols[at.col].n]) |other| {
+ for (v.cols[at.col].panes[0..p.view().cols[at.col].n]) |other| {
if (other == id or p.panes[other].?.collapsed) continue;
const body_rows: u16 = if (other == context) 6 else 3;
const minimum = @max(minRows(p, other), p.panes[other].?.tag_rows + body_rows);
@@ -396,17 +405,18 @@ pub fn givePaneRows(p: *Pardes, id: usize, wanted: u16, context: usize) void {
}
pub fn applyRowSplit(p: *Pardes, cc: usize, k: usize, cur_y: u16) void {
- if (k + 1 >= p.cols[cc].n) return;
- const a = p.panes[p.cols[cc].panes[k]] orelse return;
- const b = p.panes[p.cols[cc].panes[k + 1]] orelse return;
- const ra = p.rects[p.cols[cc].panes[k]];
- const rb = p.rects[p.cols[cc].panes[k + 1]];
+ const v = p.view();
+ if (k + 1 >= v.cols[cc].n) return;
+ const a = p.panes[v.cols[cc].panes[k]] orelse return;
+ const b = p.panes[v.cols[cc].panes[k + 1]] orelse return;
+ const ra = v.rects[v.cols[cc].panes[k]];
+ const rb = v.rects[v.cols[cc].panes[k + 1]];
const combined: f32 = @floatFromInt(ra.h + rb.h);
var nt: f32 = @floatFromInt((cur_y + 1) -| ra.y);
// A terminal keeps its tag and two body rows (floorRows); a text pane
// gives up all but its tag, as acme's do.
- const lo: f32 = @floatFromInt(@max(BOX_H, floorRows(p, p.cols[cc].panes[k])));
- const hi = combined - @as(f32, @floatFromInt(@max(BOX_H, floorRows(p, p.cols[cc].panes[k + 1]))));
+ const lo: f32 = @floatFromInt(@max(BOX_H, floorRows(p, v.cols[cc].panes[k])));
+ const hi = combined - @as(f32, @floatFromInt(@max(BOX_H, floorRows(p, v.cols[cc].panes[k + 1]))));
nt = std.math.clamp(nt, @min(lo, @max(@as(f32, BOX_H), hi)), @max(@as(f32, BOX_H), hi));
if (combined <= 0) return;
// Dragging a folded tag open is an explicit resize, so switch this pair
@@ -425,30 +435,32 @@ pub fn applyRowSplit(p: *Pardes, cc: usize, k: usize, cur_y: u16) void {
}
pub fn findPane(p: *const Pardes, id: usize) ?struct { col: usize, idx: usize } {
- for (0..p.ncol) |c| {
- for (0..p.cols[c].n) |k| {
- if (p.cols[c].panes[k] == id) return .{ .col = c, .idx = k };
+ for (0..p.view().ncol) |c| {
+ for (0..p.view().cols[c].n) |k| {
+ if (p.view().cols[c].panes[k] == id) return .{ .col = c, .idx = k };
}
}
return null;
}
pub fn insert(p: *Pardes, c: usize, idx: usize, id: usize) void {
- var k = p.cols[c].n;
- while (k > idx) : (k -= 1) p.cols[c].panes[k] = p.cols[c].panes[k - 1];
- p.cols[c].panes[idx] = id;
- p.cols[c].n += 1;
+ const v = p.view();
+ var k = v.cols[c].n;
+ while (k > idx) : (k -= 1) v.cols[c].panes[k] = v.cols[c].panes[k - 1];
+ v.cols[c].panes[idx] = id;
+ v.cols[c].n += 1;
}
/// Take a pane out of its column. The column stays, empty if this was its
/// last pane, as acme's colclose leaves it (cols.c:186): only Delcol and
/// Joincol take a column away.
pub fn removePane(p: *Pardes, id: usize) void {
+ const v = p.view();
const f = findPane(p, id) orelse return;
const c = f.col;
var k = f.idx;
- while (k + 1 < p.cols[c].n) : (k += 1) p.cols[c].panes[k] = p.cols[c].panes[k + 1];
- p.cols[c].n -= 1;
+ while (k + 1 < v.cols[c].n) : (k += 1) v.cols[c].panes[k] = v.cols[c].panes[k + 1];
+ v.cols[c].n -= 1;
}
/// Close column `c` once its panes are gone, its width to the one left of
@@ -461,19 +473,20 @@ pub fn dropColumn(p: *Pardes, c: usize) void {
/// dropColumn with no `delcol`, for a column never announced.
pub fn dropColumnUnannounced(p: *Pardes, c: usize) void {
- std.debug.assert(p.cols[c].n == 0);
+ const v = p.view();
+ std.debug.assert(v.cols[c].n == 0);
tagline.exitHeader(p);
- const heir: ?usize = if (p.ncol <= 1) null else if (c > 0) c - 1 else c + 1;
- if (heir) |h| p.cols[h].weight +|= p.cols[c].weight;
- p.col_tags[c].deinit(p.gpa);
+ const heir: ?usize = if (v.ncol <= 1) null else if (c > 0) c - 1 else c + 1;
+ if (heir) |h| v.cols[h].weight +|= v.cols[c].weight;
+ v.col_tags[c].deinit(p.gpa);
var j = c;
- while (j + 1 < p.ncol) : (j += 1) {
- p.cols[j] = p.cols[j + 1];
- p.col_tags[j] = p.col_tags[j + 1];
+ while (j + 1 < v.ncol) : (j += 1) {
+ v.cols[j] = v.cols[j + 1];
+ v.col_tags[j] = v.col_tags[j + 1];
}
- p.ncol -= 1;
- p.col_tags[p.ncol] = .{ .what = .column, .gpa = p.gpa };
- p.cols[p.ncol].serial = 0;
+ v.ncol -= 1;
+ v.col_tags[v.ncol] = .{ .what = .column, .gpa = p.gpa };
+ v.cols[v.ncol].serial = 0;
}
/// `newcol <serial>` or `delcol <serial>` in the log: the column's serial,
@@ -485,7 +498,7 @@ pub fn noteColumn(p: *Pardes, what: []const u8, serial: u32) void {
/// Fold column `c` into the one on its right, its panes below theirs.
pub fn joinCol(p: *Pardes, c: usize) void {
- if (c + 1 >= p.ncol) return;
+ if (c + 1 >= p.view().ncol) return;
const dst = c + 1;
joinPanes(p, c, dst);
dropColumn(p, c);
@@ -495,24 +508,26 @@ pub fn joinCol(p: *Pardes, c: usize) void {
/// its tag and 2 rows (minimaFit), tried in place and undone: nothing is
/// dropped or logged for a join that does not fit.
pub fn joinFits(p: *Pardes, c: usize) bool {
- if (c + 1 >= p.ncol) return true;
+ const v = p.view();
+ if (c + 1 >= v.ncol) return true;
const dst = c + 1;
- const kept = .{ p.cols[c], p.cols[dst] };
+ const kept = .{ v.cols[c], v.cols[dst] };
joinPanes(p, c, dst);
const fits = minimaFit(p);
- p.cols[c] = kept[0];
- p.cols[dst] = kept[1];
+ v.cols[c] = kept[0];
+ v.cols[dst] = kept[1];
compute(p);
return fits;
}
fn joinPanes(p: *Pardes, c: usize, dst: usize) void {
- for (0..p.cols[c].n) |k| p.cols[dst].panes[p.cols[dst].n + k] = p.cols[c].panes[k];
- p.cols[dst].n += p.cols[c].n;
- p.cols[c].n = 0;
+ const v = p.view();
+ for (0..p.view().cols[c].n) |k| v.cols[dst].panes[v.cols[dst].n + k] = v.cols[c].panes[k];
+ v.cols[dst].n += v.cols[c].n;
+ v.cols[c].n = 0;
// The width goes right, where the panes went.
- p.cols[dst].weight +|= p.cols[c].weight;
- p.cols[c].weight = 0;
+ v.cols[dst].weight +|= v.cols[c].weight;
+ v.cols[c].weight = 0;
}
/// Put new pane `id` in column `c` as acme's makenewwindow does
@@ -524,7 +539,7 @@ fn joinPanes(p: *Pardes, c: usize, dst: usize) void {
/// that is in the column and not much smaller.
pub fn acmePlace(p: *Pardes, c: usize, origin: ?usize, id: usize) void {
compute(p);
- const n = p.cols[c].n;
+ const n = p.view().cols[c].n;
if (n == 0) {
insert(p, c, 0, id);
p.panes[id].?.vweight = 1;
@@ -532,7 +547,7 @@ pub fn acmePlace(p: *Pardes, c: usize, origin: ?usize, id: usize) void {
}
var big: ?usize = null;
var empty: ?usize = null;
- for (p.cols[c].panes[0..n]) |k| {
+ for (p.view().cols[c].panes[0..n]) |k| {
const pane = p.panes[k] orelse continue;
// An attachment is its owner's: never halved for another pane.
if (pane.collapsed or ownerOf(p, k) != null) continue;
@@ -548,7 +563,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.cols[c].panes[0..n]) |k| if (!p.panes[k].?.collapsed and ownerOf(p, k) == null) {
+ for (p.view().cols[c].panes[0..n]) |k| if (!p.panes[k].?.collapsed and ownerOf(p, k) == null) {
last = k;
};
break :y bodyFrom(p, last) + bodyRows(p, last) / 2;
@@ -560,8 +575,8 @@ pub fn acmePlace(p: *Pardes, c: usize, origin: ?usize, id: usize) void {
// 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;
+ const v = if (mine and @as(u32, p.view().rects[root].h) * 3 > @as(u32, p.view().rects[v0].h) * 2) root else v0;
+ break :y p.view().rects[v].y + p.view().rects[v].h / 2;
};
splitAt(p, c, id, y);
}
@@ -581,12 +596,13 @@ pub fn minRows(p: *Pardes, id: usize) u16 {
/// the weights happen to share the rows, so a size that fits keeps fitting
/// when it grows. One pane alone in its column always fits.
pub fn minimaFit(p: *Pardes) bool {
+ const v = p.view();
compute(p);
- const avail = p.screen_h -| (p.topBarHeight() + columnBarHeight(p));
- for (0..p.ncol) |c| {
- if (p.cols[c].n < 2) continue;
+ const avail = v.screen_h -| (p.topBarHeight() + columnBarHeight(p));
+ for (0..p.view().ncol) |c| {
+ if (v.cols[c].n < 2) continue;
var need: u32 = 0;
- for (p.cols[c].panes[0..p.cols[c].n]) |k| {
+ for (v.cols[c].panes[0..p.view().cols[c].n]) |k| {
const pane = p.panes[k] orelse continue;
need += if (pane.collapsed) BOX_H else minRows(p, k);
}
@@ -598,11 +614,12 @@ pub fn minimaFit(p: *Pardes) bool {
/// Whether every unfolded pane of column `c` has its `minRows` (a pane
/// alone in its column always does).
pub fn columnAtMinimums(p: *Pardes, c: usize) bool {
+ const v = p.view();
compute(p);
- if (p.cols[c].n < 2) return true;
- for (p.cols[c].panes[0..p.cols[c].n]) |k| {
+ if (v.cols[c].n < 2) return true;
+ for (v.cols[c].panes[0..p.view().cols[c].n]) |k| {
const pane = p.panes[k] orelse continue;
- if (!pane.collapsed and p.rects[k].h < minRows(p, k)) return false;
+ if (!pane.collapsed and v.rects[k].h < minRows(p, k)) return false;
}
return true;
}
@@ -611,16 +628,17 @@ pub fn columnAtMinimums(p: *Pardes, c: usize) bool {
/// `minRows`, as evenly as that allows; false, changing nothing, when the
/// column has not the rows for all of them (or holds a folded pane).
pub fn shareColumn(p: *Pardes, c: usize) bool {
+ const v = p.view();
compute(p);
- const n = p.cols[c].n;
+ const n = v.cols[c].n;
if (n == 0) return false;
- const avail = p.screen_h -| (p.topBarHeight() + columnBarHeight(p));
+ const avail = v.screen_h -| (p.topBarHeight() + columnBarHeight(p));
var mins: [MAX_PANES]u16 = undefined;
var need: u32 = 0;
- for (p.cols[c].panes[0..n], 0..) |k, i| {
+ for (v.cols[c].panes[0..n], 0..) |k, i| {
const pane = p.panes[k] orelse return false;
if (pane.collapsed) return false;
- p.rects[k].w = p.col_w[c];
+ v.rects[k].w = v.col_w[c];
mins[i] = minRows(p, k);
need += mins[i];
}
@@ -636,7 +654,7 @@ pub fn shareColumn(p: *Pardes, c: usize) bool {
};
hs[low] += 1;
}
- for (p.cols[c].panes[0..n], 0..) |k, i| p.panes[k].?.vweight = @floatFromInt(hs[i]);
+ for (v.cols[c].panes[0..n], 0..) |k, i| p.panes[k].?.vweight = @floatFromInt(hs[i]);
compute(p);
return true;
}
@@ -646,7 +664,7 @@ pub fn shareColumn(p: *Pardes, c: usize) bool {
pub fn minima(p: *Pardes) [MAX_PANES]u16 {
compute(p);
var mins: [MAX_PANES]u16 = @splat(0);
- for (0..p.ncol) |c| for (p.cols[c].panes[0..p.cols[c].n]) |k| {
+ for (0..p.view().ncol) |c| for (p.view().cols[c].panes[0..p.view().cols[c].n]) |k| {
mins[k] = minRows(p, k);
};
return mins;
@@ -659,22 +677,23 @@ pub fn minima(p: *Pardes) [MAX_PANES]u16 {
/// minimum before the step (its tag may wrap onto more rows since): one
/// that had that minimum is lifted to its minimum now.
pub fn restoreMinimaFrom(p: *Pardes, before: *const [MAX_PANES]Rect, before_min: ?*const [MAX_PANES]u16) void {
+ const v = p.view();
compute(p);
- for (0..p.ncol) |c| {
- const n = p.cols[c].n;
+ for (0..p.view().ncol) |c| {
+ const n = v.cols[c].n;
if (n < 2) continue;
var hs: [MAX_PANES]u16 = undefined;
var mins: [MAX_PANES]u16 = undefined;
var short = false;
- for (p.cols[c].panes[0..n], 0..) |k, i| {
+ for (v.cols[c].panes[0..n], 0..) |k, i| {
const pane = p.panes[k] orelse return;
if (pane.collapsed) break;
- hs[i] = p.rects[k].h;
+ hs[i] = v.rects[k].h;
mins[i] = minRows(p, k);
if (hs[i] < mins[i] and before[k].h >= if (before_min) |bm| bm[k] else mins[i]) short = true;
} else {
if (!short) continue;
- for (p.cols[c].panes[0..n], 0..) |k, i| {
+ for (v.cols[c].panes[0..n], 0..) |k, i| {
if (hs[i] >= mins[i] or before[k].h < if (before_min) |bm| bm[k] else mins[i]) continue;
while (hs[i] < mins[i]) {
var giver: ?usize = null;
@@ -688,7 +707,7 @@ pub fn restoreMinimaFrom(p: *Pardes, before: *const [MAX_PANES]Rect, before_min:
hs[i] += 1;
}
}
- for (p.cols[c].panes[0..n], 0..) |k, i| p.panes[k].?.vweight = @floatFromInt(@max(1, hs[i]));
+ for (v.cols[c].panes[0..n], 0..) |k, i| p.panes[k].?.vweight = @floatFromInt(@max(1, hs[i]));
}
}
compute(p);
@@ -702,13 +721,14 @@ pub fn restoreMinimaFrom(p: *Pardes, before: *const [MAX_PANES]Rect, before_min:
/// where that is not enough. False, `c` dropped unannounced, where nothing
/// holds every minimum, as a size too small is refused.
pub fn keepColumn(p: *Pardes, c: usize, before: *const [MAX_PANES]Rect, before_min: *const [MAX_PANES]u16) bool {
+ const v = p.view();
restoreMinimaFrom(p, before, before_min);
- for (0..p.ncol) |k| {
+ for (0..p.view().ncol) |k| {
compute(p);
- if (p.cols[k].n < 2) continue;
- const squeezed = for (p.cols[k].panes[0..p.cols[k].n]) |id| {
+ if (v.cols[k].n < 2) continue;
+ const squeezed = for (v.cols[k].panes[0..p.view().cols[k].n]) |id| {
const pane = p.panes[id] orelse continue;
- if (!pane.collapsed and p.rects[id].h < minRows(p, id) and before[id].h >= before_min[id]) break true;
+ if (!pane.collapsed and v.rects[id].h < minRows(p, id) and before[id].h >= before_min[id]) break true;
} else false;
if (squeezed and !shareColumn(p, k)) {
dropColumnUnannounced(p, c);
@@ -723,17 +743,18 @@ pub fn keepColumn(p: *Pardes, c: usize, before: *const [MAX_PANES]Rect, before_m
/// dragged short keeps its rows and the others give only what they must.
/// False, changing nothing, when the column has not the rows.
pub fn takeRoom(p: *Pardes, c: usize, id: usize) bool {
+ const v = p.view();
compute(p);
- const n = p.cols[c].n;
- p.rects[id].w = p.col_w[c];
+ const n = v.cols[c].n;
+ v.rects[id].w = v.col_w[c];
var need = minRows(p, id);
var hs: [MAX_PANES]u16 = undefined;
var mins: [MAX_PANES]u16 = undefined;
var spare: u32 = 0;
- for (p.cols[c].panes[0..n], 0..) |k, i| {
+ for (v.cols[c].panes[0..n], 0..) |k, i| {
const pane = p.panes[k] orelse return false;
if (pane.collapsed) return false;
- hs[i] = p.rects[k].h;
+ hs[i] = v.rects[k].h;
mins[i] = minRows(p, k);
spare += hs[i] -| mins[i];
}
@@ -745,7 +766,7 @@ pub fn takeRoom(p: *Pardes, c: usize, id: usize) bool {
};
hs[giver] -= 1;
}
- for (p.cols[c].panes[0..n], 0..) |k, i| p.panes[k].?.vweight = @floatFromInt(@max(1, hs[i]));
+ for (v.cols[c].panes[0..n], 0..) |k, i| p.panes[k].?.vweight = @floatFromInt(@max(1, hs[i]));
insert(p, c, n, id);
p.panes[id].?.vweight = @floatFromInt(minRows(p, id));
compute(p);
@@ -757,14 +778,15 @@ pub fn takeRoom(p: *Pardes, c: usize, id: usize) bool {
/// the rects as they were). A pane alone in its column always has room: the
/// window can be no taller.
pub fn placedRoomy(p: *Pardes, id: usize, before: *const [MAX_PANES]Rect) bool {
+ const v = p.view();
compute(p);
const f = findPane(p, id) orelse return true;
- if (p.cols[f.col].n == 1) return true;
- for (p.cols[f.col].panes[0..p.cols[f.col].n]) |k| {
+ if (v.cols[f.col].n == 1) return true;
+ for (v.cols[f.col].panes[0..p.view().cols[f.col].n]) |k| {
const pane = p.panes[k] orelse continue;
if (pane.collapsed) continue;
- if (k != id and p.rects[k].h >= before[k].h) continue;
- if (p.rects[k].h < minRows(p, k)) return false;
+ if (k != id and v.rects[k].h >= before[k].h) continue;
+ if (v.rects[k].h < minRows(p, k)) return false;
}
return true;
}
@@ -775,17 +797,17 @@ pub fn placedRoomy(p: *Pardes, id: usize, before: *const [MAX_PANES]Rect) bool {
pub fn splitTallest(p: *Pardes, c: usize, id: usize) bool {
compute(p);
var tall: ?usize = null;
- for (p.cols[c].panes[0..p.cols[c].n]) |k| {
+ for (p.view().cols[c].panes[0..p.view().cols[c].n]) |k| {
if (k == id or p.panes[k].?.collapsed or ownerOf(p, k) != null) continue;
- if (tall == null or p.rects[k].h > p.rects[tall.?].h) tall = k;
+ if (tall == null or p.view().rects[k].h > p.view().rects[tall.?].h) tall = k;
}
const v = tall orelse return false;
- const h = p.rects[v].h;
+ const h = p.view().rects[v].h;
// Its tag wraps at the column's width: measured there, not at none.
- p.rects[id].w = p.col_w[c];
+ p.view().rects[id].w = p.view().col_w[c];
if (h < minRows(p, v) + minRows(p, id)) return false;
snapColWeights(p, c);
- insert(p, c, p.cols[c].n, id);
+ insert(p, c, p.view().cols[c].n, id);
const given = std.math.clamp(h / 2, minRows(p, id), h - minRows(p, v));
p.panes[v].?.vweight = @floatFromInt(h - given);
p.panes[id].?.vweight = @floatFromInt(given);
@@ -798,11 +820,11 @@ pub fn splitTallest(p: *Pardes, c: usize, id: usize) bool {
/// `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.cols[c].n;
+ const n = p.view().cols[c].n;
var k: usize = 0;
- while (k + 1 < n and y >= p.rects[p.cols[c].panes[k]].y + p.rects[p.cols[c].panes[k]].h) k += 1;
- const v = groupRoot(p, p.cols[c].panes[k]);
- const r = p.rects[v];
+ while (k + 1 < n and y >= p.view().rects[p.view().cols[c].panes[k]].y + p.view().rects[p.view().cols[c].panes[k]].h) k += 1;
+ const v = groupRoot(p, p.view().cols[c].panes[k]);
+ const r = p.view().rects[v];
snapColWeights(p, c);
insert(p, c, groupEnd(p, c, k), id);
const pane = p.panes[id].?;
@@ -821,11 +843,11 @@ fn splitAt(p: *Pardes, c: usize, id: usize, y: u16) void {
/// A pane's top plus its tag's rows, as heights go: a split this far down
/// leaves the pane its tag and that much of its body.
fn bodyFrom(p: *const Pardes, id: usize) u16 {
- return p.rects[id].y + p.panes[id].?.tag_rows;
+ return p.view().rects[id].y + p.panes[id].?.tag_rows;
}
fn bodyRows(p: *const Pardes, id: usize) u16 {
- return p.rects[id].h -| p.panes[id].?.tag_rows;
+ return p.view().rects[id].h -| p.panes[id].?.tag_rows;
}
/// The rows at the bottom of a pane's body that show nothing: acme's
@@ -846,15 +868,16 @@ fn blankRows(p: *const Pardes, id: usize) u16 {
/// Whether column `col` has the width and the weight to halve for a new
/// column beside it.
pub fn canSplitCol(p: *Pardes, col: usize) bool {
- if (p.ncol >= MAX_COLS or col >= p.ncol) return false;
+ const v = p.view();
+ if (v.ncol >= MAX_COLS or col >= v.ncol) return false;
// Refresh derived widths: public layout surgery may be chained between
// syncs, and a cached width must never admit a now-too-narrow split.
compute(p);
- if (p.col_w[col] < config.MINW * 2) return false;
+ if (v.col_w[col] < config.MINW * 2) return false;
- const weight = p.cols[col].weight;
+ const weight = v.cols[col].weight;
if (weight >= 2 and weight % 2 == 0) return true;
- for (0..p.ncol) |column| if (p.cols[column].weight > std.math.maxInt(u64) / 2)
+ for (0..p.view().ncol) |column| if (v.cols[column].weight > std.math.maxInt(u64) / 2)
return false;
return weight > 0;
}
@@ -864,10 +887,11 @@ pub fn canSplitCol(p: *Pardes, col: usize) bool {
/// A window with no column left (Delcol took the last, as acme lets it):
/// its first one, empty; null when it has columns.
pub fn firstColumn(p: *Pardes) ?usize {
- if (p.ncol != 0) return null;
- p.cols[0] = .{ .weight = 2 };
- p.col_tags[0] = .{ .what = .column, .gpa = p.gpa };
- p.ncol = 1;
+ const v = p.view();
+ if (v.ncol != 0) return null;
+ v.cols[0] = .{ .weight = 2 };
+ v.col_tags[0] = .{ .what = .column, .gpa = p.gpa };
+ v.ncol = 1;
noteColumn(p, "newcol", columnSerial(p, 0));
return 0;
}
@@ -881,24 +905,25 @@ pub fn insertColumn(p: *Pardes, col: usize, before: bool) ?usize {
/// insertColumn with no serial given and no `newcol`: a column that may
/// yet be taken back (Newcol's trial), which then leaves no trace.
pub fn insertColumnUnannounced(p: *Pardes, col: usize, before: bool) ?usize {
+ const v = p.view();
if (!canSplitCol(p, col)) return null;
- var old_weight = p.cols[col].weight;
+ var old_weight = v.cols[col].weight;
if (old_weight < 2 or old_weight % 2 != 0) {
old_weight *= 2;
- for (0..p.ncol) |column| p.cols[column].weight *= 2;
+ for (0..p.view().ncol) |column| v.cols[column].weight *= 2;
}
const source_weight = old_weight / 2;
- p.cols[col].weight = source_weight;
+ v.cols[col].weight = source_weight;
const c = col + @intFromBool(!before);
tagline.exitHeader(p);
- var j = p.ncol;
+ var j = v.ncol;
while (j > c) : (j -= 1) {
- p.cols[j] = p.cols[j - 1];
- p.col_tags[j] = p.col_tags[j - 1];
+ v.cols[j] = v.cols[j - 1];
+ v.col_tags[j] = v.col_tags[j - 1];
}
- p.cols[c] = .{ .weight = old_weight - source_weight };
- p.col_tags[c] = .{ .what = .column, .gpa = p.gpa };
- p.ncol += 1;
+ v.cols[c] = .{ .weight = old_weight - source_weight };
+ v.col_tags[c] = .{ .what = .column, .gpa = p.gpa };
+ v.ncol += 1;
return c;
}
@@ -910,7 +935,7 @@ pub fn splitColumn(p: *Pardes, source_id: usize, id: usize, before: bool) bool {
const source = findPane(p, source_id) orelse return false;
if (!canSplitCol(p, source.col)) return false;
if (id == source_id) {
- if (p.cols[source.col].n <= 1) return false;
+ if (p.view().cols[source.col].n <= 1) return false;
_ = absorbVWeight(p, id, null);
removePane(p, id);
} else if (findPane(p, id) != null) return false;
@@ -920,10 +945,10 @@ pub fn splitColumn(p: *Pardes, source_id: usize, id: usize, before: bool) bool {
}
pub fn snapColWeights(p: *Pardes, c: usize) void {
- for (0..p.cols[c].n) |k| {
- const pid = p.cols[c].panes[k];
+ for (0..p.view().cols[c].n) |k| {
+ const pid = p.view().cols[c].panes[k];
if (p.panes[pid]) |pp| if (!pp.collapsed) {
- pp.vweight = @floatFromInt(@max(1, p.rects[pid].h));
+ pp.vweight = @floatFromInt(@max(1, p.view().rects[pid].h));
};
}
}
@@ -934,27 +959,28 @@ pub fn snapColWeights(p: *Pardes, c: usize) void {
/// Give a closing pane's rows to `to`, or to the nearest document above it
/// (the pane below for the top one); the pane that got them is returned.
pub fn absorbVWeight(p: *Pardes, id: usize, to: ?usize) ?usize {
+ const v = p.view();
const f = findPane(p, id) orelse return null;
- if (p.cols[f.col].n <= 1) return null;
+ if (v.cols[f.col].n <= 1) return null;
// A refused pane held no weight: the rows the others' rounding lent it
// go back to them as they were, nobody's weight snapped.
if (p.panes[id]) |pane| if (!pane.collapsed and pane.vweight == 0) return null;
snapColWeights(p, f.col);
- var sib = to orelse if (f.idx > 0) p.cols[f.col].panes[f.idx - 1] else p.cols[f.col].panes[f.idx + 1];
+ var sib = to orelse if (f.idx > 0) v.cols[f.col].panes[f.idx - 1] else v.cols[f.col].panes[f.idx + 1];
var k = f.idx;
while (to == null and k > 0) : (k -= 1) {
- sib = p.cols[f.col].panes[k - 1];
+ sib = v.cols[f.col].panes[k - 1];
// 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)));
+ s.vweight += @as(f32, @floatFromInt(@max(1, v.rects[id].h)));
return sib;
}
pub fn splitBelow(p: *Pardes, src_id: usize, nw: *Pane) void {
const src = p.panes[src_id] orelse return;
- const src_h = p.rects[src_id].h;
+ const src_h = p.view().rects[src_id].h;
// Its tag may wrap to more than one row: the rows under it are its body.
const tag_h = @max(BOX_H, src.tag_rows);
const body: u16 = if (src_h > tag_h) src_h - tag_h else 1;
@@ -1020,9 +1046,10 @@ fn descends(p: *const Pardes, id: usize, root: usize) bool {
/// 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.cols[c].panes[idx]);
+ const v = p.view();
+ const root = groupRoot(p, v.cols[c].panes[idx]);
var k = idx + 1;
- while (k < p.cols[c].n and descends(p, p.cols[c].panes[k], root) and p.cols[c].panes[k] != root) k += 1;
+ while (k < v.cols[c].n and descends(p, v.cols[c].panes[k], root) and v.cols[c].panes[k] != root) k += 1;
return k;
}
@@ -1033,10 +1060,10 @@ pub fn groupEnd(p: *const Pardes, c: usize, idx: usize) usize {
/// 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.cols[c].panes[0..p.cols[c].n], 0..) |id, k| {
+ for (0..p.view().ncol) |c| for (p.view().cols[c].panes[0..p.view().cols[c].n], 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.cols[c].panes[k - 1], owner) else false;
+ const kept = if (ownerOf(p, id)) |owner| k > 0 and descends(p, p.view().cols[c].panes[k - 1], owner) else false;
if (kept) continue;
pane.attached_to = null;
};
@@ -1053,6 +1080,7 @@ pub fn keepGroups(p: *Pardes) void {
/// itself or a pane of its group (a +Preview added under a query pane later
/// takes the text's rows, so the query pane, typed into, keeps its own).
pub fn attachFrom(p: *Pardes, owner: usize, id: usize, rows: u16, giver: usize) bool {
+ const v = p.view();
const o = p.panes[owner] orelse return false;
const g = p.panes[giver] orelse return false;
const nw = p.panes[id] orelse return false;
@@ -1062,8 +1090,8 @@ pub fn attachFrom(p: *Pardes, owner: usize, id: usize, rows: u16, giver: usize)
const f = findPane(p, owner) orelse return false;
if (giver != owner and groupRoot(p, owner) != groupRoot(p, giver)) return false;
// Its tag wraps at the column's width: measured there.
- p.rects[id].w = p.col_w[f.col];
- const oh = p.rects[giver].h;
+ v.rects[id].w = v.col_w[f.col];
+ const oh = v.rects[giver].h;
const least = minRows(p, id);
if (oh < least + BOX_H) return false;
const giver_keeps = if (oh >= least + minRows(p, giver)) minRows(p, giver) else BOX_H;
@@ -1083,14 +1111,15 @@ pub fn attachFrom(p: *Pardes, owner: usize, id: usize, rows: u16, giver: usize)
/// so no other pane moves. False when the gainer is folded or too short
/// to give a pane its minimum and keep its own.
pub fn giveBack(p: *Pardes, id: usize, gainer: usize, below: bool, rows: u16) bool {
+ const v = p.view();
const g = p.panes[gainer] orelse return false;
const nw = p.panes[id] orelse return false;
if (g.collapsed) return false;
compute(p);
const f = findPane(p, gainer) orelse return false;
// Its tag wraps at the column's width: measured there.
- p.rects[id].w = p.col_w[f.col];
- const gh = p.rects[gainer].h;
+ v.rects[id].w = v.col_w[f.col];
+ const gh = v.rects[gainer].h;
const least = minRows(p, id);
const keeps = minRows(p, gainer);
if (gh < least + keeps) return false;
@@ -1109,16 +1138,17 @@ pub fn giveBack(p: *Pardes, id: usize, gainer: usize, below: bool, rows: u16) bo
/// 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.cols[c].panes[start]].?;
+ const v = p.view();
+ const root = p.panes[v.cols[c].panes[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.cols[c].panes[start..end]) |k| {
+ var spare: u16 = if (neighbor) |other| v.rects[other].h -| BOX_H else 0;
+ for (v.cols[c].panes[start..end]) |k| {
const pane = p.panes[k] orelse continue;
if (folding) {
if (pane.collapsed) continue;
- const h = p.rects[k].h;
+ const h = v.rects[k].h;
pane.vweight = @floatFromInt(@max(1, h));
pane.collapsed = true;
if (neighbor) |other| p.panes[other].?.vweight += @floatFromInt(h -| BOX_H);
@@ -1151,7 +1181,7 @@ pub fn toggleCollapse(p: *Pardes, id: usize) void {
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 old_height = p.view().rects[id].h;
const remembered_height = pane.vweight;
// Convert only visible panes to row weights. The toggle then changes one
// pair, rather than letting the column redistribute the released space.
@@ -1160,7 +1190,7 @@ pub fn toggleCollapse(p: *Pardes, id: usize) void {
if (was_collapsed) {
if (neighbor) |other| {
const wanted: u16 = @intFromFloat(@min(@as(f32, @floatFromInt(std.math.maxInt(u16))), @max(1, @round(remembered_height))));
- const give = @min(wanted -| old_height, p.rects[other].h -| BOX_H);
+ const give = @min(wanted -| old_height, p.view().rects[other].h -| BOX_H);
p.panes[other].?.vweight -= @floatFromInt(give);
pane.vweight = @floatFromInt(@max(1, old_height + give));
}
@@ -1182,20 +1212,21 @@ pub fn toggleCollapse(p: *Pardes, id: usize) void {
/// would spread over the whole column instead.
pub fn expandedNeighbor(p: *const Pardes, col: usize, index: usize, side: enum { above, below }) ?usize {
var k = index;
- while (if (side == .above) k > 0 else k + 1 < p.cols[col].n) {
+ while (if (side == .above) k > 0 else k + 1 < p.view().cols[col].n) {
k = if (side == .above) k - 1 else k + 1;
- const id = p.cols[col].panes[k];
+ const id = p.view().cols[col].panes[k];
if (p.panes[id]) |pane| if (!pane.collapsed) return id;
}
return null;
}
fn computeCollapsedColumn(p: *Pardes, c: usize, x: u16, width: u16, folded: usize) void {
+ const v = p.view();
var y: u16 = p.topBarHeight() + columnBarHeight(p);
- const available = p.screen_h -| y;
+ const available = v.screen_h -| y;
var expanded_left: usize = 0;
var weight: f32 = 0;
- for (p.cols[c].panes[0..p.cols[c].n]) |id| if (p.panes[id]) |pane| {
+ for (v.cols[c].panes[0..p.view().cols[c].n]) |id| if (p.panes[id]) |pane| {
if (!pane.collapsed) {
expanded_left += 1;
weight += pane.vweight;
@@ -1204,9 +1235,9 @@ fn computeCollapsedColumn(p: *Pardes, c: usize, x: u16, width: u16, folded: usiz
const expanded_height = available -| @as(u16, @intCast(folded));
var budget = expanded_height;
var folded_left = folded;
- for (p.cols[c].panes[0..p.cols[c].n]) |id| {
+ for (v.cols[c].panes[0..p.view().cols[c].n]) |id| {
const pane = p.panes[id] orelse continue;
- const room = p.screen_h -| y;
+ const room = v.screen_h -| y;
const height: u16 = if (pane.collapsed) blk: {
folded_left -= 1;
break :blk @min(room, BOX_H);
@@ -1218,32 +1249,33 @@ fn computeCollapsedColumn(p: *Pardes, c: usize, x: u16, width: u16, folded: usiz
budget -|= h;
break :blk h;
};
- p.rects[id] = .{ .x = x, .y = y, .w = width, .h = height };
+ v.rects[id] = .{ .x = x, .y = y, .w = width, .h = height };
y +|= height;
}
}
pub fn compute(p: *Pardes) void {
- p.rects = @splat(.{});
- if (p.ncol == 0) return;
+ const v = p.view();
+ v.rects = @splat(.{});
+ if (v.ncol == 0) return;
keepGroups(p);
- for (0..p.ncol) |column| _ = columnSerial(p, column);
+ for (0..p.view().ncol) |column| _ = columnSerial(p, column);
var wsum: u128 = 0;
- for (0..p.ncol) |c| wsum += p.cols[c].weight;
+ for (0..p.view().ncol) |c| wsum += v.cols[c].weight;
if (wsum == 0) wsum = 1;
// Round cumulative boundaries so widths still sum to the available screen.
var prefix: u128 = 0;
- for (0..p.ncol) |c| {
- const x = columnBoundary(p.screen_w, prefix, wsum);
- prefix += p.cols[c].weight;
- const end: u16 = if (c == p.ncol - 1) p.screen_w else columnBoundary(p.screen_w, prefix, wsum);
+ for (0..p.view().ncol) |c| {
+ const x = columnBoundary(v.screen_w, prefix, wsum);
+ prefix += v.cols[c].weight;
+ const end: u16 = if (c == v.ncol - 1) v.screen_w else columnBoundary(v.screen_w, prefix, wsum);
const cw = end -| x;
- p.col_x[c] = x;
- p.col_w[c] = cw;
+ v.col_x[c] = x;
+ v.col_w[c] = cw;
var folded: usize = 0;
- for (p.cols[c].panes[0..p.cols[c].n]) |id| if (p.panes[id]) |pane| {
+ for (v.cols[c].panes[0..p.view().cols[c].n]) |id| if (p.panes[id]) |pane| {
if (pane.collapsed) folded += 1;
};
if (folded > 0) {
@@ -1252,34 +1284,34 @@ pub fn compute(p: *Pardes) void {
}
var vsum: f32 = 0;
- for (0..p.cols[c].n) |k| {
- if (p.panes[p.cols[c].panes[k]]) |pane| vsum += pane.vweight;
+ for (0..p.view().cols[c].n) |k| {
+ if (p.panes[v.cols[c].panes[k]]) |pane| vsum += pane.vweight;
}
if (vsum <= 0) vsum = 1;
var y: u16 = p.topBarHeight() + columnBarHeight(p);
const top = y;
- const avail_h = p.screen_h -| y;
+ const avail_h = v.screen_h -| y;
var hs: [MAX_PANES]u16 = undefined;
- const n = p.cols[c].n;
+ const n = v.cols[c].n;
for (0..n) |k| {
- const id = p.cols[c].panes[k];
+ const id = v.cols[c].panes[k];
const pane = p.panes[id] orelse {
hs[k] = 0;
continue;
};
const lastk = k + 1 == n;
const fh = @as(f32, @floatFromInt(avail_h)) * pane.vweight / vsum;
- const room = p.screen_h -| y;
+ const room = v.screen_h -| y;
hs[k] = if (lastk) room else @min(room, @max(1, @as(u16, @intFromFloat(@round(fh)))));
y +|= hs[k];
}
keepTerminalFloors(p, c, hs[0..n]);
y = top;
for (0..n) |k| {
- const id = p.cols[c].panes[k];
+ const id = v.cols[c].panes[k];
if (p.panes[id] == null) continue;
- p.rects[id] = .{ .x = x, .y = y, .w = cw, .h = hs[k] };
+ v.rects[id] = .{ .x = x, .y = y, .w = cw, .h = hs[k] };
y +|= hs[k];
}
}
@@ -1299,13 +1331,13 @@ fn floorRows(p: *Pardes, id: usize) u16 {
// ponytail: a row per step, O(panes x rows); a column holds few of both.
fn keepTerminalFloors(p: *Pardes, c: usize, hs: []u16) void {
for (hs, 0..) |*h, k| {
- const floor = floorRows(p, p.cols[c].panes[k]);
+ const floor = floorRows(p, p.view().cols[c].panes[k]);
while (h.* < floor) {
var giver: ?usize = null;
var spare: u16 = 0;
for (hs, 0..) |other, j| {
if (j == k) continue;
- const extra = other -| floorRows(p, p.cols[c].panes[j]);
+ const extra = other -| floorRows(p, p.view().cols[c].panes[j]);
if (extra > spare) {
spare = extra;
giver = j;
@@ -1328,18 +1360,18 @@ test "collapse transfers height upward and restores the original layout" {
p.panes[0].?.vweight = 3;
p.panes[1].?.vweight = 2;
compute(p);
- const original = p.rects;
+ const original = p.view().rects;
toggleCollapse(p, 1);
- try std.testing.expectEqual(@as(u16, 1), p.rects[1].h);
+ try std.testing.expectEqual(@as(u16, 1), p.view().rects[1].h);
try std.testing.expectEqual(@as(f32, @floatFromInt(original[1].h)), p.panes[1].?.vweight);
- try std.testing.expectEqual(p.screen_h, p.rects[2].y + p.rects[2].h);
- try std.testing.expect(p.rects[0].h > original[0].h);
- try std.testing.expectEqualDeep(original[2], p.rects[2]);
+ try std.testing.expectEqual(p.view().screen_h, p.view().rects[2].y + p.view().rects[2].h);
+ try std.testing.expect(p.view().rects[0].h > original[0].h);
+ try std.testing.expectEqualDeep(original[2], p.view().rects[2]);
toggleCollapse(p, 1);
- try std.testing.expectEqualDeep(original, p.rects);
+ try std.testing.expectEqualDeep(original, p.view().rects);
toggleCollapse(p, 2);
- try std.testing.expectEqual(@as(u16, 1), p.rects[2].h);
- try std.testing.expectEqual(p.screen_h, p.rects[2].y + p.rects[2].h);
+ try std.testing.expectEqual(@as(u16, 1), p.view().rects[2].h);
+ try std.testing.expectEqual(p.view().screen_h, p.view().rects[2].y + p.view().rects[2].h);
}
test "collapse changes only one neighbor at the top middle and bottom" {
@@ -1352,16 +1384,16 @@ test "collapse changes only one neighbor at the top middle and bottom" {
}
for ([_]f32{ 11, 7, 5, 3 }, 0..) |weight, id| p.panes[id].?.vweight = weight;
compute(p);
- const original = p.rects;
+ const original = p.view().rects;
const recipient = if (target > 0) target - 1 else 1;
for (0..5) |_| {
toggleCollapse(p, target);
- try std.testing.expectEqual(@as(u16, BOX_H), p.rects[target].h);
- try std.testing.expectEqual(original[recipient].h + original[target].h - BOX_H, p.rects[recipient].h);
+ try std.testing.expectEqual(@as(u16, BOX_H), p.view().rects[target].h);
+ try std.testing.expectEqual(original[recipient].h + original[target].h - BOX_H, p.view().rects[recipient].h);
for (0..4) |id| if (id != target and id != recipient)
- try std.testing.expectEqualDeep(original[id], p.rects[id]);
+ try std.testing.expectEqualDeep(original[id], p.view().rects[id]);
toggleCollapse(p, target);
- try std.testing.expectEqualDeep(original, p.rects);
+ try std.testing.expectEqualDeep(original, p.view().rects);
}
}
}
@@ -1376,21 +1408,21 @@ test "collapse skips folded neighbors and expansion reclaims only its neighbor"
compute(p);
toggleCollapse(p, 1);
toggleCollapse(p, 2);
- const original = p.rects;
+ const original = p.view().rects;
toggleCollapse(p, 3);
- try std.testing.expectEqual(original[0].h + original[3].h - BOX_H, p.rects[0].h);
- try std.testing.expectEqualDeep(original[4], p.rects[4]);
- for (1..4) |id| try std.testing.expectEqual(@as(u16, BOX_H), p.rects[id].h);
+ try std.testing.expectEqual(original[0].h + original[3].h - BOX_H, p.view().rects[0].h);
+ try std.testing.expectEqualDeep(original[4], p.view().rects[4]);
+ for (1..4) |id| try std.testing.expectEqual(@as(u16, BOX_H), p.view().rects[id].h);
toggleCollapse(p, 3);
- try std.testing.expectEqualDeep(original, p.rects);
+ try std.testing.expectEqualDeep(original, p.view().rects);
// With no expanded pane above, give the space to the first one below.
toggleCollapse(p, 0);
- const with_folded_above = p.rects;
+ const with_folded_above = p.view().rects;
toggleCollapse(p, 3);
- try std.testing.expectEqual(with_folded_above[4].h + with_folded_above[3].h - BOX_H, p.rects[4].h);
- for (0..3) |id| try std.testing.expectEqualDeep(with_folded_above[id], p.rects[id]);
+ try std.testing.expectEqual(with_folded_above[4].h + with_folded_above[3].h - BOX_H, p.view().rects[4].h);
+ for (0..3) |id| try std.testing.expectEqualDeep(with_folded_above[id], p.view().rects[id]);
toggleCollapse(p, 3);
- try std.testing.expectEqualDeep(with_folded_above, p.rects);
+ try std.testing.expectEqualDeep(with_folded_above, p.view().rects);
}
test "collapse local transfers remain bounded through repeated tiny window resizes" {
@@ -1401,19 +1433,19 @@ test "collapse local transfers remain bounded through repeated tiny window resiz
insert(p, 0, id, id);
}
for (0..180) |step| {
- p.screen_h = @intCast((step * 17) % 81);
+ p.view().screen_h = @intCast((step * 17) % 81);
compute(p);
toggleCollapse(p, step % 5);
var used: u16 = 0;
for (0..5) |id| {
const pane = p.panes[id].?;
- const rect = p.rects[id];
+ const rect = p.view().rects[id];
try std.testing.expect(std.math.isFinite(pane.vweight) and pane.vweight > 0);
- try std.testing.expect(rect.h <= p.screen_h -| rect.y);
+ try std.testing.expect(rect.h <= p.view().screen_h -| rect.y);
if (pane.collapsed) try std.testing.expect(rect.h <= BOX_H);
used += rect.h;
}
- try std.testing.expect(used <= p.screen_h);
+ try std.testing.expect(used <= p.view().screen_h);
}
}
@@ -1423,7 +1455,7 @@ test "collapse expansion is limited to space available in one neighbor" {
// Text panes: a terminal would keep its tag and two rows (floorRows).
for (1..4) |id| {
p.newScratchBelow(0);
- try std.testing.expectEqual(id, p.cols[0].panes[id]);
+ try std.testing.expectEqual(id, p.view().cols[0].panes[id]);
}
compute(p);
toggleCollapse(p, 2);
@@ -1432,28 +1464,28 @@ test "collapse expansion is limited to space available in one neighbor" {
p.panes[0].?.vweight += p.panes[1].?.vweight - 2;
p.panes[1].?.vweight = 2;
compute(p);
- const before = p.rects;
+ const before = p.view().rects;
toggleCollapse(p, 2);
- try std.testing.expectEqual(@as(u16, 2), p.rects[2].h);
- try std.testing.expectEqual(@as(u16, BOX_H), p.rects[1].h);
- try std.testing.expectEqualDeep(before[0], p.rects[0]);
- try std.testing.expectEqualDeep(before[3], p.rects[3]);
+ try std.testing.expectEqual(@as(u16, 2), p.view().rects[2].h);
+ try std.testing.expectEqual(@as(u16, BOX_H), p.view().rects[1].h);
+ try std.testing.expectEqualDeep(before[0], p.view().rects[0]);
+ try std.testing.expectEqualDeep(before[3], p.view().rects[3]);
}
test "collapse single and all panes leave unused column space" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 32 });
defer p.deinit();
toggleCollapse(p, 0);
- try std.testing.expectEqual(@as(u16, 1), p.rects[0].h);
+ try std.testing.expectEqual(@as(u16, 1), p.view().rects[0].h);
_ = try p.newShell(1, "");
insert(p, 0, 1, 1);
toggleCollapse(p, 1);
- try std.testing.expectEqual(@as(u16, 1), p.rects[0].h);
- try std.testing.expectEqual(@as(u16, 1), p.rects[1].h);
- try std.testing.expectEqual(p.rects[0].y + 1, p.rects[1].y);
- try std.testing.expect(p.rects[1].y + 1 < p.screen_h);
+ try std.testing.expectEqual(@as(u16, 1), p.view().rects[0].h);
+ try std.testing.expectEqual(@as(u16, 1), p.view().rects[1].h);
+ try std.testing.expectEqual(p.view().rects[0].y + 1, p.view().rects[1].y);
+ try std.testing.expect(p.view().rects[1].y + 1 < p.view().screen_h);
toggleCollapse(p, 0);
- try std.testing.expectEqual(p.screen_h - 1, p.rects[1].y);
+ try std.testing.expectEqual(p.view().screen_h - 1, p.view().rects[1].y);
}
test "collapse survives tiny resizes" {
@@ -1465,20 +1497,20 @@ test "collapse survives tiny resizes" {
insert(p, 0, 2, 2);
p.panes[1].?.collapsed = true;
for (0..50) |height| {
- p.screen_h = @intCast(height);
+ p.view().screen_h = @intCast(height);
compute(p);
var used: u16 = 0;
- for (p.cols[0].panes[0..p.cols[0].n]) |id| {
- const rect = p.rects[id];
- try std.testing.expect(rect.h <= p.screen_h -| rect.y);
+ for (p.view().cols[0].panes[0..p.view().cols[0].n]) |id| {
+ const rect = p.view().rects[id];
+ try std.testing.expect(rect.h <= p.view().screen_h -| rect.y);
used += rect.h;
}
- try std.testing.expect(used <= p.screen_h);
- if (height >= 5) try std.testing.expectEqual(@as(u16, 1), p.rects[1].h);
+ try std.testing.expect(used <= p.view().screen_h);
+ if (height >= 5) try std.testing.expectEqual(@as(u16, 1), p.view().rects[1].h);
}
- p.screen_h = 32;
+ p.view().screen_h = 32;
compute(p);
- try std.testing.expectEqual(@as(u16, 1), p.rects[1].h);
+ try std.testing.expectEqual(@as(u16, 1), p.view().rects[1].h);
}
test "collapse can be expanded by dragging its boundary" {
@@ -1487,15 +1519,15 @@ test "collapse can be expanded by dragging its boundary" {
_ = try p.newShell(1, "");
insert(p, 0, 1, 1);
toggleCollapse(p, 0);
- applyRowSplit(p, 0, 0, p.rects[0].y + 5);
+ applyRowSplit(p, 0, 0, p.view().rects[0].y + 5);
compute(p);
try std.testing.expect(!p.panes[0].?.collapsed);
- try std.testing.expectEqual(@as(u16, 6), p.rects[0].h);
+ try std.testing.expectEqual(@as(u16, 6), p.view().rects[0].h);
toggleCollapse(p, 1);
- applyRowSplit(p, 0, 0, p.rects[1].y - 6);
+ applyRowSplit(p, 0, 0, p.view().rects[1].y - 6);
compute(p);
try std.testing.expect(!p.panes[1].?.collapsed);
- try std.testing.expectEqual(@as(u16, 6), p.rects[1].h);
+ try std.testing.expectEqual(@as(u16, 6), p.view().rects[1].h);
}
test "collapse dump restores folded state and retained expansion weight" {
@@ -1503,47 +1535,47 @@ test "collapse dump restores folded state and retained expansion weight" {
defer p.deinit();
p.panes[0].?.vweight = 7;
compute(p);
- const original_height = p.rects[0].h;
+ const original_height = p.view().rects[0].h;
toggleCollapse(p, 0);
try dump.dumpState(p);
const restored = try dump.initFromDump(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 32 }, p.dump_out.?);
defer restored.deinit();
try std.testing.expect(restored.panes[0].?.collapsed);
try std.testing.expectEqual(@as(f32, @floatFromInt(original_height)), restored.panes[0].?.vweight);
- try std.testing.expectEqual(@as(u16, 1), restored.rects[0].h);
+ try std.testing.expectEqual(@as(u16, 1), restored.view().rects[0].h);
toggleCollapse(restored, 0);
- try std.testing.expectEqual(restored.screen_h - restored.rects[0].y, restored.rects[0].h);
+ try std.testing.expectEqual(restored.view().screen_h - restored.view().rects[0].y, restored.view().rects[0].h);
}
test "column reorder moves only crossed state and preserves weights" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 173, .rows = 20 });
defer p.deinit();
- p.ncol = 4;
+ p.view().ncol = 4;
for (0..4) |c| {
- p.cols[c].n = 0;
- p.cols[c].weight = c * 13 + 7;
- p.cols[c].serial = @intCast(c + 10);
- p.cols[c].panes[0] = c;
- p.col_tags[c].cur_col = @intCast(c + 20);
+ p.view().cols[c].n = 0;
+ p.view().cols[c].weight = c * 13 + 7;
+ p.view().cols[c].serial = @intCast(c + 10);
+ p.view().cols[c].panes[0] = c;
+ p.view().col_tags[c].cur_col = @intCast(c + 20);
}
compute(p);
const original_weights = weightsOf(p);
- const outer_x = p.col_x[0];
- const outer_w = p.col_w[0];
- p.header = .{ .column = 1 };
- p.header_scroll = 9;
+ const outer_x = p.view().col_x[0];
+ const outer_w = p.view().col_w[0];
+ p.view().header = .{ .column = 1 };
+ p.view().header_scroll = 9;
reorderColumn(p, 1, 3);
- try std.testing.expectEqual(original_weights[1], p.cols[3].weight);
- try std.testing.expectEqual(original_weights[2], p.cols[1].weight);
- try std.testing.expectEqual(@as(u32, 11), p.cols[3].serial);
+ try std.testing.expectEqual(original_weights[1], p.view().cols[3].weight);
+ try std.testing.expectEqual(original_weights[2], p.view().cols[1].weight);
+ try std.testing.expectEqual(@as(u32, 11), p.view().cols[3].serial);
try std.testing.expectEqual(@as(?usize, 3), p.headerColumn());
try std.testing.expectEqual(@as(i32, 21), tagline.focusedHeader(p).?.cur_col);
- try std.testing.expectEqual(@as(usize, 9), p.header_scroll);
- try std.testing.expectEqual(outer_x, p.col_x[0]);
- try std.testing.expectEqual(outer_w, p.col_w[0]);
+ try std.testing.expectEqual(@as(usize, 9), p.view().header_scroll);
+ try std.testing.expectEqual(outer_x, p.view().col_x[0]);
+ try std.testing.expectEqual(outer_w, p.view().col_w[0]);
reorderColumn(p, 3, 1);
try std.testing.expectEqualSlices(u64, original_weights[0..4], weightsOf(p)[0..4]);
- try std.testing.expectEqual(@as(i32, 21), p.col_tags[1].cur_col);
+ 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.
@@ -1560,31 +1592,31 @@ test "an attachment takes its rows from its owner alone, keeps them, and moves,
_ = try p.setTestFile("alpha one\nbeta\nalphabet\ngamma\nalpha two\ndelta\nepsilon\nzeta\n");
p.newScratchBelow(0);
p.newScratchBelow(0);
- const other_col = p.cols[0].panes[2];
+ const other_col = p.view().cols[0].panes[2];
try std.testing.expect(splitColumn(p, other_col, other_col, false));
- const below = p.cols[0].panes[1];
- p.active = 0;
+ const below = p.view().cols[0].panes[1];
+ p.view().active = 0;
compute(p);
- const before = p.rects;
+ const before = p.view().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(attachFrom(p, 0, s, 9, 0));
try std.testing.expectEqual(@as(?usize, 0), ownerOf(p, s));
- try std.testing.expectEqual(s, p.cols[0].panes[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);
+ try std.testing.expectEqual(s, p.view().cols[0].panes[1]);
+ for (unrelated) |k| try std.testing.expectEqualDeep(before[k], p.view().rects[k]);
+ try std.testing.expectEqual(before[0].h, p.view().rects[0].h + p.view().rects[s].h);
+ try std.testing.expectEqual(before[0].y, p.view().rects[0].y);
+ try std.testing.expectEqual(@as(u16, 9), p.view().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(attachFrom(p, s, pv, 4, s));
- try std.testing.expectEqual(pv, p.cols[0].panes[2]);
+ try std.testing.expectEqual(pv, p.view().cols[0].panes[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);
+ for (unrelated) |k| try std.testing.expectEqualDeep(before[k], p.view().rects[k]);
+ try std.testing.expectEqual(@as(u16, 5), p.view().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');
@@ -1592,55 +1624,55 @@ test "an attachment takes its rows from its owner alone, keeps them, and moves,
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);
+ try std.testing.expectEqual(@as(u16, 5), p.view().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.cols[at.col].panes[at.idx + 1]);
- try std.testing.expectEqual(pv, p.cols[at.col].panes[at.idx + 2]);
+ try std.testing.expectEqual(s, p.view().cols[at.col].panes[at.idx + 1]);
+ try std.testing.expectEqual(pv, p.view().cols[at.col].panes[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);
+ const pre_drag = p.view().rects;
+ applyRowSplit(p, at.col, at.idx, p.view().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.cols[at.col].panes[0..p.cols[at.col].n]) |k| if (k != 0 and k != s) try std.testing.expectEqualDeep(pre_drag[k], p.rects[k]);
+ try std.testing.expectEqual(pre_drag[0].h + pre_drag[s].h, p.view().rects[0].h + p.view().rects[s].h);
+ for (p.view().cols[at.col].panes[0..p.view().cols[at.col].n]) |k| if (k != 0 and k != s) try std.testing.expectEqualDeep(pre_drag[k], p.view().rects[k]);
// Collapse folds the group and unfolds it.
compute(p);
- const open = p.rects;
+ const open = p.view().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);
+ try std.testing.expectEqual(open[0].h + open[s].h + open[pv].h, p.view().rects[0].h + p.view().rects[s].h + p.view().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);
+ movePane(p, 0, p.view().col_x[1] + 4, p.view().rects[other_col].y + p.view().rects[other_col].h / 2);
at = findPane(p, 0).?;
try std.testing.expectEqual(@as(usize, 1), at.col);
- try std.testing.expectEqual(s, p.cols[1].panes[at.idx + 1]);
- try std.testing.expectEqual(pv, p.cols[1].panes[at.idx + 2]);
+ try std.testing.expectEqual(s, p.view().cols[1].panes[at.idx + 1]);
+ try std.testing.expectEqual(pv, p.view().cols[1].panes[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;
+ var pair = p.view().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);
+ try std.testing.expectEqual(pair[s].h + pair[pv].h, p.view().rects[s].h);
// Del of the attachment gives its rows back to the owner, nobody else.
- pair = p.rects;
+ pair = p.view().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.cols[1].panes[0..p.cols[1].n]) |k| if (k != 0) try std.testing.expectEqualDeep(pair[k], p.rects[k]);
+ try std.testing.expectEqual(pair[0].h + pair[s].h, p.view().rects[0].h);
+ for (p.view().cols[1].panes[0..p.view().cols[1].n]) |k| if (k != 0) try std.testing.expectEqualDeep(pair[k], p.view().rects[k]);
// Del of the owner closes what is attached to it.
const again = try testAttachment(p);
@@ -1688,7 +1720,7 @@ pub fn topBarHeight(p: *const Pardes) u16 {
/// 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;
+ if (p.view().screen_h < 3) return 0;
return COLBAR_H;
}
@@ -1701,14 +1733,14 @@ pub fn newColumn(p: *Pardes, from_id: usize) void {
// 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);
+ for (0..p.view().ncol) |k| _ = columnSerial(p, k);
const unspent = p.next_column_serial;
const before_min = minima(p);
- const before = p.rects;
+ const before = p.view().rects;
// Said, not silently nothing: "no space" is 9ns's word for ENOSPC.
const c = insertColumnUnannounced(p, col, false) orelse {
var said: [96]u8 = undefined;
- return p.reportFailure(from_id, if (p.ncol >= MAX_COLS)
+ return p.reportFailure(from_id, if (p.view().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");
@@ -1724,18 +1756,18 @@ pub fn newColumn(p: *Pardes, from_id: usize) void {
}
pub fn setActiveColumn(p: *Pardes, c: usize) void {
- if (c < p.ncol) p.active_column = columnSerial(p, c);
+ 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.active_column);
+ return columnBySerial(p, p.view().active_column);
}
pub fn noteTyped(p: *Pardes) void {
- if (p.header != null) {
+ if (p.view().header != null) {
if (p.headerColumn()) |c| p.setActiveColumn(c);
- } else if (findPane(p, p.active)) |f| p.setActiveColumn(f.col);
+ } else if (findPane(p, p.view().active)) |f| p.setActiveColumn(f.col);
}
/// New, run from column `c`'s tag.
@@ -1755,14 +1787,15 @@ pub fn keyboardColumn(p: *Pardes, id: usize) ?usize {
/// 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 != null) null else p.rects[p.active]) else null;
+ 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 = p.cols[col].n;
+ const count = v.cols[col].n;
for (0..count) |i| {
- ids[i] = p.cols[col].panes[i];
+ ids[i] = v.cols[col].panes[i];
parents[i] = p.panes[ids[i]].?;
}
try panes.detachCwds(p, parents[0..count]);
@@ -1772,25 +1805,27 @@ pub fn removeColumn(p: *Pardes, col: usize) !void {
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[p.active] == null) p.active = beside orelse panes.anyPane(p) orelse return tagline.enterHeader(p, null);
+ 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 {
- if (p.ncol < 2) return null;
+ 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 p.screen_h / 2;
+ const mid = if (r) |at| at.y + at.h / 2 else v.screen_h / 2;
var pick: ?usize = null;
- for (p.cols[next].panes[0..p.cols[next].n]) |k| {
+ for (v.cols[next].panes[0..p.view().cols[next].n]) |k| {
if (p.panes[k] == null) continue;
pick = k;
- if (p.rects[k].y +| p.rects[k].h > mid) break;
+ if (v.rects[k].y +| v.rects[k].h > mid) break;
}
return pick;
}
@@ -1800,7 +1835,7 @@ pub fn besideColumn(p: *const Pardes, col: usize, r: ?Rect) ?usize {
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
+ 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();
@@ -1823,7 +1858,7 @@ pub fn tagTop(_: *const Pardes, _: *const Pane, r: Rect) u16 {
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);
+ return layer.capacity(p.view().row_metrics);
}
// ---- render: build the canonical surface (draw.zig) ----
@@ -1836,7 +1871,7 @@ pub fn reprojectTagHit(p: *const Pardes, tag_hit: TagHit) ?TagHit {
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.cols[tag_hit.id].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);
}
@@ -1850,23 +1885,23 @@ test "a new column takes width only from the column that created it" {
try std.testing.expect(splitColumn(p, 0, 2, true));
p.sync();
- const left_before = p.rects[2];
- const right_before = p.rects[1];
+ const left_before = p.view().rects[2];
+ const right_before = p.view().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);
+ try std.testing.expectEqual(left_before.x, p.view().rects[2].x);
+ try std.testing.expectEqual(left_before.w, p.view().rects[2].w);
+ try std.testing.expectEqual(right_before.x, p.view().rects[1].x);
+ try std.testing.expectEqual(right_before.w, p.view().rects[1].w);
+ try std.testing.expectEqual(@as(u16, 16), p.view().rects[0].w);
+ try std.testing.expectEqual(@as(u16, 15), p.view().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);
+ try std.testing.expectEqual(@as(usize, 1), narrow.view().ncol);
const sequential = try Pardes.init(std.testing.allocator, .{ .cols = config.MINW * 2, .rows = 10 });
defer sequential.deinit();
@@ -1874,16 +1909,16 @@ test "a new column takes width only from the column that created it" {
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);
+ try std.testing.expectEqual(@as(usize, 2), sequential.view().ncol);
const coarse = try Pardes.init(std.testing.allocator, .{ .cols = 22, .rows = 10 });
defer coarse.deinit();
- coarse.cols[0].weight = 3;
+ coarse.view().cols[0].weight = 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]);
+ try std.testing.expectEqual(@as(u16, 11), coarse.view().col_w[0]);
+ try std.testing.expectEqual(@as(u16, 11), coarse.view().col_w[1]);
const extreme = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 20 });
defer extreme.deinit();
@@ -1891,14 +1926,14 @@ test "a new column takes width only from the column that created it" {
try std.testing.expect(splitColumn(extreme, 0, 1, false));
_ = try extreme.newShell(2, "");
insert(extreme, 0, 1, 2);
- extreme.cols[0].weight = std.math.maxInt(u64) / 2 + 2; // odd and cannot double
- extreme.cols[1].weight = 1;
+ extreme.view().cols[0].weight = std.math.maxInt(u64) / 2 + 2; // odd and cannot double
+ extreme.view().cols[1].weight = 1;
compute(extreme);
- const cols_before = extreme.cols;
+ const cols_before = extreme.view().cols;
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(cols_before, extreme.cols);
+ try std.testing.expectEqual(cols_before, extreme.view().cols);
try std.testing.expectEqual(source_vweight, extreme.panes[0].?.vweight);
try std.testing.expectEqual(sibling_vweight, extreme.panes[2].?.vweight);
}
@@ -1912,8 +1947,8 @@ test "Newcol refuses an unsplittable restored weight before spawning" {
try std.testing.expect(splitColumn(p, id - 1, id, false));
}
const restored_cap = std.math.maxInt(u64) / 6;
- for (0..4) |column| p.cols[column].weight = restored_cap;
- p.cols[3].weight -= 1;
+ for (0..4) |column| p.view().cols[column].weight = restored_cap;
+ p.view().cols[3].weight -= 1;
for ([_]usize{ 3, 2, 1 }) |id| {
const pane = p.panes[id].?;
try panes.deinitPane(p, pane);
@@ -1923,8 +1958,8 @@ test "Newcol refuses an unsplittable restored weight before spawning" {
}
while (p.nextEffect()) |_| {}
- try std.testing.expect(p.cols[0].weight > std.math.maxInt(u64) / 2);
- try std.testing.expect(p.cols[0].weight % 2 == 1);
+ try std.testing.expect(p.view().cols[0].weight > std.math.maxInt(u64) / 2);
+ try std.testing.expect(p.view().cols[0].weight % 2 == 1);
try std.testing.expect(!canSplitCol(p, findPane(p, 0).?.col));
const panes_before = p.panes;
const serial_before = p.next_serial;
@@ -1942,7 +1977,7 @@ test "repeated non-dyadic column splits preserve every unrelated boundary" {
try std.testing.expect(splitColumn(p, 0, 1, false));
p.sync();
- const handle = p.col_x[0] + p.col_w[0] - 1;
+ const handle = p.view().col_x[0] + p.view().col_w[0] - 1;
const untouched = weightsOf(p);
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 } });
@@ -1952,19 +1987,19 @@ test "repeated non-dyadic column splits preserve every unrelated boundary" {
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 std.testing.expectEqual(@as(u16, 418), p.view().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];
+ const left_before = p.view().rects[0];
+ const middle_before = p.view().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]);
+ try std.testing.expectEqual(left_before, p.view().rects[0]);
+ try std.testing.expectEqual(middle_before, p.view().rects[1]);
}
test "dragging a column's only pane to another leaves that column in place, empty" {
@@ -1972,37 +2007,37 @@ test "dragging a column's only pane to another leaves that column in place, empt
defer p.deinit();
newColumn(p, 0);
newScratchIn(p, 0, activeColumn(p).?);
- const moved = p.active;
+ const moved = p.view().active;
p.sync();
const weights = weightsOf(p);
- const tag_serial = p.cols[1].serial;
- movePane(p, moved, p.col_x[0] + 4, p.rects[0].y + p.rects[0].h / 2);
+ const tag_serial = p.view().cols[1].serial;
+ movePane(p, moved, p.view().col_x[0] + 4, p.view().rects[0].y + p.view().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.cols[1].n);
- try std.testing.expectEqual(tag_serial, p.cols[1].serial);
+ try std.testing.expectEqual(@as(usize, 2), p.view().ncol);
+ try std.testing.expectEqual(@as(usize, 0), p.view().cols[1].n);
+ try std.testing.expectEqual(tag_serial, p.view().cols[1].serial);
try std.testing.expectEqualDeep(weights, weightsOf(p));
- try std.testing.expectEqual(@as(usize, 2), p.cols[0].n);
+ try std.testing.expectEqual(@as(usize, 2), p.view().cols[0].n);
// ...and a pane dragged into the empty column takes it whole.
- movePane(p, moved, p.col_x[1] + 4, p.screen_h / 2);
+ movePane(p, moved, p.view().col_x[1] + 4, p.view().screen_h / 2);
p.sync();
- try std.testing.expectEqualSlices(usize, &.{moved}, p.cols[1].panes[0..p.cols[1].n]);
- try std.testing.expectEqual(@as(usize, 1), p.cols[0].n);
+ try std.testing.expectEqualSlices(usize, &.{moved}, p.view().cols[1].panes[0..p.view().cols[1].n]);
+ try std.testing.expectEqual(@as(usize, 1), p.view().cols[0].n);
}
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 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.cols) |*w, c| w.* = c.weight;
+ for (&out, p.view().cols) |*w, c| w.* = c.weight;
return out;
}