diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-13 10:09:48 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-13 10:09:48 -0300 |
| commit | 771e377f6db9c3aed1ab85bc36a774f4176272cc (patch) | |
| tree | 6fc14d41139976a514efc4817c8f7c64d9856719 /gui/src | |
| parent | 5c0230ea5a77f85de77ad4354788cd6c27aa1ef6 (diff) | |
| download | computeranim-771e377f6db9c3aed1ab85bc36a774f4176272cc.tar.gz computeranim-771e377f6db9c3aed1ab85bc36a774f4176272cc.zip | |
debugger: decouple font zoom from window size and add restart
Diffstat (limited to 'gui/src')
| -rw-r--r-- | gui/src/Geometry.zig | 7 | ||||
| -rw-r--r-- | gui/src/Sources.zig | 2 | ||||
| -rw-r--r-- | gui/src/app.zig | 107 |
3 files changed, 88 insertions, 28 deletions
diff --git a/gui/src/Geometry.zig b/gui/src/Geometry.zig index 3ebdf4e..0fa2aee 100644 --- a/gui/src/Geometry.zig +++ b/gui/src/Geometry.zig @@ -2,6 +2,7 @@ const std = @import("std"); const Rect = @import("sketch").Rect; const Geometry = @This(); +pub const toolbar_count = 17; w: f32, h: f32, content: f32, @@ -15,7 +16,7 @@ pub fn init(w: f32, h: f32, scroll: f32) Geometry { const gap: f32 = 16; const room = @max(240, w - 2 * margin); const columns: usize = @intFromFloat(@max(1, @floor((w - 48) / 42))); - const top: f32 = 78 + @as(f32, @floatFromInt((16 + columns - 1) / columns)) * 34; + const top: f32 = 78 + @as(f32, @floatFromInt((toolbar_count + columns - 1) / columns)) * 34; const tall = @max(320, (h - top - 64) * 0.64); const bottom = @max(240, h - top - tall - gap - 48); var code: Rect = .{ .x = margin, .y = top, .w = room, .h = tall }; @@ -45,6 +46,10 @@ pub fn seam(r: Rect) f32 { return @min(536, r.w * 0.34); } +pub fn scaled(r: Rect, scale: f32) Rect { + return .{ .x = r.x * scale, .y = r.y * scale, .w = r.w * scale, .h = r.h * scale }; +} + test "wide panes sit beside each other; narrow panes stack without overlap at every font scale" { for ([_][2]f32{ .{ 2240, 1260 }, .{ 1440, 900 }, .{ 800, 700 } }) |size| { for ([_]f32{ 0.6, 1, 2 }) |scale| { diff --git a/gui/src/Sources.zig b/gui/src/Sources.zig index 3bd4cbf..93e5b90 100644 --- a/gui/src/Sources.zig +++ b/gui/src/Sources.zig @@ -19,7 +19,7 @@ pub fn map(s: *Sources, gpa: std.mem.Allocator, io: std.Io, from: []const u8, to s.clear(gpa); } -fn clear(s: *Sources, gpa: std.mem.Allocator) void { +pub fn clear(s: *Sources, gpa: std.mem.Allocator) void { for (s.files.items) |file| { if (file.text) |t| t.deinit(); gpa.free(file.path); diff --git a/gui/src/app.zig b/gui/src/app.zig index e334b0b..bed93f4 100644 --- a/gui/src/app.zig +++ b/gui/src/app.zig @@ -17,7 +17,9 @@ var trace: ?zt.Trace = null; var client: ?*Client = null; var latest: Client.Update = .{}; var loaded: u64 = 0; +var launch_command: ?[]const u8 = null; pub const State = struct { + launches: u32 = 0, moment: u64 = 0, tid: u32 = 0, following: bool = true, @@ -60,6 +62,7 @@ pub const State = struct { drag_content: bool = false, }; pub const Session = struct { + launches: u32 = 0, action: sk.Handled = .{}, graph: bool = false, scroll: [3]f64 = @splat(0), @@ -100,10 +103,11 @@ pub const Session = struct { }; const Pane = enum { code, stack, memory }; const Lens = struct { address: u64 = 0, origin: Box = .{}, cycle: bool = false }; -const Kind = enum { graph, dim, segments, select_segment, cont, pause, stepi, step, previous, next, live, follow, back, forward, source, map, annotate, shallower, deeper, ascii, lens, close_lens, help, breakpoint, command, go, submit }; +const Kind = enum { restart, graph, dim, segments, select_segment, cont, pause, stepi, step, previous, next, live, follow, back, forward, source, map, annotate, shallower, deeper, ascii, lens, close_lens, help, breakpoint, command, go, submit }; const Action = struct { kind: Kind, pane: Pane = .code, address: u64 = 0, manual: bool = false }; const Box = sk.Rect; var geometry = Geometry.init(width, height, 0); +var display_scale: f32 = 1; var panes = Geometry.init(width, height, 0).panes; var ranges: [3]zt.store.Mapping = @splat(.{ .addr = 0, .len = 2048, .prot = 0, .offset = 0 }); var code_after: u64 = 0; @@ -139,21 +143,46 @@ pub fn close() void { trace = null; if (client) |c| c.close(); client = null; + if (launch_command) |saved| gpa.free(saved); + launch_command = null; } pub fn command(s: *State, c: *sk.Console, line: []const u8) !bool { var words = std.mem.tokenizeScalar(u8, line, ' '); const cmd = words.next() orelse return false; - if (std.mem.eql(u8, cmd, "launch")) { - if (client != null) return error.AlreadyLaunched; + if (std.mem.eql(u8, cmd, "launch") or std.mem.eql(u8, cmd, "restart")) { + const restart = std.mem.eql(u8, cmd, "restart"); + if (client != null and !restart) return error.AlreadyLaunched; + const args = if (restart) launch_command orelse return error.NoLaunchCommand else words.rest(); + const saved = try gpa.dupe(u8, args); + var retained = false; + defer if (!retained) gpa.free(saved); + var arguments = std.mem.tokenizeScalar(u8, saved, ' '); var argv: std.ArrayList([]const u8) = .empty; defer argv.deinit(gpa); // shortcut: whitespace-separated argv; add quoting when launch needs arguments containing spaces. - while (words.next()) |word| try argv.append(gpa, word); + while (arguments.next()) |word| try argv.append(gpa, word); if (argv.items.len == 0) return error.MissingProgram; - client = try Client.start(gpa, io, argv.items); - s.following = true; - s.status = .running; + if (launch_command) |old| gpa.free(old); + launch_command = saved; + retained = true; + if (client) |old| old.close(); + client = null; + if (trace) |*old| old.close(); + trace = null; + sources.clear(gpa); + latest = .{}; + loaded = 0; + s.* = .{ .launches = s.launches + 1, .status = .running, .zoom = s.zoom, .maps = s.maps, .source = s.source, .annotations = s.annotations, .depth = s.depth, .ascii = s.ascii, .dim = s.dim }; + client = Client.start(gpa, io, argv.items) catch |err| { + s.status = .failed; + return err; + }; + if (sources.paths) |paths| for (paths.source_maps.items) |map| { + const mapped = try std.fmt.allocPrint(gpa, "source-map {s} {s}", .{ map.from, map.to }); + defer gpa.free(mapped); + try client.?.send(mapped); + }; return true; } if (std.mem.eql(u8, cmd, "observed")) { @@ -216,7 +245,7 @@ pub fn command(s: *State, c: *sk.Console, line: []const u8) !bool { const code = try std.fmt.parseInt(u16, words.next() orelse return error.MissingButton, 10); const x = try std.fmt.parseFloat(f32, words.next() orelse return error.MissingX); const y = try std.fmt.parseFloat(f32, words.next() orelse return error.MissingY); - pointer(s, code, x, y); + pointer(s, code, x / display_scale, y / display_scale); return true; } if (!std.mem.eql(u8, cmd, "open")) return false; @@ -262,7 +291,7 @@ fn addressAt(x: f32, y: f32) ?Hit { } fn collectAddress(_: *anyopaque, address: u64, box: Box) void { if (hit_count == hits.len) return; - hits[hit_count] = .{ .address = address, .box = box, .lens = current_lens }; + hits[hit_count] = .{ .address = address, .box = Geometry.scaled(box, 1 / display_scale), .lens = current_lens }; hit_count += 1; } fn scroll(s: *State, pane: Pane, delta: f64) void { @@ -333,7 +362,7 @@ fn act(s: *State, a: Action) void { .close_lens => { s.lens_count = @intCast(@min(a.address, s.lens_count)); s.lens = s.lens_count > 0; - s.lens_scroll = @min(s.lens_scroll, @max(0, @as(f32, @floatFromInt(s.lens_count)) * 204 - (geometry.h - 180))); + s.lens_scroll = @min(s.lens_scroll, lensScroll(s.lens_count, true)); }, .help => s.help = !s.help, .command => { @@ -390,14 +419,23 @@ fn openLens(s: *State, hit: Hit) void { s.lens_count = at + 1; s.lens = true; s.lens_address = hit.address; - s.lens_scroll = @max(0, @as(f32, @floatFromInt(s.lens_count)) * 204 - (geometry.h - 160)); + s.lens_scroll = lensScroll(s.lens_count, true); if (client != null and s.status == .stopped and s.following and !cycle) { var buf: [96]u8 = undefined; client.?.send(std.fmt.bufPrint(&buf, "inspect memory 0x{x} 128", .{(hit.address & ~@as(u64, 15)) -| 32}) catch return) catch {}; } } +fn lensViewport() Box { + const y = @max(140, geometry.top + 12); + return .{ .x = @max(24, geometry.w - 1132), .y = y, .w = @min(1108, geometry.w - 48), .h = @max(0, geometry.h - y - 40) }; +} +fn lensScroll(count: u8, reveal_header: bool) f32 { + const room = if (reveal_header) @max(188, lensViewport().h) else lensViewport().h; + return @max(0, @as(f32, @floatFromInt(count)) * 204 - 16 - room); +} fn enabled(s: *const State, a: Action) bool { return switch (a.kind) { + .restart => launch_command != null, .cont, .stepi, .step, .breakpoint => client != null and s.status == .stopped, .pause => client != null and s.status == .running, .back => s.nav[@backingInt(a.pane)].cursor > 0, @@ -451,7 +489,10 @@ fn pointer(s: *State, mouse_button: u16, x: f32, y: f32) void { pub fn update(s: *State, in: *const sk.Input) void { if (in.console) return; s.page_scroll = std.math.clamp(s.page_scroll, 0, @max(0, geometry.content - geometry.h)); - for (in.events) |e| { + for (in.events) |event| { + var e = event; + e.x /= display_scale; + e.y /= display_scale; if (s.segments) { if (e.kind == .text and e.value >= 32 and e.value < 127 and s.segment_len < s.segment_query.len) { s.segment_query[s.segment_len] = @intCast(e.value); @@ -506,7 +547,7 @@ pub fn update(s: *State, in: *const sk.Input) void { }, .wheel => { if (s.lens and lens_box != null and contains(lens_box.?, e.x, e.y)) { - s.lens_scroll = std.math.clamp(s.lens_scroll - e.wheel() * 48, 0, @max(0, @as(f32, @floatFromInt(s.lens_count)) * 204 - (geometry.h - 160))); + s.lens_scroll = std.math.clamp(s.lens_scroll - e.wheel() * 48, 0, lensScroll(s.lens_count, false)); } else if (contains(geometry.registers, e.x, e.y) and e.y >= geometry.registers.y + 30) { s.register_scroll = std.math.clamp(s.register_scroll - e.wheel() * 3, 0, @max(0, 29 - Geometry.visible(geometry.registers))); } else if (paneAt(e.x, e.y)) |pane| { @@ -582,6 +623,9 @@ pub fn update(s: *State, in: *const sk.Input) void { .c => if (s.status == .stopped) { act(s, .{ .kind = .cont }); }, + .r => if (shift and launch_command != null) { + act(s, .{ .kind = .restart }); + }, .escape => if (s.lens or s.help) { s.lens = false; s.lens_count = 0; @@ -600,7 +644,7 @@ pub fn update(s: *State, in: *const sk.Input) void { .m => act(s, .{ .kind = .map, .pane = s.focus }), .v => act(s, .{ .kind = .source }), .a => act(s, .{ .kind = .annotate }), - .z => act(s, .{ .kind = .lens, .address = if (addressAt(in.mouse_x, in.mouse_y)) |h| h.address else s.pc }), + .z => act(s, .{ .kind = .lens, .address = if (addressAt(in.mouse_x / display_scale, in.mouse_y / display_scale)) |h| h.address else s.pc }), .tab => s.focus = @fromBackingInt(@intCast((@backingInt(s.focus) + 1) % 3)), else => {}, } @@ -614,6 +658,7 @@ fn icon(u: ui.Ui, kind: Kind, active: bool) void { const c = if (active) ui.theme.mark(.current).solid else ui.theme.color.text.primary; const v = u.moved(6, 5); switch (kind) { + .restart => v.route(&.{ .{ 5, 5 }, .{ 17, 5 }, .{ 21, 10 }, .{ 21, 16 }, .{ 16, 21 }, .{ 7, 21 }, .{ 2, 16 } }, c, .{ .width = 2, .head = .{ 6, 6 }, .radius = 4 }), .cont => sk.draw.polygon(u.f, &.{ .{ 3, 2 }, .{ 3, 20 }, .{ 18, 11 } }, v.at.pen(.{ .fill = ui.rgb(c) })), .pause => { v.fill(.{ .x = 4, .y = 3, .w = 4, .h = 16 }, c); @@ -688,18 +733,19 @@ fn icon(u: ui.Ui, kind: Kind, active: bool) void { } fn button(u: ui.Ui, x: f32, y: f32, action: Action, label: []const u8, available: bool, active: bool) void { const a = u.at.xf.apply(.{ x, y }); - var box: Box = .{ .x = a[0], .y = a[1], .w = 34, .h = 32 }; + var box: Box = .{ .x = a[0], .y = a[1], .w = 34 * display_scale, .h = 32 * display_scale }; if (ui.bound) |clip| { const x0 = @max(box.x, clip.x); const y0 = @max(box.y, clip.y); box = .{ .x = x0, .y = y0, .w = @max(0, @min(box.x + box.w, clip.x + clip.w) - x0), .h = @max(0, @min(box.y + box.h, clip.y + clip.h) - y0) }; } + box = Geometry.scaled(box, 1 / display_scale); const b: Button = .{ .action = action, .box = box, .label = label, .enabled = available }; if (box.w > 0 and box.h > 0 and button_count < buttons.len) { buttons[button_count] = b; button_count += 1; } - const hover = !u.f.in.console and contains(box, u.f.in.mouse_x, u.f.in.mouse_y); + const hover = !u.f.in.console and contains(box, u.f.in.mouse_x / display_scale, u.f.in.mouse_y / display_scale); if (hover) tooltip = b; const v = (if (available) u else u.faded(0.32)).moved(x, y); v.fill(.{ .x = 0, .y = 0, .w = 34, .h = 32 }, if (hover or active) ui.theme.color.rule else ui.theme.color.raised); @@ -724,7 +770,9 @@ fn paneButtons(u: ui.Ui, s: *const State, pane: Pane) void { } pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { + if (ss.launches != s.launches) ss.* = .{ .launches = s.launches }; if (ss.action.take(s.action)) |action| switch (action.kind) { + .restart => f.run("restart"), .cont => f.run("continue"), .pause => f.run("interrupt"), .stepi => f.run("stepi"), @@ -748,7 +796,9 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { const scale = @as(f32, @floatFromInt(s.zoom)) / 100; const w = @max(1, @round(f.in.window_width / scale)); const h = @max(1, @round(f.in.window_height / scale)); - f.canvas(@intFromFloat(w), @intFromFloat(h)); + // canvas() also resizes the native window: only its real dimensions belong here. + f.canvas(@intFromFloat(@max(1, f.in.window_width)), @intFromFloat(@max(1, f.in.window_height))); + display_scale = scale; const next_geometry = Geometry.init(w, h, s.page_scroll); if (geometry.w != w or geometry.h != h) { ss.mini_key = @splat(0); @@ -758,7 +808,9 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { geometry = next_geometry; panes = geometry.panes; f.clear(ui.theme.color.page); - const u = ui.Ui.init(f); + var u = ui.Ui.init(f); + u.at.xf = .scale(scale, scale); + u.at.scale = scale; const ink = ui.theme.color.text; const gold = ui.theme.mark(.current); hit_count = 0; @@ -775,10 +827,12 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { u.text(.mono_bold, 17, if (geometry.w < 700) 162 else 312, 27, status, if (s.status == .failed) ui.theme.mark(.write).solid else gold.solid); if (geometry.w > 850) u.text(.sans, 18, 434, 27, canim.rows.fitted(u, latest.reason(), geometry.w - 740), ink.dim); if (client != null and geometry.w > 1200) u.text(.mono, 17, geometry.w - 310, 27, u.print("command {d:.3} ms", .{@as(f64, @floatFromInt(latest.elapsed_ns)) / 1e6}), ink.dim); - const kinds = [_]Kind{ if (s.status == .running) .pause else .cont, .stepi, .step, .breakpoint, .previous, .next, .live, .source, .map, .annotate, .lens, .segments, .graph, .dim, .command, .help }; - const labels = [_][]const u8{ if (s.status == .running) "Pause · Esc / Ctrl-c" else "Continue · c", "Single instruction · s", "Next source line (enters calls) · n", "Breakpoint at PC · b", "Previous recorded stop · [", "Next recorded stop · ]", "Live stop · F (Shift-f)", "Aligned source · v", "Focused pane minimap · m", "Operand annotations · a", "Address lens · z", "Memory segments · p (active pane)", "Control-flow graph · t", "Unobserved instruction opacity · d", "Command line · :", "Keyboard help · ?" }; + const kinds = [_]Kind{ if (s.status == .running) .pause else .cont, .stepi, .step, .breakpoint, .previous, .next, .live, .source, .map, .annotate, .lens, .segments, .graph, .dim, .command, .help, .restart }; + const labels = [_][]const u8{ if (s.status == .running) "Pause · Esc / Ctrl-c" else "Continue · c", "Single instruction · s", "Next source line (enters calls) · n", "Breakpoint at PC · b", "Previous recorded stop · [", "Next recorded stop · ]", "Live stop · F (Shift-f)", "Aligned source · v", "Focused pane minimap · m", "Operand annotations · a", "Address lens · z", "Memory segments · p (active pane)", "Control-flow graph · t", "Unobserved instruction opacity · d", "Command line · :", "Keyboard help · ?", "Restart at entry · Shift-r (fresh breakpoints)" }; + comptime std.debug.assert(kinds.len == Geometry.toolbar_count); for (kinds, labels, 0..) |kind, label, i| { const available = switch (kind) { + .restart => launch_command != null, .cont, .pause, .stepi, .step, .breakpoint => client != null and s.status != .exited and (kind == .pause or s.status == .stopped), else => true, }; @@ -853,8 +907,9 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { break; } }; - ui.bound = .{ .x = 0, .y = geometry.top, .w = geometry.w, .h = @max(0, geometry.h - geometry.top - 40) }; - u.clip(.{ .x = ui.bound.?.x, .y = ui.bound.?.y, .w = ui.bound.?.w, .h = ui.bound.?.h }); + const content: Box = .{ .x = 0, .y = geometry.top, .w = geometry.w, .h = @max(0, geometry.h - geometry.top - 40) }; + ui.bound = Geometry.scaled(content, scale); + u.clip(.{ .x = content.x, .y = content.y, .w = content.w, .h = content.h }); drawCode(u.moved(panes[0].x, panes[0].y), s, ss, m, regs, heat, fire, travel); drawRegisters(u.moved(geometry.registers.x, geometry.registers.y), s, ss, m, regs, heat, seconds, fire); drawStack(u.moved(panes[1].x, panes[1].y), s, ss, m, regs, heat, seconds); @@ -1254,7 +1309,7 @@ fn segmentMini(u: ui.Ui, p: usize, first: u64, len: u64, current: u64) void { } fn ties(u: ui.Ui, s: *const State, ss: *Session, m: zt.store.Moment, travel: f32) void { _ = m; - const hovered = addressAt(u.f.in.mouse_x, u.f.in.mouse_y); + const hovered = addressAt(u.f.in.mouse_x / display_scale, u.f.in.mouse_y / display_scale); const address = if (hovered) |hit| hit.address else if (ss.touch) |touch| touch.addr else return; var from: ?Box = if (hovered) |hit| hit.box else null; for (hits[0..hit_count]) |hit| if (hit.address == address) { @@ -1278,10 +1333,10 @@ fn overlays(u: ui.Ui, s: *const State, ss: *Session, moment: ?zt.store.Moment) v const ink = ui.theme.color.text; if (!s.lens) ss.lens_previous = 0; if (s.lens and moment != null) { - const overlay: Box = .{ .x = @max(24, geometry.w - 1132), .y = 140, .w = @min(1108, geometry.w - 48), .h = @max(0, geometry.h - 180) }; + const overlay = lensViewport(); lens_box = overlay; const previous_bound = ui.bound; - ui.bound = overlay; + ui.bound = Geometry.scaled(overlay, display_scale); u.clip(.{ .x = overlay.x, .y = overlay.y, .w = overlay.w, .h = overlay.h }); if (ss.lens_previous != s.lens_address) { ss.lens_at = u.f.in.now; @@ -1358,7 +1413,7 @@ fn overlays(u: ui.Ui, s: *const State, ss: *Session, moment: ?zt.store.Moment) v v.fill(.{ .x = 0, .y = 0, .w = w, .h = @min(560, geometry.h - 160) }, ui.theme.color.lift); _ = v.panel(w, @min(560, geometry.h - 160), "Keys · Esc closes", .{ .focus = true }); v.clip(.{ .x = 0, .y = 32, .w = w, .h = @max(0, geometry.h - 192) }); - const lines = [_][]const u8{ "s instruction · n source · c run", "Esc / Ctrl-c pause", "Ctrl +/− font · Ctrl-0 reset", "j/k scroll · PgUp/PgDn page", "h/l source · Shift-h/l horizontal", "Ctrl-d/u half page · Tab focus", "Ctrl-o/i address history", "[ / ] stops · F live · f follow", "t graph · d unobserved opacity", "p segments · v source · m minimap", "click: lens · right-click: navigate", "z lens · : command · ? help", "Gold current · bright observed", "Branch target: prediction only", "Dim: unknown between live stops" }; + const lines = [_][]const u8{ "s instruction · n source · c run", "Esc / Ctrl-c pause · Shift-r restart", "Ctrl +/− font · Ctrl-0 reset", "j/k scroll · PgUp/PgDn page", "h/l source · Shift-h/l horizontal", "Ctrl-d/u half page · Tab focus", "Ctrl-o/i address history", "[ / ] stops · F live · f follow", "t graph · d unobserved opacity", "p segments · v source · m minimap", "click: lens · right-click: navigate", "z lens · : command · ? help", "Gold current · bright observed", "Branch target: prediction only", "Dim: unknown between live stops" }; for (lines, 0..) |line, i| v.text(.mono, 16, 12, 58 + @as(f32, @floatFromInt(i)) * 30, canim.rows.fitted(v, line, w - 24), ink.primary); v.unclip(); } |
