Skip to content

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

Add persistent taskbar chip ordering

Mel140098d36d06
diff --git a/src/Taskbar.zig b/src/Taskbar.zig
index 699e2f1..65afa0c 100644
--- a/src/Taskbar.zig
+++ b/src/Taskbar.zig
@@ -162,6 +162,9 @@ capture_sessions: usize = 0,
start: start_button.State = .{},
start_logo: ?icon_service.Entry = null,
chips: std.ArrayList(ChipState) = .empty,
+// Stable window IDs in taskbar presentation order. This is deliberately
+// independent of `world.toplevels`, which remains the focus/raise stack.
+chip_order: std.ArrayList(u64) = .empty,
tray: [tray_count]TrayState = .{ .{ .kind = .volume }, .{ .kind = .wifi }, .{ .kind = .power }, .{ .kind = .wired }, .{ .kind = .input_source } },
// Touchpad scrolling over the input source item, gathered into steps.
source_scroll: f32 = 0,
@@ -318,6 +321,7 @@ pub fn destroy(bar: *Taskbar) void {
if (chip.icon) |e| bar.server.iconRelease(e.id);
}
bar.chips.deinit(gpa);
+ bar.chip_order.deinit(gpa);
bar.tree.node.destroy();
gpa.destroy(bar);
}
@@ -521,6 +525,7 @@ pub const battery_width = battery_badge.width;
pub const refreshStartLogo = taskbar_reconcile.refreshStartLogo;
pub const updateIcon = taskbar_reconcile.updateIcon;
pub const taskbarOrderLessThan = taskbar_reconcile.taskbarOrderLessThan;
+pub const chipOrderIndex = taskbar_reconcile.chipOrderIndex;
pub const reconcile = taskbar_reconcile.reconcile;
// taskbar/tick.zig
diff --git a/src/taskbar/reconcile.zig b/src/taskbar/reconcile.zig
index 851fda6..43650e5 100644
--- a/src/taskbar/reconcile.zig
+++ b/src/taskbar/reconcile.zig
@@ -68,6 +68,17 @@ pub fn taskbarOrderLessThan(_: void, a: *Toplevel, b: *Toplevel) bool {
return (if (a.tab_group != 0) a.tab_group else a.id) < (if (b.tab_group != 0) b.tab_group else b.id);
}
+/// Returns a window's position in the taskbar-owned order, if present.
+pub fn chipOrderIndex(order: []const u64, id: u64) ?usize {
+ return std.mem.indexOfScalar(u64, order, id);
+}
+
+fn chipOrderLessThan(order: []const u64, a: *Toplevel, b: *Toplevel) bool {
+ const a_index = chipOrderIndex(order, a.id) orelse return taskbarOrderLessThan({}, a, b);
+ const b_index = chipOrderIndex(order, b.id) orelse return taskbarOrderLessThan({}, a, b);
+ return a_index < b_index;
+}
+
// Returns whether any chip needs a frame: a new raster, a FLIP, or removal.
pub fn reconcile(bar: *Taskbar, now_ms: i64) bool {
var changed = false;
@@ -80,6 +91,43 @@ pub fn reconcile(bar: *Taskbar, now_ms: i64) bool {
}
std.mem.sort(*Toplevel, visible.items, {}, taskbarOrderLessThan);
+ // Keep order entries for live windows even while they are hidden, on
+ // another workspace/output, or temporarily excluded from the taskbar.
+ // Only a window that has left the world is removed.
+ var order_index: usize = 0;
+ while (order_index < bar.chip_order.items.len) {
+ const id = bar.chip_order.items[order_index];
+ var live = false;
+ var world_it = bar.server.world.toplevels.iterator(.forward);
+ while (world_it.next()) |toplevel| {
+ if (toplevel.id == id) {
+ live = true;
+ break;
+ }
+ }
+ if (live) {
+ order_index += 1;
+ } else {
+ _ = bar.chip_order.orderedRemove(order_index);
+ }
+ }
+
+ // Seed new windows in the existing stable order. Once a window has an
+ // entry, later focus changes and reconciliation cannot move it.
+ var custom_order_ready = true;
+ bar.chip_order.ensureTotalCapacity(gpa, bar.chip_order.items.len + visible.items.len) catch {
+ log.err("reconcile: could not reserve taskbar order", .{});
+ custom_order_ready = false;
+ };
+ if (custom_order_ready) {
+ for (visible.items) |toplevel| {
+ if (chipOrderIndex(bar.chip_order.items, toplevel.id) == null) {
+ bar.chip_order.appendAssumeCapacity(toplevel.id);
+ }
+ }
+ std.mem.sort(*Toplevel, visible.items, bar.chip_order.items, chipOrderLessThan);
+ }
+
const layout = bar.calculateLayout(visible.items.len);
if (!std.meta.eql(layout, bar.item_layout)) {
bar.item_layout = layout;
diff --git a/src/taskbar/tests.zig b/src/taskbar/tests.zig
index 9358a56..13e0d31 100644
--- a/src/taskbar/tests.zig
+++ b/src/taskbar/tests.zig
@@ -37,6 +37,15 @@ const chipTitleRect = Taskbar.chipTitleRect;
const drawChipTitle = Taskbar.drawChipTitle;
const paintChipTitleColumns = Taskbar.paintChipTitleColumns;
+test "taskbar chip order looks up stable window IDs" {
+ const order = [_]u64{ 42, 7, 19 };
+
+ try std.testing.expectEqual(@as(?usize, 0), Taskbar.chipOrderIndex(&order, 42));
+ try std.testing.expectEqual(@as(?usize, 1), Taskbar.chipOrderIndex(&order, 7));
+ try std.testing.expectEqual(@as(?usize, 2), Taskbar.chipOrderIndex(&order, 19));
+ try std.testing.expectEqual(@as(?usize, null), Taskbar.chipOrderIndex(&order, 99));
+}
+
test "chip/tile/tray sizing matches default theme" {
const saved = ui_theme.global;
defer ui_theme.global = saved;