diff options
Diffstat (limited to 'src/replica.zig')
| -rw-r--r-- | src/replica.zig | 149 |
1 files changed, 148 insertions, 1 deletions
diff --git a/src/replica.zig b/src/replica.zig index 155492c3..95ccb58f 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -600,6 +600,9 @@ pub const Replica = struct { /// Its predicted echo (src/echo.zig), drawn over its core's terminals: /// fed by its keys, its units and its deltas here, and by `echoFrame`. echo: pardes.echo.Echo = .{}, + /// The frontend's cursor and focus springs, never rewound by a copy. + motion: pardes.views.RenderMotion = .{}, + frame_input_ns: ?u64 = null, const ScreenOf = struct { serial: u32, @@ -659,6 +662,9 @@ pub const Replica = struct { pardes.dump.terminal_gpa = terminal.emulator_gpa; defer pardes.dump.terminal_gpa = null; const c = try pardes.dump.load(epoch, o, bytes, null); + // Chrome animation is local drawing state, omitted from a join's + // dump. Its initial colours must follow the restored theme too. + c.chrome_animation.snap(pardes.colors.ChromeTheme.fromTheme(c.theme())); // What loading queued (a watch, a theme asked for) is the // authority's errand, done there already: the session was dumped // between steps, with nothing queued. @@ -707,6 +713,7 @@ pub const Replica = struct { /// The replica's own input: applied at once when it can be, and the /// message for the authority returned (valid until the next call). pub fn input(r: *Replica, ev: pardes.Event) ![]const u8 { + if (ev == .key or ev == .paste or (ev == .mouse and ev.mouse.kind != .motion)) r.frame_input_ns = nowNs(); r.stats.inputs += 1; const seq = r.next_seq; r.next_seq += 1; @@ -1041,9 +1048,20 @@ pub const Replica = struct { /// (views.drawOne): nothing the digest covers moves. The epoch's trees /// held while a copy is (a render can make or drop a syntax tree). pub fn draw(r: *Replica, present: pardes.views.Present) void { + r.drawAt(present, nowNs()); + } + + /// Only presentation reads this clock. It never advances the session + /// or sends ticks, so a quiet daemon cannot hold a cursor mid-glide. + pub fn drawAt(r: *Replica, present: pardes.views.Present, now_ns: u64) void { rollback.Epoch.select(&r.epoch); defer rollback.Epoch.select(null); - pardes.views.drawOne(r.core(), r.view, present) catch {}; + if (r.frame_input_ns == null) r.frame_input_ns = now_ns; + pardes.views.drawOneAt(r.core(), r.view, present, &r.motion, now_ns, r.frame_input_ns.?) catch {}; + } + + pub fn animating(r: *const Replica) bool { + return pardes.views.motionActive(&r.motion); } /// `f(ctx, core)` with its own view live, outside any step, as `draw` @@ -3095,6 +3113,102 @@ fn key(r: *Replica, cp: u21, sent: *std.ArrayListUnmanaged([]u8)) !void { try sent.append(std.testing.allocator, try std.testing.allocator.dupe(u8, msg)); } +test "a replica cursor lands on its local clock without another daemon event" { + if (comptime !pardes.builtins.frontend.lift) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + const present: pardes.views.Present = .{ .ctx = null, .f = null }; + const ms = std.time.ns_per_ms; + const start = nowNs(); + r.drawAt(present, start); + const before = r.core().surface.cursor.?; + const msg = try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = 'j', .text = "j" } })); + defer gpa.free(msg); + const typed = r.frame_input_ns.?; + const digest = r.digest(); + const clock = r.core().now_ns; + r.drawAt(present, typed + 16 * ms); + const target = r.core().surface.cursor.?; + try std.testing.expect(target.y > before.y); + try std.testing.expectEqual(@as(u8, 3), r.core().surface.chrome.cursor_glide); + try std.testing.expect(r.animating()); + r.drawAt(present, typed + 250 * ms); + const frame = &r.core().surface; + try std.testing.expectEqual(@as(u8, 2), frame.chrome.cursor_glide); + try std.testing.expectEqual(@as(f32, @floatFromInt(target.x)), frame.chrome.cursor_quad[0]); + try std.testing.expectEqual(@as(f32, @floatFromInt(target.y)), frame.chrome.cursor_quad[1]); + try std.testing.expectEqual(@as(u32, 250), frame.chrome.cursor_idle_ms); + try std.testing.expectEqual(clock, r.core().now_ns); + try std.testing.expectEqual(digest, r.digest()); + try std.testing.expect(!r.animating()); + r.confirm(try a.take(msg)); + r.settle(); + r.drawAt(present, typed + 500 * ms); + try std.testing.expectEqual(@as(u32, 500), r.core().surface.chrome.cursor_idle_ms); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "a replica cursor keeps its drawn position across rollback and another live view" { + if (comptime !pardes.builtins.frontend.lift) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + const other = try a.openView(2); + r.confirm(other.unit); + r.settle(); + const present: pardes.views.Present = .{ .ctx = null, .f = null }; + const ms = std.time.ns_per_ms; + const start = 100 * std.time.ns_per_s; + r.drawAt(present, start); + r.ready(); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |msg| gpa.free(msg); + sent.deinit(gpa); + } + for ("5j") |cp| try key(r, cp, &sent); + r.drawAt(present, start + 16 * ms); + r.drawAt(present, start + 40 * ms); + const quad = r.core().surface.chrome.cursor_quad; + const target = r.core().surface.cursor.?; + try std.testing.expect(r.animating()); + try std.testing.expect(quad[1] < @as(f32, @floatFromInt(target.y))); + + // The authority changes while both keys are speculative. Replaying + // them must continue from the frame already seen, not the saved copy. + r.confirm(try a.local(.{ .focus = !a.core.window_focused })); + r.settle(); + try std.testing.expect(r.stats.foreign > 0); + r.drawAt(present, start + 40 * ms); + try std.testing.expectEqual(quad, r.core().surface.chrome.cursor_quad); + for (sent.items) |msg| { + r.confirm(try a.take(msg)); + r.settle(); + } + try std.testing.expect(r.diverged == null and r.quiet()); + + // Another client's input leaves its view live. Only our own cursor + // is drawn, and the live view and its clock are untouched afterwards. + var buf: [256]u8 = undefined; + r.confirm(try a.take(try inputOf(&buf, 2, 1, .{ .key = .{ .cp = 'l', .text = "l" } }))); + r.settle(); + try std.testing.expectEqual(other.view, r.core().view); + const clock = r.core().now_ns; + r.drawAt(present, start + 500 * ms); + try std.testing.expectEqual(other.view, r.core().view); + try std.testing.expectEqual(clock, r.core().now_ns); + try std.testing.expectEqual(target, r.core().surface.cursor.?); + try std.testing.expectEqual(@as(f32, @floatFromInt(target.y)), r.core().surface.chrome.cursor_quad[1]); + try std.testing.expect(!r.animating()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + test "a replica's own keys apply at once, confirm on the fast path, and end at the authority's digest" { const gpa = std.testing.allocator; const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); @@ -3664,6 +3778,39 @@ test "a replica joined from the session's dump replays the units after it" { try std.testing.expectEqual(digestOf(a), r.digest()); } +test "a replica joined from a themed dump paints its taglines in the restored theme" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + defer a.deinit(); + _ = try a.local(.{ .command = "Theme rose_pine_moon" }); + const was = Stepping.begin(&a.t, null); + const bytes = try pardes.dump.dumpBytes(a.core); + tape.resync(&a.t); + was.end(); + defer a.core.gpa.free(bytes); + const r = try Replica.initDump(gpa, 1, 0, bytes, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + const expected = pardes.colors.ChromeTheme.fromTheme(a.core.theme()); + try std.testing.expectEqualStrings("rose_pine_moon", r.core().theme().name); + try std.testing.expectEqual(expected.tag_bg, r.core().chromeTheme().tag_bg); + try std.testing.expectEqual(expected.tag_fg, r.core().chromeTheme().tag_fg); + r.draw(.{ .ctx = null, .f = null }); + try std.testing.expectEqual(expected.tag_bg, r.core().surface.chrome.tag_bg); + try std.testing.expectEqual(expected.tag_fg, r.core().surface.chrome.tag_fg); + try std.testing.expectEqual(digestOf(a), r.digest()); + // Later theme changes still arrive as ordinary confirmed units. + r.confirm(try a.local(.{ .command = "Theme monokai_pro_classic" })); + r.settle(); + // A quiet remote authority sends no transition ticks. Drawing alone + // must still update the chrome, without changing replicated state. + r.draw(.{ .ctx = null, .f = null }); + const changed = pardes.colors.ChromeTheme.fromTheme(a.core.theme()); + try std.testing.expectEqual(changed.tag_bg, r.core().surface.chrome.tag_bg); + try std.testing.expectEqual(changed.tag_fg, r.core().surface.chrome.tag_fg); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + test "a host's Adopt and a terminal reader's failure are inputs: a replica makes the same pane" { const gpa = std.testing.allocator; // Its terminals as screen state, as a session's for its clients are. |
