summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gui/gui.zig106
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);