From d00c40694dc239854263616f710b4a197d25d145 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 21:00:30 -0300 Subject: views: a client that joins again has its own view back, layout, focus and the panes opened in it: an Upgrade's clients and one whose bye says it joins again (a divergence's, wire 19's `bye.again`) leave their view kept for them (`views_away`, digested and dumped, so an Upgrade's new build has it; taped `awayView`), the client names it in its next hello (`Hello.view`, handed on by the daemon with the connection) and its join takes it back (taped `rejoinView`) rather than a copy of the session's; the live view is never a kept one, `views.count` counts the others, and a join with every view taken lets one go (every Upgrade made each client's own panes leave its view: the hunt's 80 ms cell lost `+Search` and `+New`) Co-Authored-By: Claude Opus 5.5 --- src/views.zig | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 8 deletions(-) (limited to 'src/views.zig') diff --git a/src/views.zig b/src/views.zig index ac274621..58d7a1f7 100644 --- a/src/views.zig +++ b/src/views.zig @@ -173,6 +173,7 @@ pub fn undumped(p: *Pardes, others: []const Dumped) !void { }; // Nothing fails from here. p.views_open &= take | bit(p.view); + p.views_away &= p.views_open; // The live view's slot owning nothing, as `installed` makes it. for (p.panes) |s| if (s) |pane| { if (take != 0) pane.pv[p.view] = fresh(pane); @@ -214,6 +215,7 @@ pub fn undumped(p: *Pardes, others: []const Dumped) !void { /// The live view alone, as view 0: every other closed. A session loaded /// for one client (a Restore, `-l`) is one view. pub fn only(p: *Pardes) void { + p.views_away = 0; for (0..max) |k| if (k != p.view) close(p, @intCast(k)); emptied(p, p.view); const k = p.view; @@ -255,8 +257,20 @@ pub fn isOpen(p: *const Pardes, k: u8) bool { return k < max and p.views_open & (@as(u8, 1) << @intCast(k)) != 0; } +/// Kept for its client's rejoin (`away`). +pub fn isAway(p: *const Pardes, k: u8) bool { + return k < max and p.views_away & (@as(u8, 1) << @intCast(k)) != 0; +} + +/// The views someone looks at: open, none kept for a rejoin. pub fn count(p: *const Pardes) usize { - return @popCount(p.views_open); + return @popCount(p.views_open & ~p.views_away); +} + +/// An open view but `k` someone looks at (`count`), if there is one. +fn looked(p: *const Pardes, k: u8) ?u8 { + for (0..max) |i| if (i != k and isOpen(p, @intCast(i)) and !isAway(p, @intCast(i))) return @intCast(i); + return null; } /// More than one view: the one test the single client's path pays. @@ -448,9 +462,8 @@ const sized = .{ "screen_w", "screen_h", "cell_pixels", "row_metrics" }; pub fn closeKeeping(p: *Pardes, k: u8) void { if (!isOpen(p, k) or count(p) == 1) return; to(p, k); - const other: u8 = for (0..max) |i| { - if (i != k and isOpen(p, @intCast(i))) break @intCast(i); - } else unreachable; + // Never into one kept for a rejoin (`away`): that one is its client's. + const other = looked(p, k).?; const slot = p.parked[other].?; // Its size too: the terminals it sized keep theirs, no shell redrawn for // a size it is given back at the rejoin. @@ -469,11 +482,12 @@ pub fn closeKeeping(p: *Pardes, k: u8) void { } /// View `k` gone, and all it held; the live one moves to another first. -/// The last view stays: a session without a client keeps its own. +/// The last view someone looks at stays: a session without a client keeps +/// its own. One kept for a rejoin (`away`) goes whenever it is closed. pub fn close(p: *Pardes, k: u8) void { - if (!isOpen(p, k) or count(p) == 1) return; + if (!isOpen(p, k) or @popCount(p.views_open) == 1 or (!isAway(p, k) and count(p) == 1)) return; if (k == p.view) { - const other: u8 = for (0..max) |i| { + const other: u8 = looked(p, k) orelse for (0..max) |i| { if (i != k and isOpen(p, @intCast(i))) break @intCast(i); } else unreachable; park(p); @@ -493,6 +507,19 @@ pub fn close(p: *Pardes, k: u8) void { if (pane.prompt == .name and pane.prompt_view == k) pane.closePrompt(); }; p.views_open &= ~(@as(u8, 1) << @intCast(k)); + p.views_away &= ~(@as(u8, 1) << @intCast(k)); +} + +/// View `k`'s client gone a moment, to join again for it (an Upgrade's +/// rejoin, a divergence's): the view kept as it is, layout, focus and the +/// panes opened in it, until its client is back (Pardes.rejoinView) or a +/// join wants its place (Pardes.openView). Dumped with the rest, so an +/// Upgrade's new build has it too. The live view is never one: the session +/// goes into another someone looks at, or into a copy of this one of its own. +pub fn away(p: *Pardes, k: u8) void { + if (!isOpen(p, k)) return; + if (k == p.view) if (looked(p, k) orelse open(p)) |other| to(p, other); + p.views_away |= bit(k); } /// The session's end: what each view not live holds. @@ -541,7 +568,7 @@ pub fn drawOthers(p: *Pardes, h: pardes.Host) !void { } for (0..max) |i| { const k: u8 = @intCast(i); - if (k == live or !isOpen(p, k) or p.undrawn & (@as(u8, 1) << @intCast(k)) != 0) continue; + if (k == live or !isOpen(p, k) or (p.undrawn | p.views_away) & bit(k) != 0) continue; // No post_present: what a shell acknowledges there moves the // session's pointer and hover, which are the live view's to move; // a view's panels are acknowledged when it is live and drawn. @@ -836,6 +863,46 @@ test "the last client's view going leaves the session its layout, which a rejoin try testing.expectEqual(@as(i32, 60), pane.body.cur_row); } +test "a client's view kept for its rejoin: through an Upgrade's dump, each client back in its own; a join with no room lets one go" { + const gpa = testing.allocator; + const opts: pardes.Options = .{ .tty_only = true, .cols = 100, .rows = 30 }; + const p = try Pardes.init(gpa, opts); + defer p.deinit(); + _ = try p.setTestFile("one\ntwo\n"); + // Two clients, each a pane of its own in its own view. + const ks: [2]u8 = .{ p.openView().?, p.openView().? }; + var made: [2]usize = undefined; + for (ks, &made) |k, *m| { + p.toView(k); + p.update(.{ .command = "New" }); + m.* = p.active; + } + // Both let go to upgrade, the live view one of theirs: theirs are kept, + // and the session's own is live. + for (ks) |k| p.awayView(k); + try testing.expectEqual(@as(usize, 1), count(p)); + try testing.expect(!isAway(p, p.view)); + // The new build loads the dump; each client is back in its own view. + const bytes = try pardes.dump.dumpBytes(p); + defer gpa.free(bytes); + const q = try pardes.dump.load(gpa, opts, bytes, null); + defer q.deinit(); + for (ks, made, 0..) |k, m, i| { + try testing.expect(isAway(q, k)); + try testing.expect(q.rejoinView(k)); + q.toView(k); + try testing.expectEqual(m, q.active); + try testing.expect(layout.findPane(q, m) != null); + try testing.expect(layout.findPane(q, made[1 - i]) == null); + } + try testing.expect(!q.rejoinView(ks[0])); + // A client that never comes back: its view goes when a join has no room. + p.toView(0); + try testing.expectEqual(@as(?u8, 3), p.openView()); + try testing.expectEqual(ks[0], p.openView()); + try testing.expect(!isAway(p, ks[0]) and isAway(p, ks[1])); +} + test "drawing the other views leaves the live one as it was, to the digest" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); -- cgit v1.3