RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
Complete coupled manual half-tile resizing
Mel83d5b4bf9337
diff --git a/src/input/grabs.zig b/src/input/grabs.zigindex 0dbad49..2334e1a 100644--- a/src/input/grabs.zig+++ b/src/input/grabs.zig@@ -223,6 +223,10 @@ fn manualHalfPeer(input: *Input, toplevel: *Toplevel, edges: resize.Edges) ?*Topcontinue;}+ // One pointer delta maps to one shared client-width delta only when+ // both halves use the same presented client scale.+ if (@abs(candidate.worldScale() - toplevel.worldScale()) > 0.0001) continue;+const peer_target = candidate.tile_target.?;const peer_position = input.server.world.toLayout(@floatFromInt(peer_target.x),@@ -367,6 +371,7 @@ pub fn startResize(@import("../effects/window_move.zig").startResize(toplevel);toplevel.setResizing(true);+ if (peer) |peer_toplevel| peer_toplevel.setResizing(true);const wait = toplevel.requestSize(client_w, client_h);if (input.resize_session) |*sess| {sess.outstanding_configure = wait;@@ -391,12 +396,17 @@ pub fn finishResize(input: *Input) void {if (@import("../effects/window_move.zig").resizeEnabled(input.server)) input.server.scheduleFrames();toplevel.setResizing(false);+ if (session.peer) |peer| peer.toplevel.setResizing(false);const final_size = session.latest_desired_size;const shown = @import("../effects/window_move.zig").resizeSize(toplevel, Toplevel.nowMs(), final_size);// Only a follow-through still easing toward the final size leaves work// pending. Otherwise dirty is cleared here, so flushResizeConfigure ignores// a settling session (it used to return early on `settling`).- session.dirty = !std.meta.eql(shown, final_size);+ // Preserve a final pointer size that has not yet been sent because an+ // earlier configure is still outstanding.+ session.dirty = !std.meta.eql(shown, final_size) or+ session.last_sent_width != final_size.width or+ session.last_sent_height != final_size.height;if (!session.dirty and session.outstanding_configure == .none) {session.outstanding_configure = toplevel.requestSize(final_size.width, final_size.height);session.last_sent_width = final_size.width;@@ -432,7 +442,10 @@ pub fn finishResize(input: *Input) void {// Compositor-owned windows have already applied the final size and// position; no client commit will arrive to finish their resize session.- if (!session.dirty and session.outstanding_configure == .none) input.finishSettlement(session.generation);+ const peer_dirty = if (session.peer) |peer| peer.dirty else false;+ if (!session.dirty and !peer_dirty and !session.hasOutstandingConfigure()) {+ input.finishSettlement(session.generation);+ }input.processCursorMotion(0);}@@ -458,6 +471,7 @@ pub fn cancelResize(input: *Input) void {}if (peer) |pair| {if (pair.toplevel.isMapped()) {+ pair.toplevel.setResizing(false);_ = pair.toplevel.requestSize(pair.snapshot.initial_client_width,pair.snapshot.initial_client_height,@@ -487,6 +501,7 @@ pub fn flushResizeConfigure(input: *Input, output: *Output) void {session.last_sent_width = desired.width;session.last_sent_height = desired.height;session.dirty = following;+ toplevel.positionDuringResize();}}@@ -500,6 +515,7 @@ pub fn flushResizeConfigure(input: *Input, output: *Output) void {peer.last_sent_width = desired.width;peer.last_sent_height = desired.height;peer.dirty = false;+ peer.toplevel.positionDuringResize();}}}diff --git a/src/input/motion.zig b/src/input/motion.zigindex e3ed6c0..79cef29 100644--- a/src/input/motion.zig+++ b/src/input/motion.zig@@ -584,16 +584,39 @@ pub fn processResize(input: *Input) void {const toplevel = session.toplevel;const cursor_world = input.server.world.toWorld(input.cursor.x, input.cursor.y);+ const cursor_x = cursor_world.x / toplevel.worldScale();+ const cursor_y = cursor_world.y / toplevel.worldScale();+ const active_constraints = toplevel.sizeConstraints();// Each participant measures the same pointer in its own unscaled// client space. Their snapshots select opposite edges, so the shared// boundary moves in opposite directions while both outer edges stay put.- const desired = resize.computeDesiredClientSize(+ var desired = resize.computeDesiredClientSize(session.snapshot,- cursor_world.x / toplevel.worldScale(),- cursor_world.y / toplevel.worldScale(),- toplevel.sizeConstraints(),+ cursor_x,+ cursor_y,+ active_constraints,);+ var peer_desired: ?resize.ClientSize = null;+ if (session.peer) |peer| {+ if (resize.computeCoupledHorizontalClientSizes(+ session.snapshot,+ peer.snapshot,+ cursor_x,+ active_constraints,+ peer.toplevel.sizeConstraints(),+ )) |coupled| {+ desired = coupled.active;+ peer_desired = coupled.peer;+ } else {+ peer_desired = resize.computeDesiredClientSize(+ peer.snapshot,+ cursor_world.x / peer.toplevel.worldScale(),+ cursor_world.y / peer.toplevel.worldScale(),+ peer.toplevel.sizeConstraints(),+ );+ }+ }var changed = false;if (desired.width != session.latest_desired_size.width or desired.height != session.latest_desired_size.height) {session.latest_desired_size = desired;@@ -603,16 +626,11 @@ pub fn processResize(input: *Input) void {}if (session.peer) |*peer| {- const peer_desired = resize.computeDesiredClientSize(- peer.snapshot,- cursor_world.x / peer.toplevel.worldScale(),- cursor_world.y / peer.toplevel.worldScale(),- peer.toplevel.sizeConstraints(),- );- if (peer_desired.width != peer.latest_desired_size.width or- peer_desired.height != peer.latest_desired_size.height)+ const next_size = peer_desired.?;+ if (next_size.width != peer.latest_desired_size.width or+ next_size.height != peer.latest_desired_size.height){- peer.latest_desired_size = peer_desired;+ peer.latest_desired_size = next_size;peer.dirty = true;changed = true;}diff --git a/src/resize.zig b/src/resize.zigindex 8fac5d0..5bb727b 100644--- a/src/resize.zig+++ b/src/resize.zig@@ -103,6 +103,80 @@ pub const SizeConstraints = struct {pub const ClientSize = struct { width: i32, height: i32 };+pub const CoupledClientSizes = struct {+ active: ClientSize,+ peer: ClientSize,+};++fn effectiveMinWidth(constraints: SizeConstraints) i32 {+ var min_width = @max(1, constraints.min_width);+ if (constraints.control_min_width > min_width and+ (constraints.max_width == 0 or constraints.control_min_width <= constraints.max_width))+ {+ min_width = @intCast(constraints.control_min_width);+ }+ return min_width;+}++/// Resize two opposite horizontal edges from one pointer delta. Both widths+/// share one integer delta, clamped to the intersection of both clients'+/// constraints, so their common boundary cannot split.+pub fn computeCoupledHorizontalClientSizes(+ active_snapshot: ResizeSnapshot,+ peer_snapshot: ResizeSnapshot,+ cursor_x: f64,+ active_constraints: SizeConstraints,+ peer_constraints: SizeConstraints,+) ?CoupledClientSizes {+ const direction: i32 = if (active_snapshot.edges.right and peer_snapshot.edges.left)+ 1+ else if (active_snapshot.edges.left and peer_snapshot.edges.right)+ -1+ else+ return null;++ const active_min = effectiveMinWidth(active_constraints);+ const peer_min = effectiveMinWidth(peer_constraints);+ const lower_from_active = active_min - active_snapshot.initial_client_width;+ const lower_from_peer = if (peer_constraints.max_width > 0)+ peer_snapshot.initial_client_width - peer_constraints.max_width+ else+ std.math.minInt(i32);+ const upper_from_active = if (active_constraints.max_width > 0)+ active_constraints.max_width - active_snapshot.initial_client_width+ else+ std.math.maxInt(i32);+ const upper_from_peer = peer_snapshot.initial_client_width - peer_min;+ const lower = @max(lower_from_active, lower_from_peer);+ const upper = @min(upper_from_active, upper_from_peer);++ // If constraints changed so that no shared boundary is feasible, keep+ // the existing boundary instead of allowing independent clamps to split.+ const raw_delta = (cursor_x - active_snapshot.initial_cursor_x) *+ @as(f64, @floatFromInt(direction));+ const bounded_delta = if (lower <= upper)+ std.math.clamp(raw_delta, @as(f64, @floatFromInt(lower)), @as(f64, @floatFromInt(upper)))+ else+ 0;+ const delta: i32 = @intFromFloat(@round(bounded_delta));++ var active_size = computeDesiredClientSize(+ active_snapshot,+ active_snapshot.initial_cursor_x,+ active_snapshot.initial_cursor_y,+ active_constraints,+ );+ var peer_size = computeDesiredClientSize(+ peer_snapshot,+ peer_snapshot.initial_cursor_x,+ peer_snapshot.initial_cursor_y,+ peer_constraints,+ );+ active_size.width = active_snapshot.initial_client_width + delta;+ peer_size.width = peer_snapshot.initial_client_width - delta;+ return .{ .active = active_size, .peer = peer_size };+}+pub fn computeDesiredClientSize(snapshot: ResizeSnapshot,cursor_x: f64,@@ -343,6 +417,21 @@ test "resize geometry calculation for all 8 directions" {}}+test "coupled horizontal resize clamps one shared boundary to both clients" {+ const active = createSnapshot(100, 200, 0, 0, 800, 600, 0, 1, 0, .{ .right = true });+ const peer = createSnapshot(100, 200, 801, 0, 800, 600, 0, 1, 0, .{ .left = true });+ const active_limits = SizeConstraints{ .min_width = 200, .max_width = 1000 };+ const peer_limits = SizeConstraints{ .min_width = 300, .max_width = 900 };++ const grow = computeCoupledHorizontalClientSizes(active, peer, 600, active_limits, peer_limits).?;+ try std.testing.expectEqual(@as(i32, 1000), grow.active.width);+ try std.testing.expectEqual(@as(i32, 600), grow.peer.width);++ const shrink = computeCoupledHorizontalClientSizes(active, peer, -400, active_limits, peer_limits).?;+ try std.testing.expectEqual(@as(i32, 700), shrink.active.width);+ try std.testing.expectEqual(@as(i32, 900), shrink.peer.width);+}+test "quadrant selection for Alt + right-drag" {const fx: i32 = 100;const fy: i32 = 100;diff --git a/src/toplevel/lifecycle.zig b/src/toplevel/lifecycle.zigindex 43f9a8c..bfa165b 100644--- a/src/toplevel/lifecycle.zig+++ b/src/toplevel/lifecycle.zig@@ -34,11 +34,9 @@ pub fn handleSurfaceCommit(toplevel: *Toplevel) void {toplevel.positionDuringResize();- // An acknowledgement can carry no pixel damage. Pending resize- // work of the flame (following the pointer, or left over once it- // has faded away) still needs a frame; plain resizes already- // scheduled theirs, so with the effect off nothing changes.- if (@import("../effects/window_move.zig").resizeEnabled(toplevel.server) and session.dirty and session.outstanding_configure == .none) {+ // An acknowledgement can carry no pixel damage. Any newer+ // pointer size waiting behind this configure still needs a frame.+ if (session.dirty and session.outstanding_configure == .none) {if (session.pacing_output) |output| output.wlr_output.scheduleFrame() else toplevel.server.scheduleFrames();}@@ -51,7 +49,7 @@ pub fn handleSurfaceCommit(toplevel: *Toplevel) void {}} else if (session.peer) |*peer| {if (peer.toplevel == toplevel) {- toplevel.positionDuringResize();+ toplevel.positionDuringResize();if (peer.outstanding_configure == .x11_commit) {peer.outstanding_configure = .none;} else if (peer.outstanding_configure != .none) {@@ -60,9 +58,9 @@ pub fn handleSurfaceCommit(toplevel: *Toplevel) void {}}- if (peer.dirty and peer.outstanding_configure == .none) {- if (session.pacing_output) |output| output.wlr_output.scheduleFrame() else toplevel.server.scheduleFrames();- }+ if (peer.dirty and peer.outstanding_configure == .none) {+ if (session.pacing_output) |output| output.wlr_output.scheduleFrame() else toplevel.server.scheduleFrames();+ }// The peer has its own protocol configure lifecycle. Its// commit must not clear or settle the active window's wait.diff --git a/src/toplevel/zoom.zig b/src/toplevel/zoom.zigindex dd281b5..cd851ee 100644--- a/src/toplevel/zoom.zig+++ b/src/toplevel/zoom.zig@@ -210,6 +210,14 @@ pub fn positionDuringResize(toplevel: *Toplevel) void {initial_x + @as(i32, @intFromFloat(@round(dx * toplevel.worldScale()))),initial_y + @as(i32, @intFromFloat(@round(dy * toplevel.worldScale()))),);+ if (session.peer != null and toplevel.tile != null) {+ if (toplevel.tile_target) |*target| {+ target.x = toplevel.x;+ target.y = toplevel.y;+ target.width = geom.width;+ target.height = geom.height;+ }+ }}}