Skip to content

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

editor: publish tabs to the compositor's titlebar under SSD

Claudeb6f032b75792
diff --git a/src/editor/app.zig b/src/editor/app.zig
index 240451b..0565469 100644
--- a/src/editor/app.zig
+++ b/src/editor/app.zig
@@ -111,6 +111,9 @@ pub const App = struct {
recent: std.ArrayList([:0]u8) = .empty,
environ: std.process.Environ = .empty,
has_titlebar: bool = true,
+ /// The compositor draws the tabs in its titlebar (`client_tabs.zig`), so the
+ /// window has no strip of its own and its content starts at the top.
+ strip_in_titlebar: bool = false,
dragging: bool = false,
drag_word: ?[2]usize = null,
last_text_click: ?struct { time: i64, x: f64, y: f64, tab: usize } = null,
@@ -272,6 +275,8 @@ pub const App = struct {
pub const resize = app_view.resize;
pub const viewTop = app_view.viewTop;
+ pub const topInset = app_view.topInset;
+ pub const openMenuAt = app_paint.openMenuAt;
pub const viewHeight = app_view.viewHeight;
pub const cancelDrag = app_view.cancelDrag;
pub const cursorName = app_view.cursorName;
diff --git a/src/editor/app_controls.zig b/src/editor/app_controls.zig
index f815285..af56fdc 100644
--- a/src/editor/app_controls.zig
+++ b/src/editor/app_controls.zig
@@ -41,7 +41,7 @@ fn nearestButton(self: *App, x: f64, y: f64) f32 {
}
return nearest;
}
- for (0..self.tabs.items.len) |index| {
+ for (0..if (self.strip_in_titlebar) 0 else self.tabs.items.len) |index| {
const tab_x = self.tabX(index);
if (tab_x >= self.tabRight()) break;
if (tab_x + self.tabWidth() <= 0) continue;
@@ -53,7 +53,8 @@ fn nearestButton(self: *App, x: f64, y: f64) f32 {
);
nearest = @min(nearest, sample.distance);
}
- for ([_]Rect{ self.newButtonBox() }) |box| {
+ const strip_boxes = [_]Rect{self.newButtonBox()};
+ for (strip_boxes[0..if (self.strip_in_titlebar) 0 else 1]) |box| {
const sample = button.sampleProximity(
.{ .x = @floatCast(box.x), .y = @floatCast(box.y), .w = @floatCast(box.w), .h = @floatCast(box.h) },
@floatCast(x), @floatCast(y),
@@ -61,7 +62,7 @@ fn nearestButton(self: *App, x: f64, y: f64) f32 {
);
nearest = @min(nearest, sample.distance);
}
- if (!self.has_titlebar) {
+ if (!self.has_titlebar and !self.strip_in_titlebar) {
const box = self.menuButtonBox();
const sample = button.sampleProximity(
.{ .x = @floatCast(box.x), .y = @floatCast(box.y), .w = @floatCast(box.w), .h = @floatCast(box.h) },
@@ -182,6 +183,7 @@ pub fn hoverTarget(self: *App) Hover {
}
if (self.menu_open) return .none;
if (self.find_open) if (self.searchHit(self.px, self.py)) |hit| return .{ .search = hit };
+ if (self.strip_in_titlebar) return .none;
if (self.newButtonBox().contains(self.px, self.py)) return .new;
if (!self.has_titlebar and self.menuButtonBox().contains(self.px, self.py)) return .menu;
if (self.tabAt(self.px)) |i| if (self.tabCloseBox(i).contains(self.px, self.py)) return .{ .close = i };
diff --git a/src/editor/app_input.zig b/src/editor/app_input.zig
index 4bd9f6d..8f8590e 100644
--- a/src/editor/app_input.zig
+++ b/src/editor/app_input.zig
@@ -111,7 +111,7 @@ pub fn handleButton(self: *App, code: u32, pressed: bool) void {
return;
}
if (self.py < 0) return;
- if (self.py < top) {
+ if (self.py < self.topInset()) {
if (!self.has_titlebar and self.menuButtonBox().contains(self.px, self.py)) {
self.toggleMenu(@as(f64, @floatFromInt(self.w)) - 8);
return;
diff --git a/src/editor/app_paint.zig b/src/editor/app_paint.zig
index d78118c..3fa5e58 100644
--- a/src/editor/app_paint.zig
+++ b/src/editor/app_paint.zig
@@ -154,11 +154,17 @@ pub fn toggleMenu(self: *App, anchor: f64) void {
self.dirty = true;
}
+/// Opens the menu with its left edge at `left`, under the leading button of the
+/// compositor's titlebar.
+pub fn openMenuAt(self: *App, left: f64) void {
+ self.toggleMenu(left + menu.width);
+}
+
pub fn menuBox(self: *const App) menu.Rect {
const height = labels.len * menu.row_height + 2 * menu.padding + 2 * menu.separator_space;
return .{
.x = std.math.clamp(self.menu_anchor - menu.width, 6, @max(6, @as(f64, @floatFromInt(self.w)) - menu.width - 6)),
- .y = @min(if (self.has_titlebar) @as(f64, 6) else top + 4, @max(6, @as(f64, @floatFromInt(self.h)) - height - 6)),
+ .y = @min(if (self.has_titlebar or self.strip_in_titlebar) @as(f64, 6) else top + 4, @max(6, @as(f64, @floatFromInt(self.h)) - height - 6)),
.w = menu.width,
.h = height,
};
@@ -217,9 +223,9 @@ pub fn paintButton(self: *App, cr: *c.cairo_t, r: field.Rect, label: []const u8,
});
}
-pub fn render(self: *App, cr: *c.cairo_t) void {
+/// The window's own tab strip, for when the compositor does not draw the tabs.
+fn paintStrip(self: *App, cr: *c.cairo_t) void {
const t = theme.global;
- fill(cr, 0, 0, @floatFromInt(self.w), @floatFromInt(self.h), t.app_bg);
fill(cr, 0, 0, @floatFromInt(self.w), 38, t.app_bar);
c.cairo_save(cr);
c.cairo_rectangle(cr, 0, 0, self.tabRight(), 38);
@@ -246,6 +252,12 @@ pub fn render(self: *App, cr: *c.cairo_t) void {
c.cairo_restore(cr);
self.paintIcon(cr, self.newButtonBox(), .plus, tr("New tab"), false);
if (!self.has_titlebar) self.paintIcon(cr, self.menuButtonBox(), .more, tr("Menu"), self.menu_open);
+}
+
+pub fn render(self: *App, cr: *c.cairo_t) void {
+ const t = theme.global;
+ fill(cr, 0, 0, @floatFromInt(self.w), @floatFromInt(self.h), t.app_bg);
+ if (!self.strip_in_titlebar) self.paintStrip(cr);
if (self.find_open) self.paintSearch(cr);
self.layouts.beginFrame();
const pt = self.port();
diff --git a/src/editor/app_view.zig b/src/editor/app_view.zig
index cec4b48..6fe6785 100644
--- a/src/editor/app_view.zig
+++ b/src/editor/app_view.zig
@@ -19,8 +19,14 @@ pub fn resize(self: *App, w: i32, h: i32) void {
self.changed();
}
+/// What the window's own tab strip takes from the top: nothing when the
+/// compositor draws the tabs in its titlebar.
+pub fn topInset(self: *const App) f64 {
+ return if (self.strip_in_titlebar) 0 else top;
+}
+
pub fn viewTop(self: *App) f64 {
- return top + if (self.find_open) @as(f64, if (self.replace_open) 80 else 40) else 0;
+ return self.topInset() + if (self.find_open) @as(f64, if (self.replace_open) 80 else 40) else 0;
}
pub fn viewHeight(self: *App) f64 {
@@ -91,7 +97,7 @@ pub fn barTimeout(self: *const App, now: i64) ?u32 {
pub fn handleScroll(self: *App, amount: f64) void {
if (self.close_tab != null or self.menu_open) return;
- if (self.py >= 0 and self.py < top) {
+ if (self.py >= 0 and self.py < self.topInset()) {
self.scrollTabs(amount * 3);
} else if (self.shift and !self.wrap) {
self.current().scroll_x = @max(0, self.current().scroll_x + amount * 3);
diff --git a/src/editor/client_tabs.zig b/src/editor/client_tabs.zig
new file mode 100644
index 0000000..1e8892c
--- /dev/null
+++ b/src/editor/client_tabs.zig
@@ -0,0 +1,97 @@
+//! The editor's tabs in the compositor's titlebar (`ui/client_tabs.zig`). The
+//! strip exists only under server-side decorations; otherwise, or with more
+//! tabs than the protocol carries, the window keeps drawing its own.
+const std = @import("std");
+const ClientTabs = @import("ui").client_tabs;
+const i18n = @import("i18n");
+const tr = i18n.tr;
+const main_mod = @import("main.zig");
+const Client = main_mod.Client;
+
+/// The protocol's limit on tabs in one commit.
+const max_tabs = 128;
+
+pub fn attach(self: *Client) void {
+ self.tabs.attach(self.surface, .{
+ .ctx = self,
+ .select = select,
+ .close = close,
+ .new_tab = newTab,
+ .move = move,
+ .leading = leading,
+ });
+}
+
+fn client(ctx: *anyopaque) *Client {
+ return @ptrCast(@alignCast(ctx));
+}
+
+fn select(ctx: *anyopaque, id: u32) void {
+ const app = &client(ctx).app;
+ if (app.close_tab != null) return;
+ const i = app.findTab(id) orelse return;
+ app.selected = i;
+ app.changed();
+}
+
+fn close(ctx: *anyopaque, id: u32) void {
+ const app = &client(ctx).app;
+ if (app.close_tab != null) return;
+ const i = app.findTab(id) orelse return;
+ app.selected = i;
+ app.changed();
+ app.closeSelected();
+}
+
+fn newTab(ctx: *anyopaque) void {
+ client(ctx).app.act(.new);
+}
+
+/// Moves the tab so that it ends at `index`; the selection stays on the same tab.
+fn move(ctx: *anyopaque, id: u32, index: u32) void {
+ const app = &client(ctx).app;
+ if (app.close_tab != null or app.tabs.items.len == 0) return;
+ const from = app.findTab(id) orelse return;
+ const to = @min(@as(usize, index), app.tabs.items.len - 1);
+ if (from == to) return;
+ const selected_id = app.tabs.items[app.selected].id;
+ const tab = app.tabs.orderedRemove(from);
+ app.tabs.insertAssumeCapacity(to, tab);
+ app.selected = app.findTab(selected_id) orelse 0;
+ app.changed();
+}
+
+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.
+ app.openMenuAt(@floatFromInt(@max(0, x)));
+}
+
+/// Shows the strip in the titlebar or takes it back, and keeps it current.
+/// Cheap to call often: an unchanged strip sends nothing.
+pub fn sync(self: *Client) void {
+ const app = &self.app;
+ const want = self.tabs.strip != null and self.decoration_mode == .server_side and
+ !self.fullscreen and app.tabs.items.len > 0 and app.tabs.items.len <= max_tabs;
+ if (app.strip_in_titlebar != want) {
+ app.strip_in_titlebar = want;
+ app.dirty = true;
+ // Sizes and the view's top depend on it.
+ app.changed();
+ }
+ if (self.tabs.strip == null) return;
+ if (!want) {
+ self.tabs.publish(&.{}, 0, false);
+ return;
+ }
+ var list: [max_tabs]ClientTabs.Tab = undefined;
+ for (app.tabs.items, 0..) |t, i| {
+ const name = t.name();
+ list[i] = .{
+ .id = @truncate(t.id),
+ .title = if (std.unicode.utf8ValidateSlice(name)) name else tr("Text file"),
+ .modified = t.doc.modified(),
+ };
+ }
+ self.tabs.publish(list[0..app.tabs.items.len], @truncate(app.tabs.items[app.selected].id), true);
+}
diff --git a/src/editor/client_window.zig b/src/editor/client_window.zig
index edff8f0..d673cb9 100644
--- a/src/editor/client_window.zig
+++ b/src/editor/client_window.zig
@@ -17,6 +17,7 @@ pub fn registryEvent(registry: *wl.Registry, event: wl.Registry.Event, self: *Cl
.global => |g| {
const iface = std.mem.span(g.interface);
self.cursor_shape.bind(registry, g.name, iface, g.version);
+ self.tabs.bind(registry, g.name, iface, g.version);
if (std.mem.eql(u8, iface, "zwp_text_input_manager_v3")) self.ime.manager = registry.bind(g.name, @import("wayland").client.zwp.TextInputManagerV3, 1) catch null;
if (std.mem.eql(u8, iface, "wl_data_device_manager")) self.clipboard.manager = registry.bind(g.name, wl.DataDeviceManager, @min(g.version, 3)) catch null;
if (std.mem.eql(u8, iface, "wl_compositor")) {
diff --git a/src/editor/main.zig b/src/editor/main.zig
index d4af7f9..9e96cf9 100644
--- a/src/editor/main.zig
+++ b/src/editor/main.zig
@@ -2,6 +2,8 @@
const std = @import("std");
const platform = @import("platform");
const ClientCursor = @import("ui").client_cursor;
+const ClientTabs = @import("ui").client_tabs;
+const client_tabs = @import("client_tabs.zig");
const wl = @import("wayland").client.wl;
const xdg = @import("wayland").client.xdg;
const zxdg = @import("wayland").client.zxdg;
@@ -55,6 +57,7 @@ pub const Client = struct {
pointer_x: f64 = 0,
pointer_y: f64 = 0,
cursor_shape: ClientCursor = .{},
+ tabs: ClientTabs = .{},
cursor_surface: *wl.Surface = undefined,
cursor_theme: ?*wl.CursorTheme = null,
cursor_serial: u32 = 0,
@@ -243,6 +246,11 @@ pub fn run(init: std.process.Init, paths: []const [:0]const u8, instance: *@impo
if (self.cursor_shape.manager == null) self.cursor_theme = ClientCursor.loadFallback(self.shm.?);
defer if (self.cursor_theme) |theme_ptr| theme_ptr.destroy();
+ // Before the shell role, like the decoration: the compositor adopts the strip
+ // when the toplevel appears. It stays hidden until `client_tabs.sync` commits.
+ client_tabs.attach(&self);
+ defer self.tabs.deinit();
+
self.xdg_surface = try self.wm_base.?.getXdgSurface(self.surface);
defer self.xdg_surface.destroy();
self.xdg_surface.setListener(*Client, Client.xdgSurfaceEvent, &self);
@@ -278,6 +286,7 @@ pub fn run(init: std.process.Init, paths: []const [:0]const u8, instance: *@impo
defer a.free(pending_token);
while (self.running) {
self.app.sync();
+ client_tabs.sync(&self);
self.app.stepDrafts(key_repeat.nowMs());
// The session is ending: leave without asking; `deinit` writes the drafts.
if (self.app.signaled()) self.app.closed = true;