Skip to content

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

refactor: adapt retained UI to interaction surface

Petar Margetić90cc772f45b7
diff --git a/src/ui/input.zig b/src/ui/input.zig
index 33f9206..9e464eb 100644
--- a/src/ui/input.zig
+++ b/src/ui/input.zig
@@ -13,6 +13,8 @@ const layout = @import("layout.zig");
const Widget = layout.Widget;
const button_widget = @import("widgets/button.zig");
+const interaction_surface = @import("interaction_surface.zig");
+const theme = @import("theme.zig");
const checkbox_widget = @import("widgets/checkbox.zig");
const scroll_widget = @import("widgets/scroll_container.zig");
const text_input_widget = @import("widgets/text_input.zig");
@@ -69,9 +71,11 @@ fn sampleButtonProximity(
) void {
switch (widget.kind) {
.button => |*data| {
- 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;
+ const sample = interaction_surface.sampleButtonProximity(
+ widgetRect(widget), x, y, clip, theme.global.border_glow_reach,
+ );
+ const sample_x = sample.x;
+ const sample_y = sample.y;
if (data.proximity_distance != sample.distance or
data.proximity_x != sample_x or data.proximity_y != sample_y)
{
@@ -151,6 +155,78 @@ pub fn findInteractive(start: ?*Widget) ?*Widget {
return start;
}
+/// Adapts the tree's point-query result to the shared interaction surface.
+/// The widget pointer remains host-owned; only its integer identity and
+/// visible geometry cross the surface boundary.
+pub fn interactionHit(
+ root: *Widget,
+ target: ?*Widget,
+ x: f32,
+ y: f32,
+) ?interaction_surface.Hit {
+ const widget = target orelse return null;
+
+ const role: interaction_surface.Role = switch (widget.kind) {
+ .button => .button,
+ .row, .checkbox, .text_input, .secret_input, .toggle, .slider,
+ .segmented, .select, .swatch, .stepper, .arrangement => .interactive,
+ else => .passive,
+ };
+
+ const enabled = switch (widget.kind) {
+ .button => |data| data.state != .disabled,
+ .row => |data| data.state != .disabled,
+ .checkbox => |data| !data.disabled,
+ .toggle => |data| !data.disabled,
+ .select => |data| !data.disabled,
+ else => true,
+ };
+
+ var rect = widgetRect(widget);
+ var clip = widgetRect(root);
+
+ // Select popups are overlays. Their hit geometry is independent
+ // of the select control's normal ancestor clipping.
+ if (widget.kind == .select and widget.kind.select.open) {
+ const popup = select_widget.popupBounds(widget);
+ if (x >= popup.x and x < popup.x + popup.w and
+ y >= popup.y and y < popup.y + popup.h)
+ {
+ rect = .{
+ .x = popup.x,
+ .y = popup.y,
+ .w = popup.w,
+ .h = popup.h,
+ };
+ clip = rect;
+ } else {
+ // Outside the popup, use only the select control itself.
+ rect = widgetRect(widget);
+ var ancestor = widget.parent;
+ while (ancestor) |parent| {
+ clip = button_widget.intersectRect(clip, widgetRect(parent));
+ if (parent == root) break;
+ ancestor = parent.parent;
+ }
+ }
+ } else {
+ var ancestor = widget.parent;
+ while (ancestor) |parent| {
+ clip = button_widget.intersectRect(clip, widgetRect(parent));
+ if (parent == root) break;
+ ancestor = parent.parent;
+ }
+ }
+
+ return .{
+ .id = @intCast(@intFromPtr(widget)),
+ .rect = rect,
+ .clip = clip,
+ .role = role,
+ .enabled = enabled,
+ };
+}
+
pub fn isInteractiveButtonOrRow(widget: *const Widget) bool {
return switch (widget.kind) {
.button => |data| data.state != .disabled,
@@ -199,6 +275,7 @@ pub const Caret = input_caret.Caret;
pub var caret_config: CaretConfig = .{};
pub const Dispatcher = struct {
+ surface: interaction_surface.Surface = .{},
hovered: ?*Widget = null,
pressed: ?*Widget = null,
focused: ?*Widget = null,
@@ -240,6 +317,7 @@ pub const Dispatcher = struct {
/// `root`'s to clear.
pub fn pointerLeave(self: *Dispatcher, root: *Widget) void {
if (self.tree != root) return;
+ _ = self.surface.pointerLeave();
if (self.thumb_hover) |prev| setThumbHover(prev, false);
self.thumb_hover = null;
// A slider is only lit while the pointer is over it; forgetting it as
diff --git a/src/ui/input_pointer.zig b/src/ui/input_pointer.zig
index 1fc26b5..c97a506 100644
--- a/src/ui/input_pointer.zig
+++ b/src/ui/input_pointer.zig
@@ -68,7 +68,9 @@ pub fn pointerMotion(self: *Dispatcher, root: *Widget, x: f32, y: f32) void {
if (select_widget.optionAt(w, x, y)) |index| select_widget.highlight(w, index);
}
const result = hitTestWithScrollAncestor(root, x, y);
- self.setHovered(findInteractive(result.widget));
+ const target = findInteractive(result.widget);
+ const change = self.surface.pointerMotion(input_mod.interactionHit(root, target, x, y));
+ if (change.hover_changed or self.hovered != target) self.setHovered(target);
// Safer than `.?` on `scroll_ancestor`: over_thumb is only true when
// that pointer is non-null, but a hit-test race must not crash.
diff --git a/src/ui/input_tests.zig b/src/ui/input_tests.zig
index bd676da..7574e3c 100644
--- a/src/ui/input_tests.zig
+++ b/src/ui/input_tests.zig
@@ -108,7 +108,9 @@ test "button proximity samples visible nested-scroll rectangles and skips unchan
// A distant pointer position is retained without repeatedly dirtying paint.
try std.testing.expect(input_src.updateButtonProximity(&root, 120, 15, viewport));
- try std.testing.expectEqual(@as(f32, 75), visible_button.proximity_distance);
+ try std.testing.expect(std.math.isInf(visible_button.proximity_distance));
+ try std.testing.expectEqual(@as(f32, 0), visible_button.proximity_x);
+ try std.testing.expectEqual(@as(f32, 0), visible_button.proximity_y);
const distant_revision = layout.paint_revision;
try std.testing.expect(!input_src.updateButtonProximity(&root, 120, 15, viewport));
try std.testing.expectEqual(distant_revision, layout.paint_revision);
@@ -144,6 +146,64 @@ test "dispatcher pointer motion updates button proximity samples" {
try std.testing.expectEqual(@as(f32, 0), button.proximity_distance);
}
+test "dispatcher forwards retained-tree hits to the interaction surface" {
+ const text = Widget{
+ .kind = .{ .text = .{
+ .content = "Child",
+ .font_size = 12,
+ .color = .{ 1, 1, 1, 1 },
+ } },
+ .computed_x = 15,
+ .computed_y = 15,
+ .computed_width = 30,
+ .computed_height = 10,
+ };
+ var row_children = [_]Widget{text};
+ const row = Widget{
+ .kind = .{ .row = .{} },
+ .computed_x = 10,
+ .computed_y = 10,
+ .computed_width = 60,
+ .computed_height = 30,
+ .children = &row_children,
+ };
+ var root_children = [_]Widget{row};
+ var root = Widget{
+ .kind = .container,
+ .computed_width = 100,
+ .computed_height = 80,
+ .children = &root_children,
+ };
+ root.linkParents();
+
+ var dispatcher = Dispatcher{};
+ dispatcher.pointerMotion(&root, 20, 20);
+
+ try std.testing.expectEqual(@as(?*Widget, &root.children[0]), dispatcher.hovered);
+ try std.testing.expect(dispatcher.surface.hovered != null);
+
+ const target = dispatcher.surface.hovered.?;
+ try std.testing.expectEqual(
+ @as(u64, @intCast(@intFromPtr(&root.children[0]))),
+ target.id,
+ );
+ try std.testing.expectEqual(@as(f32, 10), target.rect.x);
+ try std.testing.expectEqual(@as(f32, 10), target.rect.y);
+ try std.testing.expectEqual(@as(f32, 60), target.rect.w);
+ try std.testing.expectEqual(@as(f32, 30), target.rect.h);
+
+ // A passive hit clears the shared target and the old widget hover.
+ dispatcher.pointerMotion(&root, 90, 70);
+ try std.testing.expect(dispatcher.surface.hovered == null);
+ try std.testing.expect(root.children[0].kind.row.state != .hover);
+
+ // Returning to the row restores the target; leaving clears its snapshot.
+ dispatcher.pointerMotion(&root, 20, 20);
+ try std.testing.expect(dispatcher.surface.hovered != null);
+ dispatcher.pointerLeave(&root);
+ try std.testing.expect(dispatcher.surface.hovered == null);
+}
+
test "hitTest returns the topmost overlapping widget" {
var kids = [_]Widget{
.{ .kind = .container },