//! Views: several clients on one session, kakoune's way (docs/next-daemon.md, //! "Phase 4 notes"). Each view has its own layout, focus, selections and //! scroll over the panes every view shares: their text, terminals, tags, //! registers and messages. //! //! The core still sees one view, the live one, in the fields it always had: //! a view's share of the session (`fields`) and of each pane (`PaneView`) is //! copied out when another view goes live (`park`) and back when it is live //! again (`unpark`). No line of the core reads a view through an //! indirection, and one view never parks: today's single client runs //! exactly as before. //! //! A parked view is in no digest (Pardes.replica's `parked`, Pane's `pv`): //! a slot is written whenever its view goes out, so the live view's slot is //! stale by design, and a parked view that diverged shows when it is live //! again. The pump draws the other views by going into each and back //! (`drawOthers`), which leaves every digested field as it was; a replica //! that draws nothing skips it. const std = @import("std"); const pardes = @import("pardes.zig"); const Pardes = pardes.Pardes; const Pane = pardes.Pane; const Text = pardes.panes.Text; const File = pardes.panes.File; const layout = pardes.layout; const MAX_PANES = pardes.MAX_PANES; /// Views at once: a window or terminal each. pub const max = 4; /// The session's fields that are each view's own: its screen and layout, /// its focus, its pointer and gestures, its jumps, its key prefixes, and /// what drawing it keeps (animations, the presentation of its panels). pub const fields = [_][:0]const u8{ "screen_w", "screen_h", "cell_pixels", "row_metrics", "ncol", "col_weight", "col_panes", "col_n", "col_tags", "col_serial", "finder", "rects", "col_x", "col_w", "presentation", "active", "jumps", "njumps", "jcur", "jump_force", "active_column", "lifts", "lift_serials", "cursor_glide", "cursor_view", "cursor_crossing", "cursor_held", "shell_continuous", "input_ns", "native_images", "native_image_patches", "windowed", "drag", "mouse_grab", "window_focused", "focus_told", "hover_col", "hover_row", "pointer_raw_col", "pointer_raw_row", "pointer_body_hit", "pointer_tag_hit", "raw_hover_intent", "pointer_inside", "last_click", "click_as", "chord_hold", "wheel_guard", "ctrl_w_pending", "register", "leader_on", "window_pick", "leader_keys", "leader_n", "header_focus", "header_column", "header_drag", "header_sweep", "header_scroll", "header_top", "ov_pinch_scale", "ov_touch_scroll_delta", "chord_arg", "clip_pending", }; /// A pane's fields that are each view's own: where it is in the view's /// layout, which of its texts has the keyboard, how far it is scrolled, and /// what the view's step measured of it (its tag's rows, its bands). const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "attached_to", "hscroll", "tag_scroll", "tag_top", "tag_rows", "view", "notices" }; /// The body's: its cursor, selections and mode. Its undo stays the pane's. const body_fields = [_][:0]const u8{ "mode", "cur_pinned", "cur_row", "cur_col", "msel", "vsel", "sels", "nsel", "select", "sticky_col", "restore_cursor", "last_edit", "reg_wait", "normal", "find_op", "find_ch" }; pub const View = Part(Pardes, &fields); /// Named field by field, not taken from Pane's type: a pane holds its own. pub const PaneView = struct { focus: Pane.Focus = .body, vweight: f32 = 1, collapsed: bool = false, attached_to: ?u32 = null, hscroll: i32 = 0, tag_scroll: u16 = 0, tag_top: u16 = 0, tag_rows: u16 = 1, view: Pane.SessionView = .{}, notices: Pane.Notices = .{}, body: Part(Text, &body_fields) = .{}, /// A document's scroll and size; a terminal's size is its pty's, which /// every view shows (sized by its last typist, `Pardes.sync`). scroll: usize = 0, cols: u16 = 1, rows: u16 = 1, }; /// A view not live: its share of the session, and the serial of each pane /// slot when it went out, by which the panes closed since leave its layout. pub const Parked = struct { view: View, serials: [MAX_PANES]u32 = @splat(0), }; /// The fields of `T` named, as a struct of their own, each of its type and /// default. fn Part(comptime T: type, comptime names: []const [:0]const u8) type { @setEvalBranchQuota(100_000); const s = @typeInfo(T).@"struct"; var types: [names.len]type = undefined; var attrs: [names.len]std.lang.Type.Struct.FieldAttributes = undefined; for (names, 0..) |name, i| { const at = for (s.field_names, 0..) |n, j| { if (std.mem.eql(u8, n, name)) break j; } else @compileError("views: " ++ @typeName(T) ++ " has no field " ++ name); types[i] = s.field_types[at]; attrs[i] = .{ .default_value_ptr = s.field_attrs[at].default_value_ptr }; } return @Struct(.auto, null, names, &types, &attrs); } fn copy(comptime names: []const [:0]const u8, dst: anytype, src: anytype) void { inline for (names) |name| @field(dst, name) = @field(src, name); } pub fn isOpen(p: *const Pardes, k: u8) bool { return k < max and p.views_open & (@as(u8, 1) << @intCast(k)) != 0; } pub fn count(p: *const Pardes) usize { return @popCount(p.views_open); } /// More than one view: the one test the single client's path pays. pub fn several(p: *const Pardes) bool { return p.views_open & (p.views_open -% 1) != 0; } /// Pane `pane`'s share of the live view. fn extract(pane: *const Pane) PaneView { var v: PaneView = .{ .cols = pane.cols, .rows = pane.rows }; copy(&pane_fields, &v, pane); copy(&body_fields, &v.body, &pane.body); if (pane.file) |f| v.scroll = f.scroll; return v; } /// Its share of view `v` put back. fn apply(pane: *Pane, v: *const PaneView) void { copy(&pane_fields, pane, v); copy(&body_fields, &pane.body, &v.body); if (!pane.isTerminal()) { pane.cols = v.cols; pane.rows = v.rows; } const f = if (pane.file) |*file| file else return; // The text may have changed while the view was out: what it held is // brought back inside it. const lines = File.nlines(pane.gpa, f); f.scroll = @min(v.scroll, lines -| 1); if (pane.body.cur_row >= lines) { pane.body.vsel.active = false; pane.body.msel.active = false; pane.body.nsel = 0; } _ = pardes.edit.clampFileCursor(&pane.body); } /// A copy of the live view's share for another view, owning nothing of the /// live one's (its extra selections stay behind). fn fresh(pane: *const Pane) PaneView { var v = extract(pane); v.body.sels = &.{}; v.body.nsel = 0; return v; } /// A new pane is in each other view as it was made, there to be shown. pub fn installed(p: *Pardes, pane: *Pane) void { pane.sized_by = p.view; if (!several(p)) return; for (0..max) |k| if (k != p.view and isOpen(p, @intCast(k))) { pane.pv[k] = fresh(pane); }; pane.pv_known = p.views_open; } /// A pane going: the other views' selections of it go too. pub fn tornDown(p: *Pardes, pane: *Pane) void { for (0..max) |k| if (k != p.view and pane.pv_known & (@as(u8, 1) << @intCast(k)) != 0) { const sels = pane.pv[k].body.sels; if (sels.len > 0) p.gpa.free(sels); pane.pv[k].body.sels = &.{}; }; } fn park(p: *Pardes) void { const slot = p.parked[p.view].?; copy(&fields, &slot.view, p); for (p.panes, 0..) |s, id| { const pane = s orelse { slot.serials[id] = 0; continue; }; slot.serials[id] = pane.serial; pane.pv[p.view] = extract(pane); } } fn unpark(p: *Pardes, k: u8) void { const slot = p.parked[k].?; copy(&fields, p, &slot.view); p.view = k; for (p.panes) |s| if (s) |pane| apply(pane, &pane.pv[k]); // The panes closed while it was out (a slot empty or another pane's // now) leave its layout, and its keyboard leaves them. for (0..p.ncol) |c| { var n: usize = 0; for (p.col_panes[c][0..p.col_n[c]]) |id| { const pane = p.panes[id] orelse continue; if (pane.serial != slot.serials[id]) continue; p.col_panes[c][n] = id; n += 1; } p.col_n[c] = n; } if (layout.findPane(p, p.active) == null) { for (0..p.ncol) |c| if (p.col_n[c] > 0) { p.active = p.col_panes[c][0]; break; }; } layout.compute(p); } /// The pane with the keyboard is one the live view shows: focused where its /// layout has it not (a Look at a file another view opened, a jump, a 9P /// focus), it goes into the view's active column, as kakoune shows a buffer /// in the window that asked for it. pub fn show(p: *Pardes) void { if (p.panes[p.active] == null or layout.findPane(p, p.active) != null) return; const c = layout.firstColumn(p) orelse layout.columnBySerial(p, p.active_column) orelse p.ncol - 1; layout.acmePlace(p, c, null, p.active); } /// Into view `k`: the live one parked, `k`'s put in its place. pub fn to(p: *Pardes, k: u8) void { std.debug.assert(isOpen(p, k)); if (k == p.view) return; park(p); unpark(p, k); } /// A new view, as the live one is laid out and focused, with its panes /// where they are scrolled and its cursors where they are; the rest of it /// (column tags, gestures, animations) as a session starts. Null when there /// are `max` already. pub fn open(p: *Pardes) ?u8 { const k: u8 = for (0..max) |i| { if (!isOpen(p, @intCast(i))) break @intCast(i); } else return null; // Each live view parks too: its slot is made with the first other. for ([_]u8{ p.view, k }) |i| if (p.parked[i] == null) { const slot = p.gpa.create(Parked) catch return null; slot.* = .{ .view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h } }; p.parked[i] = slot; }; 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| @field(slot.view, name) = @field(p, name); slot.view.col_tags = @splat(.{ .what = .column, .gpa = p.gpa }); for (p.panes, 0..) |s, id| { const pane = s orelse continue; slot.serials[id] = pane.serial; pane.pv[k] = fresh(pane); pane.pv_known |= @as(u8, 1) << @intCast(k); } p.views_open |= @as(u8, 1) << @intCast(k); return 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 { if (!isOpen(p, k) or count(p) == 1) return; if (k == p.view) { const other: u8 = for (0..max) |i| { if (i != k and isOpen(p, @intCast(i))) break @intCast(i); } else unreachable; park(p); unpark(p, other); } const v = &p.parked[k].?.view; for (&v.col_tags) |*t| t.deinit(p.gpa); if (v.chord_arg) |a| p.gpa.free(a); v.presentation.deinit(p.gpa); for (p.panes) |s| if (s) |pane| { const sels = pane.pv[k].body.sels; if (sels.len > 0) p.gpa.free(sels); pane.pv[k].body.sels = &.{}; pane.pv_known &= ~(@as(u8, 1) << @intCast(k)); }; p.views_open &= ~(@as(u8, 1) << @intCast(k)); } /// The session's end: what each view not live holds. pub fn deinit(p: *Pardes) void { for (0..max) |i| { const k: u8 = @intCast(i); const slot = p.parked[k] orelse continue; if (k != p.view and isOpen(p, k)) { const v = &slot.view; for (&v.col_tags) |*t| t.deinit(p.gpa); if (v.chord_arg) |a| p.gpa.free(a); v.presentation.deinit(p.gpa); } p.gpa.destroy(slot); } } /// What drawing a view keeps of its own (its animations, its panels as /// presented), kept from a frame drawn while it was out. const drawn_fields = [_][:0]const u8{ "presentation", "lifts", "lift_serials", "cursor_glide", "cursor_view", "cursor_crossing", "cursor_held", "shell_continuous" }; /// Each view but the live one drawn and presented, the live one put back /// after exactly as it was: no digested field moves, so a replica that /// draws nothing (and skips this) stays the same. A view drawn keeps only /// what its drawing made (`drawn_fields`); whatever its unpark brought /// inside a changed text is not kept, and is done again when it is next /// live. Before the live one's frame: the surface after the pump is its. pub fn drawOthers(p: *Pardes, h: pardes.Host) !void { const live = p.view; const v = h.vtable; park(p); defer { copy(&fields, p, &p.parked[live].?.view); p.view = live; for (p.panes) |s| if (s) |pane| { const pv = &pane.pv[live]; copy(&pane_fields, pane, pv); copy(&body_fields, &pane.body, &pv.body); if (!pane.isTerminal()) { pane.cols = pv.cols; pane.rows = pv.rows; } if (pane.file) |*f| f.scroll = pv.scroll; }; } for (0..max) |i| { const k: u8 = @intCast(i); if (k == live or !isOpen(p, k)) continue; unpark(p, k); _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept }); const surface = try p.render(p.frame_arena.allocator()); if (v.present) |f| f(h.ctx, surface); // 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. copy(&drawn_fields, &p.parked[k].?.view, p); } } // ---- tests ---- const testing = std.testing; const Key = pardes.Key; fn typeKeys(p: *Pardes, text: []const u8) void { for (text) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); } fn escape(p: *Pardes) void { p.update(.{ .key = .{ .cp = Key.escape } }); } fn numbered(gpa: std.mem.Allocator, n: usize) ![]u8 { var out: std.ArrayList(u8) = .empty; for (1..n + 1) |i| try out.print(gpa, "line {d}\n", .{i}); return out.toOwnedSlice(gpa); } /// The cursor put on `row`, as a jump puts it, and the step after. fn cursorTo(p: *Pardes, pane: *Pane, row: i32) void { pane.body.vsel.active = false; pane.body.nsel = 0; pane.body.cur_row = row; pane.body.cur_col = 0; pane.body.cur_pinned = true; pane.ensureCursorVisible(); p.sync(); } test "each view has its own cursor and scroll, and an edit in one is in the other" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const text = try numbered(gpa, 200); defer gpa.free(text); const pane = try p.setTestFile(text); try testing.expectEqual(@as(?u8, 1), p.openView()); cursorTo(p, pane, 119); const scrolled = pane.file.?.scroll; try testing.expect(scrolled > 0); // The other view is where the session was when it came. p.toView(1); try testing.expectEqual(@as(i32, 0), pane.body.cur_row); try testing.expectEqual(@as(usize, 0), pane.file.?.scroll); typeKeys(p, "ix"); escape(p); try testing.expectEqual(Text.Mode.normal, pane.body.mode); p.toView(0); try testing.expectEqual(@as(i32, 119), pane.body.cur_row); try testing.expectEqual(scrolled, pane.file.?.scroll); try testing.expect(std.mem.startsWith(u8, pane.file.?.content, "xline 1\n")); // Its mode is its own: insert here leaves the other in normal. typeKeys(p, "i"); try testing.expectEqual(Text.Mode.insert, pane.body.mode); p.toView(1); try testing.expectEqual(Text.Mode.normal, pane.body.mode); } test "each view has its own layout and focus over the same panes" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); defer p.deinit(); _ = try p.setTestFile("one\ntwo\n"); const k = p.openView().?; const ncol = p.ncol; p.toView(k); p.update(.{ .command = "Newcol" }); p.update(.{ .command = "New" }); const made = p.active; try testing.expect(made != 0); try testing.expectEqual(ncol + 1, p.ncol); p.toView(0); try testing.expectEqual(ncol, p.ncol); try testing.expectEqual(@as(usize, 0), p.active); try testing.expect(layout.findPane(p, made) == null); try testing.expectEqual(pardes.Rect{}, p.rects[made]); // The pane is the session's: focused here, this view shows it too. p.active = made; p.sync(); try testing.expect(layout.findPane(p, made) != null); try testing.expect(p.rects[made].h > 0); // Closed here, it leaves the other view's layout, and its keyboard. try p.removePane(made, null); p.sync(); p.toView(k); try testing.expect(layout.findPane(p, made) == null); try testing.expect(p.panes[p.active] != null); try testing.expect(layout.findPane(p, p.active) != null); } test "registers are the session's: yanked in one view, put in another" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const pane = try p.setTestFile("alpha\nbeta\n"); const k = p.openView().?; typeKeys(p, "xy"); p.toView(k); cursorTo(p, pane, 1); typeKeys(p, "p"); try testing.expectEqualStrings("alpha\nbeta\nalpha\n", pane.file.?.content); } test "a terminal shown at two sizes takes the size of the view that typed into it last" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const term = p.panes[p.active].?; try testing.expect(term.isTerminal()); const small = .{ term.cols, term.rows }; const k = p.openView().?; p.toView(k); p.update(.{ .resize = .{ .cols = 120, .rows = 40 } }); // Shown bigger here, it keeps the size of the view that typed last. try testing.expect(p.rects[p.active].w > 80); try testing.expectEqual(small, .{ term.cols, term.rows }); p.update(.{ .key = .{ .cp = Key.right } }); try testing.expect(term.cols > small[0] and term.rows > small[1]); const big = .{ term.cols, term.rows }; p.toView(0); p.sync(); try testing.expectEqual(big, .{ term.cols, term.rows }); p.update(.{ .key = .{ .cp = Key.right } }); try testing.expectEqual(small, .{ term.cols, term.rows }); // That view gone, the one left sizes it. p.toView(k); p.closeView(0); try testing.expectEqual(k, p.view); try testing.expectEqual(big, .{ term.cols, term.rows }); } test "a view closing leaves the others as they were" { 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 a = p.openView().?; const b = p.openView().?; try testing.expectEqual(@as(usize, 3), count(p)); p.toView(b); p.update(.{ .command = "Newcol" }); cursorTo(p, pane, 40); typeKeys(p, "xy"); p.toView(a); cursorTo(p, pane, 70); p.toView(0); cursorTo(p, pane, 10); const ncol = p.ncol; // The live one closing: the session goes into another. p.toView(b); p.closeView(b); try testing.expect(p.view != b); try testing.expectEqual(@as(usize, 2), count(p)); p.toView(a); try testing.expectEqual(@as(i32, 70), pane.body.cur_row); p.closeView(0); try testing.expectEqual(a, p.view); try testing.expectEqual(ncol, p.ncol); // The last stays. p.closeView(a); try testing.expectEqual(@as(usize, 1), count(p)); try testing.expectEqual(@as(i32, 70), 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 }); 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); cursorTo(p, pane, 60); typeKeys(p, "dd"); p.toView(0); cursorTo(p, pane, 95); const before = pardes.walk.digest(Pardes, p); try drawOthers(p, .{}); try testing.expectEqual(@as(u8, 0), p.view); try testing.expectEqual(before, pardes.walk.digest(Pardes, p)); p.toView(k); try testing.expectEqual(@as(i32, 60), pane.body.cur_row); } test "a view not live keeps its place in a text another view edits" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const text = try numbered(gpa, 200); defer gpa.free(text); const pane = try p.setTestFile(text); const k = p.openView().?; p.toView(k); cursorTo(p, pane, 100); const scrolled = pane.file.?.scroll; p.toView(0); // Three lines put above it, one taken below it. typeKeys(p, "xyppp"); cursorTo(p, pane, 150); typeKeys(p, "xd"); p.toView(k); try testing.expectEqual(@as(i32, 103), pane.body.cur_row); try testing.expectEqual(scrolled + 3, pane.file.?.scroll); try testing.expect(std.mem.startsWith(u8, pardes.panes.File.sourceLine(pane, 103), "line 101")); }