diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 16:05:20 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 16:43:58 -0300 |
| commit | 165bb6c1344b98de7bef1d3c2b72147b4a40797a (patch) | |
| tree | 0e70e6222700063f604f78673f6ab3a5bfb9242d /anim/src/source.zig | |
| parent | 66a2a7cf733f6abc1f79561c52d901ce67019fb1 (diff) | |
| download | computeranim-165bb6c1344b98de7bef1d3c2b72147b4a40797a.tar.gz computeranim-165bb6c1344b98de7bef1d3c2b72147b4a40797a.zip | |
canim: the renderer speaks the design language
The blocks, their states and the tokens carry the catalogue's names:
atoms in ui, chain/reg/slot in rows, insn with its line column, jump lane
and annotations (read, write, taken or not, a call with its arguments, a
return, a syscall) in disasm, bytes and hexdump with field paper and the
reading column, the three splits and relayout in stage, the ties (thread,
link and pin, projection, echo). A lens is a band over the whole context,
its drawing left and its bytes right. Frames' spans on the stack, each
instruction's line and a callee's parameter count come from the tracer.
A gallery scene draws every block; the test compares six of its pages
with pictures kept in test/gallery, drawn from a recording kept there
too. The scenes are ported; the motion checks report no pop and no
metronome in any of them.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'anim/src/source.zig')
| -rw-r--r-- | anim/src/source.zig | 108 |
1 files changed, 36 insertions, 72 deletions
diff --git a/anim/src/source.zig b/anim/src/source.zig index 6c10b50..51059a7 100644 --- a/anim/src/source.zig +++ b/anim/src/source.zig @@ -1,57 +1,57 @@ -//! The source panel: the program's text as pardes colours it, line numbers, -//! the current mark (gold: a band along the line and a plaque over its -//! number), and the three lines before it (the trail, gold cooling). The -//! mark fires on the line about to run, and moves on last, when what the -//! line did has been seen: a slide to a line near by, a jump (out, then -//! in) into a call or out of one. -//! Reads `Moment.line` (through the recording's index of it). +//! `source` (design/README.md, "3. Panels"): a window on the whole file, +//! scrolled by whole lines to keep the current line in its middle third: +//! fourteen lines, eight beside a lens, ten when the lens is the subject. +//! Its rows are `line`s, pardes's colours; the current mark is a gold +//! plaque on the number and its ember across, the three lines before it +//! the trail. The mark fires on the line about to run and moves on last: +//! a slide to a line near by, a jump (out, then in) into a call or out +//! of one. Reads `Moment.line` (through the recording's index of it). const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); const pardes = @import("pardes-syntax"); const ui = @import("ui.zig"); +const rows = @import("rows.zig"); +const context = @import("context.zig"); +const ties = @import("ties.zig"); const cursors = @import("cursor.zig"); +const Stage = @import("stage.zig").Stage; const Cursor = cursors.Cursor; const theme = ui.theme; const Rect = ui.Rect; +const m = theme.metric.context; const gpa = std.heap.smp_allocator; pub const Source = struct { cursor: Cursor, - box: Rect, + stage: *const Stage, focus: bool = false, - pub fn draw(p: *const Source, f: *sk.Frame, at: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = at }; + pub fn draw(p: *const Source, f: *sk.Frame, placed: anim.Placed) void { + const box = p.stage.box(placed, .source); + const u = context.at(f, placed, box); const rec = p.cursor.rec; - const now = p.cursor.now(at); - const size = theme.size.mono.context; - const adv = ui.advance(size); + const now = p.cursor.now(placed); const text = prepared(rec.source, rec.file) orelse return; - const fit: u32 = @intFromFloat(@floor((p.box.h - m.tag - 2 * m.pad) / m.row)); + const fit: u32 = @intCast(theme.layout.rowsIn(box.h)); // The mark: leaving the line of one moment for that of the next. const was = rec.own[now.marks[0]]; const here = rec.own[now.marks[1]]; - const on_line = rec.own[now.marked()]; // The window: where the line it is on has it, and (as the mark // moves on) gliding to where the next one does. It never cuts. const going = now.advancing(); const windows = [2]f32{ @floatFromInt(rec.viewFirst(fit, was)), @floatFromInt(rec.viewFirst(fit, here)) }; const first = std.math.lerp(windows[0], windows[1], cursors.glide(going)); const from: u32 = @intFromFloat(@floor(first)); - const last: u32 = @min(from + fit, rec.line_count); - const body = u.panel(p.box.w, p.box.h, rec.file, .{ .focus = p.focus, .note = if (rec.line_count > fit) u.print("lines {d} to {d} of {d}", .{ from, @min(from + fit - 1, rec.line_count), rec.line_count }) else u.print("{d} lines", .{rec.line_count}) }); + const last: u32 = @min(from + @as(u32, @intCast(context.rowsOf(box))), rec.line_count); + const shown_first: u32 = @intFromFloat(windows[if (going >= 0.5) 1 else 0]); + const body = u.panel(box.w, box.h, rec.file, .{ .focus = p.focus, .note = if (rec.line_count > fit) u.print("lines {d} to {d} of {d}", .{ shown_first, @min(shown_first + fit - 1, rec.line_count), rec.line_count }) else u.print("{d} lines", .{rec.line_count}) }); const top = body.y + m.pad; - const gutter = 3 * adv + 22; - const code_x = gutter + 12; - const max_chars: usize = @intFromFloat(@floor((p.box.w - code_x - m.side) / adv)); // Where the mark is, and how much of it. - const current = theme.mark(.current); const travel = now.plan.travel; const moved = going * travel; - const scrolls = windows[0] != windows[1]; var mark_line = here; var mark_at: f32 = @floatFromInt(here); var shows: f32 = 1; @@ -73,11 +73,15 @@ pub const Source = struct { } } const mark_y = top + (mark_at - first) * m.row; - u.clip(.{ .x = theme.stroke.rule, .y = body.y + 2, .w = p.box.w - 2 * theme.stroke.rule, .h = body.h - 4 }); - const marked = u.faded(shows); - if (shows > 0) marked.fill(.{ .x = theme.stroke.rule, .y = mark_y, .w = p.box.w - 2 * theme.stroke.rule, .h = m.row }, current.ember); + context.clipBody(u, box); + defer u.unclip(); + // The plaque over the number. Fire: on the line about to run its + // edges go out and come back, once, and nothing else moves. + if (shows > 0) u.faded(shows).current(.{ .x = theme.stroke.rule, .y = mark_y, .w = box.w - 2 * theme.stroke.rule, .h = m.row }, theme.metric.plaque.line, now.fires(.source)); + if (shows >= 1 and mark_y >= body.y and mark_y + m.row <= box.h) ties.anchor(u, .source, .{ .x = 0, .y = mark_y, .w = box.w, .h = m.row }); // The trail: the last three other lines it was on. It shifts without animation. + const on_line = rec.own[now.marked()]; var trail: [3]u32 = @splat(0); var found: usize = 0; var back = now.marked(); @@ -88,62 +92,19 @@ pub const Source = struct { trail[found] = l; found += 1; } - for (trail[0..found], 0..) |l, k| u.fill(.{ .x = theme.stroke.rule, .y = top + (@as(f32, @floatFromInt(l)) - first) * m.row + 2, .w = 4, .h = m.row - 4 }, theme.color.trail[k]); - - // The plaque over the number. Fire: on the line about to run its - // edges go out and come back, once, and nothing else moves. - const out = now.fires(.source); - if (shows > 0) marked.fill(.{ .x = theme.stroke.rule - out, .y = mark_y - out, .w = gutter - 2 + 2 * out, .h = m.row + 2 * out }, current.solid); - if (shows >= 1 and mark_y >= body.y and mark_y + m.row <= body.y + body.h) @import("ties.zig").anchor(u, .source, .{ .x = 0, .y = mark_y, .w = gutter, .h = m.row }); var line = @max(from, 1); while (line <= last) : (line += 1) { const y = top + (@as(f32, @floatFromInt(line)) - first) * m.row; - const row: Rect = .{ .x = 0, .y = y, .w = gutter - 10, .h = m.row }; - // The number under the plaque is the one in ink. - const on = shows >= 0.5 and line == mark_line and !(scrolls and going > 0 and going < 1 and line != here); - u.label(if (on) .mono_bold else .mono, size, row, .right, u.print("{d}", .{line}), if (on) current.on_solid else theme.color.text.faint); const span = text.lines[line - 1]; - const bytes = text.bytes[span[0]..span[1]]; - const styles = text.styles[span[0]..span[1]]; - // Runs of one class, each set where its column is. - var i: usize = 0; - const end = @min(bytes.len, max_chars); - while (i < end) { - var j = i + 1; - while (j < end and styles[j] == styles[i]) j += 1; - const class: pardes.Syn = @enumFromInt(styles[i]); - const x = code_x + @as(f32, @floatFromInt(i)) * adv; - if (std.mem.indexOfNone(u8, bytes[i..j], " ") != null) u.text(face(class), size, x, theme.baseline(y, m.row, size), bytes[i..j], ink(class)); - i = j; - } - // A line longer than the panel is cut, and says so. - if (bytes.len > max_chars) u.text(.mono, size, code_x + @as(f32, @floatFromInt(max_chars)) * adv, theme.baseline(y, m.row, size), "\u{2026}", theme.color.text.faint); + const k = std.mem.indexOfScalar(u32, trail[0..found], line); + const on = shows >= 0.5 and line == mark_line; + rows.line(u, y, box.w, line, text.bytes[span[0]..span[1]], text.styles[span[0]..span[1]], if (on) .marked else if (k) |n| .{ .trail = n } else .plain); } - u.unclip(); + context.veiled(u, box, now.veil); } }; -const m = theme.metric.context; - -fn ink(class: pardes.Syn) u24 { - const c = theme.color.code; - return switch (class) { - .none => c.plain, - .keyword => c.keyword, - .string, .number => c.literal, - .comment => c.comment, - }; -} - -fn face(class: pardes.Syn) ui.Face { - return switch (class) { - .keyword => .mono_bold, - .comment => .mono_italic, - else => .mono, - }; -} - /// A source ready to draw: a class for each byte, and where each line is. const Text = struct { bytes: []const u8, @@ -190,4 +151,7 @@ test "a source is cut into lines and classed byte by byte" { try std.testing.expectEqual(pardes.Syn.keyword, @as(pardes.Syn, @enumFromInt(text.styles[at]))); try std.testing.expectEqual(pardes.Syn.number, @as(pardes.Syn, @enumFromInt(text.styles[at + 7]))); try std.testing.expectEqual(pardes.Syn.comment, @as(pardes.Syn, @enumFromInt(text.styles[at + 10]))); + // rows.line reads these classes by number. + try std.testing.expectEqual(@as(u8, 1), @intFromEnum(pardes.Syn.keyword)); + try std.testing.expectEqual(@as(u8, 4), @intFromEnum(pardes.Syn.comment)); } |
