diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/FrameStats.zig | 73 | ||||
| -rw-r--r-- | src/animation.zig | 137 | ||||
| -rw-r--r-- | src/detached/wire.zig | 8 | ||||
| -rw-r--r-- | src/gui/gui.zig | 154 | ||||
| -rw-r--r-- | src/pardes.zig | 8 | ||||
| -rw-r--r-- | src/pdf_view.zig | 180 |
6 files changed, 434 insertions, 126 deletions
diff --git a/src/FrameStats.zig b/src/FrameStats.zig index ee5ee3f2..cf3b00d3 100644 --- a/src/FrameStats.zig +++ b/src/FrameStats.zig @@ -45,8 +45,9 @@ pub const Frame = struct { /// The shell had no image to draw into (a busy swapchain). skipped: bool = false, /// The shell's motion in it: a scroll's picture offset from the text, - /// rows (0 at rest), and where it drew the caret, its top in rows - /// (the jitter check: a caret on a fixed row must not wobble). + /// rows (0 at rest), or a focused PDF's strip as drawn, pixels down + /// the document; and where it drew the caret, its top in rows (the + /// jitter check: a caret on a fixed row must not wobble). scroll: f32 = 0, caret: f32 = 0, /// From the first input this frame answers to its present's end, µs; @@ -286,6 +287,8 @@ pub const Step = struct { act: union(enum) { /// Wheel notches, down positive, as a mouse's. wheel: i8, + /// A touchpad's scroll, lines down, a fraction (no notch). + touch: f32, key: pardes.Key, /// A named stretch of the run starts (the dump lists them). phase: []const u8, @@ -316,6 +319,7 @@ pub fn named(name: []const u8) ?[]const Step { if (std.mem.eql(u8, name, "stress")) return &stress; if (std.mem.eql(u8, name, "kinds")) return &kinds; if (std.mem.eql(u8, name, "panes")) return &panes; + if (std.mem.eql(u8, name, "pdf-scroll")) return &pdf_scroll; if (std.mem.eql(u8, name, "one")) return &one; if (std.mem.eql(u8, name, "jumps")) return &jumps; if (std.mem.eql(u8, name, "longlines")) return &longlines; @@ -449,6 +453,71 @@ fn scrollSet(steps: []Step, n: *usize, t: *u32) void { t.* += 500; } +/// A PDF's strip put through what moves it (anim 11): a wheel's notches, +/// then spun; PageDown held; the far jumps ge and gg; a Look at a page and +/// Back; then slow: a notch at a time, and a touchpad's drag a pixel or so +/// a frame. +pub const pdf_scroll = blk: { + @setEvalBranchQuota(20_000); + var steps: [512]Step = undefined; + var n: usize = 0; + var t: u32 = 1500; + const at = struct { + fn add(s: []Step, k: *usize, ms: u32, act: @FieldType(Step, "act")) void { + s[k.*] = .{ .at_ms = ms, .act = act }; + k.* += 1; + } + }; + at.add(&steps, &n, t, .{ .exec = "Look ../../docs/design.pdf" }); + t += 2000; + at.add(&steps, &n, t, .{ .phase = "spin" }); + for (0..6) |_| { + at.add(&steps, &n, t, .{ .wheel = 1 }); + t += 150; + } + for (0..60) |_| { + at.add(&steps, &n, t, .{ .wheel = 1 }); + t += 8; + } + t += 600; + for (0..30) |_| { + at.add(&steps, &n, t, .{ .wheel = -1 }); + t += 8; + } + t += 600; + at.add(&steps, &n, t, .{ .phase = "held-pagedown" }); + for (0..20) |_| { + at.add(&steps, &n, t, .{ .key = .{ .cp = pardes.Key.page_down } }); + t += 33; + } + t += 600; + at.add(&steps, &n, t, .{ .phase = "far-jump" }); + for ([_][]const u8{ "e", "g" }) |second| { + at.add(&steps, &n, t, .{ .key = .{ .cp = 'g', .text = "g" } }); + at.add(&steps, &n, t + 20, .{ .key = .{ .cp = second[0], .text = second } }); + t += 800; + } + at.add(&steps, &n, t, .{ .exec = "Look ../../docs/design.pdf:12" }); + t += 800; + at.add(&steps, &n, t, .{ .key = .{ .cp = 'o', .ctrl = true } }); + t += 800; + at.add(&steps, &n, t, .{ .phase = "slow-notches" }); + for (0..8) |_| { + at.add(&steps, &n, t, .{ .wheel = 1 }); + t += 300; + } + at.add(&steps, &n, t, .{ .phase = "slow-touch" }); + for (0..120) |_| { + at.add(&steps, &n, t, .{ .touch = 0.06 }); + t += 16; + } + t += 600; + at.add(&steps, &n, t, .{ .phase = "done" }); + at.add(&steps, &n, t, .end); + const out = steps[0..n].*; + break :blk out; +}; + /// `scrollSet` alone, on the file the session opened: one kind, for a /// profiler's run. pub const one = blk: { diff --git a/src/animation.zig b/src/animation.zig index ae6f9ca0..764aa0b3 100644 --- a/src/animation.zig +++ b/src/animation.zig @@ -949,6 +949,143 @@ test "a flavour's stretched pane lands exactly on its box, and a click finds the } } +/// A view that follows its input (anim 9, 11): the picture's offset from +/// the core's position now (in the view's own unit: rows of text, pixels of +/// a PDF strip; negative, the picture still shows what is above), its speed +/// (units a second), when it was last integrated, and what has been feeding +/// it. The text body (the GUI's Glide) and a PDF strip are each one. +pub const Follow = struct { + offset: f32 = 0, + speed: f32 = 0, + at_ns: u64 = 0, + /// The last inputs (down positive), their spacing, the last's time, and + /// how many came as one stream. + feed: struct { amount: f32 = 0, gap_ns: u64 = 0, last_ns: u64 = 0, count: u16 = 0 } = .{}, + + /// How far the picture may lag, in bodies (a flick past that jumps); + /// how fast it may move, in bodies a second, so moving text stays + /// readable. + pub const reach = 2; + pub const bodies_per_s: f32 = 15; + /// Inputs this close, each the way the last went, are one stream. + pub const stream_ns = 150 * std.time.ns_per_ms; + + /// What a view's size and the flavour make of the rules: the spring at + /// 1.5x the flavour's pace, the speed cap, the lag cap, and the offset + /// under which it is at rest (a tenth of a pixel, in its unit). + pub const Pace = struct { omega: f32, cap: f32, limit: f32, rest: f32 }; + + pub fn pace(motion: Motion, body: f32, rest: f32) Pace { + return .{ .omega = motion.omega * 1.5, .cap = body * bodies_per_s, .limit = body * reach, .rest = rest }; + } + + pub fn moving(follow: *const Follow) bool { + return follow.offset != 0 or follow.speed != 0; + } + + /// An input: the core moved `amount` (down positive). The picture now + /// lags by as much more, its speed as it was. + pub fn kick(follow: *Follow, amount: f32, now_ns: u64, p: Pace) void { + follow.advance(now_ns, p); + follow.offset = std.math.clamp(follow.offset - amount, -p.limit, p.limit); + const f = &follow.feed; + const like = f.count > 0 and (amount > 0) == (f.amount > 0) and now_ns -| f.last_ns < stream_ns; + if (like) { + const gap = now_ns -| f.last_ns; + f.gap_ns = if (f.count == 1) gap else (f.gap_ns + gap) / 2; + f.amount = (f.amount + amount) / 2; + f.count +|= 1; + } else f.* = .{ .amount = amount, .count = 1 }; + f.last_ns = now_ns; + } + + /// The system moved on to `now_ns`. Fed by a stream, at the stream's + /// rate (an input's amount over their spacing), held a lag `lead` + /// behind so a late input never finds it stopped and the stream's end + /// never runs it past its mark; else on the spring, critically damped, + /// from the speed it has. Never faster than `p.cap`. + pub fn advance(follow: *Follow, now_ns: u64, p: Pace) void { + const dt: f32 = @floatCast(@as(f64, @floatFromInt(@min(now_ns -| follow.at_ns, 2 * frame_ns))) / std.time.ns_per_s); + follow.at_ns = now_ns; + if (follow.offset == 0 and follow.speed == 0) return; + const omega = p.omega; + const toward: f32 = if (follow.offset < 0) 1 else -1; + const f = follow.feed; + if (f.count >= 2 and f.gap_ns > 0 and now_ns -| f.last_ns < f.gap_ns * 3 / 2) { + const gap: f32 = @floatCast(@as(f64, @floatFromInt(f.gap_ns)) / std.time.ns_per_s); + const rate = @abs(f.amount) / gap; + const lead = @max(@abs(f.amount), rate / omega); + // ponytail: a gentle pull to the lead (a tenth an input), so the + // per-frame step varies by a tenth; a stream's own jitter beyond + // that shows. An estimate of the input clock if it ever matters. + // A slow stream's lag runs out before its next input: never + // faster than the spring's own pace from there (omega a unit of + // lag a second), so it eases onto the mark, not into it at speed. + const v = std.math.clamp(rate + 0.1 / gap * (@abs(follow.offset) - lead), 0, @min(p.cap, omega * @abs(follow.offset))); + const step = @min(v * dt, @abs(follow.offset)); + follow.offset += toward * step; + follow.speed = if (follow.offset == 0) 0 else toward * v; + return; + } + const x = follow.offset; + // Coming to rest from a stream: never faster than the spring's own + // pace from here (omega a unit of lag a second), so it eases in and + // does not pass its mark. + if (follow.speed * x < 0 and @abs(follow.speed) > omega * @abs(x)) follow.speed = std.math.sign(follow.speed) * omega * @abs(x); + const lead_v = follow.speed + omega * x; + const decay = @exp(-omega * dt); + var next = (x + lead_v * dt) * decay; + var speed = (follow.speed - omega * dt * lead_v) * decay; + if (@abs(next - x) > p.cap * dt) { + next = x + std.math.sign(next - x) * p.cap * dt; + speed = std.math.sign(next - x) * p.cap; + } + follow.offset = next; + follow.speed = speed; + if (@abs(next) < p.rest and @abs(speed) < 10 * p.rest) follow.* = .{ .at_ns = now_ns }; + } +}; + +test "a follow comes to rest without a snap: past its fastest frame each step is shorter, the last under a tenth of a pixel" { + // A flick's spin (a stream of notches, then nothing), one page, and + // notches slow enough that each lag runs out before the next (its + // stream must ease onto each mark, never pass it). + const p = Follow.pace(.{}, 600, 0.1); + for ([_]struct { n: u16, gap_ms: u64 }{ .{ .n = 12, .gap_ms = 16 }, .{ .n = 1, .gap_ms = 16 }, .{ .n = 6, .gap_ms = 144 } }) |run| { + var f: Follow = .{}; + var now: u64 = std.time.ns_per_s; + for (0..run.n) |_| { + f.kick(if (run.n == 1) 600 else 25, now, p); + var waited: u64 = 0; + while (waited < run.gap_ms) : (waited += 8) { + now += 8 * std.time.ns_per_ms; + const was = f.offset; + f.advance(now, p); + // Toward its mark only, and onto it from under a pixel. + try std.testing.expect(f.offset <= 0 and f.offset >= was); + if (f.offset == 0) try std.testing.expect(-was <= 3 * p.rest); + } + } + var last: f32 = std.math.inf(f32); + var before = f.offset; + var frames: usize = 0; + var fastest: f32 = 0; + while (f.moving()) : (frames += 1) { + now += frame_ns; + f.advance(now, p); + const step = @abs(f.offset - before); + try std.testing.expect(@abs(f.offset) <= @abs(before)); + if (f.offset == 0) try std.testing.expect(step <= 3 * p.rest); + fastest = @max(fastest, step); + if (step < fastest) try std.testing.expect(step <= last); + last = step; + before = f.offset; + try std.testing.expect(frames < 120); + } + try std.testing.expect(last <= 0.1); + } +} + /// The focused cursor's quad (docs/render-pipeline.md §5.4), in grid cells: /// four corners, each on its own springs, so a jump smears along its path /// (the leading corners stiffer than the trailing ones, Motion.follow) and diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 9890dc1a..0e141686 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -12,7 +12,7 @@ const ShaderAnimation = pardes.config.Runtime.ShaderAnimation; /// to be refused, which is what this number is for. /// 8: one layer type: a tag band carries all its rows (not a layer per /// line), and a frame carries the core's placed regions after its layers. -pub const version: u16 = 11; +pub const version: u16 = 12; /// The Chrome palette is carried field by field as its declaration lists /// them, so any change to it changes the wire: bump `version`, then this. @@ -980,6 +980,7 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 { .pdf_scroll => |s| { try w.writeByte(s.pane); try w.writeInt(u32, @bitCast(s.delta_pixels), .little); + try w.writeByte(@intFromBool(s.glide)); }, .pinch => |v| try w.writeInt(u32, @bitCast(v), .little), .touch_scroll => |v| try w.writeInt(u32, @bitCast(v), .little), @@ -1043,7 +1044,7 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { }, .paste => .{ .event = .{ .paste = try getSlice32(&r) } }, .command => .{ .event = .{ .command = try r.take(try r.takeInt(u16, .little)) } }, - .pdf_scroll => .{ .event = .{ .pdf_scroll = .{ .pane = try getPane(&r), .delta_pixels = try getF32(&r) } } }, + .pdf_scroll => .{ .event = .{ .pdf_scroll = .{ .pane = try getPane(&r), .delta_pixels = try getF32(&r), .glide = try getBool(&r) } } }, .pinch => .{ .event = .{ .pinch = try getF32(&r) } }, .touch_scroll => .{ .event = .{ .touch_scroll = try getF32(&r) } }, .pointer_leave => .{ .event = .pointer_leave }, @@ -1297,9 +1298,10 @@ test "detached wire: every Event variant round-trips" { try testing.expectEqualStrings("clip", (try roundClient(&buf, .{ .event = .{ .paste = "clip" } })).event.paste); try testing.expectEqualStrings("Look /x", (try roundClient(&buf, .{ .event = .{ .command = "Look /x" } })).event.command); { - const got = (try roundClient(&buf, .{ .event = .{ .pdf_scroll = .{ .pane = 2, .delta_pixels = -12.5 } } })).event.pdf_scroll; + const got = (try roundClient(&buf, .{ .event = .{ .pdf_scroll = .{ .pane = 2, .delta_pixels = -12.5, .glide = false } } })).event.pdf_scroll; try testing.expectEqual(@as(u8, 2), got.pane); try testing.expectEqual(@as(f32, -12.5), got.delta_pixels); + try testing.expect(!got.glide); } try testing.expectEqual(@as(f32, 1.25), (try roundClient(&buf, .{ .event = .{ .pinch = 1.25 } })).event.pinch); try testing.expectEqual(@as(f32, -0.75), (try roundClient(&buf, .{ .event = .{ .touch_scroll = -0.75 } })).event.touch_scroll); diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 3f7dd7ca..94eabb6c 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -3617,6 +3617,7 @@ fn wheelAt(g: *Gui, in: *Input, w: WheelTurn, mc: MouseCell) void { in.post(.{ .pdf_scroll = .{ .pane = @intCast(id), .delta_pixels = -w.y * @as(f32, @floatFromInt(g.cell_h)), + .glide = notchOf(w.y, w.integer_y) != null, } }); } else if (!w.mods.shift and pane.isTerminal() and pardes.panes.terminal.ownsWheel(pane)) { // The program's (codex, htop, less): every whole @@ -4338,6 +4339,10 @@ fn runDemo(g: *Gui, in: *Input, core: *pardes.Pardes) void { core.frames.input(now); wheelAt(g, in, .{ .y = @floatFromInt(-notches), .integer_y = -notches }, .{ .col = r.x + r.w / 2, .row = r.y + r.h / 2 }); }, + .touch => |lines| { + core.frames.input(now); + wheelAt(g, in, .{ .y = -lines }, .{ .col = r.x + r.w / 2, .row = r.y + r.h / 2 }); + }, .key => |key| { core.frames.input(now); in.post(.{ .key = key }); @@ -5061,16 +5066,8 @@ test "a wheel over codex in a terminal reaches its pty as the mouse reports it a /// (an ease, a turn through zero); a jump mid-glide adds its rows with the /// speed kept. The spring's settle only comes once the inputs stop. const Glide = struct { - /// The picture's offset from the core's rows now (rows; negative, the - /// picture still shows what is above), its speed (offset's rows a - /// second), and when it was last integrated. - offset: f32 = 0, - speed: f32 = 0, - at_ns: u64 = 0, - /// What has been feeding it: the rows of the last inputs (down - /// positive), their spacing, the last's time, and how many came as one - /// stream. - feed: struct { rows: f32 = 0, gap_ns: u64 = 0, last_ns: u64 = 0, count: u16 = 0 } = .{}, + /// The picture's lag behind the core's rows and its motion (rows). + follow: pardes.animation.Follow = .{}, now_ns: u64 = 0, pane: usize = 0, rect: pardes.Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 }, @@ -5100,21 +5097,16 @@ const Glide = struct { /// whatever moved it. seen: [pardes.MAX_PANES]struct { serial: u32 = 0, line: i64 = 0, file: u64 = 0, revision: u32 = 0 } = @splat(.{}), - /// How far the picture may lag, in bodies: the rows kept past each - /// edge, so a jump made while one glides carries on from where it is. - const reach = 2; - const bodies_per_s: f32 = 15; - /// Inputs this close, each the way the last went, are one stream. - const stream_ns = 150 * std.time.ns_per_ms; + /// The rows kept past each edge, in bodies, so a jump made while one + /// glides carries on from where it is: as far as the picture may lag. + const reach = pardes.animation.Follow.reach; fn moving(glide: *const Glide) bool { - return glide.offset != 0 or glide.speed != 0 or glide.pending; + return glide.follow.moving() or glide.pending; } fn stop(glide: *Glide) void { - glide.speed = 0; - glide.feed = .{}; - glide.offset = 0; + glide.follow = .{ .at_ns = glide.follow.at_ns }; glide.pending = false; glide.nabove = 0; glide.nbelow = 0; @@ -5126,8 +5118,8 @@ const Glide = struct { /// The glide's lag in the core's lines: its rows, scaled by the lines /// they carried. fn lagLines(glide: *const Glide) f32 { - if (glide.rows_moved == 0) return glide.offset; - return glide.offset * glide.lines_moved / glide.rows_moved; + if (glide.rows_moved == 0) return glide.follow.offset; + return glide.follow.offset * glide.lines_moved / glide.rows_moved; } fn deinit(glide: *Glide, gpa: std.mem.Allocator) void { @@ -5140,70 +5132,18 @@ const Glide = struct { return glide.rect.w -| config.GUTTER; } - /// An input: the core's rows moved by `rows` (down positive) since the - /// last frame. The picture now lags by as many more, its speed as it was. + /// The rules at this body's size: a tenth of a pixel is 5e-3 rows. + fn pace(glide: *const Glide, motion: pardes.animation.Motion) pardes.animation.Follow.Pace { + return pardes.animation.Follow.pace(motion, @floatFromInt(glide.body_h), 5e-3); + } + + /// An input: the core's rows moved by `rows` (down positive). fn kick(glide: *Glide, rows: f32, now_ns: u64, motion: pardes.animation.Motion) void { - glide.advance(now_ns, motion); - // Never more than `reach` bodies behind: a flick past that jumps. - const limit: f32 = @floatFromInt(@as(u32, glide.body_h) * reach); - glide.offset = std.math.clamp(glide.offset - rows, -limit, limit); - const f = &glide.feed; - const like = f.count > 0 and (rows > 0) == (f.rows > 0) and now_ns -| f.last_ns < stream_ns; - if (like) { - const gap = now_ns -| f.last_ns; - f.gap_ns = if (f.count == 1) gap else (f.gap_ns + gap) / 2; - f.rows = (f.rows + rows) / 2; - f.count +|= 1; - } else f.* = .{ .rows = rows, .count = 1 }; - f.last_ns = now_ns; + glide.follow.kick(rows, now_ns, glide.pace(motion)); } - /// The system moved on to `now_ns`. Fed by a stream, at the stream's - /// rate (rows an input over their spacing), held a lag `lead` behind - /// the text so a late input never finds it stopped and the stream's end - /// never runs it past its mark; else on the flavour's spring at 1.5x - /// its pace, critically damped, from the speed it has. Never more than - /// `bodies_per_s` of the body a second, so moving text stays readable. fn advance(glide: *Glide, now_ns: u64, motion: pardes.animation.Motion) void { - const dt: f32 = @floatCast(@as(f64, @floatFromInt(@min(now_ns -| glide.at_ns, 2 * pardes.animation.frame_ns))) / std.time.ns_per_s); - glide.at_ns = now_ns; - if (glide.offset == 0 and glide.speed == 0) return; - const omega = motion.omega * 1.5; - const cap = @as(f32, @floatFromInt(glide.body_h)) * bodies_per_s; - const toward: f32 = if (glide.offset < 0) 1 else -1; - const f = glide.feed; - if (f.count >= 2 and f.gap_ns > 0 and now_ns -| f.last_ns < f.gap_ns * 3 / 2) { - const gap: f32 = @floatCast(@as(f64, @floatFromInt(f.gap_ns)) / std.time.ns_per_s); - const rate = @abs(f.rows) / gap; - const lead = @max(@abs(f.rows), rate / omega); - // ponytail: a gentle pull to the lead (a tenth an input), so the - // per-frame step varies by a tenth; a stream's own jitter beyond - // that shows. An estimate of the input clock if it ever matters. - const v = std.math.clamp(rate + 0.1 / gap * (@abs(glide.offset) - lead), 0, cap); - const step = @min(v * dt, @abs(glide.offset)); - glide.offset += toward * step; - glide.speed = toward * v; - return; - } - const x = glide.offset; - // Coming to rest from a stream: never faster than the spring's own - // pace from here (omega a row of lag a second), so it eases in and - // does not pass its mark. - if (glide.speed * x < 0 and @abs(glide.speed) > omega * @abs(x)) glide.speed = std.math.sign(glide.speed) * omega * @abs(x); - const lead_v = glide.speed + omega * x; - const decay = @exp(-omega * dt); - var next = (x + lead_v * dt) * decay; - var speed = (glide.speed - omega * dt * lead_v) * decay; - if (@abs(next - x) > cap * dt) { - next = x + std.math.sign(next - x) * cap * dt; - speed = std.math.sign(next - x) * cap; - } - glide.offset = next; - glide.speed = speed; - if (@abs(next) < 5e-3 and @abs(speed) < 5e-2) { - glide.offset = 0; - glide.speed = 0; - } + glide.follow.advance(now_ns, glide.pace(motion)); } /// Finds how far the pane's body rows moved, `dir` rows at a time, by @@ -5307,11 +5247,11 @@ fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { defer core.shell_continuous = glide.moving() or g.sel_glow.fading or g.hover_glow.fading; glide.now_ns = core.now_ns; glide.motion = pardes.animation.Motion.of(core.settings.motion); - if (glide.offset != 0 or glide.speed != 0) { + if (glide.follow.offset != 0 or glide.follow.speed != 0) { glide.advance(core.now_ns, glide.motion); core.needs_frame = true; - if (glide.offset == 0) { - glide.offset = 0; + if (glide.follow.offset == 0) { + glide.follow.offset = 0; glide.nabove = 0; glide.nbelow = 0; glide.lines_moved = 0; @@ -5425,16 +5365,16 @@ fn emitGlideRows(g: *Gui, instances: [*]CellInstance, base: u32, surface: *parde g.scroll_body_y = y0; g.scroll_body_h = bh; var shifted = layout; - shifted.y_off -= glide.offset * layout.h; + shifted.y_off -= glide.follow.offset * layout.h; const kept = holdContext(g, surface, glide.pane, held, bh, layout); shifted.y_off += kept.lift; var n: u32 = 0; // Only the rows the scissor shows: lagging a body or more, every body // row is past the edge, and drawing them all was twice the frame's // instances for the length of a long glide. - const lag: u16 = @intFromFloat(@min(@floor(@abs(glide.offset)), @as(f32, @floatFromInt(bh)))); - var row = y0 + kept.rows + if (glide.offset > 0) lag else 0; - const row_end = y0 + bh - if (glide.offset < 0) lag else 0; + const lag: u16 = @intFromFloat(@min(@floor(@abs(glide.follow.offset)), @as(f32, @floatFromInt(bh)))); + var row = y0 + kept.rows + if (glide.follow.offset > 0) lag else 0; + const row_end = y0 + bh - if (glide.follow.offset < 0) lag else 0; while (row < row_end) : (row += 1) { const line = surface.cells[@as(usize, row) * surface.cols ..][0..surface.cols]; const row_cursor: ?usize = if (surface.cursor) |cu| if (cu.y == row) cu.x else null else null; @@ -5448,13 +5388,13 @@ fn emitGlideRows(g: *Gui, instances: [*]CellInstance, base: u32, surface: *parde } // The gap: rows past the top while the picture lags a move down, past // the bottom for one up; as many as it opens. - const gap: u16 = @intFromFloat(@min(@ceil(@abs(glide.offset)), @as(f32, @floatFromInt(@as(u32, bh) * Glide.reach)))); - const nkept = if (glide.offset < 0) glide.nabove else glide.nbelow; - const rows = if (glide.offset < 0) glide.above else glide.below; + const gap: u16 = @intFromFloat(@min(@ceil(@abs(glide.follow.offset)), @as(f32, @floatFromInt(@as(u32, bh) * Glide.reach)))); + const nkept = if (glide.follow.offset < 0) glide.nabove else glide.nbelow; + const rows = if (glide.follow.offset < 0) glide.above else glide.below; for (0..@min(gap, nkept)) |i| { const cells = rows[i * bw ..][0..bw]; // Row -1 - i above the body, or bh + i below it, in grid rows. - const at_row: i32 = if (glide.offset < 0) @as(i32, y0) - 1 - @as(i32, @intCast(i)) else @as(i32, y0) + bh + @as(i32, @intCast(i)); + const at_row: i32 = if (glide.follow.offset < 0) @as(i32, y0) - 1 - @as(i32, @intCast(i)) else @as(i32, y0) + bh + @as(i32, @intCast(i)); var at = shifted; // emitInstance takes an unsigned row: move the layout instead. at.y_off += @as(f32, @floatFromInt(at_row - @as(i32, y0))) * layout.h; @@ -5514,7 +5454,7 @@ test "a notch scrolls the core at once and the picture glides after it, a wrappe g.glide.measure(gpa, s); // Its two rows: the picture two rows behind, the rows that left kept, // nearest first. - try std.testing.expectApproxEqAbs(@as(f32, -2), g.glide.offset, 1e-4); + try std.testing.expectApproxEqAbs(@as(f32, -2), g.glide.follow.offset, 1e-4); try std.testing.expectEqual(@as(u16, 2), g.glide.nabove); try std.testing.expect(sameRow(g.glide.above[0 .. r.w - config.GUTTER], row_before)); // Frame by frame it comes back, never past, and settles. @@ -5523,11 +5463,11 @@ test "a notch scrolls the core at once and the picture glides after it, a wrappe now += 7 * ms; core.advance(now); stepGlide(&g, core, gpa); - try std.testing.expect(g.glide.offset <= 1e-4); + try std.testing.expect(g.glide.follow.offset <= 1e-4); if (frames > 200) return error.NeverSettled; } try std.testing.expect(frames > 5); - try std.testing.expectEqual(@as(f32, 0), g.glide.offset); + try std.testing.expectEqual(@as(f32, 0), g.glide.follow.offset); try std.testing.expect(!core.shell_continuous); // Motion off: a notch lands at once. core.settings.motion = .off; @@ -5560,7 +5500,7 @@ const GlideRun = struct { const s = try core.render(arena.allocator()); if (g.glide.pending) g.glide.measure(gpa, s); if (g.glide.moving()) g.glide.advance(g.glide.now_ns, g.glide.motion); - seq.offsets[frame] = g.glide.offset; + seq.offsets[frame] = g.glide.follow.offset; seq.lines[frame] = @intCast(core.panes[core.active].?.file.?.scroll); seq.carets[frame] = caretRow(g, s) orelse -1; seq.cells[frame] = if (s.cursor) |cu| @floatFromInt(cu.y) else -1; @@ -5809,7 +5749,7 @@ fn stepScroll(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { fn emitScrollRows(g: *Gui, instances: [*]CellInstance, base: u32, surface: *pardes.Surface, layout: CellLayout, win_w: f32, win_h: f32, page: Ground, held: u16) u32 { g.scroll_held_px = 0; - if (g.glide.offset != 0) return emitGlideRows(g, instances, base, surface, layout, win_w, win_h, page, held); + if (g.glide.follow.offset != 0) return emitGlideRows(g, instances, base, surface, layout, win_w, win_h, page, held); const scroll_pane = g.scroll_pane orelse return 0; if (g.scroll_lag == 0) return 0; for (surface.panelTracks()) |track| @@ -6436,7 +6376,7 @@ fn renderFrame( if (chrome.fx & 8 != 0 and chrome.decor_dots != null) for (surface.regionList()) |region| { if (region.kind != .body or cell_decor.nbodies == cell_decor.bodies.len) continue; var rows: f32 = @floatFromInt(region.line); - if (g.glide.moving() and g.glide.pane == region.owner) rows += g.glide.offset; + if (g.glide.moving() and g.glide.pane == region.owner) rows += g.glide.follow.offset; const period = cell_decor.uniforms[4]; cell_decor.bodies[cell_decor.nbodies] = bodyPixels(region, layout); cell_decor.shifts[cell_decor.nbodies] = @mod(-rows * layout.h * 0.25, period); @@ -6463,12 +6403,14 @@ fn renderFrame( const blink_alpha: f32 = if (blinkNow(g, chrome)) |b| b.alpha else 1; // While a glide moves the text, the caret is its quad, drawn from its // own springs, not a cell of the moving rows. - const quad_mode = chrome.cursor_glide & 2 != 0 and (chrome.cursor_glide & 1 != 0 or blink_alpha < 1 or g.glide.offset != 0); + const quad_mode = chrome.cursor_glide & 2 != 0 and (chrome.cursor_glide & 1 != 0 or blink_alpha < 1 or g.glide.follow.offset != 0); g.cursor_quad_alpha = if (quad_mode) chrome.cursor_alpha * blink_alpha else 0; // The probe's view of this frame's motion (FrameStats.Frame). if (core) |p| { const probe = &@constCast(p).frames; - probe.scroll = g.glide.offset; + // A focused PDF's strip is the core's (pdf_view.draw). + const pdf_focus = if (p.panes[p.active]) |pane| pane.hasPdf() else false; + if (!pdf_focus) probe.scroll = g.glide.follow.offset; probe.caret = caretRow(g, surface) orelse 0; } const cursors_shown = groups.tracks == 0 and !quad_mode; @@ -6726,7 +6668,7 @@ fn renderFrame( for (groups.items[0..groups.len], 0..) |group, index| std.debug.assert(cell_next[index] == group.cell_start + group.cell_count); // The body's context rows, which its scroll leaves where they are. - const scrolling: ?usize = if (g.glide.offset != 0) g.glide.pane else g.scroll_pane; + const scrolling: ?usize = if (g.glide.follow.offset != 0) g.glide.pane else g.scroll_pane; const held: u16 = if (core) |p| if (scrolling) |id| if (p.panes[id]) |pane| pane.context_rows else 0 else 0 else 0; shifted = emitScrollRows(g, instances, cell_total, surface, layout, win_w, win_h, page, held); // Over a theme's plaques a see-through cell with no ink blends to @@ -9620,7 +9562,7 @@ fn paneDecor(g: *Gui, gpa: std.mem.Allocator, regions: []const pardes.Region, ti if (rail.thumb_h != 0 and rail.thumb_span > 0) { // In pixels, not rows, and with the picture while it glides // or lags behind the core's rows (both in rows of the body). - const lag: f32 = if (g.glide.offset != 0 and g.glide.pane == rail.owner) + const lag: f32 = if (g.glide.follow.offset != 0 and g.glide.pane == rail.owner) g.glide.lagLines() else if (g.scroll_pane != null and g.scroll_pane.? == rail.owner) g.scroll_lag else 0; const track_px = body_y1 - y0; @@ -10261,13 +10203,13 @@ test "the scrollbar thumb moves by the pixel and follows a glide" { // A line of a thousand moves it a fraction of a row, not 0 or 20 px. try std.testing.expect(at11 > at10 and at11 - at10 < 1); g.glide.pane = core.active; - g.glide.offset = -1; + g.glide.follow.offset = -1; try std.testing.expectApproxEqAbs(at10, try thumb.top(&g, core), 1e-3); // A wrapped line: the picture moved two rows for the one line, and the // thumb lags the line, not two. g.glide.lines_moved = 1; g.glide.rows_moved = 2; - g.glide.offset = -2; + g.glide.follow.offset = -2; try std.testing.expectApproxEqAbs(at10, try thumb.top(&g, core), 1e-3); } @@ -10757,7 +10699,7 @@ const max_cursors = 1 + pardes.MAX_PANES + pardes.MAX_TAG_LAYERS; fn caretRow(g: *const Gui, surface: *const pardes.Surface) ?f32 { const chrome = &surface.chrome; const cursor = surface.cursor orelse return null; - const quad = chrome.cursor_glide & 2 != 0 and (chrome.cursor_glide & 1 != 0 or g.glide.offset != 0); + const quad = chrome.cursor_glide & 2 != 0 and (chrome.cursor_glide & 1 != 0 or g.glide.follow.offset != 0); return if (quad) chrome.cursor_quad[1] else @floatFromInt(cursor.y); } diff --git a/src/pardes.zig b/src/pardes.zig index 0f3f87f2..829a7bfd 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -3690,7 +3690,9 @@ pub const Event = union(enum) { command: []const u8, /// Native shells may preserve sub-cell wheel distance in physical pixels. /// TTY button events still enter through the ordinary mouse path. - pdf_scroll: struct { pane: u8, delta_pixels: f32 }, + /// `glide`: a wheel's notch, which the strip glides; a touchpad's + /// fraction is followed as it comes. + pdf_scroll: struct { pane: u8, delta_pixels: f32, glide: bool = true }, pinch: f32, touch_scroll: f32, /// The host pointer left the drawable area. Unlike an out-of-range motion, @@ -6164,7 +6166,7 @@ pub const Pardes = struct { if (scroll.pane < MAX_PANES) { if (p.panes[scroll.pane]) |pane| { if (pane.hasPdf() and p.native_images) - _ = panes.Pdf.scrollPane(p, pane, @floatCast(scroll.delta_pixels)); + _ = panes.Pdf.scrollPane(p, pane, @floatCast(scroll.delta_pixels), scroll.glide); } } } @@ -7737,7 +7739,7 @@ pub const Pardes = struct { /// is for the stepped animations, which count frames of `frame_ns`. pub fn continuous(p: *const Pardes) bool { for (p.lifts) |spring| if (!spring.settled) return true; - return p.cursor_glide.moving() or p.shell_continuous; + return p.cursor_glide.moving() or p.shell_continuous or panes.Pdf.moving(p); } /// For shells that only need a yes or no: macOS and the board, which diff --git a/src/pdf_view.zig b/src/pdf_view.zig index 2a89ef01..37e4b216 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -197,6 +197,81 @@ test "PDF selections retain their owning page across page focus" { try std.testing.expectEqual(@as(usize, 0), state.selection_text.len); } +test "a PDF strip glides a notch and a page to their marks, eased and never past them, and follows a touchpad's fraction at once" { + if (comptime !enabled) return; + const gpa = std.testing.allocator; + var state = try State.open(gpa, "docs/design.pdf", 1); + defer state.deinit(gpa); + const view: Viewport = .{ .pixel_w = 800, .pixel_h = 600 }; + const motion = pardes.animation.Motion.of(.smooth); + ensureLayout(&state, view); + var now: u64 = std.time.ns_per_s; + try std.testing.expectEqual(state.document_scroll_y, stripAt(&state, view, motion, now)); + for ([_]f64{ 20, 600 }) |step| { + const from = state.document_scroll_y; + _ = scrollDocument(&state, view, step); + const mark = state.document_scroll_y; + // The input's frame still shows where it was; then it eases in. + var shown = stripAt(&state, view, motion, now); + try std.testing.expectEqual(from, shown); + var last_step: f64 = 0; + var peaked = false; + for (0..60) |_| { + now += pardes.animation.frame_ns; + const next = stripAt(&state, view, motion, now); + const moved = next - shown; + try std.testing.expect(moved >= 0 and next <= mark); + // Past its fastest, each frame's step is no longer than the last. + if (moved < last_step) peaked = true; + if (peaked) try std.testing.expect(moved <= last_step + 1e-9); + last_step = moved; + shown = next; + } + try std.testing.expectEqual(mark, shown); + try std.testing.expect(!state.follow.moving()); + try std.testing.expect(last_step <= 1); + } + // A touchpad's fraction: drawn where the core is, the same frame. + const from = state.document_scroll_y; + _ = scrollDocument(&state, view, 0.25); + state.direct_y += state.document_scroll_y - from; + try std.testing.expectEqual(from + 0.25, stripAt(&state, view, motion, now)); +} + +test "a PDF glide renders the pages it crosses in its input's frame, but a far jump's only where it comes to rest" { + if (comptime !enabled) return; + const gpa = std.testing.allocator; + var state = try State.open(gpa, "docs/design.pdf", 1); + defer state.deinit(gpa); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const view: Viewport = .{ .pixel_w = 400, .pixel_h = 300 }; + const motion = pardes.animation.Motion.of(.smooth); + const key: TintKey = .{ .mode = .disabled, .colors = undefined }; + const none: HighlightInput = .{ .hover_color = .{ 0, 0, 0 }, .selection_color = .{ 0, 0, 0 } }; + ensureLayout(&state, view); + const frame = struct { + fn f(st: *State, a: *std.heap.ArenaAllocator, m: pardes.animation.Motion, k: TintKey, h: HighlightInput) void { + const v: Viewport = .{ .pixel_w = 400, .pixel_h = 300 }; + const shown = stripAt(st, v, m, std.time.ns_per_s); + st.document_scroll_y = shown; + _ = renderFrame(st, std.testing.allocator, a.allocator(), v, pardes.pdf_raster_policy, k, h, st.drawn_y - shown, st.far); + st.document_scroll_y = st.drawn_y; + } + }.f; + frame(&state, &arena, motion, key, none); + // A body down, onto page one's lower part: what it crosses is drawn. + _ = scrollDocument(&state, view, 300); + frame(&state, &arena, motion, key, none); + try std.testing.expect(!state.far and rasterForPage(&state, 0) != null); + // Far, to page three: only it; the slide comes in over page two's place. + state.document_scroll_y = @floatFromInt(state.page_starts[3]); + frame(&state, &arena, motion, key, none); + try std.testing.expect(state.far); + try std.testing.expect(rasterForPage(&state, 3) != null); + try std.testing.expect(rasterForPage(&state, 2) == null); +} + pub const Raster = if (enabled) struct { valid: bool = false, page: usize = 0, @@ -249,6 +324,15 @@ pub const State = if (enabled) struct { layout_fit: FitMode = .width, layout_valid: bool = false, document_scroll_y: f64 = 0, + /// The strip as drawn lags `document_scroll_y` by `follow` (anim 11, + /// the text's motion system, in pixels): `drawn_y` is where the core + /// stood at the last frame, `direct_y` what moved it since that the + /// picture takes as it comes (a touchpad, a relayout). + follow: pardes.animation.Follow = .{}, + drawn_y: f64 = 0, + direct_y: f64 = 0, + /// The last kick was a jump past a body (renderFrame's `far`). + far: bool = false, scroll_to_page_pending: bool = true, rasters: [raster_max]Raster = undefined, rasters_len: usize = 0, @@ -869,23 +953,28 @@ fn pageAtOffset(state: *const State, offset: f64) usize { return if (y >= end and lo + 1 < state.page_count) lo + 1 else lo; } -fn pageVisible(state: *const State, page: usize, viewport: Viewport) bool { +fn pageVisible(state: *const State, page: usize, y: f64, h: f64) bool { if (comptime !enabled) return false; - const top = @as(f64, @floatFromInt(state.page_starts[page])) - state.document_scroll_y; + const top = @as(f64, @floatFromInt(state.page_starts[page])) - y; const bottom = top + @as(f64, @floatFromInt(state.page_heights[page])); - return bottom > 0 and top < @as(f64, @floatFromInt(viewport.pixel_h)); + return bottom > 0 and top < h; } pub fn visiblePages(state: *const State, viewport: Viewport) VisiblePages { + return pagesOver(state, state.document_scroll_y, @floatFromInt(viewport.pixel_h)); +} + +/// The pages over the strip from `y`, `h` pixels down. +fn pagesOver(state: *const State, y: f64, h: f64) VisiblePages { if (comptime !enabled) return .{}; var out: VisiblePages = .{}; - var page = pageAtOffset(state, state.document_scroll_y); - if (page > 0 and pageVisible(state, page - 1, viewport)) page -= 1; + var page = pageAtOffset(state, y); + if (page > 0 and pageVisible(state, page - 1, y, h)) page -= 1; out.first = page; while (page < state.page_count) : (page += 1) { - const top = @as(f64, @floatFromInt(state.page_starts[page])) - state.document_scroll_y; - if (top >= @as(f64, @floatFromInt(viewport.pixel_h))) break; - if (pageVisible(state, page, viewport)) out.len += 1; + const top = @as(f64, @floatFromInt(state.page_starts[page])) - y; + if (top >= h) break; + if (pageVisible(state, page, y, h)) out.len += 1; } return out; } @@ -1496,18 +1585,31 @@ pub fn renderFrame( policy: RasterPolicy, tint_key: TintKey, highlight_input: HighlightInput, + path: f64, + far: bool, ) VisiblePages { if (comptime !enabled) return .{}; ensureLayout(state, viewport); state.resolveSearch(gpa); const highlights = buildHighlights(state, arena, highlight_input) catch Highlights{}; const request = renderRequest(viewport, policy, state.fit); - var visible = visiblePages(state, viewport); + // ponytail: every page the glide crosses to its mark (`path` pixels + // on) renders now, in the input's frame, so no frame of the glide + // waits on MuPDF (it renders on the loop). A far jump's slide (`far`) + // comes in over what is there, none of the page above its mark, which + // would cost the input's frame a page; a render thread would draw it. + const span = struct { + fn f(st: *const State, view: Viewport, along: f64, from_rest: bool) VisiblePages { + const h: f64 = @floatFromInt(view.pixel_h); + return if (from_rest) pagesOver(st, st.document_scroll_y + along, h) else pagesOver(st, st.document_scroll_y + @min(0, along), h + @abs(along)); + } + }.f; + var visible = span(state, viewport, path, far); reconcile(state, gpa, request, tint_key, highlights, visible, viewport); rearmSearchReveal(state, viewport); revealSearch(state, viewport, activeGeometry(state, viewport)); - visible = visiblePages(state, viewport); + visible = span(state, viewport, path, far); reconcile(state, gpa, request, tint_key, highlights, visible, viewport); return visible; } @@ -1898,6 +2000,7 @@ pub fn ensureLayout(state: *State, viewport: Viewport) void { if (comptime !enabled) return; const tz = tracy.zone(@src(), "pdf.ensure_layout"); defer tz.end(); + var relaid = false; if (state.layout_valid and !state.scroll_to_page_pending and !state.layout_anchor_pending and (state.layout_viewport_w != viewport.pixel_w or @@ -1919,6 +2022,7 @@ pub fn ensureLayout(state: *State, viewport: Viewport) void { state.layout_viewport_h = viewport.pixel_h; state.layout_fit = state.fit; state.layout_valid = true; + relaid = true; if (state.scroll_to_page_pending) { state.document_scroll_y = @floatFromInt(state.page_starts[state.page]); state.scroll_to_page_pending = false; @@ -1933,6 +2037,12 @@ pub fn ensureLayout(state: *State, viewport: Viewport) void { consumeOutlineReveal(state, viewport); const max_scroll = @as(f64, @floatFromInt(state.document_height -| viewport.pixel_h)); state.document_scroll_y = std.math.clamp(state.document_scroll_y, 0, max_scroll); + // A new layout is not a scroll: the picture is drawn there at once. + if (relaid) { + state.follow = .{}; + state.drawn_y = state.document_scroll_y; + state.direct_y = 0; + } } pub const WordProbe = if (enabled) struct { @@ -2688,23 +2798,55 @@ pub fn panPane( return panPixels(state, placed, axis, direction, display_pixels); } -pub fn scrollPane(core: *pardes.Pardes, pane: *pardes.Pane, delta_pixels: f64) bool { +/// `glide`: a notch, which the strip glides; else a touchpad's fraction, +/// which it follows as it comes. +pub fn scrollPane(core: *pardes.Pardes, pane: *pardes.Pane, delta_pixels: f64, glide: bool) bool { if (comptime !enabled) return false; const tz = tracy.zone(@src(), "pdf.scroll_notch"); defer tz.end(); const state = &(pane.pdf orelse return false); const view = ensurePaneLayout(core, pane) orelse return false; + const from = state.document_scroll_y; const result = scrollDocument(state, view, delta_pixels) orelse return false; + if (!glide) state.direct_y += state.document_scroll_y - from; if (result.active_page != state.page) activatePage(core, pane, result.active_page, false); return true; } +/// Where the strip is drawn this frame (anim 11): what moved the core's +/// position since the last frame kicks its follow, as a scroll kicks the +/// text's, all but what moved it directly; the follow then moves on to now. +fn stripAt(state: *State, view: Viewport, motion: pardes.animation.Motion, now_ns: u64) f64 { + const pace = pardes.animation.Follow.pace(motion, @floatFromInt(view.pixel_h), 0.1); + const moved = state.document_scroll_y - state.drawn_y - state.direct_y; + state.drawn_y = state.document_scroll_y; + state.direct_y = 0; + if (motion.instant) { + state.follow = .{}; + } else if (moved != 0) { + // A move past a body slides one, as a text's far jump does (G6): + // the pages between are rendered for the slide, not crossed. + const body: f64 = @floatFromInt(view.pixel_h); + state.far = @abs(moved) > body; + state.follow.kick(@floatCast(std.math.clamp(moved, -body, body)), now_ns, pace); + } else state.follow.advance(now_ns, pace); + const max_scroll = @as(f64, @floatFromInt(state.document_height -| view.pixel_h)); + return std.math.clamp(state.drawn_y + state.follow.offset, 0, max_scroll); +} + +/// A PDF strip is still on its way to where the core stands. +pub fn moving(core: *const pardes.Pardes) bool { + if (comptime !enabled) return false; + for (core.panes) |slot| if (slot) |pane| if (pane.pdf) |state| if (state.follow.moving()) return true; + return false; +} + pub fn verticalWheel(core: *pardes.Pardes, pane: *pardes.Pane, direction: i8) void { if (comptime !enabled) return; if (!core.native_images) return stepPanePage(core, pane, direction); const rows: u32 = @intCast(@max(1, config.wheel_rows)); const pixels = scaledStep(core.cell_pixels.h, rows); - _ = scrollPane(core, pane, @as(f64, @floatFromInt(pixels)) * direction); + _ = scrollPane(core, pane, @as(f64, @floatFromInt(pixels)) * direction, true); } pub fn horizontalWheel(core: *pardes.Pardes, pane: *pardes.Pane, direction: i8) void { @@ -2730,6 +2872,14 @@ pub fn draw( const state = &(pane.pdf orelse return false); const view = paneViewport(core, pane) orelse return false; const key = TintKey{ .mode = state.tint, .colors = tintColors(core) }; + ensureLayout(state, view); + // Drawn where the strip's follow is; the core's position comes back + // after (`drawn_y`), unless a search's reveal moved it as it drew, + // which is then drawn there at once. + const shown = stripAt(state, view, pardes.animation.Motion.of(core.settings.motion), core.now_ns); + state.document_scroll_y = shown; + defer state.document_scroll_y = state.drawn_y; + if (pane_id == core.active) core.frames.scroll = @floatCast(shown); const visible = renderFrame( state, core.pdf_gpa, @@ -2738,7 +2888,13 @@ pub fn draw( pardes.pdf_raster_policy, key, highlightInput(core, pane_id, pane), + state.drawn_y - shown, + state.far, ); + if (state.document_scroll_y != shown) { + state.follow = .{}; + state.drawn_y = state.document_scroll_y; + } var placed_any = false; var page = visible.first; const visible_end = visible.first + visible.len; |
