RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
refactor: extract shared button proximity sampling
Petar Margetić21e19afaa0e6
diff --git a/src/ui/input.zig b/src/ui/input.zigindex 93e6ad0..33f9206 100644--- a/src/ui/input.zig+++ b/src/ui/input.zig@@ -33,12 +33,7 @@ pub fn hitTest(root: *Widget, x: f32, y: f32) ?*Widget {}/// Logical-pixel rectangle used to constrain proximity sampling.-pub const ProximityRect = struct {- x: f32,- y: f32,- w: f32,- h: f32,-};+pub const ProximityRect = button_widget.Rect;fn widgetRect(widget: *const Widget) ProximityRect {return .{@@ -49,29 +44,6 @@ fn widgetRect(widget: *const Widget) ProximityRect {};}-fn intersectProximityRect(a: ProximityRect, b: ProximityRect) ProximityRect {- const x0 = @max(a.x, b.x);- const y0 = @max(a.y, b.y);- const x1 = @min(a.x + a.w, b.x + b.w);- const y1 = @min(a.y + a.h, b.y + b.h);- return .{- .x = x0,- .y = y0,- .w = @max(0, x1 - x0),- .h = @max(0, y1 - y0),- };-}--fn proximityDistance(x: f32, y: f32, rect: ProximityRect) f32 {- if (rect.w <= 0 or rect.h <= 0) return std.math.inf(f32);-- const right = rect.x + rect.w;- const bottom = rect.y + rect.h;- const dx = if (x < rect.x) rect.x - x else if (x > right) x - right else 0;- const dy = if (y < rect.y) rect.y - y else if (y > bottom) y - bottom else 0;- return @sqrt(dx * dx + dy * dy);-}-/// Updates every button in the retained tree, not merely the hit-tested one./// `viewport` is the host panel's visible content rectangle. Scroll containers/// add their own paint clip as recursion descends. Returns true only when a@@ -83,7 +55,7 @@ pub fn updateButtonProximity(viewport: ProximityRect,) bool {var changed = false;- const root_clip = intersectProximityRect(viewport, widgetRect(root));+ const root_clip = button_widget.intersectRect(viewport, widgetRect(root));sampleButtonProximity(root, x, y, root_clip, &changed);return changed;}@@ -97,14 +69,13 @@ fn sampleButtonProximity() void {switch (widget.kind) {.button => |*data| {- const visible = intersectProximityRect(widgetRect(widget), clip);- const distance = proximityDistance(x, y, visible);- const sample_x = if (distance < std.math.inf(f32)) x - widget.computed_x else data.proximity_x;- const sample_y = if (distance < std.math.inf(f32)) y - widget.computed_y else data.proximity_y;- if (data.proximity_distance != distance or+ const sample = button_widget.sampleProximity(widgetRect(widget), x, y, clip);+ const sample_x = if (sample.distance < std.math.inf(f32)) sample.x else data.proximity_x;+ const sample_y = if (sample.distance < std.math.inf(f32)) sample.y else data.proximity_y;+ if (data.proximity_distance != sample.distance ordata.proximity_x != sample_x or data.proximity_y != sample_y){- data.proximity_distance = distance;+ data.proximity_distance = sample.distance;data.proximity_x = sample_x;data.proximity_y = sample_y;widget.markPaintDirty();@@ -116,7 +87,7 @@ fn sampleButtonProximity(// paintScrollContainer clips all descendants to its own full rectangle.const child_clip = if (widget.kind == .scroll_container)- intersectProximityRect(clip, widgetRect(widget))+ button_widget.intersectRect(clip, widgetRect(widget))elseclip;diff --git a/src/ui/widgets/button.zig b/src/ui/widgets/button.zigindex 683ac71..a37bef9 100644--- a/src/ui/widgets/button.zig+++ b/src/ui/widgets/button.zig@@ -16,6 +16,67 @@ const IconId = layout.IconId;pub const Rect = @import("field.zig").Rect;+pub const ProximitySample = struct {+ distance: f32,+ x: f32,+ y: f32,+};++/// Intersects two logical-pixel rectangles.+pub fn intersectRect(a: Rect, b: Rect) Rect {+ const x0 = @max(a.x, b.x);+ const y0 = @max(a.y, b.y);+ const x1 = @min(a.x + a.w, b.x + b.w);+ const y1 = @min(a.y + a.h, b.y + b.h);+ return .{+ .x = x0,+ .y = y0,+ .w = @max(0, x1 - x0),+ .h = @max(0, y1 - y0),+ };+}++/// Samples pointer proximity to the visible portion of a button.+/// Returned x/y coordinates are local to the original button rectangle.+pub fn sampleProximity(box: Rect, pointer_x: f32, pointer_y: f32, clip: Rect) ProximitySample {+ const visible = intersectRect(box, clip);+ if (visible.w <= 0 or visible.h <= 0) {+ return .{+ .distance = std.math.inf(f32),+ .x = pointer_x - box.x,+ .y = pointer_y - box.y,+ };+ }++ const right = visible.x + visible.w;+ const bottom = visible.y + visible.h;+ const dx = if (pointer_x < visible.x) visible.x - pointer_x else if (pointer_x > right) pointer_x - right else 0;+ const dy = if (pointer_y < visible.y) visible.y - pointer_y else if (pointer_y > bottom) pointer_y - bottom else 0;++ return .{+ .distance = @sqrt(dx * dx + dy * dy),+ .x = pointer_x - box.x,+ .y = pointer_y - box.y,+ };+}++test "proximity sampling respects clipping and returns button-local coordinates" {+ const box: Rect = .{ .x = 20, .y = 10, .w = 40, .h = 20 };+ const clip: Rect = .{ .x = 30, .y = 15, .w = 20, .h = 10 };++ const near = sampleProximity(box, 30, 10, clip);+ try std.testing.expectEqual(@as(f32, 5), near.distance);+ try std.testing.expectEqual(@as(f32, 10), near.x);+ try std.testing.expectEqual(@as(f32, 0), near.y);++ const inside = sampleProximity(box, 35, 20, clip);+ try std.testing.expectEqual(@as(f32, 0), inside.distance);++ const invisible_clip: Rect = .{ .x = 70, .y = 15, .w = 20, .h = 10 };+ const outside = sampleProximity(box, 10, 20, invisible_clip);+ try std.testing.expect(std.math.isInf(outside.distance));+}+/// `primary` is the accent fill (the start menu's selected category chip);/// `secondary` a quiet surface with a hairline border; `ghost` shows only on/// hover; `chrome` is a window-style close button, red on hover.