diff options
Diffstat (limited to 'src/views.zig')
| -rw-r--r-- | src/views.zig | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/src/views.zig b/src/views.zig index 34644a96..ac274621 100644 --- a/src/views.zig +++ b/src/views.zig @@ -422,7 +422,7 @@ pub fn open(p: *Pardes) ?u8 { }; const slot = p.parked[k].?; slot.* = .{ .view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h } }; - inline for (.{ "screen_w", "screen_h", "cell_pixels", "row_metrics", "ncol", "col_weight", "col_panes", "col_n", "col_serial", "finder", "active", "active_column", "jumps", "njumps", "jcur", "windowed", "native_images", "native_image_patches" }) |name| + inline for (sized ++ laid_out ++ .{ "windowed", "native_images", "native_image_patches" }) |name| @field(slot.view, name) = @field(p, name); slot.view.col_tags = @splat(.{ .what = .column, .gpa = p.gpa }); for (p.panes, 0..) |s, id| { @@ -435,6 +435,39 @@ pub fn open(p: *Pardes) ?u8 { return k; } +/// How a view is laid out and focused: what a new one takes of the live +/// one (`open`), and what one going leaves the view that stays (`closeKeeping`). +const laid_out = .{ "ncol", "col_weight", "col_panes", "col_n", "col_serial", "finder", "active", "active_column", "jumps", "njumps", "jcur" }; +const sized = .{ "screen_w", "screen_h", "cell_pixels", "row_metrics" }; + +/// View `k` gone, and the view that stays live laid out as `k` was: its +/// columns, its focus, its panes where they were scrolled and its cursors +/// where they were. For a session's last client while no one else watches +/// it (replica.Peers.leave): the client's rejoin (an Upgrade's, a diverged +/// one's) copies the live view (`open`), so it finds its own again. +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; + 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. + inline for (sized ++ laid_out) |name| @field(slot.view, name) = @field(p, name); + for (p.panes, 0..) |s, id| { + const pane = s orelse { + slot.serials[id] = 0; + continue; + }; + if (pane.pv_known & (@as(u8, 1) << @intCast(other)) != 0 and pane.pv[other].body.sels.len > 0) p.gpa.free(pane.pv[other].body.sels); + slot.serials[id] = pane.serial; + pane.pv[other] = fresh(pane); + pane.pv_known |= @as(u8, 1) << @intCast(other); + } + close(p, k); +} + /// 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. pub fn close(p: *Pardes, k: u8) void { @@ -771,6 +804,38 @@ test "a view closing leaves the others as they were" { try testing.expectEqual(@as(i32, 70), pane.body.cur_row); } +test "the last client's view going leaves the session its layout, which a rejoin copies" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + const text = try numbered(gpa, 100); + defer gpa.free(text); + const pane = try p.setTestFile(text); + const k = p.openView().?; + p.toView(k); + p.update(.{ .resize = .{ .cols = 120, .rows = 40 } }); + p.update(.{ .command = "Newcol" }); + p.update(.{ .command = "New" }); + const made = p.active; + const ncol = p.ncol; + p.active = 0; + p.sync(); + cursorTo(p, pane, 60); + p.leaveView(k); + try testing.expectEqual(@as(usize, 1), count(p)); + try testing.expectEqual(ncol, p.ncol); + try testing.expect(layout.findPane(p, made) != null); + try testing.expectEqual(@as(i32, 60), pane.body.cur_row); + try testing.expectEqual(@as(u16, 120), p.screen_w); + // Its client back (a view copied from the live one): all of it again. + const again = p.openView().?; + p.toView(again); + try testing.expectEqual(ncol, p.ncol); + try testing.expect(layout.findPane(p, made) != null); + try testing.expectEqual(@as(usize, 0), p.active); + try testing.expectEqual(@as(i32, 60), pane.body.cur_row); +} + 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 }); |
