Skip to content

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

fix: update button proximity during pointer motion

Petar Margetić2ccff5de4c86
diff --git a/src/ui/input_pointer.zig b/src/ui/input_pointer.zig
index 9455930..1fc26b5 100644
--- a/src/ui/input_pointer.zig
+++ b/src/ui/input_pointer.zig
@@ -32,6 +32,12 @@ const findScrollContainer = input_mod.findScrollContainer;
pub fn pointerMotion(self: *Dispatcher, root: *Widget, x: f32, y: f32) void {
self.bindTree(root);
+ _ = input_mod.updateButtonProximity(root, x, y, .{
+ .x = root.computed_x,
+ .y = root.computed_y,
+ .w = root.computed_width,
+ .h = root.computed_height,
+ });
if (self.text_drag) |field| {
// Sweeping a selection: the pointer may be well outside the field
// by now, and `offsetAtX` clamps to the ends, so no hit test.
diff --git a/src/ui/input_tests.zig b/src/ui/input_tests.zig
index 3db0fbc..bd676da 100644
--- a/src/ui/input_tests.zig
+++ b/src/ui/input_tests.zig
@@ -114,6 +114,36 @@ test "button proximity samples visible nested-scroll rectangles and skips unchan
try std.testing.expectEqual(distant_revision, layout.paint_revision);
}
+test "dispatcher pointer motion updates button proximity samples" {
+ var children = [_]Widget{.{
+ .kind = .{ .button = .{ .label = "Button" } },
+ .computed_width = 40,
+ .computed_height = 20,
+ }};
+ var root: Widget = .{
+ .kind = .container,
+ .computed_width = 100,
+ .computed_height = 80,
+ .children = &children,
+ };
+ root.linkParents();
+
+ var dispatcher: Dispatcher = .{};
+ dispatcher.pointerMotion(&root, 45, 10);
+
+ const button = &root.children[0].kind.button;
+ try std.testing.expectEqual(@as(f32, 5), button.proximity_distance);
+ try std.testing.expectEqual(@as(f32, 45), button.proximity_x);
+ try std.testing.expectEqual(@as(f32, 10), button.proximity_y);
+
+ const revision = layout.paint_revision;
+ dispatcher.pointerMotion(&root, 45, 10);
+ try std.testing.expectEqual(revision, layout.paint_revision);
+
+ dispatcher.pointerMotion(&root, 10, 10);
+ try std.testing.expectEqual(@as(f32, 0), button.proximity_distance);
+}
+
test "hitTest returns the topmost overlapping widget" {
var kids = [_]Widget{
.{ .kind = .container },
@@ -723,20 +753,26 @@ test "dragging across a field sweeps a selection and releasing ends it" {
try std.testing.expectEqualStrings("alpha beta", field.kind.text_input.selectedText());
}
-test "stable pointer motion and clamped scrolling leave visual revision unchanged" {
+test "pointer motion invalidates changed proximity samples but not repeated samples" {
var children = [_]Widget{.{ .kind = .{ .button = .{ .label = "Button" } }, .computed_width = 100, .computed_height = 30 }};
var root: Widget = .{ .kind = .container, .computed_width = 120, .computed_height = 80, .children = &children };
root.linkParents();
var dispatcher: Dispatcher = .{};
dispatcher.pointerMotion(&root, 10, 10);
var revision = layout.paint_revision;
+
+ dispatcher.pointerMotion(&root, 11, 10);
+ try std.testing.expect(revision != layout.paint_revision);
+ revision = layout.paint_revision;
dispatcher.pointerMotion(&root, 11, 10);
try std.testing.expectEqual(revision, layout.paint_revision);
+
dispatcher.pointerMotion(&root, 110, 60);
try std.testing.expect(revision != layout.paint_revision);
revision = layout.paint_revision;
- dispatcher.pointerMotion(&root, 111, 60);
+ dispatcher.pointerMotion(&root, 110, 60);
try std.testing.expectEqual(revision, layout.paint_revision);
+
var scroll: Widget = .{ .kind = .{ .scroll_container = .{ .content_size = 300 } }, .direction = .column, .computed_width = 100, .computed_height = 100 };
_ = dispatcher.scrollWheel(&scroll, 10, 10, -40, null);
try std.testing.expectEqual(revision, layout.paint_revision);
diff --git a/src/ui/paint_tests.zig b/src/ui/paint_tests.zig
index 7c34324..4f5f726 100644
--- a/src/ui/paint_tests.zig
+++ b/src/ui/paint_tests.zig
@@ -15,6 +15,7 @@ const anim = @import("paint.zig").anim;
const text_mod = @import("paint.zig").text_mod;
const theme = @import("paint.zig").theme;
const ui_input = @import("paint.zig").ui_input;
+const Dispatcher = @import("input.zig").Dispatcher;
const field_widget = @import("paint.zig").field_widget;
const ClipRect = @import("paint.zig").ClipRect;
const Image = @import("paint.zig").Image;
@@ -682,6 +683,54 @@ test "segmented frames, swatches and rows take their own relief keys" {
}
+test "dispatcher proximity illuminates raised and inset buttons" {
+ for ([_]layout.Relief{ .raised, .inset }) |button_relief| {
+ var children = [_]Widget{.{
+ .kind = .{ .button = .{ .variant = .secondary } },
+ .computed_x = 20,
+ .computed_y = 20,
+ .computed_width = 40,
+ .computed_height = 24,
+ }};
+ var root: Widget = .{
+ .kind = .container,
+ .computed_width = 80,
+ .computed_height = 60,
+ .children = &children,
+ };
+ root.linkParents();
+
+ var dispatcher: Dispatcher = .{};
+ dispatcher.pointerMotion(&root, 40, 16);
+
+ const button = &root.children[0];
+ const sampled = button.kind.button;
+ try std.testing.expectEqual(@as(f32, 4), sampled.proximity_distance);
+
+ var palette = reliefTheme();
+ palette.button_relief = button_relief;
+ palette.button_hover_glow_enabled = true;
+ palette.border_glow = .{ 1, 0, 0, 0.60 };
+ palette.border_glow_enabled = false;
+ palette.border_glow_reach = 56;
+ palette.border_glow_length = 72;
+ palette.border_glow_depth = 3;
+
+ var pixels: [80 * 60]u32 = @splat(0xff808080);
+ var renderer = Renderer.init(&pixels, 80, 60, 1);
+ renderer.palette = palette;
+ paintButton(button, sampled, &renderer);
+ const illuminated = pixels;
+
+ var without_proximity = sampled;
+ without_proximity.proximity_distance = std.math.inf(f32);
+ @memset(&pixels, 0xff808080);
+ paintButton(button, without_proximity, &renderer);
+
+ try std.testing.expect(!std.mem.eql(u32, &illuminated, &pixels));
+ }
+}
+
test "button hover glow is independent of window edge glow and stays rounded and clipped" {
const sample = struct {
fn pixel(pixels: []const u32, width: i32, scale: f32, x: f32, y: f32) u32 {