diff options
Diffstat (limited to 'core/draw.zig')
| -rw-r--r-- | core/draw.zig | 190 |
1 files changed, 169 insertions, 21 deletions
diff --git a/core/draw.zig b/core/draw.zig index 8baca6ca..f7b2cd3f 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -16,6 +16,7 @@ const edit = @import("edit.zig"); const tagline = @import("tagline.zig"); const body_layer = @import("body_layer.zig"); const Messages = @import("Messages.zig"); +const TagState = @import("TagState.zig"); const builtins = @import("builtins.zig"); const Color = @import("surface.zig").Color; const FontRole = @import("surface.zig").FontRole; @@ -229,7 +230,7 @@ fn joinNotice(p: *Pardes, s: *Surface, arena: std.mem.Allocator, region: Region) const kind = pane.notices.kinds[i]; const r = v.rects[id]; const chip = region.rect.w; - var leader_buf: [16]u8 = undefined; + var leader_buf: [Messages.leader_room]u8 = undefined; const text = Messages.noticeText(p, pane, i, &leader_buf); const bg = chrome.tag_bg; const fg = chrome.tag_fg; @@ -359,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); try paintNoticeRow(p, s); @@ -455,7 +468,7 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: // its ring. A pixel shell draws the ring (gui.zig gripDecor); a grid, // which cannot, marks dirty in the grip's second cell with a bold `*` // on the dirty colour, a cue that survives any palette. - const band = if (active) chrome.tag_active_bg else chrome.tag_bg; + const band = tagGround(p, pane, active); const dirty = tagline.dirty(pane); const box_bg = gripBg(p, active); const box_style: CellStyle = .{ @@ -482,13 +495,19 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: }); // In window mode the box holds the pane's letter instead of its mode. const label: ?u8 = if (p.view().window_pick != null) layout.windowLabel(p, id) else null; - s.set(r.x, tag_y, if (label) |*letter| @as(*const [1]u8, letter) else switch (pane.body.mode) { + // A tag in a typing state is typed in: its box says so (walk UI SPEC + // §3.2), whatever the body's mode. + const typing = p.view().tag_state.kind != .rest and TagState.hostOf(p) == id; + s.set(r.x, tag_y, if (label) |*letter| @as(*const [1]u8, letter) else if (typing) config.tag_insert else switch (pane.body.mode) { .normal => config.tag_normal, .insert => config.tag_insert, .tty => config.tag_tty, }, box_style); try joinPaneTag(p, s, arena, pane, r, id, active); + // The keyboard is in the head's field: its caret, not the body's. + const head_caret = if (typing) s.cursor else null; + 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. @@ -523,6 +542,10 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: } try body_layer.joinBody(p, s, arena, pane, r, id, active, rail.*); + if (typing) { + s.body_layers[id].cursor = null; + s.cursor = head_caret; + } if (pane.layers.heat > 0) @import("jj.zig").railHeat(p, s, pane, id, rail.rect); // A terminal panned to its cursor in a view shorter than it: an arrow // in the gutter's first body row says rows are above (views.zig). @@ -676,7 +699,7 @@ fn tagTintRange(pane: *const Pane, tag: []const u8, prefix_len: usize) ?struct { } return null; } - var start: usize = 0; + var start: usize = tagline.nameAt(pane); const name = if (pane.file) |file| if (pane.prompt == .name) pane.input.own orelse file.path else file.path else if (pane.addon) |k| blk: { @@ -723,7 +746,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, .notice_row => false, + .pane, .notice, .body, .band, .notice_row => false, }) tagline.focusedHeader(p) else null; const scroll = if (editing != null) v.header_scroll else 0; for (0..viewport.h) |index| { @@ -775,7 +798,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, .notice_row => false, + .pane, .notice, .body, .band, .notice_row => false, }; const swept = sweep.span(text.len); const start = modal.lineStartOffset(text, index + top); @@ -803,7 +826,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; @@ -833,7 +857,7 @@ fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r layer.cols = columns; layer.rows = rows; layer.context_rows = 0; - layer.bg = if (v.window_pick == id) p.theme().sel_bg else if (active) chrome.tag_active_bg else chrome.tag_bg; + layer.bg = if (v.window_pick == id) p.theme().sel_bg else tagGround(p, pane, active); layer.cursor = if (v.header != null) null else if (temporary.cursor) |cursor| .{ .x = cursor.x - TAG_TEXT_INSET, .y = cursor.y, .bar = cursor.bar } else @@ -850,16 +874,113 @@ 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]; + 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 = tagGround(p, pane, active); + 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 (b < part.row.pre) style.fg = .{ .rgb = if (active) chrome.tag_active_name_fg else chrome.tag_name_fg }; + if (part.row.dim or (b >= part.row.pre and b < part.row.pre + part.row.faint)) 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 }); +} + +/// Bytes `lo` to `hi` of a tag's text in `ink`, over whichever of its +/// shown rows (`lines`, from shown row `top`, drawn at `x`, `y`, `w` +/// wide) they wrapped onto. +fn inkTag(p: *Pardes, s: *Surface, shown: tagline.Shown, lines: []const []const u8, top: usize, x: u16, y: u16, w: u16, lo: usize, hi: usize, ink: [3]u8) void { + const a = modal.positionAt(shown.text, shown.fromReal(lo)); + const b = modal.positionAt(shown.text, shown.fromReal(hi)); + var row = @max(a.row, top); + while (row <= b.row and row < top + lines.len) : (row += 1) { + const line = lines[row - top]; + const from = if (row == a.row) a.col else 0; + const to = if (row == b.row) b.col else line.len; + var col = panes.File.rawDisplayCol(p.settings.tab, line, @min(from, line.len)); + const end = panes.File.rawDisplayCol(p.settings.tab, line, @min(to, line.len)); + const row_y = y + @as(u16, @intCast(row - top)); + while (col < end and col < w) : (col += 1) + s.at(x + @as(u16, @intCast(col)), row_y).style.fg = .{ .rgb = ink }; + } +} + +/// A pane's tag's ground: a reading pane's tint (its focus is its grip's), +/// else the focused tag's or the tag's. +fn tagGround(p: *Pardes, pane: *const Pane, active: bool) [3]u8 { + const chrome = p.chromeTheme(); + if (pardes.Trail.reading(p, pane)) return chrome.tag_reading_bg; + return if (active) chrome.tag_active_bg else chrome.tag_bg; +} + 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; + const tag_bg = tagGround(p, pane, active); const tag_fg = if (active) chrome.tag_active_fg else chrome.tag_fg; const tag_style: CellStyle = .{ .fg = .{ .rgb = tag_fg }, .bg = .{ .rgb = tag_bg }, .font_role = .tagline }; // Padding belongs to the same font row but keeps its default ink: a @@ -869,7 +990,9 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, .font_role = .tagline, }); const whole = try tagline.tagText(p, arena, pane); - const tail = tagline.curTail(pane); + // Out of rest, the head in place of the tag's own text (TagState). + const head = try TagState.head(p, arena, pane); + const tail = if (head) |h| h.text else tagline.curTail(pane); const prefix_len = whole.len - tail.len; // A row per line shown, from `tag_top`: the tag's first line carries // the prefix, the rest are the tag's own text alone. @@ -889,10 +1012,10 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, else if (pane.prompt == .name) caret: { // A drafted name is the prefix: its caret is on row 0 of the tag, // the rows it wrapped onto included. - const at = modal.positionAt(shown.text, shown.fromReal(@intCast(@max(0, pane.input.cur_col)))); + const at = modal.positionAt(shown.text, shown.fromReal(tagline.nameAt(pane) + @as(usize, @intCast(@max(0, pane.input.cur_col))))); if (at.row < top or at.row >= top + rows) break :caret null; break :caret .{ .row = at.row - top, .at = @min(lines[at.row - top].len, at.col) }; - } else if (pane.focus == .tag and pane.prompt == .none) caret: { + } else if (pane.focus == .tag and pane.prompt == .none and head == null) caret: { const real = modal.offsetAt(whole, .{ .row = @intCast(@max(0, pane.tag.cur_row)), .col = @intCast(@max(0, pane.tag.cur_col)) }); const at = modal.positionAt(shown.text, shown.fromReal(real)); if (at.row < top or at.row >= top + rows) break :caret null; @@ -920,10 +1043,16 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, // The name's tint, in the tag's first line, over whichever rows it // wrapped onto. const first_line = whole[0 .. std.mem.indexOfScalar(u8, whole, '\n') orelse whole.len]; - if (tagTintRange(pane, first_line, prefix_len)) |name| if (name.lo < name.hi) { - const name_fg = if (active) chrome.tag_active_name_fg else chrome.tag_name_fg; - const a = modal.positionAt(shown.text, shown.fromReal(name.lo)); - const b = modal.positionAt(shown.text, shown.fromReal(@min(name.hi, first_line.len))); + if (tagTintRange(pane, first_line, prefix_len)) |name| if (name.lo < name.hi) + inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, name.lo, @min(name.hi, first_line.len), if (active) chrome.tag_active_name_fg else chrome.tag_name_fg); + // A walk's forward steps, after the name, dimmed (Trail.zig). + const ahead = tagline.forwardSpan(pane); + if (ahead[0] < ahead[1]) inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, ahead[0], @min(ahead[1], first_line.len), chrome.lineno); + // The head's keys in the key ink, its dimmed, underlined and struck + // words (TagState.Span), over whichever rows they wrapped onto. + if (head) |h| for (h.spans) |span| { + const a = modal.positionAt(shown.text, shown.fromReal(prefix_len + span.lo)); + const b = modal.positionAt(shown.text, shown.fromReal(prefix_len + span.hi)); var row = @max(a.row, top); while (row <= b.row and row < top + rows) : (row += 1) { const line = lines[row - top]; @@ -932,13 +1061,23 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, var col = panes.File.rawDisplayCol(p.settings.tab, line, @min(from, line.len)); const end = panes.File.rawDisplayCol(p.settings.tab, line, @min(to, line.len)); const y = tag_y + @as(u16, @intCast(row - top)); - while (col < end and col < tw) : (col += 1) - s.at(tx + @as(u16, @intCast(col)), y).style.fg = .{ .rgb = name_fg }; + while (col < end and col < tw) : (col += 1) { + const style = &s.at(tx + @as(u16, @intCast(col)), y).style; + switch (span.style) { + .key => { + style.fg = .{ .rgb = chrome.box }; + style.bold = true; + }, + .dim => style.dim = true, + .under => style.ul = .single, + .strike => style.strikethrough = true, + } + } } }; // The tag's own selections, while it has the keyboard, a line at a // time. A range of one grapheme is the block cursor, not a selection. - if (pane.focus == .tag) { + if (pane.focus == .tag and head == null) { var ranges: [Text.max_selections]modal.Selection = undefined; const got = pane.tag.ranges(whole, 0, &ranges); for (ranges[0..got.n]) |range| { @@ -958,6 +1097,15 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, } } } + // A head typed in: its caret a bar at the field's. + if (head) |h| if (h.caret) |c| if (active) { + const at = modal.positionAt(shown.text, shown.fromReal(prefix_len + c)); + if (at.row >= top and at.row < top + rows) { + const line = lines[at.row - top]; + const col = panes.File.rawDisplayCol(p.settings.tab, line, @min(at.col, line.len)); + if (col < tw) s.cursor = .{ .x = tx + @as(u16, @intCast(col)), .y = tag_y + @as(u16, @intCast(at.row - top)), .bar = true }; + } + }; // the caret: bar while typing, block in normal mode (same rule as a body) if (caret) |at| { const col = panes.File.rawDisplayCol(p.settings.tab, lines[at.row], at.at) -| pane.tag_scroll; @@ -1114,7 +1262,7 @@ test "a tag of three rows has its grip on the first, and band under it that stil const top = p.tagTop(pane, r); try std.testing.expect(pane.tag_rows >= 3); const chrome = p.chromeTheme(); - const band = chrome.tag_active_bg; + const band = tagGround(p, pane, true); for (0..config.GUTTER) |dx| { const x = r.x + @as(u16, @intCast(dx)); try std.testing.expectEqual(Color{ .rgb = chrome.box }, s.at(x, top).style.bg); |
