summaryrefslogtreecommitdiff
path: root/anim/src/calls.zig
diff options
context:
space:
mode:
Diffstat (limited to 'anim/src/calls.zig')
-rw-r--r--anim/src/calls.zig139
1 files changed, 110 insertions, 29 deletions
diff --git a/anim/src/calls.zig b/anim/src/calls.zig
index 88bca71..e66da1a 100644
--- a/anim/src/calls.zig
+++ b/anim/src/calls.zig
@@ -13,6 +13,8 @@ const Cursor = cursors.Cursor;
const Recording = @import("Recording.zig");
const context = @import("context.zig");
const Stage = @import("stage.zig").Stage;
+const ties = @import("ties.zig");
+const rows = @import("rows.zig");
const theme = ui.theme;
const time = theme.time;
const Rect = ui.Rect;
@@ -30,80 +32,159 @@ pub const Tree = struct {
focus: bool = false,
pub fn draw(k: *const Tree, f: *sk.Frame, at: anim.Placed) void {
+ k.backtrace(f, at);
const box = k.stage.box(at, .band);
if (box.h < theme.metric.context.tag + 24) return;
- const u = context.at(f, at, box);
+ const u = context.at(f, at, box).faded(at.value(k.stage.drawing));
+ // A relayout moves the band: what is drawn in it stays inside it.
+ u.clip(.{ .x = 0, .y = 0, .w = box.w, .h = box.h });
+ defer u.unclip();
const now = k.cursor.now(at);
const rec = k.cursor.rec;
- const m = struct {
- const radius = 18;
- const level = 44;
- const badge = 24;
- };
- const body = u.panel(box.w, box.h, u.print("calls of {s}", .{k.function}), .{ .focus = k.focus, .note = "each call a node \u{00b7} its result on the shoulder \u{00b7} the frames are the stack's" });
+ const m = metric;
+ const body = u.panel(box.w, box.h, u.print("calls of {s}", .{k.function}), .{ .focus = k.focus, .note = "each call a node \u{00b7} its result on its shoulder" });
const nodes = layout(f.scratch, rec, k.function) catch return;
if (nodes.len == 0) return;
var leaves: f32 = 0;
for (nodes) |n| leaves = @max(leaves, n.x + 1);
- const pitch = @min(4.5 * m.radius, (body.w - 2 * theme.metric.panel.pad_x - 2 * m.radius) / @max(leaves - 1, 1));
- const left = (body.w - (leaves - 1) * pitch) / 2;
- const top = body.y + m.radius + 12;
- var deepest_level: f32 = 1;
- for (nodes) |n| deepest_level = @max(deepest_level, @as(f32, @floatFromInt(n.level)));
- const level_h = @min(m.level, (body.h - 2 * m.radius - 24) / deepest_level);
- // A call: its edge draws from the caller's node, then the node
- // fades in, gold, and the caller's goes from gold to a waiting
- // ring. A return: the badge is struck a little after the card's
- // result, and the node goes from gold to done.
+ // The caller's box left of the root on its level; the leaves spread over the rest.
+ const caller_room = m.caller[0] + 48;
+ const pitch = (body.w - 2 * theme.metric.panel.pad_x - 2 * m.radius - caller_room) / @max(leaves - 1, 1);
+ const left = theme.metric.panel.pad_x + caller_room + m.radius;
+ const top = body.y + m.root;
const since = now.since(.idea, 0);
const returned = now.walking() and rec.depths[now.stops[0]] < rec.depths[now.stops[1]];
const moment = if (now.plan.gait == .sprint or since >= (if (returned) @as(f32, badge_after) else 0)) now.stops[0] else now.stops[1];
const edge = cursors.standard(now.moving(.idea, time.quick, time.quick));
const appears = now.moving(.idea, 0, time.snap);
+ const at_of = struct {
+ fn c(lf: f32, p: f32, tp: f32, n: Node) ui.Vec {
+ return .{ lf + n.x * p, tp + @as(f32, @floatFromInt(n.level)) * metric.level };
+ }
+ }.c;
- // Edges under nodes: the waiting path in vellum.
+ // The caller: gold until the first call, then waiting, ringed; an edge into the root.
+ if (nodes[0].call.parent) |p| {
+ const caller = rec.calls[p];
+ const root_c = at_of(left, pitch, top, nodes[0]);
+ const r: Rect = .{ .x = theme.metric.panel.pad_x, .y = root_c[1] - m.caller[1] / 2, .w = m.caller[0], .h = m.caller[1] };
+ const called = nodes[0].call.enter <= moment;
+ if (nodes[0].call.enter <= now.stops[0]) {
+ const to = ui.Vec{ root_c[0] - m.radius, root_c[1] };
+ const from = ui.Vec{ r.x + r.w, root_c[1] };
+ const live = nodes[0].call.active(moment) or nodes[0].call.enter > moment;
+ u.line(from, if (nodes[0].call.enter > now.stops[1]) sk.math.mix(from, to, edge) else to, if (live) ui.rgb(t.primary) else ui.rgb(theme.color.rule), theme.stroke.line);
+ }
+ if (!called) {
+ u.fill(r, theme.mark(.current).solid);
+ u.label(.mono_bold, theme.size.mono.context, r, .center, rec.name(caller.function), theme.mark(.current).on_solid);
+ } else {
+ u.fill(r, theme.color.page);
+ u.frame(r, t.primary, theme.stroke.line);
+ u.label(.mono, theme.size.mono.context, r, .center, rec.name(caller.function), t.primary);
+ }
+ }
+ // Edges under nodes: the waiting path in primary.
for (nodes) |n| {
const parent = n.parent orelse continue;
if (n.call.enter > now.stops[0]) continue;
- const from = ui.Vec{ left + nodes[parent].x * pitch, top + @as(f32, @floatFromInt(nodes[parent].level)) * level_h };
- var to = ui.Vec{ left + n.x * pitch, top + @as(f32, @floatFromInt(n.level)) * level_h };
+ const from = at_of(left, pitch, top, nodes[parent]);
+ var to = at_of(left, pitch, top, n);
if (n.call.enter > now.stops[1]) to = sk.math.mix(from, to, edge);
const live = n.call.active(moment);
- u.line(from, to, if (live or n.call.enter > moment) ui.rgb(t.primary) else ui.rgb(theme.color.rule), if (live or n.call.enter > moment) theme.stroke.arrow else theme.stroke.line);
+ u.line(from, to, if (live or n.call.enter > moment) ui.rgb(t.primary) else ui.rgb(theme.color.rule), theme.stroke.line);
}
const deepest = deepestActive(nodes, moment);
for (nodes, 0..) |n, i| {
+ const c = at_of(left, pitch, top, n);
+ // Where a projection from its frame's way back (the word its
+ // call pushed, at the call's stack pointer) lands: under it,
+ // there before the node, which comes when it arrives.
+ if (n.call.enter <= now.stops[0]) ties.anchor(u, .{ .node_address = n.call.sp }, .{ .x = c[0] - m.radius, .y = c[1] - m.radius, .w = 2 * m.radius, .h = 2 * m.radius });
if (n.call.enter > moment) continue;
- const c = ui.Vec{ left + n.x * pitch, top + @as(f32, @floatFromInt(n.level)) * level_h };
const v = if (n.call.enter > now.stops[1]) u.faded(appears) else u;
if (deepest == i) {
v.disc(c, m.radius, ui.rgb(theme.mark(.current).solid), null, 0);
} else if (n.call.active(moment)) {
- v.disc(c, m.radius - theme.stroke.arrow / 2, ui.rgb(theme.color.page), ui.rgb(t.primary), theme.stroke.arrow);
+ v.disc(c, m.radius - theme.stroke.line / 2, ui.rgb(theme.color.page), ui.rgb(t.primary), theme.stroke.line);
} else v.disc(c, m.radius, ui.rgb(theme.color.raised), null, 0);
}
// Then the words, so shapes and text each go in one run.
- const size = theme.size.mono.context;
+ const size = theme.size.mono.small;
for (nodes, 0..) |n, i| {
if (n.call.enter > moment) continue;
- const cx = left + n.x * pitch;
- const cy = top + @as(f32, @floatFromInt(n.level)) * level_h;
+ const c = at_of(left, pitch, top, n);
const active = n.call.active(moment);
const words = if (n.call.arg) |arg| u.print("{d}", .{@as(i64, @bitCast(arg))}) else "?";
- const cell: Rect = .{ .x = cx - m.radius, .y = cy - m.radius, .w = 2 * m.radius, .h = 2 * m.radius };
+ const cell: Rect = .{ .x = c[0] - m.radius, .y = c[1] - m.radius, .w = 2 * m.radius, .h = 2 * m.radius };
const v = if (n.call.enter > now.stops[1]) u.faded(appears) else u;
if (deepest == i) {
v.label(.mono_bold, size, cell, .center, words, theme.mark(.current).on_solid);
- } else v.label(.mono, size, cell, .center, words, if (active) t.primary else t.dim);
+ } else v.label(.mono, size, cell, .center, words, t.primary);
if (active) continue;
// Done: what it returned, on its shoulder; struck as it lands.
- const badge: Rect = .{ .x = cx + 10, .y = cy - m.radius - 12, .w = m.badge, .h = m.badge };
+ const badge: Rect = .{ .x = c[0] + m.badge_at[0], .y = c[1] - m.badge_at[1], .w = m.badge, .h = m.badge };
const heat: ui.Heat = if (n.call.exit.? > now.stops[1]) now.heat(0, null, since - badge_after) else .none;
const ink = u.cellWritten(badge, heat, theme.color.page);
if (heat.level < 1) u.frame(badge, t.faint, theme.stroke.rule);
u.label(ink.face, size, badge, .center, u.print("{d}", .{@as(i64, @bitCast(n.call.result))}), if (heat.level >= 1) ink.ink else ui.rgb(t.primary));
}
}
+
+ /// Its view: the backtrace, the waiting path read as rows, newest on
+ /// top, with each call's arguments (`n=4`) and where it is.
+ fn backtrace(k: *const Tree, f: *sk.Frame, at: anim.Placed) void {
+ const box = k.stage.box(at, .view);
+ const cm = theme.metric.context;
+ if (box.h < cm.tag + cm.row) return;
+ const u = context.at(f, at, box).faded(at.value(k.stage.view));
+ u.clip(.{ .x = 0, .y = 0, .w = box.w, .h = box.h });
+ defer u.unclip();
+ const now = k.cursor.now(at);
+ const rec = k.cursor.rec;
+ // A frame is its words on the stack: the view changes with memory.
+ const moment = now.stop(.memory);
+ var buf: [24]context.Frame = undefined;
+ const chain = context.frames(rec, moment, &buf);
+ const room = theme.layout.rowsIn(box.h);
+ const body = u.panel(box.w, box.h, "backtrace", .{ .note = if (chain.len > room) u.print("the calls waiting \u{00b7} newest on top \u{00b7} {d} more", .{chain.len - room}) else "the calls waiting \u{00b7} newest on top" });
+ const open = rec.stack(f.scratch, moment);
+ for (chain[0..@min(chain.len, room)], 0..) |frame, i| {
+ const y = body.y + cm.pad + @as(f32, @floatFromInt(i)) * cm.row;
+ const name = ui.symbol(u, rec.strings.get(frame.pc) orelse u.print("{x}", .{frame.pc}));
+ // The call this frame is, by its place on the chain: its arguments.
+ const call: ?Recording.Call = if (i < open.len) open[open.len - 1 - i] else null;
+ const args = if (call) |c| arguments(u, rec, c) else "";
+ const line = if (i == 0) rec.own[now.marked()] else rec.lineOf(frame.pc) orelse 0;
+ const where = if (line > 0 and (i == 0 or rec.lineOf(frame.pc) != null)) u.print("{s}:{d}", .{ rec.file, line }) else "";
+ rows.callRow(u, y, box.w, i, name, args, where, i == 0);
+ }
+ context.veiled(u, box, now.sprint_veil);
+ }
+};
+
+/// A call's arguments as its backtrace row and its span say them: its
+/// parameter's name and value, `n=4`; "" for one that takes nothing.
+pub fn arguments(u: ui.Ui, rec: *const Recording, c: Recording.Call) []const u8 {
+ const f = for (rec.functions) |fun| {
+ if (std.mem.eql(u8, fun.name, rec.name(c.function))) break fun;
+ } else return "";
+ if (!f.takes) return "";
+ const arg = c.arg orelse return "";
+ const name = if (c.parameter_name) |id| rec.name(id) else f.param;
+ return u.print("{s}={d}", .{ name, @as(i64, @bitCast(arg)) });
+}
+
+/// The tree's measures (design/README.md, `calls`): a disc 48 across,
+/// levels 46 apart, the root 48 under the tag; the result's square of 30
+/// on the shoulder, 12 right of the centre and 42 up; the caller's box.
+const metric = struct {
+ const radius = 24;
+ const level = 46;
+ const root = 48;
+ const badge = 30;
+ const badge_at = [2]f32{ 12, 42 };
+ const caller = [2]f32{ 64, 28 };
};
pub const Node = struct {