diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-07 20:26:03 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-07 20:39:56 -0300 |
| commit | 983580b6dc85274908b73daf6da82e8455bdba10 (patch) | |
| tree | ce987e45ce3ad1e43a77a66071794e096c654adb /src/gui/gui.zig | |
| parent | 4c134eaa5c7f84eba7091c9ccef86567f1434d49 (diff) | |
| download | pardes-983580b6dc85274908b73daf6da82e8455bdba10.tar.gz pardes-983580b6dc85274908b73daf6da82e8455bdba10.zip | |
gui: a wheel's fraction that moves the picture owes a frame even when the core does not scroll: a high-resolution wheel's turns (fractions, the touchpad path) that took the picture back onto the core's row drew nothing, so the window rested on a frame a fraction of a row off ("halfway") and the next turn's frame jumped from there, against the turn when it reversed (the bounce); two gui tests turn the wheel as SDL does and hold the drawn picture to the state and to the turn's way
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/gui/gui.zig')
| -rw-r--r-- | src/gui/gui.zig | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 55efeec1..0696e312 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -4548,6 +4548,107 @@ test "a notch scrolls the core at once and the picture glides after it, a wrappe try std.testing.expect(!frontendMoving(&g)); } +/// The window's loop over a text pane under a wheel, as waitInput and the +/// pump run it but with no window: a frame's turns (wheelAt), pollFrame's +/// stepScroll, and a frame drawn only when one is owed (the core's +/// needs_frame, or the window's own motion). `drawn` is the picture the +/// window shows, its first line and its offset, as of the last frame drawn; +/// `now` the picture as the window holds it. +const WheelRun = struct { + g: Gui, + core: *pardes.Pardes, + arena: std.heap.ArenaAllocator, + mc: MouseCell, + drawn: f32 = 0, + now_ns: u64 = std.time.ns_per_s, + + fn init(gpa: std.mem.Allocator, core: *pardes.Pardes) !WheelRun { + var wr: WheelRun = .{ .g = testGui(10, 20, 6, 12), .core = core, .arena = .init(gpa), .mc = undefined }; + const r = core.view().rects[core.view().active]; + wr.mc = .{ .col = r.x + 10, .row = r.y + r.h / 2 }; + core.advance(wr.now_ns); + _ = try core.render(wr.arena.allocator()); + try wr.frame(gpa, &.{}); + return wr; + } + + fn deinit(wr: *WheelRun, gpa: std.mem.Allocator) void { + wr.g.glide.deinit(gpa); + gpa.free(wr.g.scroll_edge); + wr.arena.deinit(); + } + + /// The picture as the window holds it now, in lines. + fn picture(wr: *const WheelRun) f32 { + const line: f32 = @floatFromInt(wr.core.panes[wr.core.view().active].?.scroll()); + return line + if (wr.g.glide.follow.offset != 0) wr.g.glide.follow.offset else wr.g.scroll_lag; + } + + /// One frame: SDL's turns (up positive), then a frame if one is owed. + fn frame(wr: *WheelRun, gpa: std.mem.Allocator, turns: []const f32) !void { + var in: Input = .{ .core = wr.core }; + for (turns) |y| wheelAt(&wr.g, &in, .{ .y = y, .integer_y = @intFromFloat(@trunc(y)) }, wr.mc); + wr.now_ns += std.time.ns_per_s / 60; + wr.core.advance(wr.now_ns); + stepScroll(&wr.g, wr.core, gpa); + if (!wr.core.needs_frame and !frontendMoving(&wr.g)) return; + _ = wr.arena.reset(.retain_capacity); + const s = try wr.core.render(wr.arena.allocator()); + wr.g.now_ns = wr.now_ns; + glideFrame(&wr.g, gpa, s); + wr.core.needs_frame = false; + wr.drawn = wr.picture(); + } +}; + +fn wheelRunFile(gpa: std.mem.Allocator) !*pardes.Pardes { + const core = try pardes.Pardes.init(gpa, .{ .cols = 80, .rows = 24 }); + errdefer core.deinit(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + for (0..400) |i| try text.print(gpa, "{d:0>4} a line of the file\n", .{i}); + _ = try core.setTestFile(text.items); + for ("200G") |ch| core.update(.{ .key = .{ .cp = ch, .text = &.{ch} } }); + return core; +} + +test "a fractional wheel turn that brings the picture back onto the core's row is drawn: the window never rests on a frame half a row off" { + const gpa = std.testing.allocator; + const core = try wheelRunFile(gpa); + defer core.deinit(); + var wr = try WheelRun.init(gpa, core); + defer wr.deinit(gpa); + // A high-resolution wheel's half notches (SDL's y, up positive), a + // frame each: the first scrolls the core a line and leaves the picture + // half a row behind it; the second takes the picture onto it. + for (0..2) |_| { + try wr.frame(gpa, &.{-0.5}); + try std.testing.expectEqual(wr.picture(), wr.drawn); + } + try std.testing.expectEqual(@as(f32, 0), wr.g.scroll_lag); +} + +test "a wheel turned down then up never draws the picture moving against the turn" { + const gpa = std.testing.allocator; + const core = try wheelRunFile(gpa); + defer core.deinit(); + var wr = try WheelRun.init(gpa, core); + defer wr.deinit(gpa); + // Two half notches down (the picture back on the core's row), then + // quarter notches up, a frame each and frames between: each frame's + // picture is where the last left it or on the way the wheel turns, + // never back against it. + for ([_]struct { f32, usize }{ .{ -0.5, 2 }, .{ 0.25, 4 } }) |phase| { + const turn, const turns = phase; + for (0..turns + 4) |k| { + const was = wr.drawn; + try wr.frame(gpa, if (k < turns) &.{turn} else &.{}); + const moved = wr.drawn - was; + try std.testing.expect(moved * -turn >= -1e-4); + } + } +} + test "a wheel's caret rides with its line, the text's own glide, never a spring of its own" { const gpa = std.testing.allocator; const core = try pardes.Pardes.init(gpa, .{ .cols = 80, .rows = 24 }); @@ -4823,6 +4924,11 @@ fn stepScroll(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { return; } } + // The picture moved, by a fraction or back onto the core's row with + // the core where it was: that is a frame too, or the window rests on + // the last one, a fraction of a row off, and the next turn's frame + // jumps from there (against the turn when it reverses). + if (st.rows != 0 or st.lag != g.scroll_lag) core.needs_frame = true; g.scroll_lag = st.lag; g.scroll_rect = r; const body_y = core.bodyTop(pane, r); |
