Skip to content

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

titlebar tabs v2: drag a tab out of the window (tear_off, drop_on)

Claude46f168a9801c
diff --git a/build/protocols.zig b/build/protocols.zig
index bd4b542..cce78b6 100644
--- a/build/protocols.zig
+++ b/build/protocols.zig
@@ -57,7 +57,7 @@ pub fn module(b: *std.Build) *std.Build.Module {
scanner.generate("wp_color_manager_v1", 2);
scanner.generate("zxdg_decoration_manager_v1", 1);
scanner.generate("org_kde_kwin_server_decoration_manager", 1);
- scanner.generate("rediwm_titlebar_tabs_manager_v1", 1);
+ scanner.generate("rediwm_titlebar_tabs_manager_v1", 2);
scanner.generate("zwp_virtual_keyboard_manager_v1", 1);
scanner.generate("zwp_pointer_gestures_v1", 3);
scanner.generate("zxdg_output_manager_v1", 3);
diff --git a/docs/plans/titlebar-tabs.md b/docs/plans/titlebar-tabs.md
index d220e84..1631922 100644
--- a/docs/plans/titlebar-tabs.md
+++ b/docs/plans/titlebar-tabs.md
@@ -302,6 +302,21 @@ protocol stays compatible.
* Files: a tab is a folder view, cheap to move once per-tab state is split
from `App` (the Files phase already needs it).
+### As built in 6a
+
+* Version 2 adds `set_detachable(enabled)` (a client opts in; the default is off so
+ an app that ignores the events never starts a drag-out), `tear_off` and
+ `drop_on`. The manager is version 2; strips take the manager's version.
+* `tear_off` is not sent for the last tab of a strip. `drop_on` is only sent
+ between strips of one `wl_client`.
+* `Drag.out` is set when the pointer is more than half a titlebar above or one and a
+ half below the strip; while out, the strip shows no reorder preview. The target
+ strip shows a gap and an accent marker (`Strip.drop_index`).
+* Placement: `Manager.tear_off` remembers the client and pointer for two seconds;
+ `lifecycle.zig` opens the client's next toplevel with its tab area under the pointer.
+* Client helper: `Handler.tear_off` / `Handler.drop_on` are optional; providing
+ either turns `set_detachable` on.
+
### Phases
6a. Protocol v2, compositor drag states, drop marker, placement hint, tests on
diff --git a/protocol/rediwm-titlebar-tabs-v1.xml b/protocol/rediwm-titlebar-tabs-v1.xml
index cd3d808..780258b 100644
--- a/protocol/rediwm-titlebar-tabs-v1.xml
+++ b/protocol/rediwm-titlebar-tabs-v1.xml
@@ -11,7 +11,7 @@
Private to rediwm and its bundled applications.
</description>
- <interface name="rediwm_titlebar_tabs_manager_v1" version="1">
+ <interface name="rediwm_titlebar_tabs_manager_v1" version="2">
<description summary="titlebar tabs factory"/>
<request name="destroy" type="destructor">
@@ -36,7 +36,7 @@
</enum>
</interface>
- <interface name="rediwm_titlebar_tabs_v1" version="1">
+ <interface name="rediwm_titlebar_tabs_v1" version="2">
<description summary="a toplevel's titlebar tab strip">
State is double-buffered: add_tab, set_selected and the like build a
pending strip that replaces the shown one on commit. The pending state
@@ -96,6 +96,16 @@
<arg name="enabled" type="uint" summary="0 hides the button, anything else shows it"/>
</request>
+ <request name="set_detachable" since="2">
+ <description summary="allow tabs to be dragged out of the strip">
+ With this on, dragging a tab well away from the strip and releasing
+ sends tear_off or drop_on instead of nothing. Applies at once, not on
+ commit. Off by default: an application that does not handle the two
+ events must not turn it on.
+ </description>
+ <arg name="enabled" type="uint" summary="0 is off, anything else on"/>
+ </request>
+
<request name="commit">
<description summary="show the pending strip">
Replaces the shown strip. A commit with no tabs removes the strip and
@@ -132,6 +142,33 @@
<arg name="index" type="uint"/>
</event>
+ <event name="tear_off" since="2">
+ <description summary="the user dragged a tab out and let go in empty space">
+ Only sent while set_detachable is on, and not for the last tab of a
+ strip. The position is the pointer in layout coordinates. The
+ application opens a new toplevel holding the tab and removes it from
+ this strip; the compositor places the next toplevel of this client near
+ the pointer. Like select, it only asks: if the application does
+ nothing, the tab stays.
+ </description>
+ <arg name="id" type="uint"/>
+ <arg name="x" type="int"/>
+ <arg name="y" type="int"/>
+ </event>
+
+ <event name="drop_on" since="2">
+ <description summary="the user dropped a tab on another strip of this client">
+ Only sent while set_detachable is on. The target is another strip of
+ the same client, never of another application. The index is the place
+ the tab takes among the target's tabs, counted as in move (0 is
+ before the first). The application moves the tab and commits both
+ strips.
+ </description>
+ <arg name="id" type="uint"/>
+ <arg name="target" type="object" interface="rediwm_titlebar_tabs_v1"/>
+ <arg name="index" type="uint"/>
+ </event>
+
<event name="leading_activated">
<description summary="the user pressed the leading button">
The button's rectangle is in the surface's coordinates. The titlebar
diff --git a/src/Input.zig b/src/Input.zig
index a8abac0..84d3da1 100644
--- a/src/Input.zig
+++ b/src/Input.zig
@@ -217,6 +217,8 @@ hovered_taskbar: ?*Taskbar = null,
taskbar_drag: ?TaskbarDragSession = null,
/// The window whose published tab is pressed (a click, or a reorder once the pointer travels).
tab_drag: ?*Toplevel = null,
+/// The window whose strip shows the drop marker for that drag.
+tab_drop_target: ?*Toplevel = null,
tray_buttons: u32 = 0,
taskbar_close_buttons: u32 = 0,
hovered_toast: ?*@import("notifications/toast.zig").Toast = null,
@@ -529,7 +531,11 @@ pub fn detachOutput(input: *Input, output: *Output) void {
pub fn detachToplevel(input: *Input, toplevel: *Toplevel) void {
if (input.window_menu.references(toplevel)) input.window_menu.close();
- if (input.tab_drag == toplevel) input.tab_drag = null;
+ if (input.tab_drag == toplevel) {
+ @import("input/tab_drop.zig").clear(input);
+ input.tab_drag = null;
+ }
+ if (input.tab_drop_target == toplevel) input.tab_drop_target = null;
if (input.taskbar_drag) |session| {
if (session.window_id == toplevel.id) {
input.taskbar_drag = null;
diff --git a/src/TitlebarTabs.zig b/src/TitlebarTabs.zig
index 8d789d4..dc27c9f 100644
--- a/src/TitlebarTabs.zig
+++ b/src/TitlebarTabs.zig
@@ -43,13 +43,21 @@ pending_leading: bool = false,
items: std.ArrayList(Item) = .empty,
selected: u32 = 0,
leading: bool = false,
+/// The client handles tear_off and drop_on (set_detachable).
+detachable: bool = false,
+
+/// Where the compositor put the pointer when it asked a client to tear a tab
+/// off, so the window that follows opens there.
+pub const TearOff = struct { client: *wl.Client, x: f64, y: f64, deadline_ms: i64 };
+const tear_off_window_ms = 2000;
pub const Manager = struct {
global: *wl.Global,
+ tear_off: ?TearOff = null,
strips: wl.list.Head(TitlebarTabs, .link) = undefined,
pub fn init(self: *Manager, server: *Server) !void {
- self.* = .{ .global = try wl.Global.create(server.wl_server, ManagerResource, 1, *Manager, self, bind) };
+ self.* = .{ .global = try wl.Global.create(server.wl_server, ManagerResource, 2, *Manager, self, bind) };
self.strips.init();
}
@@ -58,6 +66,19 @@ pub const Manager = struct {
while (self.strips.first()) |strip| strip.resource.destroy();
}
+ /// Where to open `toplevel` if it is the window a tear-off asked for (once).
+ pub fn takePlacement(self: *Manager, toplevel: *Toplevel) ?struct { x: f64, y: f64 } {
+ const pending = self.tear_off orelse return null;
+ const surface = toplevel.surface() orelse return null;
+ if (Toplevel.nowMs() > pending.deadline_ms) {
+ self.tear_off = null;
+ return null;
+ }
+ if (surface.resource.getClient() != pending.client) return null;
+ self.tear_off = null;
+ return .{ .x = pending.x, .y = pending.y };
+ }
+
/// Called for each new toplevel: adopts a strip created before the shell role.
pub fn attach(self: *Manager, toplevel: *Toplevel) void {
var it = self.strips.iterator(.forward);
@@ -94,7 +115,7 @@ pub const Manager = struct {
manager.getClient().postNoMemory();
return;
};
- const resource = Resource.create(manager.getClient(), 1, args.id) catch {
+ const resource = Resource.create(manager.getClient(), manager.getVersion(), args.id) catch {
gpa.destroy(strip);
manager.getClient().postNoMemory();
return;
@@ -155,6 +176,23 @@ pub fn sendClose(self: *TitlebarTabs, id: u64) void {
self.resource.sendClose(@intCast(id));
}
+/// Asks the client to move `id` into a new window. `x`, `y` are layout
+/// coordinates of the pointer. False when the client cannot be asked.
+pub fn sendTearOff(self: *TitlebarTabs, manager: *Manager, id: u64, x: f64, y: f64) bool {
+ if (!self.detachable or self.resource.getVersion() < 2 or self.items.items.len < 2) return false;
+ manager.tear_off = .{ .client = self.resource.getClient(), .x = x, .y = y, .deadline_ms = Toplevel.nowMs() + tear_off_window_ms };
+ self.resource.sendTearOff(@intCast(id), @intFromFloat(@round(x)), @intFromFloat(@round(y)));
+ return true;
+}
+
+/// Asks the client to move `id` into `target`, another strip of the same client.
+pub fn sendDropOn(self: *TitlebarTabs, id: u64, target: *TitlebarTabs, index: usize) bool {
+ if (!self.detachable or self.resource.getVersion() < 2) return false;
+ if (self.resource.getClient() != target.resource.getClient()) return false;
+ self.resource.sendDropOn(@intCast(id), target.resource, @intCast(index));
+ return true;
+}
+
pub fn sendNewTab(self: *TitlebarTabs) void {
self.resource.sendNewTab();
}
@@ -192,6 +230,7 @@ fn handleStripRequest(resource: *Resource, request: Resource.Request, self: *Tit
.add_tab => |args| self.addTab(args.id, std.mem.span(args.title), args.flags),
.set_selected => |args| self.pending_selected = args.id,
.set_leading_button => |args| self.pending_leading = args.enabled != 0,
+ .set_detachable => |args| self.detachable = args.enabled != 0,
.commit => self.commit(),
}
}
diff --git a/src/Toplevel.zig b/src/Toplevel.zig
index e998d16..f0a4caa 100644
--- a/src/Toplevel.zig
+++ b/src/Toplevel.zig
@@ -182,6 +182,8 @@ tab_first: usize = 0,
tab_hover: u64 = 0,
/// A press on a published tab: a click until the pointer travels, then a reorder.
tab_drag: @import("ui").widgets.title_tabs.Drag = .{},
+/// Another window's tab is being dragged over this strip: the slot it would take.
+tab_drop: ?usize = null,
/// Digest of the last window record sent to IPC subscribers; most commits
/// only change pixels, and `events.onWindowChanged` skips those.
@@ -610,3 +612,7 @@ pub const tabPress = toplevel_tabs.tabPress;
pub const tabMotion = toplevel_tabs.tabMotion;
pub const tabRelease = toplevel_tabs.tabRelease;
pub const activateLeading = toplevel_tabs.activateLeading;
+pub const DropTarget = toplevel_tabs.DropTarget;
+pub const tabDropIndex = toplevel_tabs.tabDropIndex;
+pub const setTabDrop = toplevel_tabs.setTabDrop;
+pub const tabDetach = toplevel_tabs.tabDetach;
diff --git a/src/chrome_tabs.zig b/src/chrome_tabs.zig
index b0193a1..e2e156b 100644
--- a/src/chrome_tabs.zig
+++ b/src/chrome_tabs.zig
@@ -9,6 +9,8 @@ const icon_service = @import("icon_service.zig");
const chrome = @import("chrome.zig");
const window_chrome = @import("ui").window_chrome;
const Rect = button.Rect;
+/// How far tabs step aside for a tab dropped between them.
+const drop_gap: f32 = 14;
const title_tabs = @import("ui").widgets.title_tabs;
// The model, geometry, hit testing and hashing live in the shared toolkit
@@ -34,7 +36,11 @@ pub fn paint(comptime C: type, pixels: []u32, width: i32, height: i32, logical_w
// The tab being dragged is painted last, over its neighbours.
for ([_]bool{ false, true }) |dragged_pass| for (strip.tabs[g.first..][0..g.count], 0..) |t, i| {
if ((strip.drag.dragging() and t.id == strip.drag.id) != dragged_pass) continue;
- const rect = strip.drag.rect(g, i);
+ var rect = strip.drag.rect(g, i);
+ // A tab being dragged in from another window opens a gap before its slot.
+ if (strip.drop_index) |slot| if (g.first + i >= slot) {
+ rect.x += drop_gap * density;
+ };
const radius = window_chrome.cornerRadius(theme.global.chrome_round_buttons, rect.w, rect.h, theme.global.radius * density * 0.8);
// The selected tab is the window's content colour, grown down into the
// content with an accent line, so it reads as the open page; the others
@@ -54,17 +60,7 @@ pub fn paint(comptime C: type, pixels: []u32, width: i32, height: i32, logical_w
const c = style.pixel(C, (@as(f32, @floatFromInt(x)) + 0.5) / scale, (@as(f32, @floatFromInt(y)) + 0.5) / scale, shape.x, shape.y, shape.w, shape.h, radius, scale, fill, border);
pixels[index] = c.over(C.fromPremultiplied(pixels[index])).argb();
};
- if (t.active) {
- const accent = C.fromRgba(theme.shellPalette().accent);
- const ux0: usize = @max(clip_left, @as(usize, @intFromFloat(@max(0, @floor((rect.x + 8 * density) * scale)))));
- const ux1: usize = @min(clip_right, @as(usize, @intFromFloat(@max(0, @min(@as(f32, @floatFromInt(width)), @ceil((rect.x + rect.w - 8 * density) * scale))))));
- const uy1: usize = @intFromFloat(@max(0, @min(@as(f32, @floatFromInt(height)), @floor(band * scale))));
- const uy0: usize = uy1 -| @as(usize, @intFromFloat(@max(1, @round(2 * scale))));
- if (ux1 > ux0) for (uy0..uy1) |y| for (ux0..ux1) |x| {
- const index = y * @as(usize, @intCast(width)) + x;
- pixels[index] = accent.over(C.fromPremultiplied(pixels[index])).argb();
- };
- }
+ if (t.active) fillBox(C, pixels, width, height, scale, clip_left, clip_right, rect.x + 8 * density, band - 2 * density, rect.x + rect.w - 8 * density, band, theme.shellPalette().accent);
const size = @min(20 * density, rect.h - 6);
var left = rect.x + 6 * density;
if (icon) |image| {
@@ -80,6 +76,10 @@ pub fn paint(comptime C: type, pixels: []u32, width: i32, height: i32, logical_w
});
button.paint(&renderer, close, .{ .variant = .chrome, .size = .sm, .icon = .close, .icon_scale = 0.39, .label = i18n.tr("Close tab") }, .{ .pointer = if (strip.hover == key(.{ .close = t.id })) .hover else .idle });
};
+ if (strip.drop_index) |slot| {
+ const x = g.x + @as(f32, @floatFromInt(slot -| g.first)) * (g.width + 4) - 2 * density + drop_gap * density / 2 - 1.5 * density;
+ fillBox(C, pixels, width, height, scale, clip_left, clip_right, x, g.y + 4 * density, x + 3 * density, g.y + g.height - 4 * density, theme.shellPalette().accent);
+ }
if (g.leading) |r| {
button.paint(&renderer, r, .{ .variant = .ghost, .size = .sm, .radius = navigationRadius(r) }, .{ .pointer = if (strip.hover == key(.leading)) .hover else .idle });
const side = @min(20 * density, r.h - 6);
@@ -92,6 +92,20 @@ pub fn paint(comptime C: type, pixels: []u32, width: i32, height: i32, logical_w
if (g.next) |r| button.paint(&renderer, r, .{ .variant = .ghost, .size = .sm, .icon = .chevron_right, .radius = navigationRadius(r) }, .{ .pointer = if (strip.hover == key(.next)) .hover else .idle });
}
+/// A flat box in logical pixels, blended over what is there.
+fn fillBox(comptime C: type, pixels: []u32, width: i32, height: i32, scale: f32, clip_left: usize, clip_right: usize, x0: f32, y0: f32, x1: f32, y1: f32, color: [4]f32) void {
+ const src = C.fromRgba(color);
+ const px0: usize = @max(clip_left, @as(usize, @intFromFloat(@max(0, @floor(x0 * scale)))));
+ const px1: usize = @min(clip_right, @as(usize, @intFromFloat(@max(0, @min(@as(f32, @floatFromInt(width)), @ceil(x1 * scale))))));
+ const py0: usize = @intFromFloat(@max(0, @floor(y0 * scale)));
+ const py1: usize = @intFromFloat(@max(0, @min(@as(f32, @floatFromInt(height)), @ceil(y1 * scale))));
+ if (px1 <= px0 or py1 <= py0) return;
+ for (py0..py1) |y| for (px0..px1) |x| {
+ const index = y * @as(usize, @intCast(width)) + x;
+ pixels[index] = src.over(C.fromPremultiplied(pixels[index])).argb();
+ };
+}
+
fn navigationRadius(rect: Rect) f32 {
return window_chrome.cornerRadius(theme.global.chrome_round_buttons, rect.w, rect.h, theme.global.radius);
}
diff --git a/src/input/buttons.zig b/src/input/buttons.zig
index e2bbd5a..b1a4176 100644
--- a/src/input/buttons.zig
+++ b/src/input/buttons.zig
@@ -213,7 +213,7 @@ pub fn processButton(
if (state == .released and button == btn_left) {
if (input.tab_drag) |toplevel| {
input.tab_drag = null;
- toplevel.tabRelease();
+ @import("tab_drop.zig").finish(input, toplevel);
input.processCursorMotion(time_msec);
return;
}
diff --git a/src/input/motion.zig b/src/input/motion.zig
index b47d285..ab995a2 100644
--- a/src/input/motion.zig
+++ b/src/input/motion.zig
@@ -276,8 +276,14 @@ pub fn processCursorMotion(input: *Input, time_msec: u32) void {
pub fn passthroughMotion(input: *Input, time_msec: u32) void {
if (input.tab_drag) |toplevel| {
const local = toplevel.frameLocal(input.cursor.x, input.cursor.y);
- if (toplevel.tabMotion(local.x)) input.setNamedCursor("grabbing");
- if (!toplevel.tab_drag.pressed()) input.tab_drag = null;
+ const dragging = toplevel.tabMotion(local.x, local.y);
+ if (toplevel.tab_drag.pressed()) {
+ @import("tab_drop.zig").update(input, toplevel);
+ } else {
+ @import("tab_drop.zig").clear(input);
+ input.tab_drag = null;
+ }
+ if (dragging) input.setNamedCursor("grabbing");
return;
}
if (input.taskbar_drag) |*session| {
diff --git a/src/input/tab_drop.zig b/src/input/tab_drop.zig
new file mode 100644
index 0000000..c278c6d
--- /dev/null
+++ b/src/input/tab_drop.zig
@@ -0,0 +1,51 @@
+//! A tab dragged out of its window: which strip of the same client it hovers,
+//! the marker that strip shows, and the request sent when the button comes up.
+const Input = @import("../Input.zig");
+const Toplevel = @import("../Toplevel.zig");
+const scene_data = @import("../scene_data.zig");
+
+/// The strip under the pointer that may take the tab, if the drag is out.
+pub fn target(input: *Input, source: *Toplevel) ?Toplevel.DropTarget {
+ if (!source.tab_drag.out) return null;
+ const own = source.titlebar_tabs orelse return null;
+ if (!own.detachable) return null;
+ const res = switch (scene_data.hitTest(input.server, input.cursor.x, input.cursor.y)) {
+ .chrome => |chrome| chrome,
+ else => return null,
+ };
+ if (res.toplevel == source) return null;
+ const other = res.toplevel.titlebar_tabs orelse return null;
+ if (!other.active() or !other.detachable) return null;
+ // Never another application's strip.
+ if (other.resource.getClient() != own.resource.getClient()) return null;
+ if (res.sy >= @as(f64, @floatFromInt(res.toplevel.titlebarHeight()))) return null;
+ const index = res.toplevel.tabDropIndex(res.sx) orelse return null;
+ return .{ .toplevel = res.toplevel, .index = index };
+}
+
+/// Keeps the drop marker on the strip under the pointer, and only there.
+pub fn update(input: *Input, source: *Toplevel) void {
+ const found = target(input, source);
+ if (input.tab_drop_target) |previous| {
+ if (found == null or found.?.toplevel != previous) {
+ previous.setTabDrop(null);
+ input.tab_drop_target = null;
+ }
+ }
+ if (found) |f| {
+ f.toplevel.setTabDrop(f.index);
+ input.tab_drop_target = f.toplevel;
+ }
+}
+
+pub fn clear(input: *Input) void {
+ if (input.tab_drop_target) |previous| previous.setTabDrop(null);
+ input.tab_drop_target = null;
+}
+
+/// The button came up on a pressed tab.
+pub fn finish(input: *Input, source: *Toplevel) void {
+ const found = target(input, source);
+ clear(input);
+ source.tabDetach(found, input.cursor.x, input.cursor.y);
+}
diff --git a/src/toplevel/lifecycle.zig b/src/toplevel/lifecycle.zig
index 5d76abc..8873945 100644
--- a/src/toplevel/lifecycle.zig
+++ b/src/toplevel/lifecycle.zig
@@ -165,7 +165,11 @@ pub fn handleMapped(toplevel: *Toplevel) void {
if (target_out) |out| toplevel.setMaximizedOn(out);
toplevel.placed = true;
} else if (!toplevel.placed) {
- if (target_out) |output| {
+ // A tab torn off another window of this client opens under the pointer.
+ if (toplevel.server.shell.titlebar_tabs.takePlacement(toplevel)) |at| {
+ toplevel.setPosition(@intFromFloat(@round(at.x - 100)), @intFromFloat(@round(at.y - @as(f64, @floatFromInt(toplevel.titlebarHeight())) / 2)));
+ toplevel.placed = true;
+ } else if (target_out) |output| {
const frame_w = @as(i32, @intFromFloat(@round(@as(f64, @floatFromInt(toplevel.chrome_width)) * toplevel.zoom())));
const frame_h = @as(i32, @intFromFloat(@round(@as(f64, @floatFromInt(toplevel.chrome_height)) * toplevel.zoom())));
const geom = pickInitialPlacement(toplevel.server, output, toplevel.open_rules, toplevel.parentWindow(), frame_w, frame_h);
diff --git a/src/toplevel/tabs.zig b/src/toplevel/tabs.zig
index 7dc6976..76be629 100644
--- a/src/toplevel/tabs.zig
+++ b/src/toplevel/tabs.zig
@@ -14,7 +14,7 @@ pub fn hasTabStrip(toplevel: *const Toplevel) bool {
/// `storage` may be null for a window without a strip, which has no tabs.
pub fn tabStrip(toplevel: *Toplevel, storage: ?*[@import("../window_tabs.zig").max_tabs]@import("../chrome_tabs.zig").Tab) @import("../chrome_tabs.zig").Strip {
- var strip: @import("../chrome_tabs.zig").Strip = .{ .first = toplevel.tab_first, .hover = toplevel.tab_hover, .notice = toplevel.tab_notice, .drag = toplevel.tab_drag };
+ var strip: @import("../chrome_tabs.zig").Strip = .{ .first = toplevel.tab_first, .hover = toplevel.tab_hover, .notice = toplevel.tab_notice, .drag = toplevel.tab_drag, .drop_index = toplevel.tab_drop };
const selected = if (storage) |tabs| fillTabs(toplevel, tabs, &strip) else 0;
const geom = @import("../chrome_tabs.zig").geometry(toplevel.chrome_width, toplevel.chromeDensity(), strip);
if (selected < strip.first) strip.first = selected;
@@ -87,7 +87,9 @@ pub fn tabPress(toplevel: *Toplevel, sx: f64, sy: f64, lx: f64) bool {
/// The pointer moved while a tab is pressed; true once that is a reorder. A
/// strip that changed under the press (the tab is gone, or the strip) ends it.
-pub fn tabMotion(toplevel: *Toplevel, lx: f64) bool {
+/// `ly` is the pointer's y in the frame's coordinates: far from the strip, a
+/// drag of a detachable tab turns into one that leaves the window.
+pub fn tabMotion(toplevel: *Toplevel, lx: f64, ly: f64) bool {
if (!toplevel.tab_drag.pressed()) return false;
const alive = if (toplevel.titlebar_tabs) |published| published.active() and published.has(toplevel.tab_drag.id) else false;
if (!alive) {
@@ -96,7 +98,12 @@ pub fn tabMotion(toplevel: *Toplevel, lx: f64) bool {
if (was_dragging) toplevel.syncChrome(false, nowMs()) catch {};
return false;
}
- if (toplevel.tab_drag.motion(@floatCast(lx), toplevel.chromeDensity())) toplevel.syncChrome(false, nowMs()) catch {};
+ var changed = toplevel.tab_drag.motion(@floatCast(lx), toplevel.chromeDensity());
+ if (toplevel.titlebar_tabs.?.detachable) {
+ const band: f32 = @floatFromInt(toplevel.titlebarHeight());
+ if (toplevel.tab_drag.pointerY(@floatCast(ly), band)) changed = true;
+ }
+ if (changed) toplevel.syncChrome(false, nowMs()) catch {};
return toplevel.tab_drag.dragging();
}
@@ -131,3 +138,41 @@ pub fn activateLeading(toplevel: *Toplevel) void {
const r = g.leading orelse return;
published.sendLeadingActivated(r.x, r.y, r.w, r.h, toplevel.borderWidth(), toplevel.titlebarHeight());
}
+
+/// Another strip of the same client that a dragged-out tab would join.
+pub const DropTarget = struct { toplevel: *Toplevel, index: usize };
+
+/// Where a tab dropped at `sx` (frame coordinates) would go in this window's
+/// strip; null when it has none.
+pub fn tabDropIndex(toplevel: *Toplevel, sx: f64) ?usize {
+ const chrome_tabs = @import("../chrome_tabs.zig");
+ const published = toplevel.titlebar_tabs orelse return null;
+ if (!published.active()) return null;
+ var storage: [@import("../window_tabs.zig").max_tabs]chrome_tabs.Tab = undefined;
+ const strip = toplevel.tabStrip(&storage);
+ const g = chrome_tabs.geometry(toplevel.chrome_width, toplevel.chromeDensity(), strip);
+ return @import("ui").widgets.title_tabs.dropIndex(g, @floatCast(sx));
+}
+
+/// Shows (or removes) the marker for a tab about to be dropped here.
+pub fn setTabDrop(toplevel: *Toplevel, index: ?usize) void {
+ if (toplevel.tab_drop == index) return;
+ toplevel.tab_drop = index;
+ toplevel.syncChrome(false, nowMs()) catch {};
+}
+
+/// The button came up after the tab left the window: ask the client to move it
+/// into `target`, or into a new window of its own at the pointer (`x`, `y`,
+/// layout coordinates). Either only asks; the strip changes when the client
+/// commits. A tab that cannot be asked for simply stays.
+pub fn tabDetach(toplevel: *Toplevel, target: ?DropTarget, x: f64, y: f64) void {
+ if (!toplevel.tab_drag.out) return toplevel.tabRelease();
+ const id = toplevel.tab_drag.id;
+ if (toplevel.titlebar_tabs) |published| if (published.active() and published.has(id)) {
+ if (target) |t| {
+ if (t.toplevel.titlebar_tabs) |destination| _ = published.sendDropOn(id, destination, t.index);
+ } else _ = published.sendTearOff(&toplevel.server.shell.titlebar_tabs, id, x, y);
+ };
+ toplevel.tab_drag.cancel();
+ toplevel.syncChrome(false, nowMs()) catch {};
+}
diff --git a/src/ui/client_tabs.zig b/src/ui/client_tabs.zig
index 34df4bd..a4f25c7 100644
--- a/src/ui/client_tabs.zig
+++ b/src/ui/client_tabs.zig
@@ -28,6 +28,12 @@ pub const Handler = struct {
move: *const fn (ctx: *anyopaque, id: u32, index: u32) void,
/// The leading button's rectangle in surface coordinates (y is negative).
leading: *const fn (ctx: *anyopaque, x: i32, y: i32, width: i32, height: i32) void,
+ /// The tab was dragged out and dropped in empty space: open a window for it.
+ /// `x`, `y` are the pointer in layout coordinates. Needs a compositor
+ /// that offers version 2; set together with `drop_on`.
+ tear_off: ?*const fn (ctx: *anyopaque, id: u32, x: i32, y: i32) void = null,
+ /// The tab was dropped on `target`, another strip of this client, at `index`.
+ drop_on: ?*const fn (ctx: *anyopaque, id: u32, target: ?*rediwm.TitlebarTabsV1, index: u32) void = null,
};
manager: ?*rediwm.TitlebarTabsManagerV1 = null,
@@ -38,7 +44,7 @@ last: ?u64 = null,
pub fn bind(self: *Self, registry: *wl.Registry, name: u32, interface: []const u8, version: u32) void {
if (std.mem.eql(u8, interface, "rediwm_titlebar_tabs_manager_v1") and self.manager == null) {
- self.manager = registry.bind(name, rediwm.TitlebarTabsManagerV1, @min(version, 1)) catch null;
+ self.manager = registry.bind(name, rediwm.TitlebarTabsManagerV1, @min(version, 2)) catch null;
}
}
@@ -56,6 +62,7 @@ pub fn attach(self: *Self, surface: *wl.Surface, handler: Handler) void {
strip.setListener(*Self, event, self);
self.strip = strip;
self.handler = handler;
+ if (strip.getVersion() >= 2 and (handler.tear_off != null or handler.drop_on != null)) strip.setDetachable(1);
self.last = null;
}
@@ -93,6 +100,8 @@ fn event(_: *rediwm.TitlebarTabsV1, ev: rediwm.TitlebarTabsV1.Event, self: *Self
.new_tab => h.new_tab(h.ctx),
.move => |e| h.move(h.ctx, e.id, e.index),
.leading_activated => |e| h.leading(h.ctx, e.x, e.y, e.width, e.height),
+ .tear_off => |e| if (h.tear_off) |f| f(h.ctx, e.id, e.x, e.y),
+ .drop_on => |e| if (h.drop_on) |f| f(h.ctx, e.id, e.target, e.index),
}
}
diff --git a/src/ui/widgets/title_tabs.zig b/src/ui/widgets/title_tabs.zig
index dd82383..176a8bb 100644
--- a/src/ui/widgets/title_tabs.zig
+++ b/src/ui/widgets/title_tabs.zig
@@ -22,6 +22,9 @@ pub const Strip = struct {
leading: bool = false,
/// A reorder in progress; the painter follows it (`Drag.rect`).
drag: Drag = .{},
+ /// Another window's tab is being dragged over this strip: the gap opens
+ /// before this tab index (0..tabs.len) and a marker shows it.
+ drop_index: ?usize = null,
};
pub const Hit = union(enum) { none, tab: u64, close: u64, plus, previous, next, leading };
pub fn key(target: Hit) u64 {
@@ -100,6 +103,14 @@ pub fn hit(width: i32, density: f32, strip: Strip, x: f64, y: f64) Hit {
return .none;
}
+/// Where a tab dropped at `x` would be inserted: an index among all tabs,
+/// 0..tabs.len, within the visible ones.
+pub fn dropIndex(g: Geometry, x: f32) usize {
+ if (g.count == 0) return g.first;
+ const slot = @round((x - g.x) / (g.width + 4));
+ return g.first + @as(usize, @intFromFloat(std.math.clamp(slot, 0, @as(f32, @floatFromInt(g.count)))));
+}
+
pub fn hash(strip: Strip) u64 {
return hashImpl(strip, true);
}
@@ -117,9 +128,12 @@ fn hashImpl(strip: Strip, include_titles: bool) u64 {
const dragging = strip.drag.dragging();
h.update(std.mem.asBytes(&dragging));
if (dragging) {
+ h.update(std.mem.asBytes(&strip.drag.out));
h.update(std.mem.asBytes(&strip.drag.id));
h.update(std.mem.asBytes(&strip.drag.x));
}
+ const drop: usize = if (strip.drop_index) |d| d + 1 else 0;
+ h.update(std.mem.asBytes(&drop));
for (strip.tabs) |t| {
h.update(std.mem.asBytes(&t.id));
if (include_titles) h.update(t.title);
@@ -150,6 +164,9 @@ pub const Drag = struct {
grab: f32 = 0,
x: f32 = 0,
moved: bool = false,
+ /// The pointer is far from the strip: the tab is on its way out of the
+ /// window, so the strip shows no reorder preview.
+ out: bool = false,
pub fn pressed(d: Drag) bool {
return d.id != 0;
@@ -182,6 +199,16 @@ pub const Drag = struct {
return true;
}
+ /// Tells the drag where the pointer is vertically (0 is the top of the
+ /// titlebar, `band` its height); true when it went out or came back.
+ pub fn pointerY(d: *Drag, y: f32, band: f32) bool {
+ if (!d.dragging()) return false;
+ const now = y < -band * 0.5 or y > band * 1.5;
+ if (now == d.out) return false;
+ d.out = now;
+ return true;
+ }
+
/// The index, among all tabs, where the dragged tab would land now.
pub fn target(d: Drag, g: Geometry) usize {
if (g.count == 0) return d.from;
@@ -196,7 +223,7 @@ pub const Drag = struct {
/// dragged tab under the pointer, the tabs it passes one step aside.
pub fn rect(d: Drag, g: Geometry, i: usize) Rect {
var r = g.tab(i);
- if (!d.dragging() or g.count == 0) return r;
+ if (!d.dragging() or d.out or g.count == 0) return r;
const pitch = g.width + 4;
const index = g.first + i;
const to = d.target(g);
@@ -215,7 +242,7 @@ pub const Drag = struct {
/// ended where it began, or no press at all.
pub fn release(d: *Drag, g: Geometry) ?Move {
defer d.* = .{};
- if (d.id == 0 or !d.moved) return null;
+ if (d.id == 0 or !d.moved or d.out) return null;
const to = d.target(g);
if (to == d.from) return null;
return .{ .id = d.id, .index = to };
diff --git a/src/ui/widgets/title_tabs_tests.zig b/src/ui/widgets/title_tabs_tests.zig
index 343f646..d7c8380 100644
--- a/src/ui/widgets/title_tabs_tests.zig
+++ b/src/ui/widgets/title_tabs_tests.zig
@@ -198,3 +198,61 @@ test "a drag in progress changes the hash and a bare press does not" {
later.drag = d;
try std.testing.expect(tt.hash(moving) != tt.hash(later));
}
+
+test "a drag far above or below the strip is out and moves nothing" {
+ const s = strip();
+ const g = tt.geometry(width, 1, s);
+ const pitch = g.width + 4;
+ const x = g.tab(0).x + g.width / 2;
+ var d: tt.Drag = .{};
+ try std.testing.expect(d.press(s, g, 1, x));
+ // Not a drag yet: the vertical position is ignored.
+ try std.testing.expect(!d.pointerY(-400, 40));
+ try std.testing.expect(d.motion(x + pitch, 1));
+ try std.testing.expect(!d.pointerY(10, 40));
+ try std.testing.expect(d.pointerY(-30, 40));
+ try std.testing.expect(d.out);
+ // Out: no preview, and releasing is not a reorder.
+ try std.testing.expectEqual(g.tab(0).x, d.rect(g, 0).x);
+ try std.testing.expectEqual(g.tab(1).x, d.rect(g, 1).x);
+ try std.testing.expect(d.pointerY(70, 40) == false);
+ try std.testing.expect(d.pointerY(20, 40));
+ try std.testing.expect(!d.out);
+ try std.testing.expect(d.pointerY(61, 40));
+ try std.testing.expectEqual(@as(?tt.Drag.Move, null), d.release(g));
+}
+
+test "dropIndex picks the gap nearest the pointer" {
+ const g = tt.geometry(width, 1, strip());
+ const pitch = g.width + 4;
+ try std.testing.expectEqual(@as(usize, 0), tt.dropIndex(g, g.x - 50));
+ try std.testing.expectEqual(@as(usize, 0), tt.dropIndex(g, g.x + 0.2 * pitch));
+ try std.testing.expectEqual(@as(usize, 1), tt.dropIndex(g, g.x + 0.8 * pitch));
+ try std.testing.expectEqual(@as(usize, 2), tt.dropIndex(g, g.x + 1.6 * pitch));
+ try std.testing.expectEqual(@as(usize, 3), tt.dropIndex(g, g.x + 5000));
+}
+
+test "the drop marker and the out state change the hash" {
+ const s = strip();
+ var marked = s;
+ marked.drop_index = 1;
+ try std.testing.expect(tt.hash(s) != tt.hash(marked));
+ var other = s;
+ other.drop_index = 2;
+ try std.testing.expect(tt.hash(marked) != tt.hash(other));
+ // Index 0 differs from no marker at all.
+ var first = s;
+ first.drop_index = 0;
+ try std.testing.expect(tt.hash(s) != tt.hash(first));
+ const g = tt.geometry(width, 1, s);
+ var d: tt.Drag = .{};
+ const x = g.tab(0).x + g.width / 2;
+ try std.testing.expect(d.press(s, g, 1, x));
+ try std.testing.expect(d.motion(x + 20, 1));
+ var in_strip = s;
+ in_strip.drag = d;
+ try std.testing.expect(d.pointerY(-60, 40));
+ var out = s;
+ out.drag = d;
+ try std.testing.expect(tt.hash(in_strip) != tt.hash(out));
+}