diff options
Diffstat (limited to 'core/Band.zig')
| -rw-r--r-- | core/Band.zig | 251 |
1 files changed, 188 insertions, 63 deletions
diff --git a/core/Band.zig b/core/Band.zig index cd4ca75b..8ee5c9f0 100644 --- a/core/Band.zig +++ b/core/Band.zig @@ -1,13 +1,14 @@ //! 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 +//! tag while it is out of rest, in the tag's tint, drawn over the top rows +//! of its body, which stay where they were (UX DESIGN I1): 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 +//! `covered`, 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. //! @@ -131,20 +132,24 @@ pub fn takes(p: *Pardes, id: usize) u16 { return @max(doc_rows, levelRows(n)); } -/// A terminal's and a kind's band is drawn over the top rows of its body -/// (UX D6's borrowing, R3: drawn, never written): a transient state never -/// resizes a pty under a program or a kind's view (a PDF's), whose size -/// only the user's resize changes. Their `band_rows` stay 0. -pub fn over(pane: *const Pane) bool { - return pane.isTerminal() or pane.addon != null; -} - -/// The rows pane `id`'s band is drawn on: those it takes from the layout, -/// or over a terminal's or a kind's body, as many of its body rows as the -/// band wants. -pub fn shownRows(p: *Pardes, pane: *const Pane, id: usize) u16 { - if (pane.band_rows > 0 or !over(pane) or pane.collapsed) return pane.band_rows; - return @min(takes(p, id), p.view().rects[id].h -| pane.tag_rows); +/// Where pane `id`'s tag out of rest is drawn, in rows from its top (UX +/// DESIGN I1): its head's text (`text`, the rows its own text keeps from +/// rest at least where no band hangs under it) and the band under that +/// (`band`), as many as the pane has; `over` of them past the tag's own +/// rows (`tag_rows`, its rows at rest), drawn over the top rows of its +/// body, which never move for them. A picker's band keeps a body of +/// `body_rows` under it (UX D6), any other state only the pane's rows. +/// Asked of the state alone, as `takes` is: the layout, the frame and a +/// click ask it of the same step. +pub const Covered = struct { text: u16, band: u16, over: u16 }; +pub fn covered(p: *Pardes, pane: *const Pane, id: usize) Covered { + if (pane.collapsed or TagState.hostOf(p) != id) return .{ .text = pane.tag_rows, .band = 0, .over = 0 }; + const h = p.view().rects[id].h; + const text = @min(layout.headRows(p, id), h); + const keep: u16 = if (p.view().tag_state.kind == .pick) body_rows else 0; + const band = @min(takes(p, id), h -| text -| keep); + const shown = if (band > 0) text else @min(@max(text, pane.tag_rows), pardes.MAX_TAG_ROWS); + return .{ .text = shown, .band = band, .over = (shown + band) -| pane.tag_rows }; } fn levelRows(n: u16) u16 { @@ -696,8 +701,10 @@ test "a narrow band gives way and keeps its spines: the doc goes, the steps star const s = try p.render(a); const r = p.view().rects[id]; const tag_y = p.tagTop(pane, r); - try std.testing.expectEqual(@as(u16, 1), pane.tag_rows - pane.band_rows); - try std.testing.expectEqual(@max(rest, 1 + band.height), pane.tag_rows); + const at = covered(p, pane, id); + try std.testing.expectEqual(@as(u16, 1), at.text); + try std.testing.expectEqual(rest, pane.tag_rows); + try std.testing.expectEqual((1 + band.height) -| rest, at.over); try std.testing.expectEqual(tag_y + 1, bandLayer(p, id).?.viewport.y); for (band.spines[0..band.nspines]) |col| try std.testing.expectEqualStrings("│", s.at(r.x + pardes.TAG_TEXT_INSET + col, tag_y + 1).grapheme()); try std.testing.expect(std.mem.indexOf(u8, try layerRow(a, bandLayer(p, id).?, 0), "▶") != null); @@ -715,7 +722,7 @@ test "out of rest with no band, the rows a tag keeps from rest stay blank: a spi // A search prompt: a key cell in its head, no band under it. p.update(.{ .key = .{ .cp = '/', .text = "/" } }); p.sync(); - try std.testing.expectEqual(@as(u16, 0), pane.band_rows); + try std.testing.expectEqual(@as(u16, 0), covered(p, pane, id).band); try std.testing.expectEqual(rest, pane.tag_rows); var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); @@ -828,7 +835,7 @@ test "a row longer than its room is cut with `…`: a path from the left, anythi try std.testing.expectEqualStrings("short", fits.text); } -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" { +test "SPC Down unrolls the level into a band under the tag's text, drawn over the top rows of the body, which stays where it was (UX DESIGN I1); 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(); @@ -840,30 +847,40 @@ test "SPC Down unrolls the level into a band under the tag's text, the body push const r = p.view().rects[id]; const text_rows = pane.tag_rows; const body_top = p.bodyTop(pane, r); + const rows = pane.rows; _ = 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)); + const at = covered(p, pane, id); + try std.testing.expect(at.band >= doc_rows); + try std.testing.expect(at.over > 0); + // The tag keeps its rows at rest, the body its first row and its size. + try std.testing.expectEqual(text_rows, pane.tag_rows); + try std.testing.expectEqual(body_top, p.bodyTop(pane, r)); + try std.testing.expectEqual(rows, pane.rows); + try std.testing.expectEqual(at.over, pane.view.covered); _ = 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. + try std.testing.expectEqual(p.tagTop(pane, r) + at.text, layer.viewport.y); + try std.testing.expectEqual(at.band, layer.viewport.h); + // The body region starts where it did at rest: the band is over it. const body = s.region(.body, id).?; - try std.testing.expectEqual(layer.viewport.y + layer.viewport.h, body.rect.y); + try std.testing.expectEqual(body_top, body.rect.y); + try std.testing.expect(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. + // The grid has the same rows, where the layer is, the body's under + // them drawn first: its last covered row is the band's. 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); + const spine = (try of(p, frame.allocator(), pane, id)).?.spines[0]; + try std.testing.expectEqualStrings("│", s.at(r.x + pardes.TAG_TEXT_INSET + spine, layer.viewport.y + layer.viewport.h - 1).grapheme()); // The head names the level and says how to roll it up. const head = try tagline.tagText(p, frame.allocator(), pane); try std.testing.expect(std.mem.startsWith(u8, head, "␣ SPC ") and std.mem.endsWith(u8, head, " ↑ roll up · ? field · esc")); @@ -874,7 +891,7 @@ test "SPC Down unrolls the level into a band under the tag's text, the body push 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(@as(u16, 0), covered(p, pane, id).band); 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 } }); @@ -884,13 +901,14 @@ test "SPC Down unrolls the level into a band under the tag's text, the body push 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.expectEqual(@as(u16, 0), covered(p, pane, id).band); + try std.testing.expectEqual(@as(u16, 0), pane.view.covered); try std.testing.expect(!p.view().tag_state.unrolled); } -test "a band keeps its pane's body 60 % of the pane, borrowed from its column; where the column has not the rows, the band gives way before the body drops under 6" { +test "a level's band covers what it needs: its pane keeps its rows where its head and band fit, and borrows from its column only the rows they need where they do not (UX DESIGN I1)" { const Key = pardes.Key; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 60 }); defer p.deinit(); _ = try p.setTestFile("one\n"); const top = p.view().active; @@ -899,42 +917,25 @@ test "a band keeps its pane's body 60 % of the pane, borrowed from its column; w p.newScratchBelow(low); p.view().active = low; p.sync(); + const before = p.view().rects; 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(pane.band_rows >= doc_rows); - try std.testing.expect(@as(u32, r.h -| pane.tag_rows) * 5 >= @as(u32, r.h) * 3); + const at = covered(p, pane, low); + const need = at.text + at.band; + try std.testing.expect(at.band == takes(p, low) and need <= before[low].h); + try std.testing.expectEqualDeep(before, p.view().rects); p.update(.{ .key = .{ .cp = Key.escape } }); - // A window of 12 rows: the band shrinks, the body keeps 6. - p.update(.{ .resize = .{ .cols = 120, .rows = 12 } }); + // A window of 24 rows: seven a pane, the head and band need more. + p.update(.{ .resize = .{ .cols = 120, .rows = 24 } }); + const short = p.view().rects[low].h; + try std.testing.expect(short < need); p.update(.{ .key = .{ .cp = ' ' } }); p.update(.{ .key = .{ .cp = Key.down } }); - p.sync(); - const short = p.view().rects[low]; - try std.testing.expect(short.h -| pane.tag_rows >= 6 or pane.band_rows == 0); - try std.testing.expect(pane.band_rows < Band.takes(p, low)); -} - -test "a pane whose body would drop below six 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); - // Its body six rows at least (UX D6): a column too short for 60 %. - try std.testing.expect(r.h -| pane.tag_rows >= 6); + const grown = covered(p, pane, low); + try std.testing.expectEqual(need, r.h); + try std.testing.expectEqual(at, grown); // No pane dims out of rest (UX D9): the others look as at rest. var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); @@ -944,7 +945,7 @@ test "a pane whose body would drop below six rows under its band borrows them fr try std.testing.expect(!s.at(other.x + 3, other.y + 1).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); + try std.testing.expectEqual(short, p.view().rects[low].h); } test "the band's words are read-only text: B2 runs one, the tag back at rest" { @@ -1114,3 +1115,127 @@ test "an empty level gives no rows: SPC unrolled on an addon's group, or describ _ = try p.render(a); } } + +test "out of rest the body stays where it was: under a rolled level, SPC Down, Help, `:`, the registers and a picker that has not previewed, every row of its text on the screen row it had at rest, its first row and its size the same; Esc leaves the body's rows as at rest (UX DESIGN I1)" { + const Key = pardes.Key; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 40 }); + defer p.deinit(); + p.settings.jumps = .walk; + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + for (1..121) |i| try text.print(gpa, "line {d} of the text\n", .{i}); + const pane = try p.setTestFile(text.items); + const id = p.view().active; + pane.focus = .body; + p.clock.now_ns = 5 * std.time.ns_per_s; + p.clock.stepped_ns = p.clock.now_ns; + // Into the text: the view scrolled, the caret on a row under the top. + for ("60jk") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + p.sync(); + var frame = std.heap.ArenaAllocator.init(gpa); + defer frame.deinit(); + const r = p.view().rects[id]; + const body_top = p.bodyTop(pane, r); + const tag_rows, const rows, const top = .{ pane.tag_rows, pane.rows, pane.file.?.scroll }; + try std.testing.expect(top > 0); + const rest = try gpa.dupe(pardes.Cell, (try p.render(frame.allocator())).cells); + defer gpa.free(rest); + const cols = p.surface.cols; + const list = try std.fmt.allocPrint(gpa, "{s}:3:1 line 3 of the text\n", .{pane.file.?.path}); + defer gpa.free(list); + for ([_][]const u8{ "rolled", "unrolled", "help", "colon", "registers", "picker" }) |state| { + if (std.mem.eql(u8, state, "picker")) { + pardes.Pick.open(p, id, .list, "Refs", "", list); + } else { + const keys: []const Key = if (std.mem.eql(u8, state, "rolled")) &.{.{ .cp = ' ' }} else if (std.mem.eql(u8, state, "unrolled")) &.{ .{ .cp = ' ' }, .{ .cp = Key.down } } else if (std.mem.eql(u8, state, "help")) &.{ .{ .cp = ' ' }, .{ .cp = '?' } } else if (std.mem.eql(u8, state, "colon")) &.{.{ .cp = ':' }} else &.{ .{ .cp = '"' }, .{ .cp = Key.down } }; + for (keys) |k| p.update(.{ .key = k }); + // A rolled level shows after its delay. + p.advance(p.clock.now_ns); + if (p.view().tag_state.due_ns > p.clock.now_ns) p.clock.now_ns = p.view().tag_state.due_ns; + } + p.sync(); + errdefer std.debug.print("state {s}\n", .{state}); + try std.testing.expect(TagState.hostOf(p) == id); + const at = covered(p, pane, id); + try std.testing.expect(at.over > 0); + try std.testing.expectEqual(tag_rows, pane.tag_rows); + try std.testing.expectEqual(rows, pane.rows); + try std.testing.expectEqual(top, pane.file.?.scroll); + try std.testing.expectEqual(body_top, p.bodyTop(pane, r)); + _ = frame.reset(.retain_capacity); + const s = try p.render(frame.allocator()); + // Under the rows drawn over it, the body's text on its rows. + var y = body_top + at.over; + while (y < r.y + r.h) : (y += 1) for (r.x + pardes.config.GUTTER..r.x + r.w) |x| { + const i = @as(usize, y) * cols + x; + try std.testing.expectEqualStrings(rest[i].grapheme(), s.cells[i].grapheme()); + }; + p.update(.{ .key = .{ .cp = Key.escape } }); + p.sync(); + try std.testing.expect(TagState.hostOf(p) == null); + _ = frame.reset(.retain_capacity); + const back = try p.render(frame.allocator()); + y = body_top; + while (y < r.y + r.h) : (y += 1) for (r.x..r.x + r.w) |x| { + const i = @as(usize, y) * cols + x; + try std.testing.expect(std.meta.eql(rest[i], back.cells[i])); + }; + } +} + +test "a covered row belongs to the band: a click on a band row over the body moves the band's cursor, on the head's row over it runs or looks at its word, never the body under them (UX DESIGN I1)" { + const Key = pardes.Key; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + for (1..61) |i| try text.print(gpa, "line {d} of the text\n", .{i}); + const pane = try p.setTestFile(text.items); + const id = p.view().active; + pane.focus = .body; + p.sync(); + const r = p.view().rects[id]; + const body_top = p.bodyTop(pane, r); + p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = Key.down } }); + const at = covered(p, pane, id); + var frame = std.heap.ArenaAllocator.init(gpa); + defer frame.deinit(); + const band = (try of(p, frame.allocator(), pane, id)).?; + // The band's last row, over the body: its first step's row there. + const row: u16 = at.band - 1; + const y = p.tagTop(pane, r) + at.text + row; + try std.testing.expect(y >= body_top); + const ln = try band.line(frame.allocator(), p.settings.tab, row); + const part = ln.parts[0]; + const x = r.x + pardes.TAG_TEXT_INSET + part.col + 1; + const caret = .{ pane.body.cur_row, pane.body.cur_col }; + p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = x, .row = y } }); + p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = x, .row = y } }); + var children: [bindings.max_children]bindings.Child = undefined; + try std.testing.expectEqual(bindings.level(p, .leader, "", &children)[part.row.index].key, p.view().tag_state.hi); + try std.testing.expect(p.view().tag_state.unrolled); + try std.testing.expectEqual(caret, .{ pane.body.cur_row, pane.body.cur_col }); + try std.testing.expectEqual(pardes.Pane.Focus.body, pane.focus); + p.update(.{ .key = .{ .cp = Key.escape } }); + // SPC's rolled level, rows over the body under its first: a click on + // one is the head's, not the body's (B1 there does nothing). + p.clock.now_ns = 5 * std.time.ns_per_s; + p.clock.stepped_ns = p.clock.now_ns; + p.update(.{ .key = .{ .cp = ' ' } }); + p.advance(p.clock.now_ns); + p.clock.now_ns = p.view().tag_state.due_ns; + p.sync(); + const rolled = covered(p, pane, id); + try std.testing.expect(rolled.band == 0 and rolled.text > pane.tag_rows); + const hy = p.tagTop(pane, r) + rolled.text - 1; + try std.testing.expect(hy >= body_top); + const hx = r.x + pardes.TAG_TEXT_INSET + 6; + p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = hx, .row = hy } }); + p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = hx, .row = hy } }); + try std.testing.expect(TagState.hostOf(p) == id); + try std.testing.expectEqual(caret, .{ pane.body.cur_row, pane.body.cur_col }); + try std.testing.expectEqual(pardes.Pane.Focus.body, pane.focus); +} |
