diff options
| -rw-r--r-- | src/Layer.zig | 3 | ||||
| -rw-r--r-- | src/builtins.zig | 10 | ||||
| -rw-r--r-- | src/draw.zig | 20 | ||||
| -rw-r--r-- | src/exec.zig | 2 | ||||
| -rw-r--r-- | src/gui/gui.zig | 2 | ||||
| -rw-r--r-- | src/keys.zig | 2 | ||||
| -rw-r--r-- | src/layout.zig | 360 | ||||
| -rw-r--r-- | src/mouse.zig | 12 | ||||
| -rw-r--r-- | src/ninep/cols.zig | 18 | ||||
| -rw-r--r-- | src/ninep/ctl.zig | 2 | ||||
| -rw-r--r-- | src/panes.zig | 6 | ||||
| -rw-r--r-- | src/pardes.zig | 359 | ||||
| -rw-r--r-- | src/tagline.zig | 9 |
13 files changed, 411 insertions, 394 deletions
diff --git a/src/Layer.zig b/src/Layer.zig index 3522fce8..66f8b9c0 100644 --- a/src/Layer.zig +++ b/src/Layer.zig @@ -12,6 +12,7 @@ const std = @import("std"); const pardes = @import("pardes.zig"); const config = @import("config.zig"); const limits = @import("memory.zig").limits; +const layout = @import("layout.zig"); const Layer = @This(); @@ -257,7 +258,7 @@ pub fn columnAt(p: *const pardes.Pardes, kind: Kind, id: usize, supplied: ?TagHi point.pixel_x = std.math.clamp(point.pixel_x, left, @max(left, right - 0.001)); point.pixel_y = (@as(f32, @floatFromInt(v.y + line)) + 0.5) * bh; } - return if (p.reprojectTagHit(point)) |mapped| mapped.col else null; + return if (layout.reprojectTagHit(p, point)) |mapped| mapped.col else null; } /// A tag caret `w` wide at glyph `x`, its left and right edges in the unit diff --git a/src/builtins.zig b/src/builtins.zig index 0ad7d998..8a2c2f5f 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1264,7 +1264,7 @@ pub const New = struct { pub const Newcol = struct { pub const scope: Scope = .session; pub fn run(c: Ctx) void { - c.p.newColumn(c.id); + layout.newColumn(c.p, c.id); } }; @@ -1413,7 +1413,7 @@ pub const Delcol = struct { // acme's Delcol refuses a column with unsaved text (exec.c delcol, // cols.c colclean), once: the next Delcol closes it. if (warnModifiedIn(c, .Delcol, .{ .column = col })) return; - c.p.removeColumn(col) catch |err| c.p.reportError(c.id, "close column", err); + layout.removeColumn(c.p, col) catch |err| c.p.reportError(c.id, "close column", err); } }; @@ -1536,7 +1536,7 @@ pub const Joincol = struct { pub const scope: Scope = .session; pub fn run(c: Ctx) void { // The keyboard's column joins the one to its right; the last has none. - const col = c.p.keyboardColumn(c.p.active) orelse return; + const col = layout.keyboardColumn(c.p, c.p.active) orelse return; if (col + 1 >= c.p.ncol) return c.p.reportFailure(c.id, "Joincol: no column to the right"); // As Newcol is refused: a column that could not give each pane its // tag and 2 rows would leave a layout `size` refuses as it is. @@ -1757,7 +1757,7 @@ pub const Down = struct { pub fn run(c: Ctx) void { if (c.p.header_focus) { const column = layout.findPane(c.p, c.id); - if (c.p.header_column == null and c.p.columnBarHeight() > 0) + if (c.p.header_column == null and layout.columnBarHeight(c.p) > 0) return tagline.enterHeader(c.p, if (column) |at| at.col else 0); // Onto the top pane of the column the header stands over. const col = c.p.header_column orelse if (column) |at| at.col else return; @@ -1779,7 +1779,7 @@ pub const Up = struct { return; } if (layout.focusDir(c.p, c.id, .up)) return; - if (c.p.columnBarHeight() > 0) { + if (layout.columnBarHeight(c.p) > 0) { if (layout.findPane(c.p, c.id)) |at| tagline.enterHeader(c.p, at.col); } else if (c.p.topBarHeight() > 0) tagline.enterHeader(c.p, null); } diff --git a/src/draw.zig b/src/draw.zig index 75d6378b..910f4fef 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -105,7 +105,7 @@ pub fn place(p: *Pardes, s: *Surface) void { s.addRegion(.{ .kind = .notice, .owner = owner, .serial = pane.serial, .active = active, .notice = @intCast(i), .rect = .{ .x = tx + tw - chip, .y = body_y + @as(u16, pane.notices.row[i]), .w = chip, .h = BOX_H } }); } } - const column_rows = p.columnBarHeight(); + const column_rows = layout.columnBarHeight(p); if (column_rows > 0) for (0..p.ncol) |c| { if (p.col_w[c] == 0) continue; const active = if (layout.findPane(p, p.active)) |found| found.col == c else false; @@ -221,7 +221,7 @@ pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { // Separators have their own role; a scrollbar need not frame the page. const whole = s.regions[0].rect; s.fill(whole.x, whole.y, whole.w, whole.h, .{ .bg = .{ .rgb = chrome.border } }); - for (0..p.ncol) |c| if (p.col_n[c] == 0) s.fill(p.col_x[c], p.topBarHeight() + p.columnBarHeight(), p.col_w[c], s.rows -| (p.topBarHeight() + p.columnBarHeight()), .{ .bg = .{ .rgb = chrome.empty_col } }); + for (0..p.ncol) |c| if (p.col_n[c] == 0) s.fill(p.col_x[c], p.topBarHeight() + layout.columnBarHeight(p), p.col_w[c], s.rows -| (p.topBarHeight() + layout.columnBarHeight(p)), .{ .bg = .{ .rgb = chrome.empty_col } }); for (&p.panes, 0..) |*slot, id| { const pane = slot.* orelse continue; @@ -481,7 +481,7 @@ fn paintHeaders(p: *Pardes, s: *Surface) !void { try renderHeaderLayer(p, layer, .workspace, 0, viewport, tagline.topbar(p), top, chrome.tag_bg, chrome.tag_fg); joinHeaderRows(s, layer, chrome.tag_fg); } - const column_rows = p.columnBarHeight(); + const column_rows = layout.columnBarHeight(p); if (column_rows > 0) for (0..p.ncol) |c| { const region = s.region(.column_tag, c) orelse continue; const active = if (layout.findPane(p, p.active)) |found| found.col == c and p.settings.focus_tint else false; @@ -582,7 +582,7 @@ fn renderHeaderLayer(p: *Pardes, layer: *pardes.Layer, kind: TagKind, id: u16, v } var widest: usize = 0; for (0..viewport.h) |line| widest = @max(widest, panes.File.displayWidth(modal.lineSlice(text, line + top))); - const columns: u16 = @intCast(@min(std.math.maxInt(u16), @max(p.tagCapacity(viewport.w), widest + 1))); + const columns: u16 = @intCast(@min(std.math.maxInt(u16), @max(layout.tagCapacity(p, viewport.w), widest + 1))); const count = @as(usize, columns) * viewport.h; if (layer.cells.len != count) layer.cells = try p.gpa.realloc(layer.cells, count); layer.kind = kind; @@ -673,7 +673,7 @@ fn renderHeaderLayer(p: *Pardes, layer: *pardes.Layer, kind: TagKind, id: u16, v /// A pane's tag rows, painted once into their layer, a row to a line, and /// copied into the grid; the grid's caret where the layer has it. fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool) !void { - const capacity = p.tagCapacity(r.w -| TAG_TEXT_INSET); + const capacity = layout.tagCapacity(p, r.w -| TAG_TEXT_INSET); // Too narrow for a layer: only the grid's gap cell. if (r.w <= TAG_TEXT_INSET) return paintPaneTag(p, s, arena, pane, r, id, active); const chrome = p.chromeTheme(); @@ -758,7 +758,7 @@ fn joinNotice(p: *Pardes, s: *Surface, arena: std.mem.Allocator, region: Region) // flush right would put it past the pane and off the grid. Wider than // the chip, the text loses its head: the end is the part that says // something. - const capacity = p.tagCapacity(chip); + const capacity = layout.tagCapacity(p, chip); const room = capacity -| 1; const shown = panes.File.displayWidth(text); var kept = text; @@ -1067,8 +1067,8 @@ test "place puts every part where the painters and layers put it, in every layou const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 30 }); defer p.deinit(); _ = try p.setTestFile(std.mem.asBytes(&@as([80][7]u8, @splat("placed\n".*)))); - p.newColumn(0); - p.newScratchIn(0, p.activeColumn().?); + layout.newColumn(p, 0); + layout.newScratchIn(p, 0, layout.activeColumn(p).?); p.newScratchBelow(0); p.sync(); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); @@ -1129,11 +1129,11 @@ test "place puts every part where the painters and layers put it, in every layou } for (0..p.ncol) |c| { const region = s.region(.column_tag, c); - if (p.columnBarHeight() == 0) { + if (layout.columnBarHeight(p) == 0) { try std.testing.expect(region == null); continue; } - try std.testing.expectEqual(Rect{ .x = p.col_x[c] + TAG_TEXT_INSET, .y = p.topBarHeight(), .w = p.col_w[c] - TAG_TEXT_INSET, .h = p.columnBarHeight() }, region.?.rect); + try std.testing.expectEqual(Rect{ .x = p.col_x[c] + TAG_TEXT_INSET, .y = p.topBarHeight(), .w = p.col_w[c] - TAG_TEXT_INSET, .h = layout.columnBarHeight(p) }, region.?.rect); } } } diff --git a/src/exec.zig b/src/exec.zig index 6ed37624..11d003b5 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -1205,7 +1205,7 @@ pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPan /// else the asking pane's. fn acmeColumn(p: *Pardes, from: usize) usize { if (p.exec_column) |c| return c; - if (p.activeColumn()) |c| return c; + if (layout.activeColumn(p)) |c| return c; if (layout.findPane(p, p.active)) |f| return f.col; return columnOf(p, from); } diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 1bba29f8..7e60f0dc 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -8016,7 +8016,7 @@ test "an empty column is ruled on both sides, as between two filled ones" { _ = try core.newShell(1, ""); try std.testing.expect(pardes.layout.splitColumn(core, 0, 1, false)); // Newcol from the left pane: an empty column between the two filled ones. - core.newColumn(0); + pardes.layout.newColumn(core, 0); core.sync(); try std.testing.expectEqual(@as(usize, 3), core.ncol); try std.testing.expectEqual(@as(usize, 0), core.col_n[1]); diff --git a/src/keys.zig b/src/keys.zig index ad7d8e83..9910e2f6 100644 --- a/src/keys.zig +++ b/src/keys.zig @@ -168,7 +168,7 @@ pub fn handleKey(p: *Pardes, key: Key) void { } if (p.window_pick != null) return layout.windowKey(p, key); const pane = p.panes[p.active] orelse return; - p.noteTyped(); + layout.noteTyped(p); // In a raw terminal the two Escs swap unless ProgramEsc is on: Esc // backs out, and Shift-Esc is the program's, sent as a plain Esc. const raw_esc = key.cp == Key.escape and !key.ctrl and !key.alt and !p.settings.program_esc and diff --git a/src/layout.zig b/src/layout.zig index 4a4582b3..bcee3fba 100644 --- a/src/layout.zig +++ b/src/layout.zig @@ -5,6 +5,11 @@ const dump = @import("dump.zig"); const config = @import("config.zig"); const panes = @import("panes.zig"); const Pardes = pardes.Pardes; +const Layer = @import("Layer.zig"); +const TagHit = Layer.TagHit; +const TOPBAR_H = pardes.TOPBAR_H; +const COLBAR_H = pardes.COLBAR_H; +const MAX_TAG_ROWS = pardes.MAX_TAG_ROWS; const Pane = pardes.Pane; pub const Rect = struct { x: u16 = 0, y: u16 = 0, w: u16 = 0, h: u16 = 0 }; const MAX_COLS = pardes.MAX_COLS; @@ -277,7 +282,7 @@ pub fn movePlacement(p: *Pardes, id: usize, cur_x: u16, cur_y: u16) ?MovePlaceme const sib = if (src.idx > 0) p.col_panes[src.col][src.idx - 1] else p.col_panes[src.col][span_end]; heights[sib] +|= span_h; } - var y: u16 = p.topBarHeight() + p.columnBarHeight(); + var y: u16 = p.topBarHeight() + columnBarHeight(p); if (p.col_n[tc] == 0) return .{ .preview_col = tc, .above_id = null, .row = y, .above_y = y, .above_h = 0 }; var last: ?MovePlacement = null; var k: usize = 0; @@ -572,12 +577,12 @@ pub fn acmePlace(p: *Pardes, c: usize, origin: ?usize, id: usize) void { for (p.col_panes[c][0..n]) |k| if (!p.panes[k].?.collapsed and ownerOf(p, k) == null) { last = k; }; - break :y bodyTop(p, last) + bodyRows(p, last) / 2; + break :y bodyFrom(p, last) + bodyRows(p, last) / 2; }; const e = empty.?; const el = blankRows(p, e); if (el > 15 or (el > 3 and el > (bodyRows(p, v0) -| 1) / 2)) - break :y bodyTop(p, e) + bodyRows(p, e) - el; + break :y bodyFrom(p, e) + bodyRows(p, e) - el; // Asked from a results pane, it is its owner that is split. const root = groupRoot(p, o); const mine = if (findPane(p, root)) |f| f.col == c and !p.panes[root].?.collapsed else false; @@ -594,7 +599,7 @@ pub const min_body_rows = 2; /// The rows a placement, and a `size`, leave a pane: its tag at its width /// and `min_body_rows`. The one minimum both ask. pub fn minRows(p: *Pardes, id: usize) u16 { - return p.wantedTagRows(id) + min_body_rows; + return wantedTagRows(p, id) + min_body_rows; } /// Whether every column has the rows for each of its unfolded panes' @@ -603,7 +608,7 @@ pub fn minRows(p: *Pardes, id: usize) u16 { /// when it grows. One pane alone in its column always fits. pub fn minimaFit(p: *Pardes) bool { compute(p); - const avail = p.screen_h -| (p.topBarHeight() + p.columnBarHeight()); + const avail = p.screen_h -| (p.topBarHeight() + columnBarHeight(p)); for (0..p.ncol) |c| { if (p.col_n[c] < 2) continue; var need: u32 = 0; @@ -635,7 +640,7 @@ pub fn shareColumn(p: *Pardes, c: usize) bool { compute(p); const n = p.col_n[c]; if (n == 0) return false; - const avail = p.screen_h -| (p.topBarHeight() + p.columnBarHeight()); + const avail = p.screen_h -| (p.topBarHeight() + columnBarHeight(p)); var mins: [MAX_PANES]u16 = undefined; var need: u32 = 0; for (p.col_panes[c][0..n], 0..) |k, i| { @@ -843,7 +848,7 @@ fn splitAt(p: *Pardes, c: usize, id: usize, y: u16) void { /// leaves the pane its tag and that much of its body, whichever end the tag /// is on (Tagbottom's is at its foot, the height the same; behaviour test /// "placement: under Tagbottom..."). -fn bodyTop(p: *const Pardes, id: usize) u16 { +fn bodyFrom(p: *const Pardes, id: usize) u16 { return p.rects[id].y + p.panes[id].?.tag_rows; } @@ -1221,7 +1226,7 @@ pub fn expandedNeighbor(p: *const Pardes, col: usize, index: usize, side: enum { } fn computeCollapsedColumn(p: *Pardes, c: usize, x: u16, width: u16, folded: usize) void { - var y: u16 = p.topBarHeight() + p.columnBarHeight(); + var y: u16 = p.topBarHeight() + columnBarHeight(p); const available = p.screen_h -| y; var expanded_left: usize = 0; var weight: f32 = 0; @@ -1303,7 +1308,7 @@ pub fn compute(p: *Pardes) void { } if (vsum <= 0) vsum = 1; - var y: u16 = p.topBarHeight() + p.columnBarHeight(); + var y: u16 = p.topBarHeight() + columnBarHeight(p); const top = y; const avail_h = p.screen_h -| y; var hs: [MAX_PANES]u16 = undefined; @@ -1726,3 +1731,340 @@ test "an attachment survives Dump and Restore, and one left away from its owner compute(p); try std.testing.expect(p.panes[t].?.attached_to == null); } + +// ---- the session's columns and the rows its chrome takes ---- + +/// The workspace tag's height: one row while the setting keeps it, as +/// acme's row tag is, zero once a native shell takes its commands over. +/// Layout and hit testing read this rather than the constant, the way +/// they read `columnBarHeight`. +pub fn topBarHeight(p: *const Pardes) u16 { + if (!p.settings.workspace_tag) return 0; + return TOPBAR_H; +} + +/// The column tags' height: one row, as acme's (cols.c:244, colresize, +/// gives a column tag one font height); a newline in one is a space +/// (tagline.flattenHeaders). +pub fn columnBarHeight(p: *const Pardes) u16 { + if (p.screen_h < 3) return 0; + return COLBAR_H; +} + +/// Newcol: an empty column right of the calling pane's, the keyboard +/// on its tag, for the next pane made to fill. +pub fn newColumn(p: *Pardes, from_id: usize) void { + // An empty window (Delcol took the last column): its first column. + if (firstColumn(p)) |c| return tagline.enterHeader(p, c); + const keyboard_col = keyboardColumn(p, from_id) orelse return; + // Asked from the finder: the new column goes left of it, so the + // finder stays at the edge as wide. + const in_finder = p.finder.column(p) == keyboard_col; + const before_finder = in_finder and keyboard_col == 0; + const col = if (in_finder and keyboard_col > 0) keyboard_col - 1 else keyboard_col; + // A trial: laying it out gives it a serial (layout.compute), which a + // refusal takes back. Every other column has its own already, so + // the counter as it is now is the trial's to return to. + for (0..p.ncol) |k| _ = columnSerial(p, k); + const unspent = p.next_column_serial; + const before_min = minima(p); + const before = p.rects; + // Said, not silently nothing: "no space" is 9ns's word for ENOSPC. + const c = insertColumnUnannounced(p, col, before_finder) orelse { + var said: [96]u8 = undefined; + return p.reportFailure(from_id, if (p.ncol >= MAX_COLS) + std.fmt.bufPrint(&said, "Newcol: no space for a column: {d} max", .{MAX_COLS}) catch "Newcol: no space for a column" + else + "Newcol: no space for a column: this one is too narrow to split"); + }; + if (!keepColumn(p, c, &before, &before_min)) { + p.next_column_serial = unspent; + compute(p); + return p.reportFailure(from_id, "Newcol: no space for a column: the panes' tags would not fit"); + } + // Kept: now it has a serial, and the log hears of it. + noteColumn(p, "newcol", columnSerial(p, c)); + tagline.enterHeader(p, c); +} + +pub fn setActiveColumn(p: *Pardes, c: usize) void { + if (c < p.ncol) p.active_column = columnSerial(p, c); +} + +/// The active column, while it is still there. +pub fn activeColumn(p: *const Pardes) ?usize { + return columnBySerial(p, p.active_column); +} + +pub fn noteTyped(p: *Pardes) void { + if (p.header_focus) { + if (p.header_column) |c| p.setActiveColumn(c); + } else if (findPane(p, p.active)) |f| p.setActiveColumn(f.col); +} + +/// New, run from column `c`'s tag. +pub fn newScratchIn(p: *Pardes, from_id: usize, c: usize) void { + p.exec_column = c; + defer p.exec_column = null; + p.newScratchBelow(from_id); +} + +/// The column a command acts on: the one whose tag it came from, else +/// the one whose tag has the keyboard, else `id`'s. +pub fn keyboardColumn(p: *Pardes, id: usize) ?usize { + if (p.exec_column) |c| return c; + if (p.header_focus) if (p.header_column) |c| return c; + return if (findPane(p, id)) |f| f.col else null; +} + +/// Delcol: close column `col` and every pane in it. +pub fn removeColumn(p: *Pardes, col: usize) !void { + if (col >= p.ncol) return; + const had_focus = keyboardColumn(p, p.active) == col; + const beside = if (had_focus) besideColumn(p, col, if (p.header_focus) null else p.rects[p.active]) else null; + var ids: [MAX_PANES]usize = undefined; + var parents: [MAX_PANES]*Pane = undefined; + const count = p.col_n[col]; + for (0..count) |i| { + ids[i] = p.col_panes[col][i]; + parents[i] = p.panes[ids[i]].?; + } + try panes.detachCwds(p, parents[0..count]); + for (ids[0..count], parents[0..count]) |closed, pane| { + // Kept for Panes; its column goes, so it comes back as a new pane. + if (p.store.keep(p, closed)) |k| p.store.close(p, k, null); + panes.retirePane(p, pane); + removePane(p, closed); + p.panes[closed] = null; + } + dropColumn(p, col); + // The last column gone, the window stays, empty but for the + // workspace tag, as acme's does; Newcol starts again from there. + if (p.panes[p.active] == null) p.active = beside orelse panes.anyPane(p) orelse return tagline.enterHeader(p, null); +} + +/// The pane that takes over the keyboard from column `col`: in the +/// column layout.dropColumn gives the width to, the one level with the +/// middle of `r` (of the screen without one). Asked while rects hold. +pub fn besideColumn(p: *const Pardes, col: usize, r: ?Rect) ?usize { + if (p.ncol < 2) return null; + const next = if (col > 0) col - 1 else col + 1; + const mid = if (r) |at| at.y + at.h / 2 else p.screen_h / 2; + var pick: ?usize = null; + for (p.col_panes[next][0..p.col_n[next]]) |k| { + if (p.panes[k] == null) continue; + pick = k; + if (p.rects[k].y +| p.rects[k].h > mid) break; + } + return pick; +} + +/// The rows pane `id`'s tag takes at its width, wrapped as `sync` wraps +/// it: what a placement must leave it above its body rows. +pub fn wantedTagRows(p: *Pardes, id: usize) u16 { + const pane = p.panes[id] orelse return BOX_H; + if (pane.collapsed or !pane.tag_expanded) return BOX_H; + if (p.rects[id].w == 0) return pane.tag_rows; // never laid out + var wrap_room: [32 * 1024]u8 = undefined; + var wrap_fba = std.heap.FixedBufferAllocator.init(&wrap_room); + const wa = wrap_fba.allocator(); + const prefix = tagline.prefixIn(p, wa, pane) catch return pane.tag_rows; + const whole = std.mem.concat(wa, u8, &.{ prefix, tagline.curTail(pane) }) catch return pane.tag_rows; + const shown = tagline.wrapTag(wa, whole, tagline.tagWidth(p, id)) catch return pane.tag_rows; + return @intCast(@max(BOX_H, @min(shown.rows(), MAX_TAG_ROWS))); +} + +/// The pane's first body row: under the tag when it is on top, over it +/// when it is at the bottom. Notice bands do NOT move it -- they are an +/// overlay, drawn over the top rows rather than taking them. +pub fn bodyTop(p: *const Pardes, pane: *const Pane, r: Rect) u16 { + return if (p.settings.tag_bottom) r.y else r.y + pane.tag_rows; +} + +/// The tag's first row: the pane's top, or its last rows with Tagbottom. +pub fn tagTop(p: *const Pardes, pane: *const Pane, r: Rect) u16 { + return if (p.settings.tag_bottom) r.y + r.h -| pane.tag_rows else r.y; +} + +pub fn tagCapacity(p: *const Pardes, physical_width: u16) u16 { + const layer: Layer = .{ .kind = .pane, .viewport = .{ .x = 0, .y = 0, .w = physical_width, .h = 1 } }; + return layer.capacity(p.row_metrics); +} + +// ---- render: build the canonical surface (draw.zig) ---- + +/// A frontend's tag hit, if what it names is still there: resolved on +/// the place it carries (Layer.Place), not on this process's frame. +pub fn reprojectTagHit(p: *const Pardes, tag_hit: TagHit) ?TagHit { + if (tag_hit.kind == .pane) { + if (tag_hit.id >= p.panes.len) return null; + const pane = p.panes[tag_hit.id] orelse return null; + if (pane.serial != tag_hit.serial) return null; + } + if (tag_hit.kind == .column and (tag_hit.id >= p.ncol or p.col_serial[tag_hit.id] != tag_hit.serial)) return null; + return Layer.tagAgain(tag_hit); +} + +test "a new column takes width only from the column that created it" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 124, .rows = 24 }); + defer p.deinit(); + + _ = try p.newShell(1, ""); + try std.testing.expect(splitColumn(p, 0, 1, false)); + _ = try p.newShell(2, ""); + try std.testing.expect(splitColumn(p, 0, 2, true)); + p.sync(); + + const left_before = p.rects[2]; + const right_before = p.rects[1]; + _ = try p.newShell(3, ""); + try std.testing.expect(splitColumn(p, 0, 3, false)); + p.sync(); + + try std.testing.expectEqual(left_before.x, p.rects[2].x); + try std.testing.expectEqual(left_before.w, p.rects[2].w); + try std.testing.expectEqual(right_before.x, p.rects[1].x); + try std.testing.expectEqual(right_before.w, p.rects[1].w); + try std.testing.expectEqual(@as(u16, 16), p.rects[0].w); + try std.testing.expectEqual(@as(u16, 15), p.rects[3].w); + + const narrow = try Pardes.init(std.testing.allocator, .{ .cols = config.MINW * 2 - 1, .rows = 10 }); + defer narrow.deinit(); + try std.testing.expect(!splitColumn(narrow, 0, 1, false)); + try std.testing.expectEqual(@as(usize, 1), narrow.ncol); + + const sequential = try Pardes.init(std.testing.allocator, .{ .cols = config.MINW * 2, .rows = 10 }); + defer sequential.deinit(); + _ = try sequential.newShell(1, ""); + try std.testing.expect(splitColumn(sequential, 0, 1, false)); + _ = try sequential.newShell(2, ""); + try std.testing.expect(!splitColumn(sequential, 0, 2, false)); + try std.testing.expectEqual(@as(usize, 2), sequential.ncol); + + const coarse = try Pardes.init(std.testing.allocator, .{ .cols = 22, .rows = 10 }); + defer coarse.deinit(); + coarse.col_weight[0] = 3; + _ = try coarse.newShell(1, ""); + try std.testing.expect(splitColumn(coarse, 0, 1, false)); + coarse.sync(); + try std.testing.expectEqual(@as(u16, 11), coarse.col_w[0]); + try std.testing.expectEqual(@as(u16, 11), coarse.col_w[1]); + + const extreme = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 20 }); + defer extreme.deinit(); + _ = try extreme.newShell(1, ""); + try std.testing.expect(splitColumn(extreme, 0, 1, false)); + _ = try extreme.newShell(2, ""); + insert(extreme, 0, 1, 2); + extreme.col_weight[0] = std.math.maxInt(u64) / 2 + 2; // odd and cannot double + extreme.col_weight[1] = 1; + compute(extreme); + const panes_before = extreme.col_panes; + const counts_before = extreme.col_n; + const weights_before = extreme.col_weight; + const source_vweight = extreme.panes[0].?.vweight; + const sibling_vweight = extreme.panes[2].?.vweight; + try std.testing.expect(!splitColumn(extreme, 0, 0, false)); + try std.testing.expectEqual(panes_before, extreme.col_panes); + try std.testing.expectEqual(counts_before, extreme.col_n); + try std.testing.expectEqual(weights_before, extreme.col_weight); + try std.testing.expectEqual(source_vweight, extreme.panes[0].?.vweight); + try std.testing.expectEqual(sibling_vweight, extreme.panes[2].?.vweight); +} + +test "Newcol refuses an unsplittable restored weight before spawning" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 200, .rows = 20 }); + defer p.deinit(); + + for (1..4) |id| { + _ = try p.newShell(id, ""); + try std.testing.expect(splitColumn(p, id - 1, id, false)); + } + const restored_cap = std.math.maxInt(u64) / 6; + for (0..4) |column| p.col_weight[column] = restored_cap; + p.col_weight[3] -= 1; + for ([_]usize{ 3, 2, 1 }) |id| { + const pane = p.panes[id].?; + try panes.deinitPane(p, pane); + removePane(p, id); + dropColumn(p, id); + p.panes[id] = null; + } + while (p.nextEffect()) |_| {} + + try std.testing.expect(p.col_weight[0] > std.math.maxInt(u64) / 2); + try std.testing.expect(p.col_weight[0] % 2 == 1); + try std.testing.expect(!canSplitCol(p, findPane(p, 0).?.col)); + const panes_before = p.panes; + const serial_before = p.next_serial; + try std.testing.expect(p.executeBuiltinLine(0, "Newcol")); + try std.testing.expectEqual(panes_before, p.panes); + try std.testing.expectEqual(serial_before, p.next_serial); + try std.testing.expect(p.nextEffect() == null); +} + +test "repeated non-dyadic column splits preserve every unrelated boundary" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 979, .rows = 20 }); + defer p.deinit(); + + _ = try p.newShell(1, ""); + try std.testing.expect(splitColumn(p, 0, 1, false)); + p.sync(); + + const handle = p.col_x[0] + p.col_w[0] - 1; + const untouched = p.col_weight; + p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = handle, .row = TOPBAR_H + 1 } }); + p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = handle, .row = TOPBAR_H + 1 } }); + try std.testing.expectEqual(untouched, p.col_weight); + + p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = handle, .row = TOPBAR_H + 1 } }); + p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = 417, .row = TOPBAR_H + 1 } }); + p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = 417, .row = TOPBAR_H + 1 } }); + p.sync(); + try std.testing.expectEqual(@as(u16, 418), p.rects[0].w); + + _ = try p.newShell(2, ""); + try std.testing.expect(splitColumn(p, 1, 2, false)); + p.sync(); + const left_before = p.rects[0]; + const middle_before = p.rects[1]; + + _ = try p.newShell(3, ""); + try std.testing.expect(splitColumn(p, 2, 3, false)); + p.sync(); + try std.testing.expectEqual(left_before, p.rects[0]); + try std.testing.expectEqual(middle_before, p.rects[1]); +} + +test "dragging a column's only pane to another leaves that column in place, empty" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 32 }); + defer p.deinit(); + newColumn(p, 0); + newScratchIn(p, 0, activeColumn(p).?); + const moved = p.active; + p.sync(); + const weights = p.col_weight; + const tag_serial = p.col_serial[1]; + movePane(p, moved, p.col_x[0] + 4, p.rects[0].y + p.rects[0].h / 2); + p.sync(); + // acme's coldragwin: colclose then coladd (cols.c:516-517); the column + // left behind keeps its place, width and tag. + try std.testing.expectEqual(@as(usize, 2), p.ncol); + try std.testing.expectEqual(@as(usize, 0), p.col_n[1]); + try std.testing.expectEqual(tag_serial, p.col_serial[1]); + try std.testing.expectEqualDeep(weights, p.col_weight); + try std.testing.expectEqual(@as(usize, 2), p.col_n[0]); + // ...and a pane dragged into the empty column takes it whole. + movePane(p, moved, p.col_x[1] + 4, p.screen_h / 2); + p.sync(); + try std.testing.expectEqualSlices(usize, &.{moved}, p.col_panes[1][0..p.col_n[1]]); + try std.testing.expectEqual(@as(usize, 1), p.col_n[0]); +} + +test "tiny screens keep pane space before adding a column tag" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 8, .rows = 2, .tty_only = true }); + defer p.deinit(); + try std.testing.expectEqual(@as(u16, 0), columnBarHeight(p)); + try std.testing.expectEqual(TOPBAR_H, p.rects[0].y); + _ = try p.render(p.scratch.allocator()); +} diff --git a/src/mouse.zig b/src/mouse.zig index 5c315b4e..365ed560 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -103,7 +103,7 @@ fn headerPoint(p: *Pardes, column: ?usize, mcol: u16, mrow: u16, clamp: bool) ?s const focused = p.header_focus and p.header_column == column; const text = edit.flatSurface(p, t) catch return null; const base = if (column != null) p.topBarHeight() else 0; - const rows = if (column != null) p.columnBarHeight() else p.topBarHeight(); + const rows = if (column != null) layout.columnBarHeight(p) else p.topBarHeight(); const row: u16 = if (clamp) @min(mrow -| base, rows -| 1) else mrow -| base; const top: usize = if (focused) p.header_top else 0; const lines = modal.lineCount(text); @@ -230,7 +230,7 @@ pub const ChromeTarget = struct { col: u16, row: u16 }; /// on the cell, a pane's grip, a column's handle or a seam; then a cell /// away, any of a pane's tag, a handle or a seam. pub fn chromeTarget(p: *const Pardes, col: u16, row: u16) ?ChromeTarget { - if (row < p.topBarHeight() + p.columnBarHeight()) return .{ .col = col, .row = row }; + if (row < p.topBarHeight() + layout.columnBarHeight(p)) return .{ .col = col, .row = row }; for ([_]u16{ 0, 1 }) |slack| { for (p.panes, 0..) |slot, id| { const pane = slot orelse continue; @@ -510,7 +510,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { const previous_hit = p.pointer_body_hit; p.pointer_body_hit = m.body_hit; const previous_tag_hit = p.pointer_tag_hit; - p.pointer_tag_hit = if (m.tag_hit) |point| p.reprojectTagHit(point) else null; + p.pointer_tag_hit = if (m.tag_hit) |point| layout.reprojectTagHit(p, point) else null; if (m.tag_hit != null and p.pointer_tag_hit == null and m.kind == .press) return; if (m.tag_hit == null and (m.kind == .drag or m.kind == .release) and (p.drag == .select or p.header_drag or p.header_sweep != null)) { if (previous_tag_hit) |previous| { @@ -592,7 +592,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { return; } } - if (mrow >= p.topBarHeight() and mrow < p.topBarHeight() + p.columnBarHeight() and m.kind == .press) { + if (mrow >= p.topBarHeight() and mrow < p.topBarHeight() + layout.columnBarHeight(p) and m.kind == .press) { for (0..p.ncol) |column| { if (mcol < p.col_x[column] or mcol >= p.col_x[column] + @min(config.GUTTER, p.col_w[column])) continue; if (m.button == config.select_button) { @@ -617,7 +617,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { if (m.kind == .release) p.header_drag = false; return; } - if (mrow < p.topBarHeight() + p.columnBarHeight() and m.kind == .press and + if (mrow < p.topBarHeight() + layout.columnBarHeight(p) and m.kind == .press and (m.button == config.select_button or m.button == config.exec_button or m.button == config.look_button)) { if (m.button == config.select_button) if (p.panes[p.active]) |pane| { @@ -777,7 +777,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { const on_tag = mrow >= tag_y and mrow < tag_y + pane.tag_rows; if (on_tag and mcol < r.x + TAG_TEXT_INSET) return; p.active = id; - p.noteTyped(); // acme's button 1 sets activecol (acme.c:659) + layout.noteTyped(p); // acme's button 1 sets activecol (acme.c:659) pane.closePrompt(); const pointed = edit.pointerTextSelection(p, id, mcol, mrow, m.body_hit, p.pointer_tag_hit) orelse return; pane.clearPointerSelection(sel_slot); diff --git a/src/ninep/cols.zig b/src/ninep/cols.zig index f53bb433..60071bd0 100644 --- a/src/ninep/cols.zig +++ b/src/ninep/cols.zig @@ -41,7 +41,7 @@ pub fn writeLayout(p: *Pardes, w: *std.Io.Writer) !void { for (p.col_panes[c][0..p.col_n[c]]) |id| if (p.panes[id]) |pane| try w.print(" {d}", .{pane.serial}); try w.writeByte('\n'); } - if (p.activeColumn() orelse active) |c| try w.print("active {d}\n", .{layout.columnSerial(p, c)}) else try w.writeAll("active -\n"); + if (layout.activeColumn(p) orelse active) |c| try w.print("active {d}\n", .{layout.columnSerial(p, c)}) else try w.writeAll("active -\n"); } /// A header by its node: the workspace's (`serial` null) or the column's @@ -496,7 +496,7 @@ pub fn remove(p: *Pardes, req: Req, serial: u32, file: tree.ColFile) Reply { if (file != .dir) return Reply.fail(req.tag, E.PERM); const col = layout.columnBySerial(p, serial) orelse return Reply.fail(req.tag, E.NOENT); if (p.col_n[col] > 0) return tree.failText(req.tag, E.NOTEMPTY, "column not empty: close its panes, or write Delcol to its ctl"); - p.removeColumn(col) catch return Reply.fail(req.tag, E.IO); + layout.removeColumn(p, col) catch return Reply.fail(req.tag, E.IO); return .{ .tag = req.tag }; } @@ -509,7 +509,7 @@ const Node = tree.Node; test "layout lists each column's place, panes and state, and the active one" { const p = try th.withFile(testing.allocator, "x\n"); defer p.deinit(); - p.newColumn(p.active); + layout.newColumn(p, p.active); p.sync(); const first_serial = layout.columnSerial(p, 0); const new_serial = layout.columnSerial(p, 1); @@ -528,7 +528,7 @@ test "layout lists each column's place, panes and state, and the active one" { try testing.expect(std.mem.endsWith(u8, th.rd(p, @backingInt(tree.TopFile.index), 0, 4096).bytes, try std.fmt.bufPrint(&want, " {d}\n", .{first_serial}))); try testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "newcol {d}\n", .{new_serial}))); // A column that closes takes its name with it; the others keep theirs. - try p.removeColumn(1); + try layout.removeColumn(p, 1); try testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "delcol {d}\n", .{new_serial}))); try testing.expectEqual(E.NOENT, th.look_up(p, @backingInt(tree.TopFile.col), try std.fmt.bufPrint(&want, "{d}", .{new_serial})).errno()); try testing.expectEqual(first_serial, layout.columnSerial(p, 0)); @@ -564,7 +564,7 @@ test "a focus write makes its pane's column the active one: layout says so and p const p = try th.withFile(testing.allocator, "x\n"); defer p.deinit(); const left = p.panes[0].?.serial; - p.newColumn(p.active); + layout.newColumn(p, p.active); panes.newScratchFor(p, p.active, .fs); // fills the new, right column p.sync(); try testing.expectEqual(@as(usize, 1), layout.findPane(p, p.active).?.col); @@ -613,7 +613,7 @@ test "a column's ctl and exec act on it as its tag would, and rmdir closes it on const p = try th.withFile(testing.allocator, "x\n"); defer p.deinit(); const first = layout.columnSerial(p, 0); - p.newColumn(p.active); + layout.newColumn(p, p.active); const empty = layout.columnSerial(p, 1); // New on the empty column's ctl fills it, wherever the keyboard is. try testing.expectEqual(tree.Status.ok, th.wr(p, Node.ofCol(empty, .ctl), "New\n").reply.status); @@ -630,14 +630,14 @@ test "a column's ctl and exec act on it as its tag would, and rmdir closes it on try testing.expectEqual(tree.Status.ok, th.wr(p, Node.ofCol(empty, .exec), "Delcol\n").reply.status); try testing.expectEqual(@as(?usize, null), layout.columnBySerial(p, empty)); // An empty column goes on rmdir. - p.newColumn(p.active); + layout.newColumn(p, p.active); const made = layout.columnSerial(p, 1); try testing.expectEqual(tree.Status.ok, th.call(p, .{ .tag = 2, .op = .release, .node = Node.ofCol(made, .dir), .remove = true }).reply.status); try testing.expectEqual(@as(?usize, null), layout.columnBySerial(p, made)); try testing.expectEqual(first, layout.columnSerial(p, 0)); // Joincol folds a column into the one on its right, which keeps its own // serial and tag. - p.newColumn(p.active); + layout.newColumn(p, p.active); const right = layout.columnSerial(p, 1); _ = th.wr(p, Node.ofCol(right, .tag), " Mine"); try testing.expectEqual(tree.Status.ok, th.wr(p, Node.ofCol(first, .ctl), "Joincol\n").reply.status); @@ -690,7 +690,7 @@ test "a word run from the workspace's or a column's tag runs in the session's di test "a command run from a column's tag gets its pane in that column, not the last" { const p = try th.withFile(testing.allocator, "x\n"); defer p.deinit(); - p.newColumn(p.active); + layout.newColumn(p, p.active); panes.newScratchFor(p, p.active, .fs); // fills the new, right column p.sync(); const left = layout.columnSerial(p, 0); diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 2d733af6..2151126d 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -2385,7 +2385,7 @@ test "a column takes panes until their documented minimums fill it, not until no const new = @backingInt(tree.TopFile.new); while (call(p, .{ .tag = 1, .op = .open, .node = new }).reply.status == .ok) p.sync(); p.sync(); - const avail = p.screen_h - (p.topBarHeight() + p.columnBarHeight()); + const avail = p.screen_h - (p.topBarHeight() + layout.columnBarHeight(p)); for (0..p.ncol) |c| { var need: u32 = 0; for (p.col_panes[c][0..p.col_n[c]]) |k| need += layout.minRows(p, k); diff --git a/src/panes.zig b/src/panes.zig index 8786de2b..94c9615f 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -1032,7 +1032,7 @@ pub fn removePane(p: *Pardes, id: usize, to: ?usize) error{OutOfMemory}!void { // Before anything is torn down, so a failure here changes nothing. try deinitPane(p, pane); const gainer = layout.absorbVWeight(p, id, owner orelse to); - const beside = if (emptied) |c| p.besideColumn(c, p.rects[id]) else null; + const beside = if (emptied) |c| layout.besideColumn(p, c, p.rects[id]) else null; layout.removePane(p, id); p.panes[id] = null; if (kept) |k| p.store.close(p, k, gainer); @@ -1260,14 +1260,14 @@ test "owned cwd column close allocates every copy before removing any pane" { const columns = p.col_panes; const counts = p.col_n; allocator.fail_index = allocator.alloc_index + 1; - try std.testing.expectError(error.OutOfMemory, p.removeColumn(0)); + try std.testing.expectError(error.OutOfMemory, layout.removeColumn(p, 0)); for (before, p.panes) |old, current| try std.testing.expectEqual(old, current); try std.testing.expectEqualDeep(columns, p.col_panes); try std.testing.expectEqualDeep(counts, p.col_n); try std.testing.expectEqual(@as(usize, 0), p.effects_len); try std.testing.expect(first_child.cwd == .inherited and second_child.cwd == .inherited); allocator.fail_index = std.math.maxInt(usize); - try p.removeColumn(0); + try layout.removeColumn(p, 0); try std.testing.expect(p.panes[0] == null and p.panes[1] == null); p.sync(); p.sync(); diff --git a/src/pardes.zig b/src/pardes.zig index 4e10aa00..9e6a2d7f 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -173,7 +173,7 @@ pub fn topbarPaneBorderPixels(cell_h: u32, tagline_h: u32) u32 { pub fn taglineOriginCol(p: *const Pardes, col: u16, row: u16) f32 { if (row < p.topBarHeight()) return 0; - if (row < p.topBarHeight() + p.columnBarHeight()) { + if (row < p.topBarHeight() + layout.columnBarHeight(p)) { for (p.col_x[0..p.ncol], p.col_w[0..p.ncol]) |x, w| if (col >= x and col - x < w) return @floatFromInt(x); } @@ -190,7 +190,7 @@ pub fn gridColAt(p: ?*const Pardes, x: f32, row: u16, body_w: f32, tagline_w: f3 const body = @max(body_w, 1); const tag = @max(tagline_w, 1); if (row < (if (p) |core| core.topBarHeight() else TOPBAR_H)) return colFromSpan(x, tag); - if (p) |core| if (row < core.topBarHeight() + core.columnBarHeight()) { + if (p) |core| if (row < core.topBarHeight() + layout.columnBarHeight(core)) { for (core.col_x[0..core.ncol], core.col_w[0..core.ncol]) |origin, width| { if (width == 0) continue; const left = @as(f32, @floatFromInt(origin)) * body; @@ -551,7 +551,7 @@ test "TreeContext tag style preserves message rows and its opt-out survives rest // right aligned a cell short of that edge. try std.testing.expectEqual(r.x + r.w, band.viewport.x + band.viewport.w); try std.testing.expect(band.viewport.w < r.w - config.GUTTER); - try std.testing.expectEqualStrings("!", band.cells[p.tagCapacity(band.viewport.w) - 2].grapheme()); + try std.testing.expectEqualStrings("!", band.cells[layout.tagCapacity(p, band.viewport.w) - 2].grapheme()); pane.msg_len = 0; p.leader_on = true; p.sync(); @@ -772,7 +772,7 @@ test "a frame no one watches is not drawn, and is drawn once someone does" { test "an empty column shows its theme's empty_col under its tag: acme's white, else the border" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 20 }); defer p.deinit(); - p.newColumn(0); + layout.newColumn(p, 0); tagline.exitHeader(p); inline for (.{ "acme", "dark" }) |name| { try std.testing.expect(p.executeBuiltinLine(0, "Theme " ++ name)); @@ -787,31 +787,6 @@ test "an empty column shows its theme's empty_col under its tag: acme's white, e } } -test "dragging a column's only pane to another leaves that column in place, empty" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 32 }); - defer p.deinit(); - p.newColumn(0); - p.newScratchIn(0, p.activeColumn().?); - const moved = p.active; - p.sync(); - const weights = p.col_weight; - const tag_serial = p.col_serial[1]; - layout.movePane(p, moved, p.col_x[0] + 4, p.rects[0].y + p.rects[0].h / 2); - p.sync(); - // acme's coldragwin: colclose then coladd (cols.c:516-517); the column - // left behind keeps its place, width and tag. - try std.testing.expectEqual(@as(usize, 2), p.ncol); - try std.testing.expectEqual(@as(usize, 0), p.col_n[1]); - try std.testing.expectEqual(tag_serial, p.col_serial[1]); - try std.testing.expectEqualDeep(weights, p.col_weight); - try std.testing.expectEqual(@as(usize, 2), p.col_n[0]); - // ...and a pane dragged into the empty column takes it whole. - layout.movePane(p, moved, p.col_x[1] + 4, p.screen_h / 2); - p.sync(); - try std.testing.expectEqualSlices(usize, &.{moved}, p.col_panes[1][0..p.col_n[1]]); - try std.testing.expectEqual(@as(usize, 1), p.col_n[0]); -} - test "unknown Exec from a document runs as a command pane in its directory, typed into no terminal" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{}); @@ -1865,8 +1840,8 @@ test "column tag first-cell execution keeps New local to the clicked column" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 20, .tty_only = true }); defer p.deinit(); _ = try p.setTestFile("untouched\n"); - p.newColumn(0); - p.newScratchIn(0, p.activeColumn().?); + layout.newColumn(p, 0); + layout.newScratchIn(p, 0, layout.activeColumn(p).?); p.sync(); for (0..2) |column| { p.active = p.col_panes[1 - column][0]; @@ -1881,14 +1856,6 @@ test "column tag first-cell execution keeps New local to the clicked column" { try std.testing.expectEqualStrings("untouched\n", p.panes[0].?.file.?.content); } -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), p.columnBarHeight()); - try std.testing.expectEqual(TOPBAR_H, p.rects[0].y); - _ = try p.render(p.scratch.allocator()); -} - test "a screen too short for the column row releases a column tag's focus without losing its commands" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12, .tty_only = true }); defer p.deinit(); @@ -1902,7 +1869,7 @@ test "a screen too short for the column row releases a column tag's focus withou try std.testing.expect(p.header_focus and p.header_column != null); p.update(.{ .resize = .{ .cols = 60, .rows = 2 } }); - try std.testing.expectEqual(@as(u16, 0), p.columnBarHeight()); + try std.testing.expectEqual(@as(u16, 0), layout.columnBarHeight(p)); try std.testing.expectEqual(TOPBAR_H, p.rects[0].y); try std.testing.expect(!p.header_focus and p.header_column == null); p.update(.{ .resize = .{ .cols = 60, .rows = 12 } }); @@ -1916,104 +1883,6 @@ pub const newer_build_words = "a newer pardes is installed: Upgrade (SPC s U) mo pub const PendingWrite = struct { bytes: []u8, off: usize = 0 }; -test "a new column takes width only from the column that created it" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 124, .rows = 24 }); - defer p.deinit(); - - _ = try p.newShell(1, ""); - try std.testing.expect(layout.splitColumn(p, 0, 1, false)); - _ = try p.newShell(2, ""); - try std.testing.expect(layout.splitColumn(p, 0, 2, true)); - p.sync(); - - const left_before = p.rects[2]; - const right_before = p.rects[1]; - _ = try p.newShell(3, ""); - try std.testing.expect(layout.splitColumn(p, 0, 3, false)); - p.sync(); - - try std.testing.expectEqual(left_before.x, p.rects[2].x); - try std.testing.expectEqual(left_before.w, p.rects[2].w); - try std.testing.expectEqual(right_before.x, p.rects[1].x); - try std.testing.expectEqual(right_before.w, p.rects[1].w); - try std.testing.expectEqual(@as(u16, 16), p.rects[0].w); - try std.testing.expectEqual(@as(u16, 15), p.rects[3].w); - - const narrow = try Pardes.init(std.testing.allocator, .{ .cols = config.MINW * 2 - 1, .rows = 10 }); - defer narrow.deinit(); - try std.testing.expect(!layout.splitColumn(narrow, 0, 1, false)); - try std.testing.expectEqual(@as(usize, 1), narrow.ncol); - - const sequential = try Pardes.init(std.testing.allocator, .{ .cols = config.MINW * 2, .rows = 10 }); - defer sequential.deinit(); - _ = try sequential.newShell(1, ""); - try std.testing.expect(layout.splitColumn(sequential, 0, 1, false)); - _ = try sequential.newShell(2, ""); - try std.testing.expect(!layout.splitColumn(sequential, 0, 2, false)); - try std.testing.expectEqual(@as(usize, 2), sequential.ncol); - - const coarse = try Pardes.init(std.testing.allocator, .{ .cols = 22, .rows = 10 }); - defer coarse.deinit(); - coarse.col_weight[0] = 3; - _ = try coarse.newShell(1, ""); - try std.testing.expect(layout.splitColumn(coarse, 0, 1, false)); - coarse.sync(); - try std.testing.expectEqual(@as(u16, 11), coarse.col_w[0]); - try std.testing.expectEqual(@as(u16, 11), coarse.col_w[1]); - - const extreme = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 20 }); - defer extreme.deinit(); - _ = try extreme.newShell(1, ""); - try std.testing.expect(layout.splitColumn(extreme, 0, 1, false)); - _ = try extreme.newShell(2, ""); - layout.insert(extreme, 0, 1, 2); - extreme.col_weight[0] = std.math.maxInt(u64) / 2 + 2; // odd and cannot double - extreme.col_weight[1] = 1; - layout.compute(extreme); - const panes_before = extreme.col_panes; - const counts_before = extreme.col_n; - const weights_before = extreme.col_weight; - const source_vweight = extreme.panes[0].?.vweight; - const sibling_vweight = extreme.panes[2].?.vweight; - try std.testing.expect(!layout.splitColumn(extreme, 0, 0, false)); - try std.testing.expectEqual(panes_before, extreme.col_panes); - try std.testing.expectEqual(counts_before, extreme.col_n); - try std.testing.expectEqual(weights_before, extreme.col_weight); - try std.testing.expectEqual(source_vweight, extreme.panes[0].?.vweight); - try std.testing.expectEqual(sibling_vweight, extreme.panes[2].?.vweight); -} - -test "Newcol refuses an unsplittable restored weight before spawning" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 200, .rows = 20 }); - defer p.deinit(); - - for (1..4) |id| { - _ = try p.newShell(id, ""); - try std.testing.expect(layout.splitColumn(p, id - 1, id, false)); - } - const restored_cap = std.math.maxInt(u64) / 6; - for (0..4) |column| p.col_weight[column] = restored_cap; - p.col_weight[3] -= 1; - for ([_]usize{ 3, 2, 1 }) |id| { - const pane = p.panes[id].?; - try panes.deinitPane(p, pane); - layout.removePane(p, id); - layout.dropColumn(p, id); - p.panes[id] = null; - } - while (p.nextEffect()) |_| {} - - try std.testing.expect(p.col_weight[0] > std.math.maxInt(u64) / 2); - try std.testing.expect(p.col_weight[0] % 2 == 1); - try std.testing.expect(!layout.canSplitCol(p, layout.findPane(p, 0).?.col)); - const panes_before = p.panes; - const serial_before = p.next_serial; - try std.testing.expect(p.executeBuiltinLine(0, "Newcol")); - try std.testing.expectEqual(panes_before, p.panes); - try std.testing.expectEqual(serial_before, p.next_serial); - try std.testing.expect(p.nextEffect() == null); -} - test "screen resize keeps its previous storage until allocation succeeds" { var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{}); const p = try Pardes.init(failing.allocator(), .{ .cols = 80, .rows = 24, .tty_only = true }); @@ -2036,39 +1905,6 @@ test "screen resize keeps its previous storage until allocation succeeds" { try std.testing.expectEqual(@as(usize, 81 * 25), resized.cells.len); } -test "repeated non-dyadic column splits preserve every unrelated boundary" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 979, .rows = 20 }); - defer p.deinit(); - - _ = try p.newShell(1, ""); - try std.testing.expect(layout.splitColumn(p, 0, 1, false)); - p.sync(); - - const handle = p.col_x[0] + p.col_w[0] - 1; - const untouched = p.col_weight; - p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = handle, .row = TOPBAR_H + 1 } }); - p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = handle, .row = TOPBAR_H + 1 } }); - try std.testing.expectEqual(untouched, p.col_weight); - - p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = handle, .row = TOPBAR_H + 1 } }); - p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = 417, .row = TOPBAR_H + 1 } }); - p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = 417, .row = TOPBAR_H + 1 } }); - p.sync(); - try std.testing.expectEqual(@as(u16, 418), p.rects[0].w); - - _ = try p.newShell(2, ""); - try std.testing.expect(layout.splitColumn(p, 1, 2, false)); - p.sync(); - const left_before = p.rects[0]; - const middle_before = p.rects[1]; - - _ = try p.newShell(3, ""); - try std.testing.expect(layout.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]); -} - pub const Rect = layout.Rect; /// The session layouts `init` can put on screen. One name each, so choosing @@ -2539,6 +2375,14 @@ pub const Pardes = struct { /// `p.executeBuiltinLine(...)`; it lives with the rest of exec in exec.zig. pub const executeBuiltinLine = exec.executeBuiltinLine; + /// Geometry, called from everywhere as `p.bodyTop(pane, r)` and the rest + /// (setActiveColumn and tagTop by Store.zig and Handover.zig); it lives with the + /// columns in layout.zig. + pub const topBarHeight = layout.topBarHeight; + pub const bodyTop = layout.bodyTop; + pub const tagTop = layout.tagTop; + pub const setActiveColumn = layout.setActiveColumn; + /// Pane slots, called from everywhere as `p.freeSlot()` and the rest /// (emitSpawn and installPane by dump.zig); they live with the panes /// in panes.zig. @@ -2926,68 +2770,6 @@ pub const Pardes = struct { return r; } - /// The workspace tag's height: one row while the setting keeps it, as - /// acme's row tag is, zero once a native shell takes its commands over. - /// Layout and hit testing read this rather than the constant, the way - /// they read `columnBarHeight`. - pub fn topBarHeight(p: *const Pardes) u16 { - if (!p.settings.workspace_tag) return 0; - return TOPBAR_H; - } - - /// The column tags' height: one row, as acme's (cols.c:244, colresize, - /// gives a column tag one font height); a newline in one is a space - /// (tagline.flattenHeaders). - pub fn columnBarHeight(p: *const Pardes) u16 { - if (p.screen_h < 3) return 0; - return COLBAR_H; - } - - /// Newcol: an empty column right of the calling pane's, the keyboard - /// on its tag, for the next pane made to fill. - pub fn newColumn(p: *Pardes, from_id: usize) void { - // An empty window (Delcol took the last column): its first column. - if (layout.firstColumn(p)) |c| return tagline.enterHeader(p, c); - const keyboard_col = p.keyboardColumn(from_id) orelse return; - // Asked from the finder: the new column goes left of it, so the - // finder stays at the edge as wide. - const in_finder = p.finder.column(p) == keyboard_col; - const before_finder = in_finder and keyboard_col == 0; - const col = if (in_finder and keyboard_col > 0) keyboard_col - 1 else keyboard_col; - // A trial: laying it out gives it a serial (layout.compute), which a - // refusal takes back. Every other column has its own already, so - // the counter as it is now is the trial's to return to. - for (0..p.ncol) |k| _ = layout.columnSerial(p, k); - const unspent = p.next_column_serial; - const before_min = layout.minima(p); - const before = p.rects; - // Said, not silently nothing: "no space" is 9ns's word for ENOSPC. - const c = layout.insertColumnUnannounced(p, col, before_finder) orelse { - var said: [96]u8 = undefined; - return p.reportFailure(from_id, if (p.ncol >= MAX_COLS) - std.fmt.bufPrint(&said, "Newcol: no space for a column: {d} max", .{MAX_COLS}) catch "Newcol: no space for a column" - else - "Newcol: no space for a column: this one is too narrow to split"); - }; - if (!layout.keepColumn(p, c, &before, &before_min)) { - p.next_column_serial = unspent; - layout.compute(p); - return p.reportFailure(from_id, "Newcol: no space for a column: the panes' tags would not fit"); - } - // Kept: now it has a serial, and the log hears of it. - layout.noteColumn(p, "newcol", layout.columnSerial(p, c)); - tagline.enterHeader(p, c); - } - - pub fn setActiveColumn(p: *Pardes, c: usize) void { - if (c < p.ncol) p.active_column = layout.columnSerial(p, c); - } - - /// The active column, while it is still there. - pub fn activeColumn(p: *const Pardes) ?usize { - return layout.columnBySerial(p, p.active_column); - } - /// DECSET 1004: a program that asked is told `CSI I` when its pane /// takes the keyboard (the active pane, its body, the window focused) /// and `CSI O` when it gives it up, to another pane, a tag or header, @@ -3007,27 +2789,6 @@ pub const Pardes = struct { p.focus_told = want; } - pub fn noteTyped(p: *Pardes) void { - if (p.header_focus) { - if (p.header_column) |c| p.setActiveColumn(c); - } else if (layout.findPane(p, p.active)) |f| p.setActiveColumn(f.col); - } - - /// New, run from column `c`'s tag. - pub fn newScratchIn(p: *Pardes, from_id: usize, c: usize) void { - p.exec_column = c; - defer p.exec_column = null; - p.newScratchBelow(from_id); - } - - /// The column a command acts on: the one whose tag it came from, else - /// the one whose tag has the keyboard, else `id`'s. - pub fn keyboardColumn(p: *Pardes, id: usize) ?usize { - if (p.exec_column) |c| return c; - if (p.header_focus) if (p.header_column) |c| return c; - return if (layout.findPane(p, id)) |f| f.col else null; - } - pub fn emitWrite(p: *Pardes, id: usize, bytes: []const u8) void { var off: usize = 0; // Anything already parked for this pane owns the stream's position, so @@ -3926,63 +3687,6 @@ pub const Pardes = struct { p.setMessage(p.active, std.fmt.bufPrint(&buf, "WindowOpacity: {s}", .{reason[0..@min(reason.len, 240)]}) catch "WindowOpacity: compositor rejected the request"); } - /// 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 = p.keyboardColumn(p.active) == col; - const beside = if (had_focus) p.besideColumn(col, if (p.header_focus) null else p.rects[p.active]) else null; - var ids: [MAX_PANES]usize = undefined; - var parents: [MAX_PANES]*Pane = undefined; - const count = p.col_n[col]; - for (0..count) |i| { - ids[i] = p.col_panes[col][i]; - parents[i] = p.panes[ids[i]].?; - } - try panes.detachCwds(p, parents[0..count]); - for (ids[0..count], parents[0..count]) |closed, pane| { - // Kept for Panes; its column goes, so it comes back as a new pane. - if (p.store.keep(p, closed)) |k| p.store.close(p, k, null); - panes.retirePane(p, pane); - layout.removePane(p, closed); - p.panes[closed] = null; - } - layout.dropColumn(p, col); - // The last column gone, the window stays, empty but for the - // workspace tag, as acme's does; Newcol starts again from there. - if (p.panes[p.active] == null) p.active = beside orelse panes.anyPane(p) orelse return tagline.enterHeader(p, null); - } - - /// The pane that takes over the keyboard from column `col`: in the - /// column layout.dropColumn gives the width to, the one level with the - /// middle of `r` (of the screen without one). Asked while rects hold. - pub fn besideColumn(p: *const Pardes, col: usize, r: ?Rect) ?usize { - if (p.ncol < 2) return null; - const next = if (col > 0) col - 1 else col + 1; - const mid = if (r) |at| at.y + at.h / 2 else p.screen_h / 2; - var pick: ?usize = null; - for (p.col_panes[next][0..p.col_n[next]]) |k| { - if (p.panes[k] == null) continue; - pick = k; - if (p.rects[k].y +| p.rects[k].h > mid) break; - } - return pick; - } - - /// The rows pane `id`'s tag takes at its width, wrapped as `sync` wraps - /// it: what a placement must leave it above its body rows. - pub fn wantedTagRows(p: *Pardes, id: usize) u16 { - const pane = p.panes[id] orelse return BOX_H; - if (pane.collapsed or !pane.tag_expanded) return BOX_H; - if (p.rects[id].w == 0) return pane.tag_rows; // never laid out - var wrap_room: [32 * 1024]u8 = undefined; - var wrap_fba = std.heap.FixedBufferAllocator.init(&wrap_room); - const wa = wrap_fba.allocator(); - const prefix = tagline.prefixIn(p, wa, pane) catch return pane.tag_rows; - const whole = std.mem.concat(wa, u8, &.{ prefix, tagline.curTail(pane) }) catch return pane.tag_rows; - const shown = tagline.wrapTag(wa, whole, tagline.tagWidth(p, id)) catch return pane.tag_rows; - return @intCast(@max(BOX_H, @min(shown.rows(), MAX_TAG_ROWS))); - } - /// Recompute geometry, push grid-size changes to each emulator and pty, /// fire deferred greetings. pub fn sync(p: *Pardes) void { @@ -3997,7 +3701,7 @@ pub const Pardes = struct { // A file opened or closed this step: the kept list follows. p.saveRecent(); tagline.flattenHeaders(p); - if (p.header_column != null and p.columnBarHeight() == 0) { + if (p.header_column != null and layout.columnBarHeight(p) == 0) { tagline.exitHeader(p); } panes.reapPanes(p); @@ -4299,37 +4003,6 @@ pub const Pardes = struct { return p.haptic; } - /// A frontend's tag hit, if what it names is still there: resolved on - /// the place it carries (Layer.Place), not on this process's frame. - pub fn reprojectTagHit(p: *const Pardes, tag_hit: TagHit) ?TagHit { - if (tag_hit.kind == .pane) { - if (tag_hit.id >= p.panes.len) return null; - const pane = p.panes[tag_hit.id] orelse return null; - if (pane.serial != tag_hit.serial) return null; - } - if (tag_hit.kind == .column and (tag_hit.id >= p.ncol or p.col_serial[tag_hit.id] != tag_hit.serial)) return null; - return Layer.tagAgain(tag_hit); - } - - /// The pane's first body row: under the tag when it is on top, over it - /// when it is at the bottom. Notice bands do NOT move it -- they are an - /// overlay, drawn over the top rows rather than taking them. - pub fn bodyTop(p: *const Pardes, pane: *const Pane, r: Rect) u16 { - return if (p.settings.tag_bottom) r.y else r.y + pane.tag_rows; - } - - /// The tag's first row: the pane's top, or its last rows with Tagbottom. - pub fn tagTop(p: *const Pardes, pane: *const Pane, r: Rect) u16 { - return if (p.settings.tag_bottom) r.y + r.h -| pane.tag_rows else r.y; - } - - pub fn tagCapacity(p: *const Pardes, physical_width: u16) u16 { - const layer: Layer = .{ .kind = .pane, .viewport = .{ .x = 0, .y = 0, .w = physical_width, .h = 1 } }; - return layer.capacity(p.row_metrics); - } - - // ---- render: build the canonical surface (draw.zig) ---- - pub const render = draw.render; pub fn appendImagePlace(p: *Pardes, place: ImagePlace) bool { diff --git a/src/tagline.zig b/src/tagline.zig index e14feab4..0bb0018b 100644 --- a/src/tagline.zig +++ b/src/tagline.zig @@ -11,6 +11,7 @@ const exec = @import("exec.zig"); const config = @import("config.zig"); const pdf_enabled = panes.Pdf.enabled; const Layer = @import("Layer.zig"); +const layout = @import("layout.zig"); const Pane = panes.Pane; const Text = panes.Text; const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET; @@ -82,11 +83,11 @@ pub fn revealHeader(p: *Pardes) void { }; const text = edit.flatSurface(p, t) catch return; const cur: usize = @intCast(@max(0, t.cur_row)); - const rows: usize = if (p.header_column != null) p.columnBarHeight() else p.topBarHeight(); + const rows: usize = if (p.header_column != null) layout.columnBarHeight(p) else p.topBarHeight(); if (cur < p.header_top) p.header_top = cur; if (rows > 0 and cur >= p.header_top + rows) p.header_top = cur + 1 - rows; const line = modal.lineSlice(text, cur); - const width = p.tagCapacity(if (p.header_column) |c| p.col_w[c] -| TAG_TEXT_INSET else p.screen_w -| TAG_TEXT_INSET); + const width = layout.tagCapacity(p, if (p.header_column) |c| p.col_w[c] -| TAG_TEXT_INSET else p.screen_w -| TAG_TEXT_INSET); const col = panes.File.rawDisplayCol(line, @min(@as(usize, @intCast(@max(0, t.cur_col))), line.len)); if (col < p.header_scroll) p.header_scroll = col; if (col >= p.header_scroll + width) p.header_scroll = col -| (width -| 1); @@ -308,7 +309,7 @@ pub fn wrapTag(arena: std.mem.Allocator, whole: []const u8, width: usize) !Shown /// than what is left (libframe frutil.c _frcklinewrap), and the caret past /// its end sits at the edge (frptofchar.c clamps it to r.max.x). pub fn tagWidth(p: *const Pardes, id: usize) usize { - return @max(1, @as(usize, p.tagCapacity(p.rects[id].w -| TAG_TEXT_INSET))); + return @max(1, @as(usize, layout.tagCapacity(p, p.rects[id].w -| TAG_TEXT_INSET))); } pub fn shownTag(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !Shown { @@ -504,7 +505,7 @@ test "a column or workspace tag is one line: a newline in it is a space" { try std.testing.expectEqualStrings("Newcol Exit", p.global_tag.own.?); try std.testing.expectEqual(@as(i32, 0), p.col_tags[0].cur_row); try std.testing.expectEqual(@as(i32, 6), p.col_tags[0].cur_col); - try std.testing.expectEqual(pardes.COLBAR_H, p.columnBarHeight()); + try std.testing.expectEqual(pardes.COLBAR_H, layout.columnBarHeight(p)); try std.testing.expectEqual(pardes.TOPBAR_H, p.topBarHeight()); } |
