//! `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 map = @import("map.zig"); const cursors = @import("cursor.zig"); const Stage = @import("stage.zig").Stage; const Recording = @import("Recording.zig"); 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, stage: *const Stage, focus: bool = false, /// In a `descent`: while the pc is in code without source, the line it /// called from waits, ringed, and the tag says where the pc is. descent: bool = false, /// Which box of its stage it is in: on the map the source may go on in `source_more`. part: @import("stage.zig").Part = .source, /// Words after the file's name in its tag (a `pair`'s shared source: `both builds`). tag: []const u8 = "", pub fn draw(p: *const Source, f: *sk.Frame, placed: anim.Placed) void { const box = p.stage.box(placed, p.part); if (box.h < m.tag + m.row) return; const u = context.at(f, placed, box); const rec = p.cursor.rec; const now = p.cursor.now(placed); // Its window, and on the map the lines of both its boxes (`continued`): this one from `skip` lines in. const head = theme.layout.rowsIn(p.stage.box(placed, .source).h); const tail = theme.layout.rowsIn(p.stage.box(placed, .source_more).h); const fit: u32 = @intCast(theme.layout.rowsIn(box.h)); const span_fit: u32 = @intCast(head + tail); const skip: f32 = if (p.part == .source_more) @floatFromInt(head) else 0; // The mark: leaving the line of one moment for that of the next, in // whichever file the pc is on a line of (a library's, where the // build gave its source), else where it waits a frame up. const at = [2]Recording.Shown{ rec.shown[now.marks[0]], rec.shown[now.marks[1]] }; const was = at[0].line; const here = at[1].line; // 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(); var windows = [2]f32{ @as(f32, @floatFromInt(rec.shownFirst(span_fit, at[0]))) + skip, @as(f32, @floatFromInt(rec.shownFirst(span_fit, at[1]))) + skip }; // The panel growing or shrinking at rest (a column's change): its window glides // from the one its old height has to its new one's, never a jump. if (now.resting()) if (p.stage.resizing(placed, p.part)) |r| { const ends = [2]f32{ @floatFromInt(rec.shownFirst(@intCast(theme.layout.rowsIn(r.from) + tail), at[1])), @floatFromInt(rec.shownFirst(@intCast(theme.layout.rowsIn(r.to) + tail), at[1])) }; windows = @splat(std.math.lerp(ends[0], ends[1], r.p) + skip); }; // Another file: the panel fades through (12 f out, 12 f in), its tag // naming the file it comes to. Farther than its own height in one // file (another function far off): its strip shows where it goes // first (24 f), then what leaves has left before what replaces it // arrives; a scroll of more lines than the window shows would be a // blur, and say nothing. const turn = at[0].src != at[1].src and going < 1; // At a walk, a window going farther than its height to lines its // column on the map has too: the pane `unroll`s to the column, its // window travels there along it, and it `roll`s back in at its new // place (`unrolling`, 96 f); the pane goes into the column as it // grows and comes out of it as it rolls back, never a fade. const jump = unrolling(rec, now, at, p.cursor.levels.source_rows); // (On the map, for the strips, half its height: a column scrolling most of its rows would be a smear on a strip.) const far = !turn and jump == null and @abs(windows[1] - windows[0]) > @as(f32, @floatFromInt(if (ui.mapping) fit / 2 else fit)) and going < 1; if (jump) |j| if (!ui.mapping and p.part == .source) { map.jumping = .{ .panel = .source, .grow = j.grow, .veil = j.veil }; }; // On the map, for the strip, a far window's column goes over as the advance starts: // the strip (6 to 11 f behind) shows where the pane goes before the pane fades. const side: usize = if (jump) |j| @intFromBool(j.travel >= 0.5) else if (turn) @intFromBool(going >= 0.5) else if (far) @intFromBool(if (ui.mapping) going > 0 else going >= 0.75) else @intFromBool(going >= 0.5); const file = rec.sources[at[side].src]; const text = prepared(file.text, file.path) orelse return; const first = if (jump) |j| (if (ui.mapping) std.math.lerp(windows[0], windows[1], cursors.glide(j.travel)) else windows[side]) else if (turn or far) windows[side] else std.math.lerp(windows[0], windows[1], cursors.glidePixels(going, now.plan.travel, @abs(windows[1] - windows[0]) * m.row * u.at.scale)); const fade: f32 = if (jump) |j| (if (ui.mapping) 1 else 1 - theme.ease(.glide, j.grow)) else if (turn) @abs(2 * going - 1) else if (far and !ui.mapping) (if (going < 0.5) 1 else @abs(4 * going - 3)) else 1; const lit = u.faded(fade); const from: u32 = @intFromFloat(@floor(first)); const last: u32 = @min(from + @as(u32, @intCast(context.rowsOf(box))), file.lines); // Waiting: the pc is in code with no source (a PLT stub, a library without it), under this line's call. const waits = p.descent and !at[side].here and at[side].line != 0; var lines_notes: [2][]const u8 = undefined; for (at, windows, &lines_notes) |a, w, *note| { const lines = rec.sources[a.src].lines; const shown_first: u32 = @intFromFloat(w - skip); note.* = if (lines > fit) u.print("lines {d} to {d} of {d}", .{ shown_first, @min(shown_first + fit - 1, lines), lines }) else u.print("{d} lines", .{lines}); } // Where the pc is, said where it waits: a change of it (on through a // stub) crossfades over the 12 f before the advance, never in a frame. var notes: [2][]const u8 = undefined; for (now.marks, at, ¬es, lines_notes) |mk, a, *note, lines_note| note.* = if (p.descent and !a.here and a.line != 0) u.print("{s} waits at line {d} \u{00b7} the pc is in {s}", .{ context.frameName(u, rec, rec.register(lastShown(rec, mk), "rip")), a.line, whereIs(u, rec, mk) }) else lines_note; const swap = !std.mem.eql(u8, notes[0], notes[1]) and now.walking(); const titles = [2][]const u8{ rec.sources[at[0].src].name(), rec.sources[at[1].src].name() }; const title = if (p.part == .source_more) u.print("{s}, continued", .{file.name()}) else if (p.tag.len > 0) u.print("{s} \u{00b7} {s}", .{ file.name(), p.tag }) else file.name(); const body = u.panel(box.w, box.h, if (turn) "" else title, .{ .focus = p.focus, .note = if (swap) "" else notes[side] }); // Its tag's file and note change with the fade through it; a note otherwise over the 12 f before the advance. if (turn) for (titles, [2]f32{ 1 - 2 * going, 2 * going - 1 }) |t, shows| u.faded(shows).tagTitle(t, p.focus); if (swap) { // Through another file, or to lines far off, it changes with the pane's fade: the lines it names are the lines shown. // A window scrolling: its range changes with the scroll, the lines it names the lines shown (never before them). const scrolls = std.mem.eql(u8, notes[0], lines_notes[0]) and std.mem.eql(u8, notes[1], lines_notes[1]); const c = if (jump) |j| std.math.clamp(2 * j.travel - 0.5, 0, 1) else if (turn) std.math.clamp(2 * going - 1, 0, 1) else if (far) std.math.clamp(4 * going - 3, 0, 1) else if (scrolls) cursors.glide(going) else now.renaming(); const old = if (jump != null) 1 - c else if (turn) 1 - 2 * going else if (far) std.math.clamp(3 - 4 * going, 0, 1) else 1 - c; u.faded(old).tagNote(box.w, notes[0]); u.faded(c).tagNote(box.w, notes[1]); } const top = body.y + m.pad; // Where the mark is, and how much of it. const travel = now.plan.travel; const moved = going * travel; var mark_line = here; var mark_at: f32 = @floatFromInt(here); var shows: f32 = 1; if (jump) |j| { // In the column it lights at its new line as the window gets there; in the pane it goes with the pane. mark_line = if (j.travel >= 0.5) here else was; mark_at = @floatFromInt(mark_line); shows = fade; } else if (turn or far) { // It goes with the fade: out with the old window, in with the new. mark_line = at[side].line; mark_at = @floatFromInt(mark_line); shows = fade; // On the map, for the strip, it lights on its new line at once: the strip shows where the pane goes before it fades. if (ui.mapping and far and going > 0) { mark_line = here; mark_at = @floatFromInt(here); shows = 1; } } else if (going < 1) { if (!now.plan.wave and !cursors.jumps(rec, now.marks[0], now.marks[1])) { // A line near by: it slides, off promptly and settling on // the next. The step's follow-through. mark_at = std.math.lerp(@as(f32, @floatFromInt(was)), mark_at, cursors.standard(going)); if (@abs(mark_at - @as(f32, @floatFromInt(was))) < 0.5) mark_line = was; } else { // A call, a return, a line far off, a continue: a jump. Out // there, then in here; it does not cross the lines between. const half = @min(theme.time.current_jump, travel / 2); if (moved < travel - half) { mark_line = was; mark_at = @floatFromInt(was); shows = 1 - std.math.clamp(moved / half, 0, 1); } else shows = std.math.clamp((moved - (travel - half)) / half, 0, 1); } } const mark_y = top + (mark_at - first) * m.row; // The numbers as wide as the widest file's largest: a scroll or another file does not shift the text. const widen = rows.numberRoom(rec.most_lines); context.clipBody(u, box); // 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 (waits) { // The number ringed: a 2-unit primary frame where the plaque would be, no wash. u.faded(shows).frame(.{ .x = theme.stroke.rule + 1, .y = mark_y + 1, .w = theme.metric.plaque.line + widen - 2, .h = m.row - 2 }, theme.color.text.primary, theme.stroke.rule); if (!ui.mapping) ties.of(u.f).thread_waits = true; } else 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 + widen, 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 of its file it was on, as // the mark left them and as it finds them: a bar that moves crossfades with the advance. var trails: [2][3]u32 = @splat(@splat(0)); var founds = [2]usize{ 0, 0 }; for (now.marks, &trails, &founds) |mk, *trail, *found| { const on = rec.shown[mk]; var back = mk; while (back > 0 and found.* < trail.len) { back -= 1; const l = rec.shown[back]; if (l.line == 0 or l.src != on.src or l.line == on.line or std.mem.indexOfScalar(u32, trail[0..found.*], l.line) != null) continue; trail[found.*] = l.line; found.* += 1; } } if (turn) { // Another file's trail is not this one's. trails[1 - side] = @splat(0); founds[1 - side] = 0; } const lines_of = struct { fn of(a: f32, n: u32, lines: u32) [2]u64 { const l: u32 = @intFromFloat(a); return .{ l, @min(l + n - 1, lines) }; } }.of; // On the strip the window glides there; a window fading through goes before the fade, so the strip shows where it went first. map.window(.source, lines_of(windows[1], fit, rec.sources[at[1].src].lines), lines_of(windows[0], fit, rec.sources[at[0].src].lines), if (jump) |j| cursors.glide(j.travel) else if (turn) @floatFromInt(side) else cursors.glide(if (far) @min(2 * going, 1) else going)); var line = @max(from, 1); while (line <= last) : (line += 1) { const y = top + (@as(f32, @floatFromInt(line)) - first) * m.row; map.row(.source, line, u, .{ .x = 0, .y = y, .w = box.w, .h = m.row }); const span = text.lines[line - 1]; const ks = [2]?usize{ std.mem.indexOfScalar(u32, trails[0][0..founds[0]], line), std.mem.indexOfScalar(u32, trails[1][0..founds[1]], line) }; const k = if (going >= 0.5) ks[1] else ks[0]; const on = shows >= 0.5 and line == mark_line; const fading = !std.meta.eql(ks[0], ks[1]) and going > 0 and going < 1; rows.line(lit, y, map.content(box.w), line, widen, text.bytes[span[0]..span[1]], text.styles[span[0]..span[1]], if (on and !waits) .marked else if (fading) .plain else if (k) |n| .{ .trail = n } else .plain); if (fading and !(on and !waits)) for (ks, [2]f32{ 1 - going, going }) |kk, amount| if (kk) |n| lit.faded(amount).trail(theme.stroke.rule, y, n); } // The source is read at a run; at a sprint it is veiled too. context.veiled(u, box, now.sprint_veil); u.unclip(); // Another file is another column: its strip fades through as the pane does. map.strip(.source, if (turn) u.faded(map.through(now, travel)) else u, box); } }; /// A walked step whose source window (`fit` lines) goes farther than its /// own height to lines the map's column holds as well (69 round the line, /// or the whole file): the far jump in the same column, told by `unroll`, /// the window's travel and `roll` (design/motion.md, "The map and the /// working frame"). Where it is in them, 0 to 1 each: how far the column /// has grown (before its `glide`), the window's travel along it, the veil. pub fn unrolling(rec: *const Recording, now: cursors.Now, at: [2]Recording.Shown, fit: u32) ?struct { grow: f32, travel: f32, veil: f32 } { const going = now.advancing(); // A step said faster in a hurry is no beat for it: it fades through as at a run. if (!now.walking() or now.hurried > 0 or going <= 0 or going >= 1 or at[0].src != at[1].src) return null; const w = [2]u32{ rec.shownFirst(fit, at[0]), rec.shownFirst(fit, at[1]) }; if (!inColumn(rec, at[0].src, w, fit)) return null; const t = theme.time.unroll; // Its phases in proportion to the advance's travel (96 f as `Cursor.travel` gives it). const g = going * (t.grow + t.travel + t.roll); const back = g - t.grow - t.travel; return .{ .grow = if (back <= 0) std.math.clamp(g / t.grow, 0, 1) else 1 - std.math.clamp(back / t.roll, 0, 1), .travel = std.math.clamp((g - t.grow) / t.travel, 0, 1), .veil = @min(std.math.clamp(g / t.veil, 0, 1), std.math.clamp((t.grow + t.travel + t.roll - g) / t.veil, 0, 1)), }; } /// Whether windows of `fit` lines at `w` in source `src` are farther apart /// than their height and both in the map's column of it (the lines its /// source panels hold round the line, or the file whole): a far jump the /// unrolled column can show. pub fn inColumn(rec: *const Recording, src: u16, w: [2]u32, fit: u32) bool { const lo = @min(w[0], w[1]); const hi = @max(w[0], w[1]); // The map's column as the scene's map packs it (69 lines where there is none). const column: u32 = if (map.current) |mp| @intCast(theme.layout.rowsIn(mp.stage.first.source.h) + theme.layout.rowsIn(mp.stage.first.source_more.h)) else theme.map.disasm_rows; return hi - lo > fit and (rec.sources[src].lines <= column or hi + fit - lo <= column); } /// The last moment at or before `i` whose pc was on a line the panel can show. fn lastShown(rec: *const Recording, i: u64) u64 { var at = i; while (at > 0 and !rec.shown[at].here) at -= 1; return at; } /// Where the pc is at moment `i`, for the waiting line's tag: `the PLT`, /// `libc's open`, `the vdso`. fn whereIs(u: ui.Ui, rec: *const Recording, i: u64) []const u8 { const name = context.frameName(u, rec, rec.register(i, "rip")); if (std.mem.endsWith(u8, name, "@plt")) return "the PLT"; var module: []const u8 = ""; var buf: [256]u8 = undefined; _ = (rec.moment(i).mapping(rec.register(i, "rip"), &module, &buf) catch null); const base = std.fs.path.basename(module); const short = base[0 .. std.mem.indexOfScalar(u8, base, '.') orelse base.len]; return u.print("{s}'s {s}", .{ short, name }); } /// A source ready to draw: a class for each byte, and where each line is. pub const Text = struct { bytes: []const u8, styles: []const u8, lines: []const [2]usize, /// `bytes` is borrowed; the caller keeps it alive for this prepared text. pub fn deinit(text: Text) void { gpa.free(text.styles); gpa.free(text.lines); } }; /// Made once for each source and kept: a scene draws the same few. var texts: std.ArrayList(Text) = .empty; fn prepared(source: []const u8, file: []const u8) ?*const Text { for (texts.items) |*t| if (t.bytes.ptr == source.ptr) return t; texts.append(gpa, prepare(source, file) catch return null) catch return null; return &texts.items[texts.items.len - 1]; } pub fn prepare(source: []const u8, file: []const u8) !Text { var analysis = try pardes.analyzeSource(gpa, file, source, false, true); errdefer analysis.deinit(gpa); var lines: std.ArrayList([2]usize) = .empty; defer lines.deinit(gpa); var start: usize = 0; while (start < source.len) { const end = std.mem.indexOfScalarPos(u8, source, start, '\n') orelse source.len; try lines.append(gpa, .{ start, end }); start = end + 1; } // A language pardes has no grammar for is drawn plain. if (analysis.colors.len != source.len) { analysis.deinit(gpa); const plain = try gpa.alloc(u8, source.len); @memset(plain, 0); analysis.colors = plain; } return .{ .bytes = source, .styles = analysis.colors, .lines = try lines.toOwnedSlice(gpa) }; } test "a source is cut into lines and classed byte by byte" { const source = "int main(void) {\n return 0; // done\n}\n"; const text = try prepare(source, "main.c"); defer gpa.free(text.lines); defer gpa.free(text.styles); try std.testing.expectEqual(@as(usize, 3), text.lines.len); try std.testing.expectEqualStrings(" return 0; // done", source[text.lines[1][0]..text.lines[1][1]]); const at = std.mem.indexOf(u8, source, "return").?; try std.testing.expectEqual(pardes.Syn.keyword, @as(pardes.Syn, @fromBackingInt(@intCast(text.styles[at])))); try std.testing.expectEqual(pardes.Syn.number, @as(pardes.Syn, @fromBackingInt(@intCast(text.styles[at + 7])))); try std.testing.expectEqual(pardes.Syn.comment, @as(pardes.Syn, @fromBackingInt(@intCast(text.styles[at + 10])))); // rows.line reads these classes by number. try std.testing.expectEqual(@as(u8, 1), @backingInt(pardes.Syn.keyword)); try std.testing.expectEqual(@as(u8, 4), @backingInt(pardes.Syn.comment)); }