diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/animation.zig | 14 | ||||
| -rw-r--r-- | src/draw.zig | 36 | ||||
| -rw-r--r-- | src/gui/gui.zig | 83 |
3 files changed, 99 insertions, 34 deletions
diff --git a/src/animation.zig b/src/animation.zig index 764aa0b3..37d74813 100644 --- a/src/animation.zig +++ b/src/animation.zig @@ -1125,9 +1125,9 @@ pub const CursorGlide = struct { /// Heads for `box` at `now_ns`. /// How a new mark is gone to: eased (a step, or a jump's smear and - /// bow), landed at once, or moved whole at a scroll's pace (the text - /// moved under it, not the caret). - pub const How = enum { ease, land, rigid }; + /// bow), or landed at once (a scroll: the text moved under it, not the + /// caret; a shell carries it with the text's own glide). + pub const How = enum { ease, land }; pub fn aim(glide: *CursorGlide, box: Box, now_ns: u64, motion: Motion, how: How) void { // The same mark: nothing to do, unless a snap lands a glide under way. @@ -1152,11 +1152,9 @@ 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 *= if (how == .rigid) 1.5 else 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; - // nor a whole move with the text (rigid: at the view's pace, - // Glide's 1.5x, its corners together). - if (!jump or how == .rigid) { + 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; } diff --git a/src/draw.zig b/src/draw.zig index e3ab9f2a..f4c43631 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -239,7 +239,7 @@ fn glideCursor(p: *Pardes, s: *Surface) void { }; const motion = animation.Motion.of(p.settings.motion); // The thin insert-mode caret glides as the block does (a jump smears - // and bows, a step eases); a scroll moves it rigidly, below. + // and bows, a step eases); a scroll lands it, below. var how: animation.CursorGlide.How = .ease; var frame: PaneFrame = .{}; // In a column's or the workspace's tag the cursor is not the pane's, @@ -259,14 +259,13 @@ 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]); - // Scrolled (a file's view, a terminal's output): the caret is its - // own body, drawn from its own springs only, never from the text's - // glide. Its mark is where it will be once the view settles (its - // cell now: the core's view is that one), so a page that keeps its - // row leaves it still, and a scroll that carries its line moves - // it there whole, at the view's pace, not as a jump's smear. A - // terminal's output scrolls it with no glide: it lands with it. - if (inside and line != view.line) how = if (pane.isTerminal()) .land else .rigid; + // Scrolled (a file's view, a terminal's output): the caret did not + // move, the text did. Its springs land on its cell (the core's + // view is the settled one), so a page that keeps its row leaves + // it still, and a scroll that carries its line has the shell draw + // it with the text's own glide (Glide.caret_rides), one picture, + // never a second spring chasing the first. + if (inside and line != view.line) how = .land; } else if (view.serial != 0 and p.cursor_glide.placed) { // Focus moved (to another pane, or between a pane and a // column's or the workspace's tag): the glide starts where the @@ -1971,7 +1970,7 @@ test "each Motion flavour reaches the lift as its own trajectory" { try std.testing.expect(peaks[2] <= 1.001 and peaks[3] > 1.25 and peaks[4] > peaks[3]); } -test "the caret is its own body: a page leaves it still under the moving text, a wheel scroll moves it whole to its line's new row" { +test "the caret is its own body: a page leaves it still under the moving text, a wheel scroll lands its springs on its line's new row" { if (!builtins.capabilities.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 30 }); defer p.deinit(); @@ -2003,23 +2002,16 @@ test "the caret is its own body: a page leaves it still under the moving text, a try std.testing.expectEqual(row_before, s.cursor.?.y); try std.testing.expectEqual(@as(u8, 0), s.chrome.cursor_glide & 1); try std.testing.expectEqual(@as(f32, @floatFromInt(row_before)), s.chrome.cursor_quad[1]); - // The view scrolled with the caret on its line: it heads for the - // line's new row whole (every corner the same way), and lands on it. + // The view scrolled with the caret on its line: its springs land on + // the line's new row at once, no second glide; the shell carries it + // there with the text's own (Glide.caret_rides). pane.scrollBy(-3); now += 16 * std.time.ns_per_ms; p.advance(now); _ = arena.reset(.retain_capacity); s = try p.render(arena.allocator()); - const mark: f32 = @floatFromInt(s.cursor.?.y); - try std.testing.expect(s.chrome.cursor_glide & 1 != 0 and s.chrome.cursor_quad[1] < mark); - try std.testing.expectApproxEqAbs(s.chrome.cursor_quad[3] - s.chrome.cursor_quad[1], s.chrome.cursor_quad[5] - s.chrome.cursor_quad[7], 1e-4); - while (p.continuous()) { - now += 7 * std.time.ns_per_ms; - p.advance(now); - _ = arena.reset(.retain_capacity); - s = try p.render(arena.allocator()); - } - try std.testing.expectEqual(mark, s.chrome.cursor_quad[1]); + try std.testing.expectEqual(@as(u8, 0), s.chrome.cursor_glide & 1); + try std.testing.expectEqual(@as(f32, @floatFromInt(s.cursor.?.y)), s.chrome.cursor_quad[1]); } test "the focused cursor glides a jump, eases a step, the thin caret too, and is exact at rest" { diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 71676194..f0802362 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -5217,6 +5217,62 @@ test "a notch scrolls the core at once and the picture glides after it, a wrappe try std.testing.expect(!g.glide.pending and !g.glide.moving()); } +test "a wheel's caret rides with its line, the text's own glide, never a spring of its own" { + if (!pardes.builtins.capabilities.lift) return error.SkipZigTest; + const gpa = std.testing.allocator; + const core = try pardes.Pardes.init(gpa, .{ .cols = 80, .rows = 24 }); + defer core.deinit(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + for (0..200) |i| try text.print(gpa, "line {d}\n", .{i}); + _ = try core.setTestFile(text.items); + core.settings.motion = .smooth; + core.settings.cursor_blink = false; + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + var now: u64 = std.time.ns_per_s; + core.advance(now); + for ("10G") |ch| core.update(.{ .key = .{ .cp = ch, .text = &.{ch} } }); + _ = try core.render(arena.allocator()); + now += std.time.ns_per_s; + core.advance(now); + _ = arena.reset(.retain_capacity); + var s = try core.render(arena.allocator()); + var g: Gui = undefined; + g.glide = .{}; + defer g.glide.deinit(gpa); + g.scroll_delta = 0; + g.scroll_lag = 0; + g.scroll_edge_len = 0; + const r = core.rects[core.active]; + g.scroll_pane = core.active; + g.scroll_col = r.x + 10; + g.scroll_row = r.y + r.h - 2; + stepGlide(&g, core, gpa); + const before: f32 = @floatFromInt(s.cursor.?.y); + g.scroll_notch = 1; + stepGlide(&g, core, gpa); + var frames: usize = 0; + while (true) : (frames += 1) { + now += 7 * std.time.ns_per_ms; + core.advance(now); + _ = arena.reset(.retain_capacity); + s = try core.render(arena.allocator()); + if (g.glide.pending) g.glide.measure(gpa, s); + if (frames == 0) { + try std.testing.expect(g.glide.caret_rides); + // Where its line is drawn: on the row it was on, the cell a row up. + try std.testing.expectApproxEqAbs(before, caretRow(&g, s).?, 1e-3); + } + // Always exactly with its line, the cell less the text's offset. + try std.testing.expectApproxEqAbs(@as(f32, @floatFromInt(s.cursor.?.y)) - g.glide.follow.offset, caretRow(&g, s).?, 1e-3); + if (!g.glide.moving()) break; + stepGlide(&g, core, gpa); + if (frames > 200) return error.NeverSettled; + } + try std.testing.expectEqual(@as(f32, @floatFromInt(s.cursor.?.y)), caretRow(&g, s).?); +} + /// The scripted scroll sequence of the glide tests: a frame a 60 Hz /// refresh, as the paced loop draws them (waitInput, then stepGlide, the /// core's render and the frame's measure), an input on the frames `script` @@ -10435,13 +10491,25 @@ const max_cursors = 1 + pardes.MAX_PANES + pardes.MAX_TAG_LAYERS; /// Where the frame draws the focused caret, its top in grid rows: from its /// own springs (the quad's, while it moves or while a glide moves the text) -/// or its cell; never from the text's glide. The probe records it and the -/// glide tests hold its invariants. +/// or its cell; carried by the text's glide when its line rode it. The probe +/// records it and the glide tests hold its invariants. 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.follow.offset != 0); - return if (quad) chrome.cursor_quad[1] else @floatFromInt(cursor.y); + return if (quad) chrome.cursor_quad[1] - caretRide(g, surface) else @floatFromInt(cursor.y); +} + +/// The rows a caret whose line rode the wheel's glide is drawn above its +/// cell: the text's own offset, so it moves with its line as a glyph does +/// (its springs landed on the cell, draw.glideCursor). +fn caretRide(g: *const Gui, surface: *const pardes.Surface) f32 { + const glide = &g.glide; + if (!glide.caret_rides or glide.follow.offset == 0) return 0; + const cu = surface.cursor orelse return 0; + const x0 = glide.rect.x + config.GUTTER; + if (cu.x < x0 or cu.x >= x0 + glide.bw() or cu.y < glide.body_y or cu.y >= glide.body_y + glide.body_h) return 0; + return glide.follow.offset; } /// Every cursor on the frame where it is drawn, in window pixels: the grid's @@ -10529,10 +10597,17 @@ fn appendCursorGlide(g: *Gui, surface: *const pardes.Surface, builder: *OverlayB const chrome = &surface.chrome; if (g.cursor_quad_alpha <= 0.001) return; const layout = fixedCellLayout(g); + const ride = caretRide(g, surface); + if (ride != 0) { + // Carried past the body's edge with its line: clipped as the text is. + const mid = chrome.cursor_quad[1] - ride + 0.5; + const top: f32 = @floatFromInt(g.glide.body_y); + if (mid < top or mid > top + @as(f32, @floatFromInt(g.glide.body_h))) return; + } var px: [4][2]f32 = undefined; var centre: [2]f32 = .{ 0, 0 }; 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] - ride) * layout.h }; centre[0] += point[0] / 4; centre[1] += point[1] / 4; } |
