diff options
Diffstat (limited to 'src/gui')
| -rw-r--r-- | src/gui/gui.zig | 164 |
1 files changed, 110 insertions, 54 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 343ad991..8d69ed80 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -999,6 +999,12 @@ const Gui = struct { trail: JumpTrail = .{}, /// The caret's ease, on the window's own clock (Caret). caret: Caret = .{}, + /// The window's clock for the frame being drawn (Shell.ownNow), and + /// what moved the glide's view since the last (stepGlide): the pane a + /// wheel scrolled, one a touchpad moves by the pixel. + now_ns: u64 = 0, + glide_wheeled: ?usize = null, + glide_direct: ?usize = null, /// The last key, click or wheel, which the blink holds for. input_ns: u64 = 0, live_ctrl: bool = false, @@ -2214,7 +2220,7 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ } } -const frame_ms: c_int = @intCast(pardes.animation.frame_ns / std.time.ns_per_ms); +const frame_ms: c_int = @intCast(pardes.frame_ns / std.time.ns_per_ms); const StdinFeed = struct { parser: vaxis.Parser = .{}, @@ -3253,6 +3259,12 @@ const Shell = struct { s.closeTheWindow(); } + /// The window's own clock: the test's virtual one, the paced tick, else + /// SDL's. + fn ownNow(s: *const Shell) u64 { + return s.test_clock orelse s.paced_ns orelse c.SDL_GetTicksNS(); + } + /// The Gui holds the window of the core's live view. fn follow(s: *Shell) void { const g = s.gui orelse return; @@ -3646,7 +3658,7 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // a present to retry. A minimized window sleeps through the core's animation // too: nothing shows it, and it is caught up at once when the window // is back. - const polls = g.scroll_lag != 0 or g.scroll_delta != 0 or g.scroll_notch != 0 or g.glide.moving() or g.gamepad != null or s.test_mode or core.needs_frame; + const polls = g.scroll_lag != 0 or g.scroll_delta != 0 or g.scroll_notch != 0 or frontendMoving(g) or g.gamepad != null or s.test_mode or core.needs_frame; const minimized = !s.test_mode and c.SDL_GetWindowFlags(g.window) & (c.SDL_WINDOW_MINIMIZED | c.SDL_WINDOW_OCCLUDED) != 0; // A latency trace is fed on stdin, whose poll below paces the loop: // waiting here too would make every tick cost two frames. @@ -3674,7 +3686,7 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // phase is our own, not the display's (no vblank time to read on the // soft present); a 59.94 Hz display drifts a tick every 17 s. The // upgrade: the presentation feedback protocol's timestamps. - const paced = s.test_clock == null and latency_fd < 0 and !s.test_mode and !minimized and core.continuous(); + const paced = s.test_clock == null and latency_fd < 0 and !s.test_mode and !minimized and frontendMoving(g); var tick: u64 = 0; if (paced) { const period = refreshNs(g); @@ -3704,7 +3716,7 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // frame with the new alpha, no core render (§5.5 level B). if (blink_ms != null and (blink.?.edge or !got)) g.blink_due = true; if (s.test_clock) |*virtual| { - if (core.continuous()) { + if (frontendMoving(g)) { const display = virtual.* + std.time.ns_per_s / s.test_hz; virtual.* = if (core.nextWake()) |due| @max(virtual.*, @min(due, display)) else display; } else if (timeout_ms != 0 and (!got or !core.needs_frame)) { @@ -3868,9 +3880,11 @@ fn pollFrame(ctx: ?*anyopaque) void { if (updateCoreResize(core, geom.cols, geom.rows, g.cell_w, g.cell_h, g.tagline_width, g.tagline_height)) resetScroll(g); stepScroll(g, core, s.gpa); if (s.test_mode) writeTestStatus(g, core, s.presented); - // A blink edge: the last frame again, the cursor's alpha moved on. - if (g.blink_due and !core.needs_frame and s.presented) if (s.surface) |frame| { + // A blink edge, or the window's own motion (its glide, its caret, a + // jump's trail): the last frame again, moved on, no core render. + if ((g.blink_due or frontendMoving(g)) and !core.needs_frame and s.presented) if (s.surface) |frame| { g.blink_due = false; + g.now_ns = s.ownNow(); s.presented = renderFrame(g, s.gpa, core, frame) catch false; }; } @@ -3975,20 +3989,65 @@ fn runDemo(g: *Gui, in: *Input, core: *pardes.Pardes) void { /// The blink now, from the frame's time since input and how long ago it /// arrived; null where nothing blinks (off, idle, an unfocused window). -fn blinkNow(g: *const Gui, on: bool) ?pardes.animation.Blink { +/// The cursor's opacity under blink (§5.4): solid while typing and for +/// `hold_ns` after, then on and off with an eased edge, solid again after +/// `idle_ns` of no input. `edge` says whether it is mid-edge (a frame is +/// owed), `next_ns` when the next edge starts (a wake). +pub const Blink = struct { + pub const hold_ns: u64 = 500 * std.time.ns_per_ms; + pub const idle_ns: u64 = 10 * std.time.ns_per_s; + pub const half_ns: u64 = 530 * std.time.ns_per_ms; + pub const edge_ns: u64 = 80 * std.time.ns_per_ms; + + alpha: f32, + edge: bool, + next_ns: ?u64, + + pub fn at(now_ns: u64, input_ns: u64) Blink { + const since = now_ns -| input_ns; + if (since < hold_ns) return .{ .alpha = 1, .edge = false, .next_ns = input_ns + hold_ns + half_ns - edge_ns }; + if (since >= hold_ns + idle_ns) return .{ .alpha = 1, .edge = false, .next_ns = null }; + const t = (since - hold_ns) % (2 * half_ns); + const cycle_start = now_ns - t; + // On for a half, easing out over its last `edge_ns`; off for a half, + // easing back in over its last. + if (t < half_ns - edge_ns) return .{ .alpha = 1, .edge = false, .next_ns = cycle_start + half_ns - edge_ns }; + if (t < half_ns) return .{ .alpha = 1 - smooth(@as(f32, @floatFromInt(t - (half_ns - edge_ns))) / @as(f32, @floatFromInt(edge_ns))), .edge = true, .next_ns = null }; + if (t < 2 * half_ns - edge_ns) return .{ .alpha = 0, .edge = false, .next_ns = cycle_start + 2 * half_ns - edge_ns }; + return .{ .alpha = smooth(@as(f32, @floatFromInt(t - (2 * half_ns - edge_ns))) / @as(f32, @floatFromInt(edge_ns))), .edge = true, .next_ns = null }; + } + + fn smooth(x: f32) f32 { + const u = std.math.clamp(x, 0, 1); + return u * u * (3 - 2 * u); + } +}; + +test "a blink holds while typing, eases at its edges and stops when idle" { + const ms = std.time.ns_per_ms; + try std.testing.expectEqual(@as(f32, 1), Blink.at(100 * ms, 0).alpha); + const out = Blink.at(Blink.hold_ns + Blink.half_ns + 100 * ms, 0); + try std.testing.expectEqual(@as(f32, 0), out.alpha); + const edge = Blink.at(Blink.hold_ns + Blink.half_ns - Blink.edge_ns / 2, 0); + try std.testing.expect(edge.edge and edge.alpha > 0.2 and edge.alpha < 0.8); + try std.testing.expectEqual(@as(?u64, null), Blink.at(Blink.hold_ns + Blink.idle_ns + ms, 0).next_ns); + try std.testing.expectEqual(@as(f32, 1), Blink.at(Blink.hold_ns + Blink.idle_ns + ms, 0).alpha); +} + +fn blinkNow(g: *const Gui, on: bool) ?Blink { if (g.no_blink or !on or !g.caret.placed) return null; if (c.SDL_GetWindowFlags(g.window) & c.SDL_WINDOW_INPUT_FOCUS == 0) return null; const since = monoNs() -| g.input_ns; // Relative to now: `next_ns` is how long until the next edge. - const b = pardes.animation.Blink.at(since, 0); + const b = Blink.at(since, 0); return .{ .alpha = b.alpha, .edge = b.edge, .next_ns = if (b.next_ns) |next| next -| since else null }; } /// One refresh of the display the window is on, in ns; a frame of the /// core's where the display does not say. fn refreshNs(g: *const Gui) u64 { - const mode = c.SDL_GetCurrentDisplayMode(c.SDL_GetDisplayForWindow(g.window)) orelse return pardes.animation.frame_ns; - if (!(mode.*.refresh_rate > 1)) return pardes.animation.frame_ns; + const mode = c.SDL_GetCurrentDisplayMode(c.SDL_GetDisplayForWindow(g.window)) orelse return pardes.frame_ns; + if (!(mode.*.refresh_rate > 1)) return pardes.frame_ns; return @intFromFloat(@as(f64, std.time.ns_per_s) / mode.*.refresh_rate); } @@ -4045,6 +4104,7 @@ fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void { const frame = surface; s.surface = frame; g.frame_arrival_ns = monoNs(); + g.now_ns = s.ownNow(); s.presented = renderFrame(g, s.gpa, core, frame) catch |err| blk: { log.err("render: {t}", .{err}); break :blk false; @@ -4559,13 +4619,10 @@ test "a wheel over codex in a terminal reaches its pty as the mouse reports it a const Glide = pardes.glide.Glide; -/// Feeds a wheel's notches to the core, then steps the glide (Glide.step) -/// and asks for frames while it moves. +/// Feeds a wheel's notches to the core, and keeps what moved the view for +/// the frame's glide (Glide.frame). fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { - const glide = &g.glide; - // The glide runs on the core's continuous path: frames every loop, and - // a virtual clock carried on. - defer core.shell_continuous = glide.moving() or g.trail.active or g.caret.moving(); + _ = gpa; const notch = g.scroll_notch; g.scroll_notch = 0; var wheeled: ?usize = null; @@ -4592,9 +4649,22 @@ fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { } if (wheeled != null) core.needs_frame = true; } + if (wheeled != null) g.glide_wheeled = wheeled; // Not a view a touchpad is moving by the pixel. - const direct: ?usize = if (g.scroll_lag != 0 or g.scroll_delta != 0) g.scroll_pane else null; - glide.step(core, gpa, wheeled, direct); + g.glide_direct = if (g.scroll_lag != 0 or g.scroll_delta != 0) g.scroll_pane else null; +} + +/// The window's own motions under way (its glide, its caret, a jump's +/// trail): it draws their frames itself, on its own clock. +fn frontendMoving(g: *const Gui) bool { + return g.glide.moving() or g.caret.moving() or g.trail.active; +} + +/// The frame's glide (Glide.frame) on the window's clock, with what moved +/// its view since the last. +fn glideFrame(g: *Gui, gpa: std.mem.Allocator, surface: *const pardes.Surface) void { + g.glide.frame(gpa, surface, g.now_ns, g.glide_wheeled, g.glide_direct); + g.glide_wheeled = null; } test "a notch scrolls the core at once and the picture glides after it, a wrapped line's rows at a time" { @@ -4610,7 +4680,6 @@ test "a notch scrolls the core at once and the picture glides after it, a wrappe try text.append(gpa, '\n'); } _ = try core.setTestFile(text.items); - core.settings.motion = .smooth; var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); const ms = std.time.ns_per_ms; @@ -4626,19 +4695,19 @@ test "a notch scrolls the core at once and the picture glides after it, a wrappe const body_y = core.bodyTop(core.panes[core.active].?, r); const row_before = try gpa.dupe(pardes.Cell, s.cells[@as(usize, body_y + 1) * s.cols + r.x + config.GUTTER ..][0 .. r.w - config.GUTTER]); defer gpa.free(row_before); - // The loop has seen the view once (stepGlide finds a view that moved). - stepGlide(&g, core, gpa); + // The window has drawn the view once (Glide.frame finds a view that moved). + g.now_ns = now; + glideFrame(&g, gpa, s); // One notch down: the core is a line on at once. g.scroll_notch = 1; const scrolled = core.panes[core.active].?.scroll(); stepGlide(&g, core, gpa); try std.testing.expectEqual(scrolled + 1, core.panes[core.active].?.scroll()); - try std.testing.expect(g.glide.pending); - // On the core's continuous path while it moves: frames every loop. - try std.testing.expect(core.shell_continuous and core.continuous()); _ = arena.reset(.retain_capacity); s = try core.render(arena.allocator()); - g.glide.measure(gpa, s); + glideFrame(&g, gpa, s); + // The window's own motion: it draws its frames while it moves. + try std.testing.expect(frontendMoving(&g)); // Its two rows: the picture two rows behind, the rows that left kept, // nearest first. try std.testing.expectApproxEqAbs(@as(f32, -2), g.glide.follow.offset, 1e-4); @@ -4650,17 +4719,14 @@ 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); + g.now_ns = now; + glideFrame(&g, gpa, s); 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.follow.offset); - try std.testing.expect(!core.shell_continuous); - // Motion off: a notch lands at once. - core.settings.motion = .off; - g.scroll_notch = 1; - stepGlide(&g, core, gpa); - try std.testing.expect(!g.glide.pending and !g.glide.moving()); + try std.testing.expect(!frontendMoving(&g)); } test "a wheel's caret rides with its line, the text's own glide, never a spring of its own" { @@ -4672,7 +4738,6 @@ test "a wheel's caret rides with its line, the text's own glide, never a spring 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(); @@ -4691,6 +4756,9 @@ test "a wheel's caret rides with its line, the text's own glide, never a spring g.scroll_col = r.x + 10; g.scroll_row = r.y + r.h - 2; stepGlide(&g, core, gpa); + // The window has drawn the view once (Glide.frame finds a view that moved). + g.now_ns = now; + glideFrame(&g, gpa, s); const before: f32 = @floatFromInt(s.cursor.?.y); g.scroll_notch = 1; stepGlide(&g, core, gpa); @@ -4700,7 +4768,8 @@ test "a wheel's caret rides with its line, the text's own glide, never a spring core.advance(now); _ = arena.reset(.retain_capacity); s = try core.render(arena.allocator()); - if (g.glide.pending) g.glide.measure(gpa, s); + g.now_ns = now; + glideFrame(&g, gpa, s); seeCaret(&g, s, fixedCellLayout(&g), 480, now); if (frames == 0) { try std.testing.expect(g.glide.caret_rides); @@ -4738,8 +4807,8 @@ const GlideRun = struct { stepGlide(g, core, gpa); _ = arena.reset(.retain_capacity); 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); + g.now_ns = now; + glideFrame(g, gpa, s); seeCaret(g, s, fixedCellLayout(g), @floatFromInt(@as(u32, s.rows) * g.cell_h), now); seq.offsets[frame] = g.glide.follow.offset; seq.lines[frame] = @intCast(core.panes[core.active].?.file.?.scroll); @@ -4772,7 +4841,7 @@ const GlideRun = struct { } }; -test "every scroll glides on one law: a half page, a page, the far jumps, a run of them retargeting, and Motion off lands at once" { +test "every scroll glides on one law: a half page, a page, the far jumps, a run of them retargeting" { const gpa = std.testing.allocator; const core = try pardes.Pardes.init(gpa, .{ .cols = 100, .rows = 40 }); defer core.deinit(); @@ -4781,7 +4850,6 @@ test "every scroll glides on one law: a half page, a page, the far jumps, a run // Numbered lines no two alike, short enough never to wrap. for (0..3000) |i| try text.print(gpa, "{d:0>5} line {d} of the long file\n", .{ i, i * 7 % 13 }); _ = try core.setTestFile(text.items); - core.settings.motion = .smooth; var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); core.advance(std.time.ns_per_s); @@ -4837,12 +4905,6 @@ test "every scroll glides on one law: a half page, a page, the far jumps, a run // done as soon after the last as one half page alone takes. for (160..180) |k| try std.testing.expect(seq.offsets[k] != 0); try std.testing.expect(seq.settle(175) <= seq.settle(2) + 4); - // Motion off: the same scroll lands at once. - core.settings.motion = .off; - var off: GlideRun = .{}; - try off.play(&g, core, gpa, &arena, &.{.{ .frame = 1, .key = ctrl.key('u') }}, 10); - try std.testing.expect(off.lines[1] != off.lines[0]); - for (off.offsets[0..off.frames]) |offset| try std.testing.expectEqual(@as(f32, 0), offset); } test "a held key moves the view at the stream's own rate, never a bounce a line, and settles once it stops" { @@ -4853,7 +4915,6 @@ test "a held key moves the view at the stream's own rate, never a bounce a line, defer text.deinit(gpa); for (0..3000) |i| try text.print(gpa, "{d:0>5} line {d} of the long file\n", .{ i, i * 7 % 13 }); _ = try core.setTestFile(text.items); - core.settings.motion = .smooth; var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); core.advance(std.time.ns_per_s); @@ -5418,10 +5479,9 @@ fn renderFrame( const chrome = &surface.chrome; // G6: what the scroll just done moved the pane's rows by, measured on // this frame against the last (the glide follows it from there). - if (g.glide.pending) g.glide.measure(gpa, surface); - if (g.glide.moving()) g.glide.advance(g.glide.now_ns, g.glide.motion); + glideFrame(g, gpa, surface); var groups: Groups = .{}; - const now = monoNs(); + const now = g.now_ns; seeCaret(g, surface, layout, win_h, now); // While the cursor glides or blinks the cells' cursors are not drawn; // the focused one is the overlay's quad. One or the other every frame @@ -5435,17 +5495,13 @@ fn renderFrame( // stay legible; solid again as it lands. const caret_alpha: f32 = if (gliding) 0.85 + 0.15 * std.math.clamp(1 - g.caret.remaining(now) / 0.5, 0, 1) else 1; g.cursor_quad_alpha = if (quad_mode) caret_alpha * blink_alpha else 0; - // A gliding caret asks for the frames it glides through. - if (gliding) core.shell_continuous = true; // The probe's view of this frame's motion (FrameStats.Frame). // A focused PDF's strip is the core's (pdf_view.draw). const pdf_focus = if (core.panes[core.active]) |pane| pane.hasPdf() else false; if (!pdf_focus) core.frames.scroll = g.glide.follow.offset; core.frames.caret = caretRow(g, surface, now) orelse 0; const cursors_shown = !quad_mode; - var overlay_count = buildOverlay(g, core.settings.pet, core.settings.jump_trail and core.settings.motion != .off, surface, now, sw, sh); - // A trail fading asks for the frames it fades through. - if (g.trail.active) core.shell_continuous = true; + var overlay_count = buildOverlay(g, core.settings.pet, core.settings.jump_trail, surface, now, sw, sh); if (overlay_count != 0 and !uploadOverlayGpu(g, cmd, overlay_count)) overlay_count = 0; if (!prepareNativeImages(g, gpa, cmd, surface, &groups, sw, sh)) { @@ -7361,11 +7417,11 @@ var test_status_step: u64 = 0; fn writeTestStatus(g: *const Gui, core: *const pardes.Pardes, presented: bool) void { test_status_step +%= 1; if (g.capture_dir.len == 0) return; - const idle = presented and !core.needs_frame and !core.continuous() and core.nextWake() == null and + const idle = presented and !core.needs_frame and !frontendMoving(g) and core.nextWake() == null and !g.glide.moving() and g.scroll_lag == 0 and g.scroll_delta == 0 and g.scroll_notch == 0; var line_buf: [64]u8 = undefined; // What keeps it busy, after: owes a frame, moves, waits, not shown. - const line = std.fmt.bufPrint(&line_buf, "{d} {d} {d} {d}{d}{d}{d}\n", .{ test_status_step, g.captured, @intFromBool(idle), @intFromBool(core.needs_frame), @intFromBool(core.continuous()), @intFromBool(core.nextWake() != null), @intFromBool(!presented) }) catch return; + const line = std.fmt.bufPrint(&line_buf, "{d} {d} {d} {d}{d}{d}{d}\n", .{ test_status_step, g.captured, @intFromBool(idle), @intFromBool(core.needs_frame), @intFromBool(frontendMoving(g)), @intFromBool(core.nextWake() != null), @intFromBool(!presented) }) catch return; var tmp_buf: [4096]u8 = undefined; var final_buf: [4096]u8 = undefined; const tmp_path = std.fmt.bufPrintSentinel(&tmp_buf, "{s}/status.tmp", .{g.capture_dir}, 0) catch return; |
