Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

editor: tear a tab off into a window and merge it into another (stage 4)

Claudea489f26680ef
diff --git a/src/editor/app.zig b/src/editor/app.zig
index c6734d4..c3b8f89 100644
--- a/src/editor/app.zig
+++ b/src/editor/app.zig
@@ -25,6 +25,7 @@ const app_search = @import("app_search.zig");
const app_edit = @import("app_edit.zig");
const app_controls = @import("app_controls.zig");
const app_drafts = @import("app_drafts.zig");
+const app_move = @import("app_move.zig");
const app_recent = @import("app_recent.zig");
const drafts = @import("drafts.zig");
const a = std.heap.c_allocator;
@@ -85,6 +86,9 @@ pub const App = struct {
closed: bool = false,
/// Set by the New Window action; the event loop opens the window.
new_window: bool = false,
+ /// A tab the strip asked to tear off into a window of its own; the event
+ /// loop opens it.
+ tear_tab: ?usize = null,
ctrl: bool = false,
shift: bool = false,
alt: bool = false,
@@ -267,6 +271,10 @@ pub const App = struct {
pub const recentEnabled = app_recent.enabled;
pub const deinitRecent = app_recent.deinitRecent;
+ pub const movable = app_move.movable;
+ pub const replaceBlank = app_move.replaceBlank;
+ pub const takeTab = app_move.take;
+ pub const adoptTab = app_move.adopt;
pub const join = app_drafts.join;
pub const openDrafts = app_drafts.openDrafts;
pub const stepDrafts = app_drafts.stepDrafts;
diff --git a/src/editor/app_move.zig b/src/editor/app_move.zig
new file mode 100644
index 0000000..33f3099
--- /dev/null
+++ b/src/editor/app_move.zig
@@ -0,0 +1,54 @@
+//! Moving a tab, with its document and undo history, from one window to
+//! another. Both windows are `App`s in the same process.
+
+const std = @import("std");
+const app_mod = @import("app.zig");
+const App = app_mod.App;
+const Tab = app_mod.Tab;
+const a = std.heap.c_allocator;
+
+/// Whether this window can give a tab away or take one: nothing waiting on an
+/// answer that names a tab.
+pub fn movable(self: *App) bool {
+ return self.operation == null and self.close_tab == null;
+}
+
+/// Removes the tab with `id` without freeing it. The selection stays on the
+/// same tab where it can. A window left with no tabs is closed.
+pub fn take(self: *App, id: usize) ?*Tab {
+ const i = self.findTab(id) orelse return null;
+ const selected_id = self.tabs.items[self.selected].id;
+ const tab = self.tabs.orderedRemove(i);
+ if (self.tabs.items.len == 0) {
+ self.selected = 0;
+ self.closed = true;
+ return tab;
+ }
+ self.selected = self.findTab(selected_id) orelse @min(i, self.tabs.items.len - 1);
+ self.changed();
+ return tab;
+}
+
+/// Puts a tab taken from another window at `index` (the end if beyond it) and
+/// selects it. The tab gets this window's next id: ids are per window.
+pub fn adopt(self: *App, tab: *Tab, index: usize) !void {
+ const at = @min(index, self.tabs.items.len);
+ try self.tabs.insert(a, at, tab);
+ tab.id = self.next_id;
+ self.next_id += 1;
+ // Measurements belong to the window's size and scale.
+ tab.measure = .{};
+ self.selected = at;
+ self.changed();
+}
+
+/// A window that starts with one empty tab swaps it for the one it was made
+/// for.
+pub fn replaceBlank(self: *App, tab: *Tab) !void {
+ const blank = self.tabs.items.len == 1 and self.tabs.items[0].path == null and
+ self.tabs.items[0].doc.text.items.len == 0 and !self.tabs.items[0].doc.modified();
+ try adopt(self, tab, self.tabs.items.len);
+ if (blank) self.tabs.orderedRemove(0).deinit();
+ self.selected = self.tabs.items.len - 1;
+ self.changed();
+}
diff --git a/src/editor/client_tabs.zig b/src/editor/client_tabs.zig
index 1e8892c..f1fe44f 100644
--- a/src/editor/client_tabs.zig
+++ b/src/editor/client_tabs.zig
@@ -19,6 +19,8 @@ pub fn attach(self: *Client) void {
.new_tab = newTab,
.move = move,
.leading = leading,
+ .tear_off = tearOff,
+ .drop_on = dropOn,
});
}
@@ -61,6 +63,29 @@ fn move(ctx: *anyopaque, id: u32, index: u32) void {
app.changed();
}
+/// The strip asked for this tab in a window of its own. The event loop opens it:
+/// a window cannot be made from inside an event.
+fn tearOff(ctx: *anyopaque, id: u32, _: i32, _: i32) void {
+ const app = &client(ctx).app;
+ if (!app.movable() or app.tabs.items.len < 2 or app.findTab(id) == null) return;
+ app.tear_tab = id;
+}
+
+/// The tab was dropped on another of this program's windows.
+fn dropOn(ctx: *anyopaque, id: u32, target: ?*@import("wayland").client.rediwm.TitlebarTabsV1, index: u32) void {
+ const from = client(ctx);
+ const strip = target orelse return;
+ const to = for (main_mod.open_windows.items) |w| {
+ if (w.tabs.strip == strip) break w;
+ } else return;
+ if (to == from or !from.app.movable() or !to.app.movable() or to.app.closed) return;
+ const tab = from.app.takeTab(id) orelse return;
+ to.app.adoptTab(tab, index) catch {
+ tab.deinit();
+ return;
+ };
+}
+
fn leading(ctx: *anyopaque, x: i32, _: i32, _: i32, _: i32) void {
const app = &client(ctx).app;
// The button's own press closes an open menu rather than reopening it.
diff --git a/src/editor/main.zig b/src/editor/main.zig
index 1dccf76..b35c923 100644
--- a/src/editor/main.zig
+++ b/src/editor/main.zig
@@ -190,6 +190,10 @@ pub const Buffer = struct {
/// What the poll array below has room for: three fixed entries plus a few per window.
const max_windows = 24;
+/// Every open window, in the order they were opened. The tab strip handlers
+/// look windows up here to move a tab between them.
+pub var open_windows: std.ArrayList(*Client) = .empty;
+
pub const Setup = struct { io: std.Io, environ: std.process.Environ, force_csd: bool };
pub fn run(init: std.process.Init, paths: []const [:0]const u8, instance: *@import("instance.zig").Instance, initial_token: []const u8) !void {
@@ -214,7 +218,7 @@ pub fn run(init: std.process.Init, paths: []const [:0]const u8, instance: *@impo
defer display.disconnect();
const setup: Setup = .{ .io = init.io, .environ = init.minimal.environ, .force_csd = force_csd };
- var windows: std.ArrayList(*Client) = .empty;
+ const windows = &open_windows;
defer {
for (windows.items) |window| window.destroy();
windows.deinit(a);
@@ -261,6 +265,31 @@ pub fn run(init: std.process.Init, paths: []const [:0]const u8, instance: *@impo
break; // the list may have grown; one a turn is plenty
}
+ // Tear-off: the tab leaves for a window of its own, where the compositor
+ // is about to place it (it was told at the same moment as this program).
+ for (windows.items) |window| {
+ const id = window.app.tear_tab orelse continue;
+ window.app.tear_tab = null;
+ if (windows.items.len >= max_windows or !window.app.movable() or window.app.tabs.items.len < 2) continue;
+ const created = Client.create(display, setup, false, &.{}) catch |err| {
+ std.log.warn("rediwm-editor: cannot open a window: {}", .{err});
+ continue;
+ };
+ const tab = window.app.takeTab(id) orelse {
+ created.destroy();
+ continue;
+ };
+ created.app.replaceBlank(tab) catch {
+ tab.deinit();
+ created.destroy();
+ continue;
+ };
+ windows.append(a, created) catch {
+ created.destroy();
+ };
+ break;
+ }
+
while (!display.prepareRead()) {
if (display.dispatchPending() != .SUCCESS) return error.DisplayFailed;
}