summaryrefslogtreecommitdiff
path: root/gui/src/Navigation.zig
diff options
context:
space:
mode:
Diffstat (limited to 'gui/src/Navigation.zig')
-rw-r--r--gui/src/Navigation.zig32
1 files changed, 32 insertions, 0 deletions
diff --git a/gui/src/Navigation.zig b/gui/src/Navigation.zig
index 7edd4d9..da888ad 100644
--- a/gui/src/Navigation.zig
+++ b/gui/src/Navigation.zig
@@ -64,3 +64,35 @@ test "large mapping row history keeps individual words reachable" {
n.back();
try std.testing.expectEqual(@as(f64, 268435454), n.place.row);
}
+
+/// A retargetable scroll with a fixed 200 ms deadline, independent of frame rate.
+pub const Scroll = struct {
+ from: f64 = 0,
+ target: f64 = 0,
+ started: u64 = 0,
+
+ pub fn sample(s: *Scroll, current: f64, target: f64, now: u64, max_travel: f64) f64 {
+ if (s.target != target) s.* = .{ .from = current, .target = target, .started = now };
+ if (@abs(current - target) > max_travel) s.from = target;
+ const t = @min(1, @as(f64, @floatFromInt(now -| s.started)) / 200_000_000);
+ const remaining = 1 - t;
+ return std.math.lerp(s.from, target, 1 - remaining * remaining * remaining);
+ }
+};
+
+test "scroll timing is independent of cadence, settles exactly and retargets continuously" {
+ var a: Scroll = .{};
+ var b: Scroll = .{};
+ var x = a.sample(0, 12, 1_000_000_000, 100);
+ var y = b.sample(0, 12, 1_000_000_000, 100);
+ for (1..21) |i| x = a.sample(x, 12, 1_000_000_000 + i * 10_000_000, 100);
+ y = b.sample(y, 12, 1_200_000_000, 100);
+ try std.testing.expectEqual(@as(f64, 12), x);
+ try std.testing.expectEqual(x, y);
+ try std.testing.expectEqual(x, a.sample(x, 24, 1_250_000_000, 100));
+ x = a.sample(x, 24, 1_350_000_000, 100);
+ try std.testing.expectEqual(@as(f64, 22.5), x);
+ try std.testing.expectEqual(x, a.sample(x, 6, 1_350_000_000, 100));
+ try std.testing.expectEqual(@as(f64, 6), a.sample(x, 6, 1_550_000_000, 100));
+ try std.testing.expectEqual(@as(f64, 1000000), a.sample(6, 1000000, 1_600_000_000, 100));
+}