diff options
Diffstat (limited to 'src/glide.zig')
| -rw-r--r-- | src/glide.zig | 383 |
1 files changed, 258 insertions, 125 deletions
diff --git a/src/glide.zig b/src/glide.zig index ced0ca85..723beba9 100644 --- a/src/glide.zig +++ b/src/glide.zig @@ -1,45 +1,41 @@ -//! The view's glide (G6, anim 3, 9, 12): the text's picture following -//! its core's rows, one system on every shell. The GUI draws it at the -//! pixel; the tty at whole rows (panel_compositor.glideRows). +//! The view's glide (G6, anim 3, 9, 12): a frontend's own picture of a +//! scroll, on its own clock, from the frames it draws alone. Whatever +//! scrolls a text (a wheel's lines, Ctrl-d, a page, G, n, a Look) scrolls +//! the core at once, so every key and click acts on where the text is; the +//! frontend sees a body's first line move between two frames (Region.line) +//! and its picture follows there, critically damped (text never passes its +//! mark), every distance on the same law. The GUI draws it at the pixel; +//! the tty at whole rows (tui.zig's glideRows). Nothing here reaches back +//! into the core. const std = @import("std"); const pardes = @import("pardes.zig"); const config = @import("config.zig"); -/// G6 (docs/render-pipeline.md §9.1): whatever scrolls a text (a wheel's -/// lines, Ctrl-d, a page, G, n, a Look) scrolls the core at once, so every -/// key and click acts on where the text is, and the picture glides there -/// on the Motion flavour's spring (critically damped: text never passes its -/// mark), every distance on the same law. How far the rows moved is -/// measured on the next frame against the last, a line wrapped over several -/// rows or a terminal's output included, and the rows that left are kept to -/// draw in the gap. A move past what the two frames share (a page and more, -/// G) has no rows between them to show: the picture comes from the last -/// frame's body as if it lay just past the new one, one body away, so a -/// jump of any length reads as one body's slide. +/// How far the rows moved is measured on the frame against the last drawn, +/// a line wrapped over several rows or a terminal's output included, and +/// the rows that left are kept to draw in the gap. A move past what the two +/// frames share (a page and more, G) has no rows between them to show: the +/// picture comes from the last frame's body as if it lay just past the new +/// one, one body away, so a jump of any length reads as one body's slide. /// /// The picture's motion is one system with its own state, integrated in -/// one place every frame (`advance`), and every scroll, whatever made it, -/// is an input into it (`kick`): it moves the target by the rows the core -/// moved and leaves the motion as it is. What the system was doing decides -/// how it is absorbed: inputs coming as a stream (each like the last, at a -/// steady spacing: a held key, a spun wheel) feed it, and it moves at the -/// stream's own rate, a lag of an input or so behind, never easing toward -/// each target (that was a bounce a line); an input into a resting view, -/// or against the way it moves, starts the spring from the speed it has -/// (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. +/// one place every frame (`Follow.advance`), and every scroll, whatever +/// made it, is an input into it (`Follow.kick`): it moves the target by the +/// rows the core moved and leaves the motion as it is. Inputs coming as a +/// stream (each like the last, at a steady spacing: a held key, a spun +/// wheel) feed it at the stream's own rate; an input into a resting view, +/// or against the way it moves, starts the spring from the speed it has; a +/// jump mid-glide adds its rows with the speed kept. pub const Glide = struct { /// The picture's lag behind the core's rows and its motion (rows). - follow: pardes.animation.Follow = .{}, + follow: Follow = .{}, now_ns: u64 = 0, pane: usize = 0, rect: pardes.Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 }, body_y: u16 = 0, body_h: u16 = 0, - /// A scroll made and not yet measured: its direction, and the body's - /// rows before it. - pending: bool = false, + /// The way the last scroll went, and the body's rows before it. dir: i8 = 0, before: []pardes.Cell = &.{}, /// The rows past the body's top (`above`) and bottom (`below`), nearest @@ -59,24 +55,25 @@ pub const Glide = struct { /// (a page, a held key's): the tty draws a carried caret with the rows. caret_row: ?u16 = null, caret_rides: bool = false, - /// The Motion flavour the last loop step read. - motion: pardes.animation.Motion = .{}, - /// Each pane's view as the last loop step saw it (stepGlide): its - /// serial, first line and file, so a view that moved since is found + /// Each body as the last frame showed it, by its pane: its serial, + /// first line, text and place, so a view that moved since is found /// whatever moved it. - seen: [pardes.MAX_PANES]struct { serial: u32 = 0, line: i64 = 0, file: u64 = 0, revision: u32 = 0 } = @splat(.{}), + seen: [pardes.MAX_PANES]struct { serial: u32 = 0, line: i32 = 0, doc: u64 = 0, rect: pardes.Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 } } = @splat(.{}), + /// The last frame's cells, which a move is measured against. + last: []pardes.Cell = &.{}, + last_cols: u16 = 0, + last_rows: u16 = 0, /// 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. - pub const reach = pardes.animation.Follow.reach; + pub const reach = Follow.reach; pub fn moving(glide: *const Glide) bool { - return glide.follow.moving() or glide.pending; + return glide.follow.moving(); } pub fn stop(glide: *Glide) void { glide.follow = .{ .at_ns = glide.follow.at_ns }; - glide.pending = false; glide.nabove = 0; glide.nbelow = 0; glide.pending_lines = 0; @@ -95,6 +92,7 @@ pub const Glide = struct { gpa.free(glide.before); gpa.free(glide.above); gpa.free(glide.below); + gpa.free(glide.last); } pub fn bw(glide: *const Glide) u16 { @@ -102,18 +100,94 @@ pub const Glide = struct { } /// The rules at this body's size: a tenth of a pixel is 5e-3 rows. - pub 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); + pub fn pace(glide: *const Glide) Follow.Pace { + return Follow.pace(@floatFromInt(glide.body_h), 5e-3); } - pub fn advance(glide: *Glide, now_ns: u64, motion: pardes.animation.Motion) void { - glide.follow.advance(now_ns, glide.pace(motion)); + /// The frame about to be drawn, at `now_ns` on the frontend's clock: + /// the glide moves on to now, and a body whose first line moved since + /// the last frame (the same text in the same place) is measured against + /// that frame and kicks it. A terminal's view glides only under the + /// wheel (`wheeled`: its output scrolls it at every line, and that is no + /// glide); not one the frontend moves by the pixel (`direct`). One glide + /// at a time: the focused body's, else the one that moved most. + pub fn frame(glide: *Glide, gpa: std.mem.Allocator, surface: *const pardes.Surface, now_ns: u64, wheeled: ?usize, direct: ?usize) void { + glide.now_ns = now_ns; + defer glide.remember(gpa, surface); + if (glide.follow.moving()) { + glide.follow.advance(now_ns, glide.pace()); + if (glide.follow.offset == 0) { + glide.nabove = 0; + glide.nbelow = 0; + glide.lines_moved = 0; + glide.rows_moved = 0; + } + } + var pick: ?pardes.Region = null; + var delta: i64 = 0; + var shown: [pardes.MAX_PANES]bool = @splat(false); + for (surface.regionList()) |region| { + if (region.kind != .body or region.owner >= glide.seen.len) continue; + const id: usize = region.owner; + shown[id] = true; + const was = glide.seen[id]; + glide.seen[id] = .{ .serial = region.serial, .line = region.line, .doc = region.doc, .rect = region.rect }; + if (was.serial != region.serial or was.doc != region.doc or was.line == region.line) continue; + // The last frame drew this body where it is now: a pane that + // moved or was resized in the same step has no picture to glide + // from. + if (!std.meta.eql(was.rect, region.rect)) continue; + if (region.doc == 0 and wheeled != id) continue; + if (direct == id) continue; + const moved = @as(i64, region.line) - was.line; + if (pick) |best| if (best.active or (!region.active and @abs(moved) <= @abs(delta))) continue; + pick = region; + delta = moved; + } + for (&glide.seen, shown) |*seen, here| if (!here) { + seen.* = .{}; + }; + const body = pick orelse return; + const r = body.rect; + const rect: pardes.Rect = .{ .x = r.x -| config.GUTTER, .y = r.y, .w = r.w + config.GUTTER, .h = r.h }; + // A new pane or place: what was kept belongs to another picture. + if (glide.pane != body.owner or !std.meta.eql(glide.rect, rect)) glide.stop(); + glide.pane = body.owner; + glide.rect = rect; + glide.body_y = r.y; + glide.body_h = r.h; + const w = r.w; + if (w == 0 or r.h == 0 or glide.last_cols != surface.cols or glide.last_rows != surface.rows or + r.x + w > surface.cols or r.y + r.h > surface.rows) return glide.stop(); + // The body as the last frame drew it, which the move is measured from. + const n = @as(usize, w) * r.h; + if (glide.before.len < n) glide.before = gpa.realloc(glide.before, n) catch &.{}; + if (glide.before.len < n) return glide.stop(); + for (0..r.h) |row| @memcpy(glide.before[row * w ..][0..w], glide.last[(@as(usize, r.y) + row) * glide.last_cols + r.x ..][0..w]); + glide.dir = @intCast(std.math.sign(delta)); + glide.pending_lines += @floatFromInt(@abs(delta)); + glide.measure(gpa, surface); + } + + /// This frame's cells and caret, for the next frame's measure. + fn remember(glide: *Glide, gpa: std.mem.Allocator, surface: *const pardes.Surface) void { + if (glide.last.len != surface.cells.len) { + gpa.free(glide.last); + glide.last = gpa.alloc(pardes.Cell, surface.cells.len) catch &.{}; + } + if (glide.last.len != surface.cells.len) { + glide.last_cols = 0; + return; + } + @memcpy(glide.last, surface.cells); + glide.last_cols = surface.cols; + glide.last_rows = surface.rows; + glide.caret_row = if (surface.cursor) |c| c.y else null; } /// Finds how far the pane's body rows moved, `dir` rows at a time, by /// the rows of `surface` against `before`; keeps the rows that left. - pub fn measure(glide: *Glide, gpa: std.mem.Allocator, surface: *const pardes.Surface) void { - glide.pending = false; + fn measure(glide: *Glide, gpa: std.mem.Allocator, surface: *const pardes.Surface) void { const w = glide.bw(); const h = glide.body_h; const x0 = glide.rect.x + config.GUTTER; @@ -187,89 +261,7 @@ pub const Glide = struct { std.mem.copyForwards(pardes.Cell, other.*[0 .. @as(usize, other_n.* - dropped) * w], other.*[@as(usize, dropped) * w ..][0 .. @as(usize, other_n.* - dropped) * w]); other_n.* -= dropped; // An input: the core's rows moved by as many (down positive). - glide.follow.kick(@as(f32, @floatFromInt(shift)) * @as(f32, @floatFromInt(glide.dir)), glide.now_ns, glide.pace(glide.motion)); - } - - /// Finds the text whose view moved since the last loop step, however - /// it moved (the wheel, a key, a Look), and snapshots its body as the - /// last frame drew it (`core.surface`), so the frame about to be drawn - /// can measure the move (`measure`); moves the glide on to now. A - /// terminal's view glides only under the wheel (`wheeled`: its output - /// scrolls it at every line, and that is no glide); not one the shell - /// moves by the pixel (`direct`). - pub fn step(glide: *Glide, core: *pardes.Pardes, gpa: std.mem.Allocator, wheeled: ?usize, direct: ?usize) void { - glide.now_ns = core.now_ns; - glide.motion = pardes.animation.Motion.of(core.settings.motion); - if (glide.follow.moving()) { - glide.advance(core.now_ns, glide.motion); - core.needs_frame = true; - if (glide.follow.offset == 0) { - glide.nabove = 0; - glide.nbelow = 0; - glide.lines_moved = 0; - glide.rows_moved = 0; - } - } - // ponytail: one glide at a time, the focused pane's, else the one that - // moved most; a second pane scrolled in the same step lands. A glide a - // pane (and a scissor each) if two ever need to move together. - // The view that moved: the focused pane's, else the one that moved - // most; not one showing another file now. - const s = &core.surface; - var pick: ?usize = null; - var delta: i64 = 0; - for (core.panes, 0..) |slot, id| { - const pane = slot orelse { - glide.seen[id] = .{}; - continue; - }; - const line: i64 = if (pane.file) |f| @intCast(f.scroll) else if (pane.isTerminal()) pardes.panes.terminal.gridOffset(pane) else 0; - const file: u64 = if (pane.file) |f| std.hash.Wyhash.hash(0, f.path) else 0; - const revision: u32 = if (pane.file) |f| f.revision else 0; - const was = glide.seen[id]; - glide.seen[id] = .{ .serial = pane.serial, .line = line, .file = file, .revision = revision }; - if (was.serial != pane.serial or was.file != file or was.line == line or pane.collapsed or pane.hasPdf()) continue; - // Generated text rewritten as its view moved (a query pane's live - // rows, a +Search refilled, Callstack): the rows are another - // text's, not this one's moved; it lands. - if (pane.file) |f| if (f.output != null and revision != was.revision) continue; - if (pane.file == null and wheeled != id) continue; - if (direct == id) continue; - const moved = line - was.line; - if (pick) |best| if (best == core.active or (id != core.active and @abs(moved) <= @abs(delta))) continue; - pick = id; - delta = moved; - } - const id = pick orelse return; - const pane = core.panes[id].?; - const r = core.rects[id]; - if (r.h <= pane.tag_rows or r.w <= config.GUTTER) return; - const body_y = core.bodyTop(pane, r); - const body_h = r.h -| pane.tag_rows; - const w = r.w - config.GUTTER; - const x0 = r.x + config.GUTTER; - // The last frame drew this body where it is now: a pane that moved or - // was resized in the same step has no picture to glide from. - const drawn = for (s.regionList()) |region| { - if (region.kind == .body and region.owner == id and region.serial == pane.serial) break std.meta.eql(region.rect, pardes.Rect{ .x = x0, .y = body_y, .w = w, .h = body_h }); - } else false; - // A new pane or place: what was kept belongs to another picture. - if (glide.pane != id or !std.meta.eql(glide.rect, r) or glide.body_y != body_y or glide.body_h != body_h or glide.pending) glide.stop(); - glide.pane = id; - glide.rect = r; - glide.body_y = body_y; - glide.body_h = body_h; - if (glide.motion.instant or !drawn or x0 + w > s.cols or body_y + body_h > s.rows or s.cells.len == 0) return glide.stop(); - // The body as the last frame drew it, which the move is measured from. - const n = @as(usize, w) * body_h; - if (glide.before.len < n) glide.before = gpa.realloc(glide.before, n) catch &.{}; - if (glide.before.len < n) return glide.stop(); - for (0..body_h) |row| @memcpy(glide.before[row * w ..][0..w], s.cells[(@as(usize, body_y) + row) * s.cols + x0 ..][0..w]); - glide.pending = true; - glide.caret_row = if (s.cursor) |c| c.y else null; - glide.dir = @intCast(std.math.sign(delta)); - glide.pending_lines += @floatFromInt(@abs(delta)); - core.needs_frame = true; + glide.follow.kick(@as(f32, @floatFromInt(shift)) * @as(f32, @floatFromInt(glide.dir)), glide.now_ns, glide.pace()); } pub fn ensure(glide: *Glide, gpa: std.mem.Allocator) bool { @@ -290,3 +282,144 @@ pub fn sameRow(a: []const pardes.Cell, b: []const pardes.Cell) bool { } return true; } + +/// A frame's length at 60 Hz, what a tick of the follow is clamped to. +const frame_ns: u64 = 16 * std.time.ns_per_ms; + +/// A view that follows its input (anim 9, 11): the picture's offset from +/// the core's position now (rows of text; negative, the picture still +/// shows what is above), its speed (rows a second), when it was last +/// integrated, and what has been feeding it. +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; + /// The spring's natural frequency, rad/s: arrivals land inside §8.1's + /// 220 ms. + pub const omega: f32 = 51; + + /// What a view's size makes of the rules: the speed cap, the lag cap, + /// and the offset under which it is at rest (a tenth of a pixel, in + /// rows). + pub const Pace = struct { cap: f32, limit: f32, rest: f32 }; + + pub fn pace(body: f32, rest: f32) Pace { + return .{ .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 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); + } +} |
