From a87f917559e3712d36e5809d1f457fbf78c8266e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 14:32:27 -0300 Subject: anim 4: the thin insert-mode cursor glides as the block does, a small move eases instead of landing, and the cursor rides a scroll's glide with its text --- src/animation.zig | 53 +++++++++++++++++++++++++++++++++++++++++------------ src/draw.zig | 13 +++++++------ src/gui/gui.zig | 27 ++++++++++++++++++++++++--- 3 files changed, 72 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/animation.zig b/src/animation.zig index a4887148..63a40cbf 100644 --- a/src/animation.zig +++ b/src/animation.zig @@ -952,8 +952,12 @@ test "a flavour's stretched pane lands exactly on its box, and a click finds the /// 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 -/// collapses at rest; a long jump bows (Motion.arc). A move of a cell or -/// less, or any the caller calls a snap, lands at once. +/// collapses at rest; a long jump bows (Motion.arc). A step of a cell or +/// less (a key held, typing) eases there whole, its corners together, on a +/// spring `step_pace` times stiffer still: in ~60 ms on the default, so at +/// a keyboard's repeat it trails by half a cell and never smears a caret. +/// Any move the caller calls a snap, and every move under Motion off, +/// lands at once. pub const CursorGlide = struct { /// Top left, top right, bottom right, bottom left; x then y. corners: [4][2]Spring = @splat(@splat(.{})), @@ -966,6 +970,8 @@ pub const CursorGlide = struct { pub const pace: f32 = 2.4; /// A jump longer than this, in cells, bows. pub const arc_from: f32 = 8; + /// How much stiffer a step's spring is than a jump's. + pub const step_pace: f32 = 1.5; fn cornersOf(box: Box) [4][2]f32 { return .{ .{ box.x, box.y }, .{ box.x + box.w, box.y }, .{ box.x + box.w, box.y + box.h }, .{ box.x, box.y + box.h } }; @@ -985,7 +991,7 @@ pub const CursorGlide = struct { const dx = to_c[0] - from_c[0]; const dy = to_c[1] - from_c[1]; const jump = @abs(dx) > 1.01 or @abs(dy) > 1.01; - const land = !glide.placed or snap or motion.instant or !jump; + const land = !glide.placed or snap or motion.instant; glide.from_centre = from_c; glide.target = box; glide.placed = true; @@ -993,7 +999,12 @@ pub const CursorGlide = struct { // Leading: the corner ahead of the move, on the side it goes. const ahead = (point[0] - to_c[0]) * dx + (point[1] - to_c[1]) * dy; var own = motion; - own.omega *= pace * (if (ahead > 0) 1 else motion.follow); + own.omega *= if (jump) pace * (if (ahead > 0) 1 else motion.follow) else pace * step_pace; + // A step neither winds up nor overshoots: it is a caret moving. + if (!jump) { + own.zeta = 1; + own.anticipate = 0; + } if (land) { for (corner, point) |*axis, value| axis.* = .{ .omega = own.omega, .zeta = own.zeta, .target = value, .from = value }; continue; @@ -1105,32 +1116,50 @@ pub const Blink = struct { } }; -test "a cursor snaps a cell at a time and glides a jump, smearing, bowing and landing exactly" { +test "a cursor eases a cell at a time, keeps up with typing, and glides a jump, smearing, bowing and landing exactly" { const ms = std.time.ns_per_ms; const smooth_motion = Motion.of(.smooth); var glide: CursorGlide = .{}; const cell: Box = .{ .x = 3, .y = 4, .w = 1, .h = 1 }; glide.aim(cell, 0, smooth_motion, false); try std.testing.expect(!glide.moving()); - // One cell over: lands at once. + // One cell over: eases there whole, no wider than a cell, in ~60 ms. glide.aim(.{ .x = 4, .y = 4, .w = 1, .h = 1 }, ms, smooth_motion, false); - try std.testing.expect(!glide.moving()); + try std.testing.expect(glide.moving()); + var step_t: u64 = ms; + while (glide.step(step_t)) : (step_t += ms) { + const q = glide.sample(step_t, smooth_motion); + try std.testing.expect(q[1][0] - q[0][0] <= 1.0 + 1e-3 and q[0][0] >= 3 - 1e-3 and q[0][0] <= 4 + 1e-3); + } + try std.testing.expect(step_t - ms >= 30 * ms and step_t - ms <= 90 * ms); + // At a keyboard's repeat it keeps up: about half a cell behind. + var typed: f32 = 4; + var key_t: u64 = step_t; + for (0..20) |_| { + typed += 1; + glide.aim(.{ .x = typed, .y = 4, .w = 1, .h = 1 }, key_t, smooth_motion, false); + key_t += 33 * ms; + try std.testing.expect(typed - glide.sample(key_t, smooth_motion)[0][0] <= 0.6); + } + while (glide.step(key_t)) key_t += ms; // A jump of 30 cells: glides, the leading edge ahead of the trailing one. + glide.aim(cell, key_t, smooth_motion, true); + const j0 = key_t; const far: Box = .{ .x = 34, .y = 4, .w = 1, .h = 1 }; - glide.aim(far, 2 * ms, smooth_motion, false); + glide.aim(far, j0 + 2 * ms, smooth_motion, false); try std.testing.expect(glide.moving()); - const mid = glide.sample(40 * ms, smooth_motion); + const mid = glide.sample(j0 + 40 * ms, smooth_motion); const width = mid[1][0] - mid[0][0]; try std.testing.expect(width > 1.5); // Bowed off the straight line on a long jump. try std.testing.expect(@abs(mid[0][1] - 4) > 0.05); - var t: u64 = 2 * ms; + var t: u64 = j0 + 2 * ms; while (glide.step(t)) t += ms; // Arrives within §8.1's 90-150 ms on the default, within a twentieth of // a cell (under a pixel); settles to rest just after, landing exactly. - const arrived = glide.sample(152 * ms, smooth_motion); + const arrived = glide.sample(j0 + 152 * ms, smooth_motion); try std.testing.expect(@abs(arrived[3][0] - 34) < 0.05 and @abs(arrived[1][0] - 35) < 0.05); - try std.testing.expect(t <= 220 * ms); + try std.testing.expect(t - j0 <= 220 * ms); const rest = glide.sample(t, smooth_motion); try std.testing.expectEqual([2]f32{ 34, 4 }, rest[0]); try std.testing.expectEqual([2]f32{ 35, 5 }, rest[2]); diff --git a/src/draw.zig b/src/draw.zig index 3ba5801f..92fd8205 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -211,9 +211,10 @@ fn focusedCursorBox(p: *const Pardes, s: *const Surface) ?animation.Box { } /// G3 (docs/render-pipeline.md §5.4): the focused cursor on its springs, -/// at the Motion flavour's pace, a jump gliding and a step or a keystroke in -/// insert mode landing at once; blinking unless typing. A pixel shell's: a -/// grid's cursor is the terminal's own. +/// at the Motion flavour's pace, a jump gliding and a step easing over a +/// few frames (CursorGlide.aim), the thin insert-mode caret as the block; +/// blinking unless typing. A pixel shell's: a grid's cursor is the +/// terminal's own. /// /// The springs hold the cursor where the pane's final layout puts it, and /// the quad is drawn through the pane's frame (paneFrame): while the pane @@ -237,7 +238,9 @@ fn glideCursor(p: *Pardes, s: *Surface) void { return; }; const motion = animation.Motion.of(p.settings.motion); - var snap = if (s.cursor) |cursor| cursor.bar else false; + // The thin insert-mode caret glides as the block does (a jump smears + // and bows, a step eases); only a scroll, below, lands it. + var snap = false; var frame: PaneFrame = .{}; // In a column's or the workspace's tag the cursor is not the pane's, // and does not move with it. @@ -256,8 +259,6 @@ fn glideCursor(p: *Pardes, s: *Surface) void { // The pane moved in the layout: the springs move with it (its // own cursor's; one in a column's tag stays). if (inside) p.cursor_glide.shift(origin[0] - view.origin[0], origin[1] - view.origin[1]); - // Typing never trails: insert mode lands every move. - if (pane.body.mode == .insert) snap = true; // Scrolled (a file's view, a terminal's output): the quad moves // with the text and lands, never gliding against text that // jumped (every Enter at a prompt scrolls one and moves the diff --git a/src/gui/gui.zig b/src/gui/gui.zig index be92fb18..1a56ecd0 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -10586,18 +10586,34 @@ const max_cursors = 1 + pardes.MAX_PANES + pardes.MAX_TAG_LAYERS; /// Every cursor on the frame where it is drawn, in window pixels: the grid's /// where no layer has its cell, and each layer's at that layer's pitch. A bar /// is an eighth of its cell wide, a block the whole cell. +/// Where a scroll's glide draws grid cell (col, row): its pixels down from +/// where the grid has it (the picture lags the text), and whether it is +/// still in the gliding body at all; nothing moved off a glide. +fn glideLift(g: *const Gui, col: f32, row: f32, layout: CellLayout) struct { px: f32, shown: bool } { + const glide = &g.glide; + if (glide.offset == 0) return .{ .px = 0, .shown = true }; + const r = glide.rect; + const top: f32 = @floatFromInt(glide.body_y); + const bottom = top + @as(f32, @floatFromInt(glide.body_h)); + if (col < @as(f32, @floatFromInt(r.x + config.GUTTER)) or col >= @as(f32, @floatFromInt(r.x + r.w)) or row < top or row >= bottom) return .{ .px = 0, .shown = true }; + const at = row - glide.offset; + return .{ .px = -glide.offset * layout.h, .shown = at >= top and at + 1 <= bottom + 0.5 }; +} + fn cursorBoxes(g: *const Gui, surface: *const pardes.Surface, layout: CellLayout, win_h: f32, out: *[max_cursors]CursorBox) usize { const chrome = &surface.chrome; const page = chrome.page orelse bg_default; var n: usize = 0; if (surface.cursor) |cursor| { - if (cursor.x < surface.cols and cursor.y < surface.rows and bodyLayerAt(surface, cursor.x, cursor.y) == null and tagLayerIn(surface.tagLayers(), cursor.x, cursor.y) == null) { + // A scroll's glide carries the cursor with the text it is in. + const lift = glideLift(g, @floatFromInt(cursor.x), @floatFromInt(cursor.y), layout); + if (lift.shown and cursor.x < surface.cols and cursor.y < surface.rows and bodyLayerAt(surface, cursor.x, cursor.y) == null and tagLayerIn(surface.tagLayers(), cursor.x, cursor.y) == null) { const cell = &surface.cells[@as(usize, cursor.y) * surface.cols + cursor.x]; const role = cellFontRole(cell); const width: f32 = if (role == .tagline) @floatFromInt(g.tagline_width) else layout.w; const height = std.math.clamp(if (role == .tagline) @as(f32, @floatFromInt(g.tagline_height)) else layout.h, 1.0, layout.h); const x0 = @as(f32, @floatFromInt(cursor.x)) * layout.w; - const y0 = @as(f32, @floatFromInt(cursor.y)) * layout.h + @as(f32, @floatFromInt(taglineBandOffset(cursor.y, win_h, @intFromFloat(layout.h), @intFromFloat(height)))); + const y0 = @as(f32, @floatFromInt(cursor.y)) * layout.h + @as(f32, @floatFromInt(taglineBandOffset(cursor.y, win_h, @intFromFloat(layout.h), @intFromFloat(height)))) + lift.px; out[n] = .{ .x0 = x0, .y0 = y0, .x1 = x0 + if (cursor.bar) @max(dp(1), width / 8.0) else width, .y1 = y0 + height, .rgb = cursorColor(chrome, cell, role), .text = page, .bar = cursor.bar }; n += 1; } @@ -10670,8 +10686,13 @@ fn appendCursorGlide(g: *Gui, surface: *const pardes.Surface, builder: *OverlayB const layout = fixedCellLayout(g); var px: [4][2]f32 = undefined; var centre: [2]f32 = .{ 0, 0 }; + // A scroll's glide carries the quad with the text, by where its centre is. + var cell_centre: [2]f32 = .{ 0, 0 }; + for (0..4) |i| cell_centre = .{ cell_centre[0] + chrome.cursor_quad[i * 2] / 4, cell_centre[1] + chrome.cursor_quad[i * 2 + 1] / 4 }; + const lift = glideLift(g, cell_centre[0], cell_centre[1], layout); + if (!lift.shown) return; for (&px, 0..) |*point, i| { - point.* = .{ chrome.cursor_quad[i * 2] * layout.w, chrome.cursor_quad[i * 2 + 1] * layout.h }; + point.* = .{ chrome.cursor_quad[i * 2] * layout.w, chrome.cursor_quad[i * 2 + 1] * layout.h + lift.px }; centre[0] += point[0] / 4; centre[1] += point[1] / 4; } -- cgit v1.3