diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Band.zig | 77 | ||||
| -rw-r--r-- | core/Messages.zig | 8 | ||||
| -rw-r--r-- | core/Pick.zig | 7 | ||||
| -rw-r--r-- | core/TagState.zig | 15 | ||||
| -rw-r--r-- | core/Trail.zig | 3 | ||||
| -rw-r--r-- | core/colors.zig | 332 | ||||
| -rw-r--r-- | core/draw.zig | 103 |
7 files changed, 509 insertions, 36 deletions
diff --git a/core/Band.zig b/core/Band.zig index 835c249f..a8d97bb4 100644 --- a/core/Band.zig +++ b/core/Band.zig @@ -39,6 +39,9 @@ pub const Row = struct { on: bool = false, /// Its first `key` bytes are a key: the key ink, bold. key: u8 = 0, + /// Its first `chord` bytes are a chord (Help's `SPC h t`): the key ink + /// at the text's weight. + chord: u8 = 0, dim: bool = false, /// The leaf its group ran last. under: bool = false, @@ -205,7 +208,9 @@ fn helpSteps(band: *Band, p: *Pardes, arena: std.mem.Allocator, s: *const TagSta const low = try std.ascii.allocLowerString(arena, leaf.row); _ = Names.match(q, leaf.row, low, 0, &marks, &nmarks); } - row.* = .{ .text = leaf.row, .on = i == s.rows.at, .marks = try arena.dupe(u16, marks[0..nmarks]), .index = @intCast(i), .dim = bindings.dimmed(p.panes[p.view().active].?, leaf.child) }; + // Its chord (`SPC h t`, before the first ` · `) in the key ink. + const chord = std.mem.indexOf(u8, leaf.row, " · ") orelse 0; + row.* = .{ .text = leaf.row, .on = i == s.rows.at, .chord = @intCast(@min(chord, 255)), .marks = try arena.dupe(u16, marks[0..nmarks]), .index = @intCast(i), .dim = bindings.dimmed(p.panes[p.view().active].?, leaf.child) }; } band.add(.{ .rows = rows }); var buf: [TagState.row_max]u8 = undefined; @@ -351,6 +356,8 @@ pub fn docStep(p: *Pardes, arena: std.mem.Allocator, root: bindings.Root, path: .role => bindings.docOf(root, c, try arena.alloc(u8, 160)), } else bindings.docOf(root, c, &.{}); try rows.append(arena, .{ .text = doc }); + // A leaf a language server answers, in a pane with none (SPEC §4.2). + if (bindings.dimmed(p.panes[p.view().active].?, c)) try rows.append(arena, .{ .text = "needs a language server", .dim = true }); 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)}) }); @@ -653,3 +660,71 @@ test "a doc's paragraphs are rows of their own: no newline in a band's row" { }; try std.testing.expect(docs > 2); } + +test "a band is drawn in the theme's roles (UX D4): spines in the rule ink, a chord in the key ink, a matched letter bold in the match ink on its row's own ground, the cursor row's too; an LSP leaf with no server in the dim ink, never faint, its doc saying why" { + const Key = pardes.Key; + const Cells = struct { + /// The cell `word` starts at, a grapheme a cell. + fn find(cells: []const pardes.Cell, word: []const u8) ?usize { + var at: usize = 0; + while (at + word.len <= cells.len) : (at += 1) { + for (word, cells[at..][0..word.len]) |c, cell| { + if (cell.len != 1 or cell.text[0] != c) break; + } else return at; + } + return null; + } + }; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 40 }); + defer p.deinit(); + const pane = try p.setTestFile("one\n"); + const id = p.view().active; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + const roles = &p.chromeTheme().roles; + const inks = roles.on(if (pardes.Trail.reading(p, pane)) .reading_focused else .focused); + const selected = roles.selectedOn(if (pardes.Trail.reading(p, pane)) .reading_focused else .focused); + // Help with `wrap` typed: `▶ SPC t w · Wrap · view` on the cursor row. + for ([_]u21{ ' ', '?', 'w', 'r', 'a', 'p' }) |c| p.update(.{ .key = .{ .cp = c } }); + _ = try p.render(a); + var layer = bandLayer(p, id).?; + var spines: usize = 0; + var cursor_row: ?usize = null; + for (0..layer.rows) |row| for (layer.cells[row * layer.cols ..][0..layer.cols]) |cell| { + try std.testing.expect(!cell.style.dim); + if (std.mem.eql(u8, cell.grapheme(), "│")) { + try std.testing.expectEqual(pardes.Color{ .rgb = inks.rule }, cell.style.fg); + spines += 1; + } + if (std.mem.eql(u8, cell.grapheme(), "▶")) cursor_row = row; + }; + try std.testing.expect(spines > 0); + const on = layer.cells[cursor_row.? * layer.cols ..][0..layer.cols]; + const s_cell = on[Cells.find(on, "SPC t w").?]; + try std.testing.expectEqual(pardes.Color{ .rgb = roles.key }, s_cell.style.fg); + try std.testing.expect(!s_cell.style.bold); + var matched: usize = 0; + for (on) |cell| if (std.meta.eql(cell.style.fg, pardes.Color{ .rgb = roles.match })) { + try std.testing.expect(cell.style.bold); + try std.testing.expectEqual(pardes.Color{ .rgb = selected.ground }, cell.style.bg); + matched += 1; + }; + try std.testing.expect(matched >= 4); + p.update(.{ .key = .{ .cp = Key.escape } }); + // SPC l unrolled in a text file no server speaks for: its leaves dim. + for ([_]u21{ ' ', 'l', Key.down }) |c| p.update(.{ .key = .{ .cp = c } }); + _ = try p.render(a); + layer = bandLayer(p, id).?; + var dimmed: usize = 0; + var why = false; + for (0..layer.rows) |row| { + const cells = layer.cells[row * layer.cols ..][0..layer.cols]; + if (Cells.find(cells, "needs a language server") != null) why = true; + const cell = cells[Cells.find(cells, "Hover") orelse continue]; + try std.testing.expect(!cell.style.dim); + try std.testing.expectEqual(pardes.Color{ .rgb = inks.dim }, cell.style.fg); + dimmed += 1; + } + try std.testing.expect(dimmed > 0 and why); +} diff --git a/core/Messages.zig b/core/Messages.zig index 2b63e647..41344d1f 100644 --- a/core/Messages.zig +++ b/core/Messages.zig @@ -413,10 +413,10 @@ pub fn markOf(m: *const Messages, pane: *const Pane) ?Kind { /// A kind's ink, for its glyph, its mark and its words at a line. pub fn ink(chrome: *const pardes.colors.ChromeTheme, kind: Kind) [3]u8 { return switch (kind) { - .@"error" => chrome.diagnostic_error, - .warn => chrome.diagnostic_warning, - .question => chrome.diagnostic_info, - else => chrome.diagnostic_hint, + .@"error" => chrome.roles.err, + .warn => chrome.roles.warn, + .question => chrome.roles.question, + else => chrome.roles.hint, }; } diff --git a/core/Pick.zig b/core/Pick.zig index 3bab0970..6026f11b 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -589,7 +589,9 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { if (pk.names.busy() or p.stream.busyFor(pk.id)) try text.appendSlice(arena, " ⋯"); // Themes have no acts and no other sources: no second row. if (pk.source == .themes) { + const hint: u32 = @intCast(text.items.len); try text.appendSlice(arena, " ⏎ wear it · esc"); + try spans.append(arena, .{ .lo = hint, .hi = @intCast(text.items.len), .style = .dim }); for (cells[0..ncells]) |c| try spans.append(arena, .{ .lo = c[0], .hi = c[1], .style = .key }); return .{ .text = text.items, .spans = spans.items, .cells = @intCast(ncells), .caret = caret }; } @@ -603,13 +605,15 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { for (cells[0..ncells]) |c| { const at = lead + File.displayWidth(p.settings.tab, text.items[0..c[0]]); while (col < at) : (col += 1) try text.append(arena, ' '); - try spans.append(arena, .{ .lo = @intCast(text.items.len), .hi = @intCast(text.items.len + "│".len), .style = .key }); + try spans.append(arena, .{ .lo = @intCast(text.items.len), .hi = @intCast(text.items.len + "│".len), .style = .rule }); try text.appendSlice(arena, "│"); col += 1; } const nspines: u8 = @intCast(spans.items.len); try text.appendSlice(arena, if (ncells == 0) "" else " "); for (acts) |act| if (offered(pk, act)) try text.print(arena, "{s} ", .{act.word}); + // The other sources' words, and the `|` before them, dim (UX D4). + const sources: u32 = @intCast(text.items.len); try text.appendSlice(arena, "|"); var first = false; inline for (comptime std.enums.values(Source)) |src| if (src != .list and src != .messages and src != .themes) { @@ -619,6 +623,7 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { try text.appendSlice(arena, words.get(src).word); } }; + try spans.append(arena, .{ .lo = sources, .hi = @intCast(text.items.len), .style = .dim }); for (cells[0..ncells]) |c| try spans.append(arena, .{ .lo = c[0], .hi = c[1], .style = .key }); return .{ .text = text.items, .spans = spans.items, .cells = nspines, .caret = caret }; } diff --git a/core/TagState.zig b/core/TagState.zig index 3bd79176..0a59bd08 100644 --- a/core/TagState.zig +++ b/core/TagState.zig @@ -444,7 +444,7 @@ pub fn helpLeaf(p: *const Pardes, at: u16, buf: *[row_max]u8) Leaf { } /// How a span of the head is drawn (draw.zig paintPaneTag). -pub const Style = enum { key, dim, under, strike, err }; +pub const Style = enum { key, dim, under, strike, err, rule }; pub const Span = struct { lo: u32, hi: u32, style: Style }; /// The head: its text, the spans drawn apart, and the caret in it when the /// head is typed in. @@ -532,9 +532,12 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { try b.put(" "); try b.put(c.label); }; - try b.put(" ⏎ run · esc"); + try b.styled(" ⏎ run · esc", .dim); } else try b.put("…"); - } else try b.put("… press a key or a sequence · esc"); + } else { + try b.put("…"); + try b.styled(" press a key or a sequence · esc", .dim); + } return b.done(); }, .pick => return @import("Pick.zig").head(p, arena, pane), @@ -548,7 +551,7 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { var h = b.done(); h.caret = @intCast(b.text.items.len + s.field.caret); try b.put(s.field.text()); - try b.put(" ⏎ rename · esc"); + try b.styled(" ⏎ rename · esc", .dim); h.text = b.text.items; h.spans = b.spans.items; return h; @@ -564,7 +567,7 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { const node = bindings.nodeAt(cur.root, cur.path) orelse return b.done(); if (s.unrolled) { try b.put(if (cur.path.len == 0) bindings.info.get(cur.root).label else node.label); - try b.put(" ↑ roll up · ? field · esc"); + try b.styled(" ↑ roll up · ? field · esc", .dim); return b.done(); } try b.styled("▸", .key); @@ -606,7 +609,7 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { try b.put(" "); try b.styled(bindings.keyName(s.miss), .strike); } - try b.put(hint); + try b.styled(hint, .dim); return b.done(); } diff --git a/core/Trail.zig b/core/Trail.zig index 9223d1d3..f3ea542a 100644 --- a/core/Trail.zig +++ b/core/Trail.zig @@ -658,8 +658,9 @@ test "the session of pick-ui §6.7: four jumps across three files, back by a cli var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); const s = try p.render(arena.allocator()); + // In the dim ink of the reading tag's ground (UX D4: 3:1 at least). const dim = try tagCell(p, r, "place.zig:40"); - try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().lineno }, s.at(dim[0], dim[1]).style.fg); + try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().roles.on(.reading_focused).dim }, s.at(dim[0], dim[1]).style.fg); // A click on a forward step goes forward to it. try clickTag(p, r, "place.zig:9", .left); r = p.view().active; diff --git a/core/colors.zig b/core/colors.zig index ef2d5aae..d4abf2b5 100644 --- a/core/colors.zig +++ b/core/colors.zig @@ -287,8 +287,8 @@ pub const Theme = struct { lineno_active: ?[3]u8 = null, /// A tag's selection, where it differs from the body's (acme's #9eeeee). tag_sel_bg: ?[3]u8 = null, - /// The tag of a reading pane, one a jump steps (Trail.zig): unset, the - /// page tinted toward the search's ground, so it reads as the body's. + /// The tag of a reading pane, one a jump steps (Trail.zig): unset, or + /// too near the tag to see, the tag toward paper yellow (readingOf). tag_reading_bg: ?[3]u8 = null, /// Each pointer button's sweep, select, exec and look (Mouse.Button /// order), and the ink over it. Unset, tints of `sel_bg` in `sel_fg`. @@ -558,6 +558,7 @@ pub const ChromeTheme = struct { lineno: [3]u8, scroll_track: [3]u8, scroll_thumb: [3]u8, + roles: Roles, pub fn fromTheme(th: *const Theme) ChromeTheme { // The initial chrome is resolved at compile time: contrast takes pow. @@ -570,6 +571,7 @@ pub const ChromeTheme = struct { const name_fg = th.tag_name_fg orelse (derivedNameTint(th, th.tag_fg, th.tag_bg) orelse th.tag_fg); const active_name_fg = th.tag_active_name_fg orelse (th.tag_name_fg orelse (derivedNameTint(th, active_fg, active_bg) orelse active_fg)); + const reading = readingOf(th); return .{ .tag_bg = th.tag_bg, .tag_fg = th.tag_fg, @@ -583,7 +585,7 @@ pub const ChromeTheme = struct { .empty_col = th.empty_col orelse separatorOf(th), .search_bg = th.search_bg orelse th.sel_bg, .search_fg = th.search_fg orelse th.sel_fg, - .tag_reading_bg = th.tag_reading_bg orelse mixOklab(th.bg orelse th.tag_bg, th.search_bg orelse th.sel_bg, 0.4), + .tag_reading_bg = reading, .diagnostic_error = th.diagnostic_error orelse (th.fg orelse th.tag_fg), .diagnostic_warning = th.diagnostic_warning orelse (th.fg orelse th.tag_fg), .diagnostic_info = th.diagnostic_info orelse (th.fg orelse th.tag_fg), @@ -595,10 +597,248 @@ pub const ChromeTheme = struct { .lineno = th.lineno, .scroll_track = th.scroll_track, .scroll_thumb = th.scroll_thumb, + .roles = .of(th, th.tag_bg, active_bg, reading), }; } }; +/// A tag's ground in the walk UI (UX DESIGN.md D4): the plain tag's, the +/// focused one's tint, a reading pane's tint, focused or not (its ink is +/// the focused tag's while it has the keyboard). +pub const TagGround = enum { plain, focused, reading, reading_focused }; + +/// The inks drawn on one ground: labels and rows (`text`, 4.5:1 at least), +/// hints, meta and forward steps (`dim`, 3.5:1, never under 3), and the +/// spines and the rules between steps (`rule`, 2:1). +pub const Inks = struct { ground: [3]u8, text: [3]u8, dim: [3]u8, rule: [3]u8 }; + +/// The walk UI's colour roles (UX DESIGN.md D4), worked out from a theme +/// once, with its chrome (never a frame), each held to its floor on every +/// ground it is drawn on: a tag's four (`tag`) and a band's cursor row on +/// each of them (`selected`, its ground that tag's selection). +pub const Roles = struct { + tag: [4]Inks, + selected: [4]Inks, + /// Key cells, a level's or a row's key letters, `▶`, `•`, a target's + /// keys: 4.5:1 on every tag ground and on the selection. + key: [3]u8, + /// The letters a query matched: an ink, never a ground, 4.5:1 on every + /// tag ground and on the selection. + match: [3]u8, + /// The body's places and a preview's landing line, and the current + /// one (its matched span): grounds off the page, its ink on them 4.5:1. + hit: [3]u8, + hit_current: [3]u8, + /// A message's ink by its kind (Messages.ink): 4.5:1 on the tags and + /// the page. + err: [3]u8, + warn: [3]u8, + question: [3]u8, + hint: [3]u8, + + pub const text_floor: f32 = 4.5; + pub const dim_floor: f32 = 3; + pub const rule_floor: f32 = 1.8; + pub const rule_ceiling: f32 = 2.5; + pub const ink_floor: f32 = 4.5; + pub const hit_floor: f32 = 1.25; + pub const hit_current_floor: f32 = 1.6; + pub const reading_distance: f32 = 0.04; + + pub fn on(r: *const Roles, ground: TagGround) *const Inks { + return &r.tag[@backingInt(ground)]; + } + + pub fn selectedOn(r: *const Roles, ground: TagGround) *const Inks { + return &r.selected[@backingInt(ground)]; + } + + /// The selection's floor off a tag's ground: 1.4:1 on a dark tag, 1.25 + /// on a light one. + pub fn selectionFloor(ground: [3]u8) f32 { + return if (relativeLuminance(ground) < 0.18) 1.4 else 1.25; + } + + fn of(th: *const Theme, plain: [3]u8, focused: [3]u8, reading: [3]u8) Roles { + const grounds = [4][3]u8{ plain, focused, reading, reading }; + const plain_ink = th.tag_fg; + const focused_ink = th.tag_active_fg orelse th.tag_fg; + const texts = [4][3]u8{ plain_ink, focused_ink, plain_ink, focused_ink }; + var r: Roles = undefined; + for (&r.tag, &r.selected, grounds, texts) |*inks, *selected, ground, ink| { + inks.* = inksOn(ground, ink); + selected.* = inksOn(selectionOf(th.tag_sel_bg orelse th.sel_bg, ground, inks.text), inks.text); + } + const tag_grounds = [_][3]u8{ plain, focused, reading, r.selected[0].ground, r.selected[1].ground, r.selected[2].ground, r.selected[3].ground }; + r.key = pushAway(th.box, &tag_grounds, ink_floor); + r.match = pushAway(matchHue(th), &tag_grounds, ink_floor); + const page = th.bg orelse null_page; + const body_ink = th.fg orelse th.tag_fg; + const dark = relativeLuminance(page) < 0.18; + // The theme's search ground where it stands off the page and the + // body reads on it; else the page a little way toward its hue (the + // warm accent's without one). The current one as far past the hit + // as 32 % is past 18 % (38 past 22 on a light page). + const hue = th.search_bg orelse th.num; + const own = th.search_bg != null and contrast(hue, page) >= hit_floor and contrast(body_ink, hue) >= ink_floor; + const at: f32 = if (own) 1 else if (dark) 0.18 else 0.22; + r.hit = if (own) hue else hitOf(page, hue, body_ink, at, hit_floor); + const past: f32 = if (dark) 0.32 / 0.18 else 0.38 / 0.22; + r.hit_current = hitOf(page, hue, body_ink, at * past, hit_current_floor); + // Never fainter than the hit, where the ink holds both short. + if (contrast(r.hit_current, page) < contrast(r.hit, page)) r.hit_current = r.hit; + const message_grounds = [_][3]u8{ plain, focused, reading, page }; + const ink = th.fg orelse th.tag_fg; + r.err = pushAway(th.diagnostic_error orelse ink, &message_grounds, ink_floor); + r.warn = pushAway(th.diagnostic_warning orelse ink, &message_grounds, ink_floor); + r.question = pushAway(th.diagnostic_info orelse ink, &message_grounds, ink_floor); + r.hint = pushAway(th.diagnostic_hint orelse ink, &message_grounds, ink_floor); + return r; + } +}; + +/// A tag's text, dim and rule inks on `ground`, from its ink (pushed to the +/// text floor where it has less). +fn inksOn(ground: [3]u8, ink: [3]u8) Inks { + const text = pushAway(ink, &.{ground}, Roles.text_floor); + return .{ .ground = ground, .text = text, .dim = toward(text, ground, 3.5), .rule = toward(text, ground, 2) }; +} + +/// `ink` mixed toward `ground` (OKLab) by hundredths as far as it still +/// reads at `floor` on it. +fn toward(ink: [3]u8, ground: [3]u8, floor: f32) [3]u8 { + const a = toOklab(ink); + const b = toOklab(ground); + var best = ink; + var step: f32 = 1; + while (step <= 100) : (step += 1) { + const t = step / 100; + const c = fromOklab(.{ a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t }); + if (contrast(c, ground) < floor) break; + best = c; + } + return best; +} + +/// `ink` pushed by fiftieths toward white or black, whichever reads better +/// on all its grounds, until it reads at `floor` on every one; where no ink +/// does (solarized's mid-grey tag), that end, the most any ink reaches. +fn pushAway(ink: [3]u8, grounds: []const [3]u8, floor: f32) [3]u8 { + const white: [3]u8 = @splat(255); + const black: [3]u8 = @splat(0); + const end = if (leastContrast(white, grounds) >= leastContrast(black, grounds)) white else black; + const a = toOklab(ink); + const b = toOklab(end); + var step: f32 = 0; + while (step <= 50) : (step += 1) { + const t = step / 50; + const c = fromOklab(.{ a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t }); + if (leastContrast(c, grounds) >= floor) return c; + } + return end; +} + +fn leastContrast(ink: [3]u8, grounds: []const [3]u8) f32 { + var least: f32 = 21; + for (grounds) |g| least = @min(least, contrast(ink, g)); + return least; +} + +/// The match's hue: the search's ink where it is a hue of its own (a port's +/// is often the page's or the tag's, set for the search's ground), else the +/// theme's warm accent. +fn matchHue(th: *const Theme) [3]u8 { + const own = th.search_fg orelse return th.num; + const lab = toOklab(own); + return if (@sqrt(lab[1] * lab[1] + lab[2] * lab[2]) >= name_tint_chroma) own else th.num; +} + +/// The selection a band's cursor row is drawn on over `ground`: the +/// theme's where it stands off it, else moved by hundredths toward the +/// text, then away from it, to the first that does with the text still at +/// 4.5:1 on it; where none does (a tag whose text has no contrast to +/// spare), the one furthest off it. +fn selectionOf(own: [3]u8, ground: [3]u8, text: [3]u8) [3]u8 { + var best = own; + var best_off: f32 = 0; + const away: [3]u8 = if (relativeLuminance(text) > relativeLuminance(own)) @splat(0) else @splat(255); + for ([2][3]u8{ text, away }) |end| { + var step: f32 = 0; + while (step <= 100) : (step += 1) { + const c = mixOklab(own, end, step / 100); + if (contrast(text, c) < Roles.text_floor) break; + const off = contrast(c, ground) / Roles.selectionFloor(ground); + if (off >= 1) return c; + if (off > best_off) { + best = c; + best_off = off; + } + } + } + return best; +} + +/// A ground off `page` toward `hue` at `t` (past 1, beyond the hue, as far +/// as the gamut goes), held where the body's `ink` reads on it at 4.5:1 +/// (or its own on the page, where that is less); moved on toward the ink +/// until it stands off the page at `floor`, as far as the ink allows. +fn hitOf(page: [3]u8, hue: [3]u8, ink: [3]u8, t: f32, floor: f32) [3]u8 { + const a = toOklab(page); + const b = toOklab(hue); + const ink_floor = @min(Roles.ink_floor, contrast(ink, page)); + var k = t; + var c = fromOklab(.{ a[0] + (b[0] - a[0]) * k, a[1] + (b[1] - a[1]) * k, a[2] + (b[2] - a[2]) * k }); + while (contrast(ink, c) < ink_floor and k > 0) { + k -= t / 50; + c = fromOklab(.{ a[0] + (b[0] - a[0]) * k, a[1] + (b[1] - a[1]) * k, a[2] + (b[2] - a[2]) * k }); + } + const start = c; + var step: f32 = 1; + while (contrast(c, page) < floor and step <= 50) : (step += 1) { + const next = mixOklab(start, ink, step / 100); + if (contrast(ink, next) < ink_floor) break; + c = next; + } + return c; +} + +/// A reading pane's tag (UX DESIGN.md D4): the theme's own where it stands +/// off the tag, else the tag shifted toward paper yellow by 0.04 in OKLab +/// and lighter by half that on a dark tag (darker on a light), so it reads +/// as a page; then less light, or darker, where its text would lose +/// contrast, and further where the bytes cannot show so small a step (a +/// near-black tag). +fn readingOf(th: *const Theme) [3]u8 { + if (th.tag_reading_bg) |own| if (oklabDistance(toOklab(own), toOklab(th.tag_bg)) >= Roles.reading_distance) return own; + const tag = toOklab(th.tag_bg); + const paper = toOklab(.{ 0xdc, 0xc8, 0x78 }); + const da = paper[1] - tag[1]; + const db = paper[2] - tag[2]; + const n = @max(@sqrt(da * da + db * db), 1e-6); + const sign: f32 = if (relativeLuminance(th.tag_bg) < 0.18) 1 else -1; + // Away from the text's lightness, where the first lean costs it. + const away: f32 = if (relativeLuminance(th.tag_fg) > relativeLuminance(th.tag_bg)) -1 else 1; + const inks = [2][3]u8{ th.tag_fg, th.tag_active_fg orelse th.tag_fg }; + var best = th.tag_bg; + var best_text: f32 = -21; + var reach = Roles.reading_distance; + while (reach <= 0.2) : (reach *= 1.25) { + for ([_]f32{ sign * 0.5, 0, away * 0.5, away, away * 1.5 }) |lean| { + const c = fromOklab(.{ tag[0] + lean * reach, tag[1] + da / n * reach, tag[2] + db / n * reach }); + if (oklabDistance(toOklab(c), tag) < Roles.reading_distance) continue; + // Each ink by its own floor: 7:1, or what it has on the tag. + var text: f32 = 21; + for (inks) |ink| text = @min(text, contrast(ink, c) - (@min(7, contrast(ink, th.tag_bg)) - 0.01)); + if (text >= 0) return c; + if (text > best_text) { + best = c; + best_text = text; + } + } + } + return best; +} + pub const initial_chrome = ChromeTheme.fromTheme(&themes[0]); fn nextThemeFileGeneration(p: *Pardes) u32 { @@ -1281,3 +1521,89 @@ test "every theme tints its names off the tag's ink, and they read on both groun } } } + +test "every theme's walk UI roles meet their floors on every ground they are drawn on, dark and light" { + // Where a theme's own colours leave no room for a floor, the most it + // allows: an ink's on a mid-grey tag (solarized's) is the better of + // white and black there; a ground's is where the ink it carries would + // drop under its floor. + const white: [3]u8 = @splat(255); + const black: [3]u8 = @splat(0); + const Reach = struct { + fn of(grounds: []const [3]u8) f32 { + return @max(leastContrast(white, grounds), leastContrast(black, grounds)); + } + }; + const a = std.testing.allocator; + const Check = struct { what: []const u8, got: f32, floor: f32 }; + var checks: std.ArrayList(Check) = .empty; + defer checks.deinit(a); + var failed: usize = 0; + for (&themes) |*th| { + checks.clearRetainingCapacity(); + const chrome = ChromeTheme.fromTheme(th); + const r = &chrome.roles; + const page = th.bg orelse null_page; + const body_ink = th.fg orelse th.tag_fg; + const tags = [_][3]u8{ r.tag[0].ground, r.tag[1].ground, r.tag[2].ground, r.selected[0].ground, r.selected[1].ground, r.selected[2].ground, r.selected[3].ground }; + const messages = [_][3]u8{ r.tag[0].ground, r.tag[1].ground, r.tag[2].ground, page }; + for (r.tag ++ r.selected) |inks| { + const text = contrast(inks.text, inks.ground); + try checks.append(a, .{ .what = "text", .got = text, .floor = @min(Roles.text_floor, Reach.of(&.{inks.ground})) }); + try checks.append(a, .{ .what = "dim", .got = contrast(inks.dim, inks.ground), .floor = @min(Roles.dim_floor, text) }); + try checks.append(a, .{ .what = "dim under text", .got = text - contrast(inks.dim, inks.ground), .floor = 0 }); + } + try checks.append(a, .{ .what = "key", .got = leastContrast(r.key, &tags), .floor = @min(Roles.ink_floor, Reach.of(&tags)) }); + try checks.append(a, .{ .what = "match", .got = leastContrast(r.match, &tags), .floor = @min(Roles.ink_floor, Reach.of(&tags)) }); + for (r.tag) |inks| { + try checks.append(a, .{ .what = "rule", .got = contrast(inks.rule, inks.ground), .floor = Roles.rule_floor }); + try checks.append(a, .{ .what = "rule ceiling", .got = Roles.rule_ceiling - contrast(inks.rule, inks.ground), .floor = 0 }); + } + for ([_][3]u8{ r.err, r.warn, r.question, r.hint }) |ink| + try checks.append(a, .{ .what = "message", .got = leastContrast(ink, &messages), .floor = @min(Roles.ink_floor, Reach.of(&messages)) }); + // A selection off its tag by 1.4:1 (1.25 light) with the text at + // 4.5:1 on it, where the text has that much over the tag. + for (r.tag, r.selected) |inks, selected| { + const floor = Roles.selectionFloor(inks.ground); + const room = contrast(inks.text, inks.ground) >= floor * Roles.text_floor * 1.03; + try checks.append(a, .{ .what = "selection", .got = contrast(selected.ground, inks.ground), .floor = if (room) floor else 0 }); + } + try checks.append(a, .{ .what = "reading", .got = oklabDistance(toOklab(chrome.tag_reading_bg), toOklab(th.tag_bg)), .floor = Roles.reading_distance }); + for ([_][3]u8{ th.tag_fg, th.tag_active_fg orelse th.tag_fg }) |ink| + try checks.append(a, .{ .what = "text on reading", .got = contrast(ink, chrome.tag_reading_bg), .floor = @min(7, contrast(ink, th.tag_bg)) - 0.02 }); + const ink_floor = @min(Roles.ink_floor, contrast(body_ink, page)); + for ([_][3]u8{ r.hit, r.hit_current }, [_]f32{ Roles.hit_floor, Roles.hit_current_floor }) |ground, floor| { + const on = contrast(body_ink, ground); + try checks.append(a, .{ .what = "hit", .got = contrast(ground, page), .floor = if (on < ink_floor + 0.3) 0 else floor }); + try checks.append(a, .{ .what = "ink on hit", .got = on, .floor = ink_floor - 0.01 }); + } + try checks.append(a, .{ .what = "current over hit", .got = contrast(r.hit_current, page) - contrast(r.hit, page), .floor = 0 }); + for (checks.items) |c| if (c.got < c.floor - 0.005) { + failed += 1; + std.debug.print("{s}: {s} {d:.2} under {d:.2}\n", .{ th.name, c.what, c.got, c.floor }); + }; + } + try std.testing.expectEqual(0, failed); +} + +test "neonotte84's and acme's roles are the ones UX derived (DESIGN.md D4, tools/roles.py)" { + const Want = struct { name: []const u8, text: ?[3]u8, dim: [3]u8, rule: [3]u8, key: [3]u8, match: [3]u8, reading: [3]u8, hit: [3]u8, hit_current: [3]u8 }; + const wants = [_]Want{ + .{ .name = "neonotte84", .text = .{ 0xb5, 0xbf, 0xe3 }, .dim = .{ 0x70, 0x76, 0x8f }, .rule = .{ 0x4c, 0x50, 0x63 }, .key = .{ 0xf8, 0xde, 0xd9 }, .match = .{ 0xf1, 0xa2, 0x71 }, .reading = .{ 0x29, 0x27, 0x1e }, .hit = .{ 0x36, 0x28, 0x2a }, .hit_current = .{ 0x52, 0x38, 0x31 } }, + // acme's key and match a little darker than UX's #5c5c8d and + // #974d00 (which hold on its tag and its #9eeeee): a reading pane's + // band's selection is pushed off the reading ground (#9eeeee is + // 1.12:1 off #f3f0cc), and they read on that one too. + .{ .name = "acme", .text = null, .dim = .{ 0x7a, 0x86, 0x86 }, .rule = .{ 0xa8, 0xb7, 0xb7 }, .key = .{ 0x55, 0x55, 0x83 }, .match = .{ 0x8f, 0x48, 0x00 }, .reading = .{ 0xf3, 0xf0, 0xcc }, .hit = .{ 0xec, 0xd8, 0xbb }, .hit_current = .{ 0xdd, 0xbc, 0x99 } }, + }; + for (wants) |want| { + const chrome = ChromeTheme.fromTheme(&themes[themeIndex(want.name).?]); + const plain = chrome.roles.on(.plain); + const got = [_][3]u8{ plain.text, plain.dim, plain.rule, chrome.roles.key, chrome.roles.match, chrome.tag_reading_bg, chrome.roles.hit, chrome.roles.hit_current }; + const expected = [_][3]u8{ want.text orelse plain.text, want.dim, want.rule, want.key, want.match, want.reading, want.hit, want.hit_current }; + for (got, expected, 0..) |g, e, i| for (g, e) |x, y| if (@abs(@as(i16, x) - y) > 2) { + std.debug.print("{s} role {d}: {x} for {x}\n", .{ want.name, i, g, e }); + return error.TestUnexpectedResult; + }; + } +} diff --git a/core/draw.zig b/core/draw.zig index 68778667..2fc69feb 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -883,13 +883,17 @@ fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: R 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; + // The walk UI's inks on this tag's ground (colors.Roles, UX DESIGN.md + // D4), and on its cursor row's selection. + const ground = groundOf(p, pane, active); + const inks = chrome.roles.on(ground); + const selected = chrome.roles.selectedOn(ground); + const bg = inks.ground; + const fg = inks.text; const storage = try arena.alloc(Cell, @as(usize, columns) * rows); @memset(storage, .{}); var t: Surface = .{ .cols = columns, .rows = rows, .cells = storage, .tab = tab }; @@ -898,32 +902,41 @@ fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: R 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. + // The spines and the rules between steps in the rule ink; a row's + // `▶` (the cursor) and `•` (marked) in 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 }; + const glyph = ln.text[at..next]; + if (std.mem.eql(u8, glyph, "│")) t.at(col, row).style.fg = .{ .rgb = inks.rule }; + if (std.mem.eql(u8, glyph, "▶") or std.mem.eql(u8, glyph, "•")) t.at(col, row).style.fg = .{ .rgb = chrome.roles.key }; 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); + const on = if (part.row.on) selected else inks; + if (part.row.on and part.col < end) t.tint(part.col, row, end - part.col, selected.ground, selected.text); 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.fg = .{ .rgb = chrome.roles.key }; style.bold = true; } + // A chord (Help's `SPC h t`): keys, at the text's weight. + if (b < part.row.chord) style.fg = .{ .rgb = chrome.roles.key }; 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.dim or (b >= part.row.pre and b < part.row.pre + part.row.faint)) style.fg = .{ .rgb = on.dim }; if (part.row.under) style.ul = .single; + // A matched letter: the match ink, bold, on the row's own + // ground (never a ground of its own: on the cursor row's + // selection one vanished). for (part.row.marks) |m| if (m == b) { - style.bg = .{ .rgb = chrome.search_bg }; + style.fg = .{ .rgb = chrome.roles.match }; + style.bold = true; }; b = next; } @@ -967,9 +980,14 @@ fn inkTag(p: *Pardes, s: *Surface, shown: tagline.Shown, lines: []const []const /// 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; + return p.chromeTheme().roles.on(groundOf(p, pane, active)).ground; +} + +/// Which of a tag's grounds pane `pane`'s is: its walk UI inks +/// (colors.Roles) are that ground's. +fn groundOf(p: *Pardes, pane: *const Pane, active: bool) colors.TagGround { + if (pardes.Trail.reading(p, pane)) return if (active) .reading_focused else .reading; + return if (active) .focused else .plain; } fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool) !void { @@ -981,7 +999,8 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, 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 = tagGround(p, pane, active); + const inks = chrome.roles.on(groundOf(p, pane, active)); + const tag_bg = inks.ground; 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 @@ -995,6 +1014,8 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, 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; + // Out of rest, the walk UI's text ink (at rest the tag's own, unchanged). + const text_style: CellStyle = if (head == null) tag_style else .{ .fg = .{ .rgb = inks.text }, .bg = .{ .rgb = tag_bg }, .font_role = .tagline }; // 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. const top = pane.tag_top; @@ -1028,7 +1049,7 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, for (lines[0..rows], 0..) |line, row| { // The scroll is the caret's line's: a shorter row shows nothing. const start = @min(line.len, panes.File.rawAtDisplay(p.settings.tab, line, pane.tag_scroll)); - _ = s.print(tx, tag_y + @as(u16, @intCast(row)), tw, line[start..], tag_style); + _ = s.print(tx, tag_y + @as(u16, @intCast(row)), tw, line[start..], text_style); } // The pane's progress at its first row's right end, where the row has // room for it (SPEC §10.2). @@ -1048,9 +1069,9 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, 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(p, 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); + 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), inks.dim); // A search word its places no longer light, dimmed (Prompt.zig). - if (pardes.Prompt.unlit(p, pane)) |span| inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, span[0], @min(span[1], first_line.len), chrome.lineno); + if (pardes.Prompt.unlit(p, pane)) |span| inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, span[0], @min(span[1], first_line.len), inks.dim); // 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| { @@ -1068,13 +1089,14 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, const style = &s.at(tx + @as(u16, @intCast(col)), y).style; switch (span.style) { .key => { - style.fg = .{ .rgb = chrome.box }; + style.fg = .{ .rgb = chrome.roles.key }; style.bold = true; }, - .dim => style.dim = true, + .dim => style.fg = .{ .rgb = inks.dim }, .under => style.ul = .single, .strike => style.strikethrough = true, - .err => style.fg = .{ .rgb = chrome.diagnostic_error }, + .err => style.fg = .{ .rgb = chrome.roles.err }, + .rule => style.fg = .{ .rgb = inks.rule }, } } } @@ -1745,3 +1767,44 @@ test "progress is said at the right end of its tag: a pane's at its tag's, a ser s = try p.render(frame.allocator()); try std.testing.expect(std.mem.indexOf(u8, Row.of(s, tag_y, &buf), "\u{22ef}") == null); } + +test "a tag out of rest draws its keys bold in the key ink and its hint in the dim ink, never faint (UX D4)" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 30 }); + defer p.deinit(); + const pane = try p.setTestFile("one\n"); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + const roles = &p.chromeTheme().roles; + const inks = roles.on(if (pardes.Trail.reading(p, pane)) .reading_focused else .focused); + // SPC's level rolled, after its delay: `␣ ▸ … l lsp… … ↓ unroll · ? help`. + 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 s = try p.render(a); + const r = p.view().rects[p.view().active]; + const row = s.cells[@as(usize, r.y) * s.cols ..][0 .. @as(usize, s.cols) * (pane.tag_rows - pane.band_rows)]; + const Row = struct { + fn find(cells: []const Cell, word: []const u8) ?usize { + var at: usize = 0; + while (at + word.len <= cells.len) : (at += 1) { + for (word, cells[at..][0..word.len]) |c, cell| { + if (cell.len != 1 or cell.text[0] != c) break; + } else return at; + } + return null; + } + }; + const hint = Row.find(row, "unroll").?; + try std.testing.expectEqual(Color{ .rgb = inks.dim }, row[hint].style.fg); + for (row) |cell| try std.testing.expect(!cell.style.dim); + var keys: usize = 0; + for (row) |cell| if (std.meta.eql(cell.style.fg, Color{ .rgb = roles.key })) { + try std.testing.expect(cell.style.bold); + keys += 1; + }; + try std.testing.expect(keys > 3); +} |
