From 6e8a6a63d45b2caef7b649d358f836e6cc4134d6 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 22:50:15 -0300 Subject: anim 11: a PDF's strip moves in the text's motion system: the integrator is animation.Follow (the Glide's, moved out), here the strip's lag behind document_scroll_y in pixels; whatever moves the core's position (a notch, a spin, PageDown, Ctrl-d/f, ge/gg, a Look at a page, Back, the rail) kicks it, a touchpad's fraction and a relayout move it directly; the pages a glide crosses render in its input's frame, a far jump's only where it comes to rest; a slow stream eases onto its mark (it ran into it at speed and passed it); pictures neither scroll nor pan --- docs/next-anim.md | 125 ++++++++++++++++++++++++++++++++++- src/FrameStats.zig | 73 +++++++++++++++++++- src/animation.zig | 137 ++++++++++++++++++++++++++++++++++++++ src/detached/wire.zig | 8 ++- src/gui/gui.zig | 158 ++++++++++++++------------------------------ src/pardes.zig | 8 ++- src/pdf_view.zig | 180 ++++++++++++++++++++++++++++++++++++++++++++++---- test/pdf.zig | 14 ++-- 8 files changed, 568 insertions(+), 135 deletions(-) diff --git a/docs/next-anim.md b/docs/next-anim.md index a64bfaf4..9c9610e7 100644 --- a/docs/next-anim.md +++ b/docs/next-anim.md @@ -25,6 +25,9 @@ Positions, not a verdict: argue with them. | anim 6 `xykrnoql` | the tty paced too, its acks and output waited through | | anim 7 `powlyrxv` | what the probe found: the spin, glide measure and emission, far-jump and long-line paints, the caret's sideways scroll, no join on the loop, pictures decoded off the loop | | anim 8 | this note | +| anim 9 `nqznmskw` | the view's motion is one system: a stream of inputs moves it at its own rate, the spring only settles it | +| anim 10 `umnwnrvn` | the caret is its own body: drawn from its own springs, never carried by the text's glide | +| anim 11 `knzzzknu` | the integrator is `animation.Follow`; a PDF's strip moves in it, in pixels; a slow stream eases onto its mark | Tests (one check each, `-Dtest-filter`): "frame stats: gaps into a motion count as jank ...", "every scroll glides on one law ...", "a cursor eases @@ -213,6 +216,64 @@ is still drawn at once: in a stream the picture is on the stream's straight line wherever a frame samples it (tried: holding repeats to the next tick cost a held key 7 ms of latency for no evenness one could see). +### A PDF's strip, the same system (anim 11) + +The integrator is `animation.Follow` now (the Glide's state and rules, +moved out; the text's Glide holds one, in rows). A PDF pane's strip holds +another, in pixels: the core's position is `document_scroll_y` as every +input leaves it, and the strip is drawn `follow.offset` from it +(`pdf_view.stripAt`, each frame at the core's clock). Whatever moved the +core's position since the last frame kicks it, so every way a PDF moves is +one input to the same rules, the text's table above unchanged: + +| input | the strip | +|---|---| +| a wheel notch (a line's pixels), a spin | eases; a spin is a stream, at its rate | +| PageDown/Up, Ctrl-d/u/f/b, held | a stream, at its rate (to the 15 bodies a second cap) | +| ge, gg, a Look at `file.pdf:12`, Back, a section in the outline | a jump: past a body, one body's slide, as a text's far jump | +| a click on the rail, a tick | eases | +| a touchpad's fraction (`pdf_scroll.glide` false) | follows, drawn where the core is the same frame | +| a resize, a fit or zoom change (a relayout) | there at once | +| a search's reveal (it moves the strip as it draws) | there at once | +| Motion off | there at once | + +Rendering stays on the loop (MuPDF; its first-frame and context work is +the open item it was). So that no frame of a glide waits on it, the pages +a glide crosses to its mark are rendered in its input's frame, where the +instant move rendered its destination; a jump past a body (`far`) renders +only where it comes to rest, and its slide comes in over what is there +(the page above the mark is not drawn for a slide of a tenth of a second: +that page was the input frame's whole extra cost). Drawn at the strip's +position, rounded only by the draw (`pixel_offset_y` keeps the +fraction). Pictures are fit to their body and neither scroll nor pan: +nothing there to move. + +**Why the slow PDF scroll read as jumps**: a wheel notch moved the strip a +whole line's pixels (25 here) in the one frame after it, and nothing in +between: at a slow turn, one 25 px jump a notch. The touchpad path was +never quantized (fractions all the way to the placement). Now a notch is +a kick, and the strip eases over ~6 frames. + +### Coming to rest (anim 9, 11) + +Found while measuring the PDF: a slow stream (notches 150 ms apart, just +inside the stream window) ran its constant rate into the mark, stopping +from 2.6 px a frame, and its last speed, never zeroed, then carried it +0.04 row past the mark and back. A stream is now never faster than the +spring's own pace from where it is (omega a unit of lag a second): it eases +onto each mark. The check: "a follow comes to rest without a snap" (a +spin, a page, and notches 144 ms apart: toward the mark only, onto it from +under a third of a pixel, past its fastest frame each step no longer than +the last); it fails on the old rule. + +Measured on every settle the runs have (frames after an input until the +view is still, speeds normalized by the frame's own time): before anim 9 +the old spring's tails were already monotonic; the anim 9+10 stream rule +had one rising tail in the scroll demo's notches (the stop from 2.6 px) +and a last step of 0.88 px; now, over the scroll, stress and PDF runs +(ReleaseSafe and Debug), no tail rises after its fastest frame and the +last step before rest is at most 0.06 px (text) and 0.05 px (PDF). + ### The cursor (anim 4) The thin caret glides as the block does. A step of a cell or less eases @@ -250,7 +311,7 @@ while the view stands still, nor for stretches over 512 KB. | a 200-line paste | one body's slide | a long jump | | terminal output | snaps | a stream would always lag | | terminal under the wheel | glides | the wheel's; keys in its normal mode snap (position 4) | -| PDF | its own scroll | untouched here | +| PDF | glides (its strip, in pixels, anim 11); a touchpad follows | the same rules as text | | no-wrap sideways scroll | snaps | horizontal glides were not asked; position 6 | ## Ponytail pass (on the stack, amended in place) @@ -853,6 +914,68 @@ the 2 s ones were few, the max falls 2801 -> 319. | all | before | 26.1/43.9/74.5/78.0 | 2.1/22.9/44.8/77.5 | 2.1/2.3/15.7/40.8 | 2189 | 1 | 108/15264 | | | after | 24.5/37.8/63.1/70.3 | 4.9/27.5/42.9/68.7 | 15.7/18.5/24.2/39.9 | 2175 | 1 | 101/15264 | +#### GUI: `pdf-scroll` (anim 11), before = anim 10 with the scenario, after = anim 11 + +ReleaseSafe, run 1 of 2 (`.scratch/runs9/v1-*`; run 2 within a few ms but far-jump max): + +| scenario | | latency ms p50/p90/p99/max | frame work ms | present gap ms (moving) | rows/s | late | +|---|---|---|---|---|---|---| +| spin | before | 4.7/9.2/33.4/33.4 | 4.7/9.2/33.4/33.4 | - | 1073 | 0 | +| | after | 2.0/6.8/22.0/22.0 | 1.7/6.9/17.2/22.0 | 12.9/16.7/21.8/28.4 | 1009 | 1 | +| held-pagedown | before | 15.3/29.4/31.1/31.1 | 15.2/29.3/31.0/31.0 | - | 9092 | 0 | +| | after | 1.8/19.2/26.8/26.8 | 1.7/9.6/26.7/26.7 | 16.6/20.5/33.9/33.9 | 6359 | 5 | +| far-jump | before | 16.5/32.8/32.8/32.8 | 6.8/18.0/32.7/32.7 | 17.0/17.2/19.1/19.1 | 24126 | 0 | +| | after | 22.6/32.1/32.1/32.1 | 5.8/7.1/32.0/32.0 | 16.6/16.8/18.8/18.8 | 23744 | 0 | +| slow-notches | before | 7.1/7.1/7.1/7.1 | 8.8/9.8/9.8/9.8 | 16.8/17.4/17.4/17.4 | 83 | 0 | +| | after | 6.9/7.0/7.0/7.0 | 1.7/6.7/6.9/6.9 | 16.6/16.8/17.3/17.3 | 87 | 0 | +| slow-touch | before | 6.9/10.7/12.3/12.3 | 6.8/10.6/12.2/12.2 | - | 94 | 0 | +| | after | 6.8/11.0/12.4/12.5 | 6.7/10.9/12.4/12.4 | - | 94 | 0 | + +Debug (`w1-*`): + +| scenario | | latency ms p50/p90/p99/max | frame work ms | present gap ms (moving) | rows/s | late | +|---|---|---|---|---|---|---| +| spin | before | 4.8/12.7/148.2/148.2 | 4.7/12.3/148.1/148.1 | - | 1074 | 0 | +| | after | 3.2/7.7/140.4/140.4 | 2.7/7.6/71.8/140.3 | 16.4/16.8/72.2/141.1 | 995 | 2 | +| held-pagedown | before | 78.5/115.5/115.5/115.5 | 78.3/115.3/115.3/115.3 | - | 8296 | 0 | +| | after | 10.1/121.7/127.6/127.6 | 6.5/98.4/127.5/127.5 | 16.6/98.6/141.2/141.2 | 6106 | 4 | +| far-jump | before | 45.4/84.2/84.2/84.2 | 6.2/45.1/83.9/83.9 | 16.6/17.7/18.7/18.7 | 24172 | 0 | +| | after | 51.4/86.9/86.9/86.9 | 6.1/8.5/86.7/86.7 | 16.6/17.5/20.8/20.8 | 23525 | 0 | +| slow-notches | before | 11.4/13.1/13.1/13.1 | 7.5/12.6/12.6/12.6 | 15.6/19.2/19.2/19.2 | 83 | 0 | +| | after | 18.3/19.0/19.0/19.0 | 2.7/11.9/18.5/18.5 | 16.6/16.9/19.2/19.2 | 88 | 0 | +| slow-touch | before | 7.3/11.8/14.1/17.8 | 7.0/11.5/13.8/17.3 | - | 94 | 0 | +| | after | 7.2/11.0/12.5/15.4 | 7.0/10.5/12.3/15.1 | - | 94 | 0 | + +The `panes` timedemo's pdf phase (`u1-*`, RS then Debug): + +| scenario | | latency ms p50/p90/p99/max | frame work ms | present gap ms (moving) | rows/s | late | +|---|---|---|---|---|---|---| +| pdf | before | 6.9/14.8/44.1/46.3 | 6.7/14.5/30.6/44.0 | 16.7/16.8/16.9/16.9 | 4678 | 0 | +| | after | 2.0/7.4/34.8/37.8 | 1.8/6.6/18.4/34.7 | 16.6/16.8/27.2/32.9 | 4678 | 9 | +| pdf | before | 7.6/57.1/149.4/158.8 | 6.3/18.0/142.1/149.3 | 16.6/16.8/17.9/17.9 | 4663 | 0 | +| | after | 3.2/16.2/144.5/155.5 | 2.8/7.2/125.0/144.4 | 16.5/16.8/134.0/149.4 | 4655 | 7 | + +Strip steps between presented frames (px, the probe's `scroll` column is +the strip's drawn y for a focused PDF; cell height 25): + +| phase | before | after | +|---|---|---| +| slow notches (one each 300 ms) | 25 px in one frame a notch, 7 of 7 steps whole lines | p10/p50/p90/max 0.04/0.8/6.8/7.2 px, none a whole line | +| slow touchpad (0.06 line a frame) | 1.5 px every frame | 1.5 px every frame (followed) | +| a spin's steps | 25 px each (a notch a frame, 100 at a burst) | 0.5/16/28/42, a stream | +| settle tails | none (nothing settled) | no rise after the fastest frame; last step <= 0.05 px | + +Latency: the spin and held PageDown answer sooner (the frame that answers +a key no longer renders the pages it moves to before showing it; they +render as the glide's input frames, a stream's, come). The far jumps +(ge, gg, a Look at a page, Back) answer within the before's spread in +ReleaseSafe (p50 16.5/17.8 -> 22.6/14.9 over two runs, max 32.8/31.7 -> +32.1/31.2); in Debug ge's frame renders what the before split between the +`g` prefix's frame (~20 ms, re-rendering pages left banded by a fling) and +ge's own (~40): the same total in one frame (60-67 ms). The `late` frames +are a held PageDown's input frames rendering a page while it moves (33 ms +gaps; before they were not moving frames, and took 30 ms to answer). + ## The coordinator's latency rows, answered - **pages-up-big** (p90 8.8 -> 24): real. The paint-ahead's upward stretch 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; - } - - /// 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. + glide.follow.kick(rows, now_ns, glide.pace(motion)); + } + 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; diff --git a/test/pdf.zig b/test/pdf.zig index d4efdfaf..5f7ebc52 100644 --- a/test/pdf.zig +++ b/test/pdf.zig @@ -774,6 +774,7 @@ test "MuPDF pane renders, navigates, searches, and round-trips its page" { .rows = 24, }); defer p.deinit(); + p.settings.motion = .off; // placements, not the glide (anim 11) const pane = p.panes[0].?; try std.testing.expect(hasPdf(pane)); try std.testing.expect(pane.pdf.?.page_count > 1); @@ -822,7 +823,7 @@ test "MuPDF pane renders, navigates, searches, and round-trips its page" { // SDL's raw dy path retains fractions in the placement itself; it does // not leave native pixels fixed while only the underlying cells slide. - p.update(.{ .pdf_scroll = .{ .pane = 0, .delta_pixels = 0.25 } }); + p.update(.{ .pdf_scroll = .{ .pane = 0, .delta_pixels = 0.25, .glide = false } }); try std.testing.expectEqual(@as(f64, 0.25), pane.pdf.?.document_scroll_y); _ = frame.reset(.retain_capacity); const fractional = try p.render(frame.allocator()); @@ -940,7 +941,7 @@ test "MuPDF pane renders, navigates, searches, and round-trips its page" { page0_revision, panes.Pdf.rasterForPage(&pane.pdf.?, 0).?.revision, ); - try std.testing.expect(panes.Pdf.scrollPane(p, pane, 1)); + try std.testing.expect(panes.Pdf.scrollPane(p, pane, 1, false)); try std.testing.expectEqual(@as(usize, 1), pane.pdf.?.page); try std.testing.expectEqual( page1_revision, @@ -1142,7 +1143,7 @@ test "a fling's banded pages show the reader exactly what whole pages would" { // is counted the way a wheel batch counts it. pv.document_scroll_y = 0; pv.scroll_travel = 0; - try std.testing.expect(panes.Pdf.scrollPane(p, pane, landing)); + try std.testing.expect(panes.Pdf.scrollPane(p, pane, landing, false)); _ = frame.reset(.retain_capacity); const flung = try p.render(frame.allocator()); try std.testing.expect(flung.nimages >= 2); @@ -1261,7 +1262,7 @@ test "a tall page is drawn at the display's resolution, in bands that are the wh defer gpa.free(middle); var middle_y: usize = 0; for ([_]f64{ 0, @round(max_scroll / 2), max_scroll }, 0..) |at, step| { - _ = panes.Pdf.scrollPane(p, pane, at - pv.document_scroll_y); + _ = panes.Pdf.scrollPane(p, pane, at - pv.document_scroll_y, false); pv.scroll_travel = 0; // read, not flung _ = frame.reset(.retain_capacity); const surface = try p.render(frame.allocator()); @@ -1291,7 +1292,7 @@ test "a tall page is drawn at the display's resolution, in bands that are the wh // Straight back to the middle from the foot: the rows rendered afresh // are the very rows rendered on the way down, so no seam moves with the // path a reader took. - _ = panes.Pdf.scrollPane(p, pane, @round(max_scroll / 2) - pv.document_scroll_y); + _ = panes.Pdf.scrollPane(p, pane, @round(max_scroll / 2) - pv.document_scroll_y, false); pv.scroll_travel = 0; _ = frame.reset(.retain_capacity); const again = (try p.render(frame.allocator())).images[0].?; @@ -1305,7 +1306,7 @@ test "a tall page is drawn at the display's resolution, in bands that are the wh middle[(common_top - middle_y) * stride .. (common_end - middle_y) * stride], again.rgba[(common_top - again_y) * stride .. (common_end - again_y) * stride], ); - _ = panes.Pdf.scrollPane(p, pane, max_scroll - pv.document_scroll_y); + _ = panes.Pdf.scrollPane(p, pane, max_scroll - pv.document_scroll_y, false); pv.scroll_travel = 0; _ = frame.reset(.retain_capacity); const foot = try gpa.dupe(u8, (try p.render(frame.allocator())).images[0].?.rgba); @@ -1663,6 +1664,7 @@ test "PDF tint and tinted theme changes rebuild every visible raster only" { .rows = 24, }); defer p.deinit(); + p.settings.motion = .off; // placements, not the glide (anim 11) const pane = p.panes[0].?; const pv = &pane.pdf.?; p.native_images = true; -- cgit v1.3