diff options
| -rw-r--r-- | abi/wire.zig | 3 | ||||
| -rw-r--r-- | core/Band.zig | 443 | ||||
| -rw-r--r-- | core/Core.zig | 4 | ||||
| -rw-r--r-- | core/Layer.zig | 3 | ||||
| -rw-r--r-- | core/TagState.zig | 40 | ||||
| -rw-r--r-- | core/draw.zig | 91 | ||||
| -rw-r--r-- | core/layout.zig | 9 | ||||
| -rw-r--r-- | core/mouse.zig | 38 | ||||
| -rw-r--r-- | core/panes.zig | 4 | ||||
| -rw-r--r-- | core/root.zig | 9 | ||||
| -rw-r--r-- | core/views.zig | 5 |
11 files changed, 635 insertions, 14 deletions
diff --git a/abi/wire.zig b/abi/wire.zig index e0a932ac..d07e6a57 100644 --- a/abi/wire.zig +++ b/abi/wire.zig @@ -12,7 +12,8 @@ const build_info = @import("build_info"); /// 36: a view's tag out of rest (walk UI: `View.tag_state`, its root's /// delay, its level unrolled and that level's cursor, the leaf each group -/// ran last) in the dump and the digest; +/// ran last) in the dump and the digest; a pointer's tag hit may be on a +/// pane's band (TagKind `band`, the rows its tag unrolls into); /// 35: a view's top is a line and a row of that line's wrapped rows: a /// file's `scroll_row` (and its share's in each view) in the dump and the /// digest; a dump without it loads at its lines' first rows; diff --git a/core/Band.zig b/core/Band.zig new file mode 100644 index 00000000..0e67bcf4 --- /dev/null +++ b/core/Band.zig @@ -0,0 +1,443 @@ +//! The band (walk UI, pick-ui SPEC.md §3.6): rows hanging under a pane's +//! tag while it is out of rest, in the tag's tint, pushing its body down: +//! the tag unrolled. Its steps stand side by side, each a column of rows +//! (a level, Help's rows, a doc; later a picker's acts and sources, a +//! prompt's history, the registers), a one-cell rule between them, right +//! of a spine under each of the head's key cells. +//! +//! Derived from the live view's state each time it is asked (`of`), never +//! stored (the coordinator's R2): the layout reads its height (`takes`, +//! Pardes.sync, layout.floorRows), the frame paints it (draw.zig +//! joinBand) as a tag layer of kind `.band`, and the mouse reads its words +//! (`line`): read-only text, which B2 executes and B3 looks at as a tag's. +//! +//! A new kind of step: a function that fills a `Step` (its `rows`, in the +//! frame's arena, as many as it shows, and whether it is a `doc`) from the +//! state that owns it, called from `of` for the tag state it belongs to, +//! with that state's rows in `height`; nothing else changes. +const Band = @This(); + +const std = @import("std"); +const pardes = @import("root.zig"); +const bindings = @import("bindings.zig"); +const tagline = @import("tagline.zig"); +const layout = @import("layout.zig"); +const modal = @import("modal.zig"); +const panes = @import("panes.zig"); +const builtins = @import("builtins.zig"); +const TagState = @import("TagState.zig"); +const Pardes = pardes.Pardes; +const Pane = pardes.Pane; +const File = panes.File; + +/// One row of a step: its text, as B2 and B3 read it, and how it is drawn. +pub const Row = struct { + text: []const u8, + /// The rows' cursor: `▶` before it and the selection's tint over it. + on: bool = false, + /// Its first `key` bytes are a key: the key ink, bold. + key: u8 = 0, + dim: bool = false, + /// The leaf its group ran last. + under: bool = false, + /// The bytes a query matched: the match tint. + marks: []const u16 = &.{}, + /// Its place in the list its step shows (a level's child, a hit): what + /// a click on it moves the cursor to. + index: u16 = 0, +}; + +/// A column of rows. A level of more than 9 rows flows into two columns +/// (`two`); a `doc` takes the width the others leave, wrapped to it, and is +/// the first to give way. +pub const Step = struct { + rows: []const Row = &.{}, + two: bool = false, + doc: bool = false, + /// Where it starts and how wide it is, in the band's columns (`place`). + x: u16 = 0, + width: u16 = 0, +}; + +pub const max_steps = 4; +pub const max_spines = 12; +/// The narrowest a step is drawn (SPEC §3.6: box, spines, one 24-cell column). +pub const min_width = 24; +/// A doc's rows, the fewest a band with one has. +const doc_rows = 4; +/// A level's rows past which it flows into two columns. +const one_column = 9; + +/// The columns of the head's key cells, in the band's columns: a spine +/// hangs under each. +spines: [max_spines]u16 = undefined, +nspines: u8 = 0, +steps: [max_steps]Step = undefined, +nsteps: u8 = 0, +height: u16 = 0, +/// The band's width at the tagline's pitch. +columns: u16 = 0, + +/// The rows the band under pane `id`'s tag takes in the live view, from +/// its state alone: the layout's term, asked before any frame is made. +pub fn takes(p: *Pardes, id: usize) u16 { + const v = p.view(); + const s = &v.tag_state; + if (id != v.active) return 0; + if (s.kind != .rest) return 0; + if (!s.unrolled or TagState.hostOf(p) != id) return 0; + const cur = TagState.current(p, v) orelse return 0; + var children: [bindings.max_children]bindings.Child = undefined; + const n: u16 = @intCast(bindings.level(p, cur.root, cur.path, &children).len); + return @max(doc_rows, levelRows(n)); +} + +fn levelRows(n: u16) u16 { + return if (n > one_column) (n + 1) / 2 else n; +} + +/// Pane `id`'s band as the live view's state has it, made in `arena`; +/// null when its tag has none. +pub fn of(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usize) !?Band { + const rows = takes(p, id); + if (rows == 0) return null; + const v = p.view(); + const s = &v.tag_state; + const cur = TagState.current(p, v).?; + var band: Band = .{ .height = rows, .columns = layout.tagCapacity(p, p.view().rects[id].w -| pardes.TAG_TEXT_INSET) }; + // The level, its cursor on the child it names, else the leaf its group + // ran last, else the first; and that child's doc beside it. + const node = bindings.nodeAt(cur.root, cur.path); + const used = if (node) |n| s.used[n.group] else 0; + var children: [bindings.max_children]bindings.Child = undefined; + const level = bindings.level(p, cur.root, cur.path, &children); + var at: usize = 0; + for (level, 0..) |c, i| if (c.key == (if (s.hi != 0) s.hi else used)) { + at = i; + }; + band.add(try levelStep(arena, pane, level, at, used)); + band.add(try docStep(p, arena, cur.root, cur.path, level[at])); + try band.place(p, arena, pane, id); + return band; +} + +fn add(band: *Band, step: Step) void { + if (band.nsteps == max_steps) return; + band.steps[band.nsteps] = step; + band.nsteps += 1; +} + +/// A level's children as rows (`k Label`, a group's `label…`), the cursor +/// on child `at`. +pub fn levelStep(arena: std.mem.Allocator, pane: *const Pane, level: []const bindings.Child, at: usize, used: u8) !Step { + const rows = try arena.alloc(Row, level.len); + for (level, rows, 0..) |c, *row, i| { + const key = bindings.keyName(c.key); + row.* = .{ + .text = try std.fmt.allocPrint(arena, "{s} {s}{s}", .{ key, c.label, if (c.group) "…" else "" }), + .on = i == at, + .key = @intCast(key.len), + .dim = bindings.dimmed(pane, c), + .under = c.key == used, + .index = @intCast(i), + }; + } + return .{ .rows = rows, .two = level.len > one_column }; +} + +/// What child `c` is: a leaf's word and its whole documentation, then its +/// keys; a group's children, a row each. +pub fn docStep(p: *Pardes, arena: std.mem.Allocator, root: bindings.Root, path: []const u8, c: bindings.Child) !Step { + var rows: std.ArrayList(Row) = .empty; + const key = bindings.keyName(c.key); + try rows.append(arena, .{ .text = try std.fmt.allocPrint(arena, "{s} {s}{s}", .{ key, c.label, if (c.group) "…" else "" }), .key = @intCast(key.len) }); + if (c.group) { + const here = try std.mem.concat(arena, u8, &.{ path, &.{c.key} }); + var below: [bindings.max_children]bindings.Child = undefined; + for (bindings.level(p, root, here, &below)) |g| { + const k = bindings.keyName(g.key); + try rows.append(arena, .{ .text = try std.fmt.allocPrint(arena, "{s} {s}{s}", .{ k, g.label, if (g.group) "…" else "" }), .key = @intCast(k.len) }); + } + return .{ .rows = rows.items, .doc = true }; + } + const doc = if (c.node) |n| switch (n.leaf.?) { + .builtin => |b| builtins.registry.doc(b).text, + .arg => |a| builtins.registry.doc(pardes.config.leader_args[a].cmd).text, + .role => bindings.docOf(root, c, try arena.alloc(u8, 160)), + } else bindings.docOf(root, c, &.{}); + try rows.append(arena, .{ .text = doc }); + var buf: [64]u8 = undefined; + const here = try std.mem.concat(arena, u8, &.{ path, &.{c.key} }); + try rows.append(arena, .{ .text = try std.fmt.allocPrint(arena, "keys {s}", .{bindings.chord(root, here, &buf)}) }); + return .{ .rows = rows.items, .doc = true }; +} + +/// Where each step goes: right of the spines, a rule's cell between steps; +/// a doc takes what is left, wrapped to it. Narrow, the doc goes first, +/// then the spines (the steps start at the band's left edge). +fn place(band: *Band, p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usize) !void { + const tab = p.settings.tab; + try band.findSpines(p, arena, pane, id); + var x: u16 = if (band.nspines > 0) band.spines[band.nspines - 1] + 2 else 0; + var need: u16 = 0; + for (band.steps[0..band.nsteps]) |*step| { + if (step.doc) continue; + var widest: usize = 0; + for (step.rows) |row| widest = @max(widest, File.displayWidth(tab, row.text)); + const column: u16 = @intCast(@min(widest + 2, std.math.maxInt(u16) / 4)); + step.width = if (step.two) 2 * column + 1 else column; + need += step.width + 2; + } + if (x + need > band.columns) { + band.nspines = 0; + x = 0; + } + var kept: u8 = 0; + for (band.steps[0..band.nsteps]) |step| { + var placed = step; + if (step.doc) { + const room = band.columns -| x; + if (room < min_width) continue; + placed.width = room; + placed.rows = try wrap(arena, tab, step.rows, room -| 1, band.height); + } + placed.x = x; + x +|= placed.width + 2; + band.steps[kept] = placed; + kept += 1; + } + band.nsteps = kept; +} + +/// The display columns of the head's key cells, when they are all on the +/// tag's last row (the spines hang from there). +fn findSpines(band: *Band, p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usize) !void { + const h = (try TagState.head(p, arena, pane)) orelse return; + const whole = try tagline.tagText(p, arena, pane); + const shown = try tagline.wrapTag(p.settings.tab, arena, whole, tagline.tagWidth(p, id)); + const lead = whole.len - h.text.len; + const last = shown.rows() -| 1; + for (h.spans[0..h.cells]) |span| { + const at = modal.positionAt(shown.text, shown.fromReal(lead + span.lo)); + if (at.row != last or band.nspines == max_spines) { + band.nspines = 0; + return; + } + band.spines[band.nspines] = @intCast(File.rawDisplayCol(p.settings.tab, modal.lineSlice(shown.text, at.row), at.col)); + band.nspines += 1; + } +} + +/// `rows` wrapped at `width` display columns, word by word, `height` rows +/// at most: a doc's paragraphs. +fn wrap(arena: std.mem.Allocator, tab: u16, rows: []const Row, width: u16, max: u16) ![]const Row { + var out: std.ArrayList(Row) = .empty; + for (rows) |row| { + if (File.displayWidth(tab, row.text) <= width) { + try out.append(arena, row); + } else { + var rest = row.text; + while (rest.len > 0 and out.items.len < max) { + var cut = File.byteAtDisplay(tab, rest, width); + if (cut < rest.len) { + if (std.mem.lastIndexOfScalar(u8, rest[0..cut], ' ')) |space| { + if (space > 0) cut = space; + } + } + if (cut == 0) cut = modal.nextGrapheme(rest, 0); + try out.append(arena, .{ .text = std.mem.trimEnd(u8, rest[0..cut], " ") }); + rest = std.mem.trimStart(u8, rest[cut..], " "); + } + } + if (out.items.len >= max) break; + } + if (out.items.len > max) out.shrinkRetainingCapacity(max); + return out.items; +} + +/// One row of the band as text, and where each step's row starts in it. +pub const Line = struct { + text: []const u8, + parts: []const Part, + pub const Part = struct { col: u16, width: u16, row: Row }; +}; + +/// Row `y`: the spines, then each step's row, padded to its width, a rule +/// between steps. +pub fn line(band: *const Band, arena: std.mem.Allocator, tab: u16, y: u16) !Line { + var text: std.ArrayList(u8) = .empty; + var parts: std.ArrayList(Line.Part) = .empty; + var col: u16 = 0; + for (band.spines[0..band.nspines]) |spine| { + while (col < spine) : (col += 1) try text.append(arena, ' '); + try text.appendSlice(arena, "│"); + col += 1; + } + for (band.steps[0..band.nsteps], 0..) |step, i| { + if (i > 0) { + while (col + 2 < step.x) : (col += 1) try text.append(arena, ' '); + try text.appendSlice(arena, "│ "); + col += 2; + } + while (col < step.x) : (col += 1) try text.append(arena, ' '); + const per = if (step.two) (step.width - 1) / 2 else step.width; + const shown: u16 = if (step.two) levelRows(@intCast(step.rows.len)) else @intCast(step.rows.len); + var lane: u16 = 0; + while (lane < @as(u16, if (step.two) 2 else 1)) : (lane += 1) { + const k = y + lane * shown; + const start = step.x + lane * (per + 1); + while (col < start) : (col += 1) try text.append(arena, ' '); + if (y >= shown or k >= step.rows.len) continue; + const row = step.rows[k]; + const lead: []const u8 = if (step.doc) "" else if (row.on) "▶ " else " "; + try text.appendSlice(arena, lead); + col += @intCast(File.displayWidth(tab, lead)); + try parts.append(arena, .{ .col = col, .width = per -| @as(u16, @intCast(File.displayWidth(tab, lead))), .row = row }); + const room = per -| @as(u16, @intCast(File.displayWidth(tab, lead))); + const cut = File.byteAtDisplay(tab, row.text, room); + try text.appendSlice(arena, row.text[0..cut]); + col += @intCast(File.displayWidth(tab, row.text[0..cut])); + } + } + return .{ .text = text.items, .parts = parts.items }; +} + +/// The word under column `col` of band row `y` of pane `id`, at the +/// tagline's pitch: what B2 executes and B3 looks at. +pub fn wordAt(p: *Pardes, arena: std.mem.Allocator, id: usize, y: u16, col: u16) !?[]const u8 { + const pane = p.panes[id] orelse return null; + const band = (try of(p, arena, pane, id)) orelse return null; + if (y >= band.height) return null; + const ln = try band.line(arena, p.settings.tab, y); + const at = File.rawAtDisplay(p.settings.tab, ln.text, col); + const b = tagline.wordBoundsAtCol(ln.text, at) orelse return null; + if (b.lo == b.hi) return null; + return ln.text[b.lo..b.hi]; +} + +fn bandLayer(p: *Pardes, id: usize) ?*pardes.Layer { + const layer = &p.surface.tag_layers[pardes.BAND_LAYER_BASE + id]; + return if (layer.rows > 0 and layer.kind == .band) layer else null; +} + +fn layerRow(arena: std.mem.Allocator, layer: *const pardes.Layer, row: usize) ![]const u8 { + var out: std.ArrayList(u8) = .empty; + for (layer.cells[row * layer.cols ..][0..layer.cols]) |cell| try out.appendSlice(arena, cell.grapheme()); + return out.items; +} + +test "SPC Down unrolls the level into a band under the tag's text, the body pushed down and never drawn over; Up on its first row rolls it up" { + const Key = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + const pane = try p.setTestFile("one\ntwo\nthree\n"); + const id = p.view().active; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + p.sync(); + const r = p.view().rects[id]; + const text_rows = pane.tag_rows; + const body_top = p.bodyTop(pane, r); + _ = try p.render(frame.allocator()); + try std.testing.expect(bandLayer(p, id) == null); + p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = Key.down } }); + try std.testing.expect(p.view().tag_state.unrolled); + // SPC's top has more than nine children: two columns, and a doc. + try std.testing.expect(pane.band_rows >= doc_rows); + try std.testing.expectEqual(text_rows + pane.band_rows, pane.tag_rows); + try std.testing.expectEqual(body_top + pane.band_rows, p.bodyTop(pane, r)); + _ = frame.reset(.retain_capacity); + const s = try p.render(frame.allocator()); + const layer = bandLayer(p, id).?; + try std.testing.expectEqual(p.tagTop(pane, r) + text_rows, layer.viewport.y); + try std.testing.expectEqual(pane.band_rows, layer.viewport.h); + // The body region starts under the band: nothing is drawn over it. + const body = s.region(.body, id).?; + try std.testing.expectEqual(layer.viewport.y + layer.viewport.h, body.rect.y); + const first = try layerRow(frame.allocator(), layer, 0); + try std.testing.expect(std.mem.indexOf(u8, first, "│") != null); + try std.testing.expect(std.mem.indexOf(u8, first, "▶ b buffer…") != null); + // The grid has the same row, where the layer is. + var grid: std.ArrayList(u8) = .empty; + for (s.cells[@as(usize, layer.viewport.y) * s.cols ..][layer.viewport.x..][0..layer.viewport.w]) |cell| try grid.appendSlice(frame.allocator(), cell.grapheme()); + try std.testing.expect(std.mem.indexOf(u8, grid.items, "▶ b buffer…") != null); + // The head names the level and says how to roll it up. + try std.testing.expect(std.mem.endsWith(u8, try tagline.tagText(p, frame.allocator(), pane), " ␣ SPC ↑ roll up · ? field · esc")); + // Down moves the cursor; Up back to the first row, then rolls up. + p.update(.{ .key = .{ .cp = Key.down } }); + try std.testing.expectEqual(@as(u8, 'c'), p.view().tag_state.hi); + p.update(.{ .key = .{ .cp = Key.up } }); + try std.testing.expect(p.view().tag_state.unrolled); + p.update(.{ .key = .{ .cp = Key.up } }); + try std.testing.expect(!p.view().tag_state.unrolled); + try std.testing.expectEqual(@as(u16, 0), pane.band_rows); + try std.testing.expectEqual(body_top, p.bodyTop(pane, r)); + // Unrolled, Right enters the cursor's group and Left leaves it. + p.update(.{ .key = .{ .cp = Key.down } }); + p.update(.{ .key = .{ .cp = Key.right } }); + try std.testing.expectEqualStrings("b", p.view().leader_keys[0..p.view().leader_n]); + try std.testing.expect(p.view().tag_state.unrolled); + p.update(.{ .key = .{ .cp = Key.left } }); + try std.testing.expectEqual(@as(u8, 0), p.view().leader_n); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(@as(u16, 0), pane.band_rows); + try std.testing.expect(!p.view().tag_state.unrolled); +} + +test "a pane whose body would drop below four rows under its band borrows them from its column" { + const Key = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 24 }); + defer p.deinit(); + _ = try p.setTestFile("one\n"); + const top = p.view().active; + p.newScratchBelow(top); + const low = p.view().active; + p.newScratchBelow(low); + p.view().active = low; + p.sync(); + const before = p.view().rects[low].h; + p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = Key.down } }); + const pane = p.panes[low].?; + const r = p.view().rects[low]; + try std.testing.expect(r.h > before); + try std.testing.expect(r.h -| pane.tag_rows >= 4); + // The other panes dim; the one out of rest does not. + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const s = try p.render(frame.allocator()); + const other = p.view().rects[top]; + try std.testing.expect(s.at(other.x + 3, other.y).style.dim); + try std.testing.expect(!s.at(r.x + 3, r.y).style.dim); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(before, p.view().rects[low].h); +} + +test "the band's words are read-only text: B2 runs one, the tag back at rest" { + const Key = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + _ = try p.setTestFile("one\n"); + const id = p.view().active; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + for ([_]u21{ ' ', 't', Key.down }) |c| p.update(.{ .key = .{ .cp = c } }); + const s = try p.render(frame.allocator()); + const layer = bandLayer(p, id).?; + // Find `Wrap` in the grid's copy of the band. + const at = found: for (0..layer.rows) |row| { + const y = layer.viewport.y + row; + var x: u16 = layer.viewport.x; + while (x + 4 < layer.viewport.x + layer.viewport.w) : (x += 1) { + const cells = s.cells[@as(usize, y) * s.cols + x ..][0..4]; + if (std.mem.eql(u8, cells[0].grapheme(), "W") and std.mem.eql(u8, cells[1].grapheme(), "r") and std.mem.eql(u8, cells[2].grapheme(), "a") and std.mem.eql(u8, cells[3].grapheme(), "p")) + break :found .{ x + 1, @as(u16, @intCast(y)) }; + } + } else return error.NoWrapInTheBand; + const wrapped = p.settings.wrap; + p.update(.{ .mouse = .{ .button = .middle, .kind = .press, .col = at[0], .row = at[1] } }); + p.update(.{ .mouse = .{ .button = .middle, .kind = .release, .col = at[0], .row = at[1] } }); + try std.testing.expect(p.settings.wrap != wrapped); + try std.testing.expect(p.view().prefix == .none and !p.view().tag_state.unrolled); +} diff --git a/core/Core.zig b/core/Core.zig index d0c9976d..11f254ab 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -2037,6 +2037,10 @@ pub fn sync(p: *Pardes) void { if (cur >= top + pane.tag_rows) top = cur + 1 - pane.tag_rows; pane.tag_top = @intCast(@min(top, lines - pane.tag_rows)); } else pane.tag_top = 0; + // The band unrolled under the tag's text pushes the body down: + // the tag's rows, as everything under them counts them. + pane.band_rows = if (pane.collapsed) 0 else @min(pardes.Band.takes(p, id), r.h -| pane.tag_rows); + pane.tag_rows += pane.band_rows; const cols = @max(1, r.w -| config.GUTTER); // The tag steals the top rows. The pty is what the pane shows // (layout keeps a terminal two body rows where the window has diff --git a/core/Layer.zig b/core/Layer.zig index 8128543a..4574aab7 100644 --- a/core/Layer.zig +++ b/core/Layer.zig @@ -20,7 +20,8 @@ const Layer = @This(); /// chord -- carried as a tagline-shaped band so it gets the tagline's height, /// small-font metrics, band offset and border instead of being a body-grid row /// wearing a tagline font. -pub const Kind = enum(u8) { workspace, column, pane, notice, body }; +/// `band` is the rows a pane's tag unrolls into below it (Band.zig). +pub const Kind = enum(u8) { workspace, column, pane, notice, body, band }; pub const Metrics = struct { body_w: u16 = 1, diff --git a/core/TagState.zig b/core/TagState.zig index 3c400b3a..daf05b98 100644 --- a/core/TagState.zig +++ b/core/TagState.zig @@ -39,6 +39,10 @@ miss: u8 = 0, /// The leaf each group ran last, by its key (bindings.groups): the cursor /// starts there and the rolled level underlines it (SPEC §4.3). used: [bindings.ngroups]u8 = @splat(0), +/// The panes showing a preview of what the state would do (a picker's +/// landing pane, a pane an action draws ghosts in), by slot: not dimmed +/// while the others are (SPEC §3.6). Set by the state that previews. +previewing: std.StaticBitSet(pardes.MAX_PANES) = .empty, /// A root struck, its time not had yet (as Pane.question_asked: the step /// that strikes it reads no clock). @@ -107,6 +111,25 @@ pub fn follow(p: *Pardes, before: ?Root) void { s.due_ns = if (now == null) 0 else asked; } +/// Back to rest in the live view: no root waits, no typing state, nothing +/// unrolled (a word run from the band, Esc in a typing state). +pub fn toRest(p: *Pardes) void { + const v = p.view(); + v.prefix = .none; + v.window_pick = null; + if (p.panes[v.active]) |pane| { + pane.body.normal.clear(); + pane.tag.normal.clear(); + } + const s = &v.tag_state; + s.kind = .rest; + s.root = null; + s.due_ns = 0; + s.unrolled = false; + s.hi = 0; + s.miss = 0; +} + /// The root a key strikes when its time is had (Pardes.advance). pub fn dated(p: *Pardes, now_ns: u64) void { for (&p.views) |*slot| if (slot.*) |*v| { @@ -145,6 +168,16 @@ pub fn lastUsed(p: *const Pardes, v: *const View, buf: []u8) ?[]const u8 { return null; } +/// The cursor put on row `index` of the band's first step (a click on it). +pub fn point(p: *Pardes, index: u16) void { + const v = p.view(); + const s = &v.tag_state; + const cur = current(p, v) orelse return; + var children: [bindings.max_children]bindings.Child = undefined; + const level = bindings.level(p, cur.root, cur.path, &children); + if (index < level.len) s.hi = level[index].key; +} + /// Remember `key` as the leaf run last at `path` under `root`. pub fn ran(v: *View, root: Root, path: []const u8, key: u8) void { const node = bindings.nodeAt(root, path) orelse return; @@ -159,6 +192,9 @@ pub const Span = struct { lo: u32, hi: u32, style: Style }; pub const Head = struct { text: []const u8, spans: []const Span = &.{}, + /// Its first `cells` spans are its key cells, which the band's spines + /// hang from (Band.zig). + cells: u8 = 0, caret: ?u32 = null, }; @@ -166,6 +202,7 @@ const Builder = struct { arena: std.mem.Allocator, text: std.ArrayList(u8) = .empty, spans: std.ArrayList(Span) = .empty, + cells: u8 = 0, fn put(b: *Builder, s: []const u8) !void { try b.text.appendSlice(b.arena, s); @@ -183,9 +220,10 @@ const Builder = struct { try b.styled(if (k == ' ') "␣" else bindings.keyName(k), .key); try b.put(" "); } + b.cells = @intCast(b.spans.items.len); } fn done(b: *const Builder) Head { - return .{ .text = b.text.items, .spans = b.spans.items }; + return .{ .text = b.text.items, .spans = b.spans.items, .cells = b.cells }; } }; diff --git a/core/draw.zig b/core/draw.zig index de08ef4f..37fdee1e 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -360,6 +360,18 @@ pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { for (s.regionList()) |region| { if (region.kind == .notice) try joinNotice(p, s, arena, region); } + // A tag out of rest: the other panes dim a little so the eye goes to + // it, but a pane showing a preview (walk UI SPEC §3.6). + if (TagState.hostOf(p)) |host| for (p.panes, 0..) |slot, id| { + if (slot == null or id == host or v.tag_state.previewing.isSet(id)) continue; + const r = v.rects[id]; + for (r.y..r.y + r.h) |y| for (r.x..r.x + r.w) |x| { + s.at(@intCast(x), @intCast(y)).style.dim = true; + }; + for ([_]*pardes.Layer{ &s.body_layers[id], &s.tag_layers[id] }) |layer| for (layer.cells[0 .. @as(usize, layer.cols) * layer.rows]) |*cell| { + cell.style.dim = true; + }; + }; try paintHeaders(p, s); @@ -483,6 +495,7 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: }, box_style); try joinPaneTag(p, s, arena, pane, r, id, active); + if (pane.band_rows > 0) try joinBand(p, s, arena, pane, r, id, active); // Folded panes retain their body state, but neither body pixels nor // its cursor belong outside the one-row tag rectangle. @@ -667,7 +680,7 @@ fn renderHeaderLayer(p: *Pardes, layer: *pardes.Layer, kind: TagKind, id: u16, v const editing: ?*Text = if (v.header != null and switch (kind) { .workspace => p.headerColumn() == null, .column => p.headerColumn() == id, - .pane, .notice, .body => false, + .pane, .notice, .body, .band => false, }) tagline.focusedHeader(p) else null; const scroll = if (editing != null) v.header_scroll else 0; for (0..viewport.h) |index| { @@ -719,7 +732,7 @@ fn renderHeaderLayer(p: *Pardes, layer: *pardes.Layer, kind: TagKind, id: u16, v const mine = switch (kind) { .workspace => column == null, .column => column == id, - .pane, .notice, .body => false, + .pane, .notice, .body, .band => false, }; const swept = sweep.span(text.len); const start = modal.lineStartOffset(text, index + top); @@ -747,7 +760,8 @@ fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r const chrome = p.chromeTheme(); const region = s.region(.tag, id).?; const tag = (try tagline.shownTag(p, arena, pane)).text; - const rows = pane.tag_rows; + // The tag's text rows; the band's under them are its own layer. + const rows = pane.tag_rows - pane.band_rows; // As wide as the widest row it shows, and never narrower than its // viewport. var widest: usize = 0; @@ -794,13 +808,82 @@ fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r } } +/// The band under pane `id`'s tag text (Band.zig), painted once into its +/// layer at the tagline's pitch, a row to a line, and copied into the +/// grid: the spines and steps in the tag's tint, the keys in the key ink, +/// the cursor's row tinted as a selection. +fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool) !void { + const layer = &s.tag_layers[pardes.BAND_LAYER_BASE + id]; + if (r.w <= TAG_TEXT_INSET) return; + const band = (try pardes.Band.of(p, arena, pane, id)) orelse return; + const th = p.theme(); + const chrome = p.chromeTheme(); + const tab = p.settings.tab; + const rows = pane.band_rows; + const columns = @max(band.columns, 1); + const bg = if (active) chrome.tag_active_bg else chrome.tag_bg; + const fg = if (active) chrome.tag_active_fg else chrome.tag_fg; + const storage = try arena.alloc(Cell, @as(usize, columns) * rows); + @memset(storage, .{}); + var t: Surface = .{ .cols = columns, .rows = rows, .cells = storage, .tab = tab }; + for (0..rows) |y| { + const row: u16 = @intCast(y); + t.fill(0, row, columns, 1, .{ .bg = .{ .rgb = bg }, .font_role = .tagline }); + const ln = try band.line(arena, tab, row); + _ = t.print(0, row, columns, ln.text, .{ .fg = .{ .rgb = fg }, .bg = .{ .rgb = bg }, .font_role = .tagline }); + // The spines and the rules between steps: the key ink. + var col: u16 = 0; + var at: usize = 0; + while (at < ln.text.len and col < columns) : (col += 1) { + const next = modal.nextGrapheme(ln.text, at); + if (std.mem.eql(u8, ln.text[at..next], "│")) t.at(col, row).style.fg = .{ .rgb = chrome.box }; + if (std.mem.eql(u8, ln.text[at..next], "▶")) t.at(col, row).style.fg = .{ .rgb = chrome.box }; + at = next; + } + for (ln.parts) |part| { + const end = @min(columns, part.col + part.width); + if (part.row.on and part.col < end) t.tint(part.col, row, end - part.col, th.tag_sel_bg orelse th.sel_bg, th.sel_fg); + var c = part.col; + var b: usize = 0; + while (b < part.row.text.len and c < end) : (c += 1) { + const next = modal.nextGrapheme(part.row.text, b); + const style = &t.at(c, row).style; + if (b < part.row.key) { + style.fg = .{ .rgb = chrome.box }; + style.bold = true; + } + if (part.row.dim) style.dim = true; + if (part.row.under) style.ul = .single; + for (part.row.marks) |m| if (m == b) { + style.bg = .{ .rgb = chrome.search_bg }; + }; + b = next; + } + } + } + const count = @as(usize, columns) * rows; + if (layer.cells.len != count) layer.cells = try p.gpa.realloc(layer.cells, count); + @memcpy(layer.cells, storage); + const y0 = p.tagTop(pane, r) + (pane.tag_rows - rows); + layer.kind = .band; + layer.id = @intCast(id); + layer.serial = pane.serial; + layer.viewport = .{ .x = r.x + TAG_TEXT_INSET, .y = y0, .w = r.w - TAG_TEXT_INSET, .h = rows }; + layer.cols = columns; + layer.rows = rows; + layer.context_rows = 0; + layer.bg = bg; + layer.cursor = null; + for (0..rows) |y| joinCells(s, r.x + TAG_TEXT_INSET, y0 + @as(u16, @intCast(y)), storage[y * columns ..][0..columns], 0, r.w - TAG_TEXT_INSET, .{ .rgb = fg }); +} + fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool) !void { const th = p.theme(); const chrome = p.chromeTheme(); const tx = r.x + @min(TAG_TEXT_INSET, r.w); const tw = r.w -| TAG_TEXT_INSET; const tag_y = p.tagTop(pane, r); - const rows = pane.tag_rows; + const rows = pane.tag_rows - pane.band_rows; const page_rgb = th.bg orelse th.tag_bg; // Focus is visible along the tag without changing its live text grid. const tag_bg = if (active) chrome.tag_active_bg else chrome.tag_bg; diff --git a/core/layout.zig b/core/layout.zig index 7fa57ee9..f9b9ef81 100644 --- a/core/layout.zig +++ b/core/layout.zig @@ -1429,9 +1429,14 @@ pub fn compute(p: *Pardes) void { /// The fewest rows a pane is laid out with: a terminal keeps its tag and /// `min_body_rows` (a shorter one loses its prompt and reads busy for -/// ever), a text pane may shrink to its tag, as acme's windows do. +/// ever), a text pane may shrink to its tag, as acme's windows do. A tag +/// out of rest keeps its band and four body rows, borrowed from the +/// column's other panes, which shrink to their tags until it rolls up +/// (walk UI SPEC §3.6). fn floorRows(p: *Pardes, id: usize) u16 { const pane = p.panes[id] orelse return 0; + const band = pardes.Band.takes(p, id); + if (band > 0) return @max(1, pane.tag_rows -| pane.band_rows) + band + 4; return if (pane.isTerminal()) @max(1, pane.tag_rows) + min_body_rows else 1; } @@ -1630,7 +1635,7 @@ pub fn tagCapacity(p: *const Pardes, physical_width: u16) u16 { /// 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.kind == .pane or tag_hit.kind == .band) { 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; diff --git a/core/mouse.zig b/core/mouse.zig index 14700995..7a918883 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -691,6 +691,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { if (m.kind == .press) { tagline.exitHeader(p); } + if (m.kind == .press) if (bandPoint(p, mcol, mrow)) |point| return bandPress(p, m.button, point); switch (m.button) { .none, .back, .forward => {}, // hover and navigation handled above @@ -871,6 +872,43 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { } } +const BandPoint = struct { id: usize, line: u16, col: u16 }; + +/// The band row and column (at the tagline's pitch) a press is on: a +/// window's layer hit, or a terminal's grid cell under a tag's text rows. +fn bandPoint(p: *Pardes, mcol: u16, mrow: u16) ?BandPoint { + const v = p.view(); + if (v.pointer_tag_hit) |hit| return if (hit.kind == .band) .{ .id = hit.id, .line = hit.line, .col = hit.col } else null; + const id = paneAt(p, mcol, mrow) orelse return null; + const pane = p.panes[id].?; + if (pane.band_rows == 0) return null; + const r = v.rects[id]; + const top = p.tagTop(pane, r) + (pane.tag_rows - pane.band_rows); + if (mrow < top or mrow >= top + pane.band_rows or mcol < r.x + TAG_TEXT_INSET) return null; + return .{ .id = id, .line = mrow - top, .col = mcol - (r.x + TAG_TEXT_INSET) }; +} + +/// A press on a band (Band.zig), read-only text: the select button puts +/// the rows' cursor on the row it is on; execute runs the word under it +/// and look looks at it, as on a tag, the tag back at rest first. +fn bandPress(p: *Pardes, button: Mouse.Button, point: BandPoint) void { + const v = p.view(); + const arena = p.scratch.allocator(); + if (button == config.select_button) { + const band = (pardes.Band.of(p, arena, p.panes[point.id].?, point.id) catch return) orelse return; + const ln = band.line(arena, p.settings.tab, point.line) catch return; + for (ln.parts) |part| if (point.col >= part.col -| 2 and point.col < part.col + part.width) { + pardes.TagState.point(p, part.row.index); + }; + return; + } + if (button != config.exec_button and button != config.look_button) return; + const word = (pardes.Band.wordAt(p, arena, point.id, point.line, point.col) catch return) orelse return; + const kept = arena.dupe(u8, word) catch return; + pardes.TagState.toRest(p); + exec.runBuiltin(p, if (button == config.exec_button) config.exec_cmd else config.look_cmd, v.active, "", kept); +} + /// Index of the pane pair in column `c` whose seam (see seamRowOf) is screen /// row `mrow`. Out-of-range columns simply have no seam. fn seamIdxAt(p: *const Pardes, c: usize, mrow: u16) ?usize { diff --git a/core/panes.zig b/core/panes.zig index 8939bd05..20feb0b3 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -31,7 +31,7 @@ pub const Pane = struct { /// it (views.zig). (The 9P server's part of `fs` says so in its type.) pub const replica = .{ .local = .{ "pv", "pv_known" }, - .derived = .{ "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "top_skip", "notices", "pan" }, + .derived = .{ "band_rows", "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "top_skip", "notices", "pan" }, .len = .{ .{ "msg", "msg_len" }, .{ "msg_older", "msg_older_len" }, .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } }, // A picture's memory is its own allocator's (a dump loaded makes // it there). @@ -288,7 +288,9 @@ pub const Pane = struct { /// Rows the tag takes on screen: one per line of it, as many as the pane /// has room for with a body row to spare. Set with the geometry in sync. /// Pointer rows count from the tag's first, so a body starts at this one. + /// Its last `band_rows` are the band unrolled under its text (Band.zig). tag_rows: u16 = 1, + band_rows: u16 = 0, /// The tag's first line on screen. A tag with more lines than rows /// scrolls to keep its caret in view while it has the keyboard; pointer /// rows count from this line, not the tag's first. diff --git a/core/root.zig b/core/root.zig index e8938fd9..bdec2d49 100644 --- a/core/root.zig +++ b/core/root.zig @@ -91,6 +91,7 @@ pub const Term = @import("Term.zig"); pub const keys = @import("keys.zig"); pub const bindings = @import("bindings.zig"); pub const TagState = @import("TagState.zig"); +pub const Band = @import("Band.zig"); pub const Addon = @import("Addon.zig"); pub const dump = @import("dump.zig"); pub const lsp = @import("lsp/lsp.zig"); @@ -140,9 +141,11 @@ pub const TagKind = Layer.Kind; /// too long for its width. pub const MAX_TAG_ROWS = 8; /// Panes, then columns, then the workspace bar, then each pane's notice -/// bands: a band to a tag, however many lines it has. -pub const MAX_TAG_LAYERS = MAX_PANES + MAX_COLS + 1 + MAX_PANES * Pane.Notices.max; +/// bands: a band to a tag, however many lines it has; then each pane's +/// rows unrolled under its tag (Band.zig). +pub const MAX_TAG_LAYERS = BAND_LAYER_BASE + MAX_PANES; pub const NOTICE_LAYER_BASE = MAX_PANES + MAX_COLS + 1; +pub const BAND_LAYER_BASE = NOTICE_LAYER_BASE + MAX_PANES * Pane.Notices.max; /// Columns a session holds (each costs about 8 KB of state, a column tag /// being most of it). pub const MAX_COLS = 16; @@ -408,6 +411,7 @@ test { _ = @import("keys.zig"); _ = @import("bindings.zig"); _ = @import("TagState.zig"); + _ = @import("Band.zig"); _ = @import("Effect.zig"); _ = @import("tape.zig"); _ = @import("replica.zig"); @@ -759,6 +763,7 @@ const kept_out = [_]struct { []const u8, []const u8, []const u8 }{ .{ "panes.zig", "pv pv_known", "the views' shares as this process parks them (views.zig)" }, .{ "panes.zig", "wrap_line wrap_col wrap_pad wrap_n body_wrapped context_rows body_rows body_visible_rows context_row_limit geometry_key geometry_len top_skip", "File.geometry's, made by whoever asks first (a frame or a step) from everything its key hashes of the step's state (the tab's width too); top_skip, keyed by the geometry it is of, read only through File.topSkip, which walks one of another key again" }, .{ "panes.zig views.zig", "notices", "made again from the step's state at the top of update and presented, and at sync (Messages.collectNotices); a frame's are its own" }, + .{ "panes.zig views.zig", "band_rows", "the band's rows under a tag (Band.takes), made again from the view's tag state at every sync before anything reads them (layout.floorRows reads the last sync's, as it reads tag_rows)" }, .{ "panes.zig views.zig", "pan", "written by steps only (sync, a view unparked) from the pane and its terminal" }, .{ "File.zig", "cap line_starts line_cap widest", "the text's line index, of its bytes, and the widest line it found (an upper bound: which walk answers, never what)" }, .{ "File.zig", "highlights highlight_start syntax_dirty", "how a text looks, painted by frames: read by frames, and by a query's rows for colours only (out of their digest; what a step asks a worker to paint is never decided by them, Stream.paintRows)" }, diff --git a/core/views.zig b/core/views.zig index e5f2038e..b05a116b 100644 --- a/core/views.zig +++ b/core/views.zig @@ -41,7 +41,7 @@ pub const away_kept_ns: u64 = 30 * std.time.ns_per_s; /// layout, which of its texts has the keyboard, how far it is scrolled (a /// terminal's history too), and what the view's step measured of it (its /// tag's rows, its bands). -const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "docked_to", "hscroll", "tag_scroll", "tag_top", "tag_rows", "view", "layers", "notices", "pan", "follow_scroll", "scrolled_to" }; +const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "docked_to", "hscroll", "tag_scroll", "tag_top", "tag_rows", "band_rows", "view", "layers", "notices", "pan", "follow_scroll", "scrolled_to" }; /// The body's: its cursor, selections and mode. Its undo stays the pane's. const body_fields = [_][:0]const u8{ "mode", "cur_pinned", "cur_row", "cur_col", "vsel", "sels", "nsel", "select", "sticky_col", "restore_cursor", "last_edit", "reg_wait", "normal", "find_op", "find_ch" }; @@ -56,6 +56,7 @@ pub const PaneView = struct { tag_scroll: u16 = 0, tag_top: u16 = 0, tag_rows: u16 = 1, + band_rows: u16 = 0, view: Pane.SessionView = .{}, layers: File.Layers = .{}, notices: Pane.Notices = .{}, @@ -73,7 +74,7 @@ pub const PaneView = struct { /// In a dump (`Shares`): what a pane's step works out again. Its `view` /// is dumped: the step after `unpark` reads it before it syncs. - pub const replica = .{ .derived = .{ "notices", "pan" } }; + pub const replica = .{ .derived = .{ "notices", "pan", "band_rows" } }; }; /// The panes' shares of a view not live, as a session's dump keeps them |
