summaryrefslogtreecommitdiff
path: root/gui/src/Navigation.zig
blob: da888ad67fbe382ec120877d1c0b0c1b70013aa5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! Bounded per-pane jump history. Scrolling changes the current location, not the target.
const std = @import("std");
const Navigation = @This();
pub const Location = struct { address: u64 = 0, row: f64 = 0, follow: bool = true, manual: bool = false };
place: Location = .{},
history: [32]Location = @splat(.{}),
len: u8 = 0,
cursor: u8 = 0,

pub fn visit(n: *Navigation, address: u64) void {
    if (n.place.address == address and !n.place.follow) return;
    if (n.len == 0) {
        n.history[0] = n.place;
        n.len = 1;
    }
    n.history[n.cursor] = n.place;
    n.len = n.cursor + 1;
    if (n.len == n.history.len) {
        std.mem.copyForwards(Location, n.history[0..31], n.history[1..]);
        n.len -= 1;
    }
    n.cursor = n.len;
    n.place = .{ .address = address, .follow = false };
    n.history[n.len] = n.place;
    n.len += 1;
}

pub fn back(n: *Navigation) void {
    if (n.len == 0 or n.cursor == 0) return;
    n.history[n.cursor] = n.place;
    n.cursor -= 1;
    n.place = n.history[n.cursor];
}

pub fn forward(n: *Navigation) void {
    if (n.cursor + 1 >= n.len) return;
    n.history[n.cursor] = n.place;
    n.cursor += 1;
    n.place = n.history[n.cursor];
}

test "pane jumps retain scroll and discard the abandoned forward branch" {
    var n: Navigation = .{};
    n.visit(0x1000);
    n.place.row = 3;
    n.visit(0x2000);
    n.back();
    try std.testing.expectEqual(@as(f64, 3), n.place.row);
    n.visit(0x3000);
    n.forward();
    try std.testing.expectEqual(@as(u64, 0x3000), n.place.address);
    for (0..40) |i| n.visit(0x4000 + i);
    try std.testing.expectEqual(@as(u8, 32), n.len);
    for (0..40) |_| n.back();
    try std.testing.expectEqual(@as(u64, 0x4008), n.place.address);
}

test "large mapping row history keeps individual words reachable" {
    var n: Navigation = .{};
    n.visit(0x100000000);
    n.place.row = 268435455;
    n.place.row -= 1;
    n.visit(0x200000000);
    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));
}