Skip to content

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

feat: add cursor proximity glow to buttons

Petar Margetićaa6346c1455c
diff --git a/build.zig b/build.zig
index b2c7313..7fc2a5a 100644
--- a/build.zig
+++ b/build.zig
@@ -80,7 +80,9 @@ pub fn build(b: *std.Build) void {
run.addArg(entry[0]);
b.step("i18n-" ++ entry[0], entry[1]).dependOn(&run.step);
}
+ const border_glow_mod = b.createModule(.{ .root_source_file = b.path("src/border_glow.zig"), .target = target, .optimize = optimize });
const ui_mod = b.createModule(.{ .root_source_file = b.path("src/ui/mod.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ ui_mod.addImport("border_glow", border_glow_mod);
ui_mod.addImport("i18n", i18n_mod);
ui_mod.addImport("memory", memory_mod);
ui_mod.addImport("wayland", wayland);
diff --git a/src/input/motion.zig b/src/input/motion.zig
index 406b8ce..8a7873e 100644
--- a/src/input/motion.zig
+++ b/src/input/motion.zig
@@ -180,8 +180,47 @@ pub fn updateDockReveal(input: *Input) void {
while (it.next()) |output| output.dock.pointerAt(input.cursor.x, input.cursor.y, engaged);
}
+fn updateWidgetProximity(input: *Input) void {
+ if (input.open_start_menu) |menu| {
+ const local = scene_data.toNodeLocal(&menu.buffer_node.node, input.cursor.x, input.cursor.y);
+ if (ui_input.updateButtonProximity(&menu.root, @floatCast(local.x), @floatCast(local.y), .{
+ .x = 0,
+ .y = 0,
+ .w = @floatFromInt(menu.panel_box.width),
+ .h = @floatFromInt(menu.panel_box.height),
+ })) {
+ menu.requestWidgetRepaint();
+ }
+ }
+
+ if (input.open_control_center) |cc| {
+ const local = cc.localPoint(input.cursor.x, input.cursor.y);
+ if (ui_input.updateButtonProximity(&cc.root, @floatCast(local.x), @floatCast(local.y), .{
+ .x = 0,
+ .y = 0,
+ .w = @floatFromInt(cc.panel_box.width),
+ .h = @floatFromInt(cc.panel_box.height),
+ })) {
+ cc.requestWidgetRepaint();
+ }
+ }
+
+ if (input.open_power_menu) |menu| {
+ const local = scene_data.toNodeLocal(&menu.buffer_node.node, input.cursor.x, input.cursor.y);
+ if (ui_input.updateButtonProximity(&menu.root, @floatCast(local.x), @floatCast(local.y), .{
+ .x = 0,
+ .y = 0,
+ .w = @floatFromInt(menu.panel_box.width),
+ .h = @floatFromInt(menu.panel_box.height),
+ })) {
+ menu.requestWidgetRepaint();
+ }
+ }
+}
+
pub fn processCursorMotion(input: *Input, time_msec: u32) void {
if (input.server.locker != null or input.server.polkit_dialog != null or input.server.switcher.active()) return;
+ updateWidgetProximity(input);
defer input.updateGrabBorderGlow();
input.positionDragIcon();
input.drag_dodge.motion();
diff --git a/src/power_menu.zig b/src/power_menu.zig
index cd186f8..c2e3056 100644
--- a/src/power_menu.zig
+++ b/src/power_menu.zig
@@ -461,7 +461,7 @@ pub const PowerMenu = struct {
);
}
- fn requestWidgetRepaint(menu: *PowerMenu) void {
+ pub fn requestWidgetRepaint(menu: *PowerMenu) void {
menu.dirty = true;
menu.wlr_output.scheduleFrame();
}
diff --git a/src/toplevel/appearance.zig b/src/toplevel/appearance.zig
index db04fe6..083e841 100644
--- a/src/toplevel/appearance.zig
+++ b/src/toplevel/appearance.zig
@@ -10,7 +10,7 @@ const projection = @import("../projection.zig");
const theme = @import("ui").theme;
const actions = @import("../config_runtime/actions.zig");
const anim = @import("ui").anim;
-const border_glow = @import("../border_glow.zig");
+const border_glow = @import("ui").border_glow;
const window_shape = @import("ui").window_shape;
const log = std.log.scoped(.toplevel);
const chip_slot_px = Toplevel.chip_slot_px;
diff --git a/src/toplevel/chrome_sync.zig b/src/toplevel/chrome_sync.zig
index 538bbb7..2aa4e79 100644
--- a/src/toplevel/chrome_sync.zig
+++ b/src/toplevel/chrome_sync.zig
@@ -13,7 +13,7 @@ const projection = @import("../projection.zig");
const theme = @import("ui").theme;
const stats = @import("../ipc/stats.zig");
const xdg = @import("../xdg.zig");
-const border_glow = @import("../border_glow.zig");
+const border_glow = @import("ui").border_glow;
const gpa = @import("../main.zig").gpa;
const log = std.log.scoped(.toplevel);
const titlebar_stretch = Toplevel.titlebar_stretch;
diff --git a/src/ui/input.zig b/src/ui/input.zig
index 2329f0d..93e6ad0 100644
--- a/src/ui/input.zig
+++ b/src/ui/input.zig
@@ -32,6 +32,99 @@ pub fn hitTest(root: *Widget, x: f32, y: f32) ?*Widget {
return hitTestWithScrollAncestor(root, x, y).widget;
}
+/// Logical-pixel rectangle used to constrain proximity sampling.
+pub const ProximityRect = struct {
+ x: f32,
+ y: f32,
+ w: f32,
+ h: f32,
+};
+
+fn widgetRect(widget: *const Widget) ProximityRect {
+ return .{
+ .x = widget.computed_x,
+ .y = widget.computed_y,
+ .w = widget.computed_width,
+ .h = widget.computed_height,
+ };
+}
+
+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
+/// button's stored sample actually changes.
+pub fn updateButtonProximity(
+ root: *Widget,
+ x: f32,
+ y: f32,
+ viewport: ProximityRect,
+) bool {
+ var changed = false;
+ const root_clip = intersectProximityRect(viewport, widgetRect(root));
+ sampleButtonProximity(root, x, y, root_clip, &changed);
+ return changed;
+}
+
+fn sampleButtonProximity(
+ widget: *Widget,
+ x: f32,
+ y: f32,
+ clip: ProximityRect,
+ changed: *bool,
+) 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
+ data.proximity_x != sample_x or data.proximity_y != sample_y)
+ {
+ data.proximity_distance = distance;
+ data.proximity_x = sample_x;
+ data.proximity_y = sample_y;
+ widget.markPaintDirty();
+ changed.* = true;
+ }
+ },
+ else => {},
+ }
+
+ // paintScrollContainer clips all descendants to its own full rectangle.
+ const child_clip = if (widget.kind == .scroll_container)
+ intersectProximityRect(clip, widgetRect(widget))
+ else
+ clip;
+
+ for (widget.children) |*child| {
+ sampleButtonProximity(child, x, y, child_clip, changed);
+ }
+}
+
pub const HitResult = struct {
widget: ?*Widget,
/// The nearest enclosing `.scroll_container`, if any — its scrollbar is
diff --git a/src/ui/input_tests.zig b/src/ui/input_tests.zig
index 64df071..d7db3cc 100644
--- a/src/ui/input_tests.zig
+++ b/src/ui/input_tests.zig
@@ -19,6 +19,87 @@ const scrollWheel = @import("input.zig").scrollWheel;
const keyEvent = @import("input.zig").keyEvent;
const input_src = @import("input.zig");
+test "button proximity samples visible nested-scroll rectangles and skips unchanged values" {
+ var button = Widget{
+ .kind = .{ .button = .{ .label = "Visible" } },
+ };
+ button.computed_x = 25;
+ button.computed_y = 0;
+ button.computed_width = 20;
+ button.computed_height = 20;
+
+ var clipped_button = Widget{
+ .kind = .{ .button = .{ .label = "Clipped" } },
+ };
+ clipped_button.computed_x = 75;
+ clipped_button.computed_y = 10;
+ clipped_button.computed_width = 10;
+ clipped_button.computed_height = 10;
+
+ var inner_children = [_]Widget{button};
+ var inner = Widget{
+ .kind = .{ .scroll_container = .{} },
+ .children = &inner_children,
+ };
+ inner.computed_x = 20;
+ inner.computed_y = 5;
+ inner.computed_width = 40;
+ inner.computed_height = 30;
+
+ var outer_children = [_]Widget{ inner, clipped_button };
+ var outer = Widget{
+ .kind = .{ .scroll_container = .{} },
+ .children = &outer_children,
+ };
+ outer.computed_x = 10;
+ outer.computed_y = 10;
+ outer.computed_width = 60;
+ outer.computed_height = 40;
+
+ var root_children = [_]Widget{outer};
+ var root = Widget{
+ .kind = .container,
+ .children = &root_children,
+ };
+ root.computed_x = 0;
+ root.computed_y = 0;
+ root.computed_width = 100;
+ root.computed_height = 100;
+ root.linkParents();
+
+ const viewport: input_src.ProximityRect = .{
+ .x = 0,
+ .y = 0,
+ .w = 100,
+ .h = 100,
+ };
+
+ // The visible button is clipped by both scroll ancestors:
+ // its visible rectangle begins at y=10, five pixels below the cursor.
+ try std.testing.expect(input_src.updateButtonProximity(&root, 30, 5, viewport));
+ try std.testing.expectEqual(
+ @as(f32, 5),
+ root.children[0].children[0].children[0].kind.button.proximity_distance,
+ );
+
+ // The sibling lies entirely outside the outer scroll viewport.
+ try std.testing.expect(std.math.isInf(
+ root.children[0].children[1].kind.button.proximity_distance,
+ ));
+
+ // Repeating the same sample must not dirty widgets or advance the revision.
+ const revision = layout.paint_revision;
+ try std.testing.expect(!input_src.updateButtonProximity(&root, 30, 5, viewport));
+ try std.testing.expectEqual(revision, layout.paint_revision);
+
+ // Moving inside the visible portion changes the sample to zero.
+ try std.testing.expect(input_src.updateButtonProximity(&root, 30, 15, viewport));
+ try std.testing.expectEqual(
+ @as(f32, 0),
+ root.children[0].children[0].children[0].kind.button.proximity_distance,
+ );
+}
+
test "hitTest returns the topmost overlapping widget" {
var kids = [_]Widget{
.{ .kind = .container },
diff --git a/src/ui/layout.zig b/src/ui/layout.zig
index dbc10cf..d366597 100644
--- a/src/ui/layout.zig
+++ b/src/ui/layout.zig
@@ -243,6 +243,12 @@ pub const ButtonData = struct {
owner: ?*anyopaque = null,
id: usize = 0,
state: ButtonState = .idle,
+ /// Cursor distance to the button's visible, clipped rectangle.
+ /// Infinity means that no part of the button is currently visible.
+ proximity_distance: f32 = std.math.inf(f32),
+ /// Last sampled cursor position relative to this button.
+ proximity_x: f32 = 0,
+ proximity_y: f32 = 0,
};
pub const CheckboxData = struct {
diff --git a/src/ui/mod.zig b/src/ui/mod.zig
index f041d01..b0c9bc2 100644
--- a/src/ui/mod.zig
+++ b/src/ui/mod.zig
@@ -5,6 +5,7 @@ pub const arrange = @import("arrange.zig");
pub const paint = @import("paint.zig");
pub const window_chrome = @import("window_chrome.zig");
pub const theme = @import("theme.zig");
+pub const border_glow = @import("border_glow");
pub const input = @import("input.zig");
pub const widgets = struct {
diff --git a/src/ui/paint_tests.zig b/src/ui/paint_tests.zig
index 9ce679e..dc19e3d 100644
--- a/src/ui/paint_tests.zig
+++ b/src/ui/paint_tests.zig
@@ -627,3 +627,62 @@ test "segmented frames, swatches and rows take their own relief keys" {
rows.row_hover_relief = .inset;
try expectInsetShading(&paintedRow(flat, .{ .state = .hover }), &paintedRow(rows, .{ .state = .hover }), 80, 35, 11, 38);
}
+
+
+test "button proximity glow follows the pointer and stays rounded and clipped" {
+ const sample = struct {
+ fn pixel(pixels: []const u32, width: i32, scale: f32, x: f32, y: f32) u32 {
+ const px: usize = @intFromFloat(@floor(x * scale));
+ const py: usize = @intFromFloat(@floor(y * scale));
+ return pixels[py * @as(usize, @intCast(width)) + px];
+ }
+
+ fn luma(pixel_value: u32) u32 {
+ const red = (pixel_value >> 16) & 0xff;
+ const green = (pixel_value >> 8) & 0xff;
+ const blue = pixel_value & 0xff;
+ return red * 212 + green * 715 + blue * 72;
+ }
+ };
+
+ for ([_]f32{ 1, 1.5, 2 }) |scale| {
+ var pixels: [256 * 192]u32 = @splat(0xff808080);
+ const width: i32 = @intFromFloat(128 * scale);
+ const height: i32 = @intFromFloat(96 * scale);
+ var renderer = Renderer.init(&pixels, width, height, scale);
+ var palette: theme.Theme = .{};
+ palette.border_glow = .{ 1, 0, 0, 0.60 };
+ palette.border_glow_enabled = true;
+ palette.border_glow_reach = 56;
+ palette.border_glow_length = 72;
+ palette.border_glow_depth = 3;
+ renderer.palette = palette;
+ renderer.clip = .{ .x = 40, .y = 20, .w = 40, .h = 34 };
+
+ const widget = Widget{
+ .kind = .container,
+ .computed_x = 20,
+ .computed_y = 20,
+ .computed_width = 80,
+ .computed_height = 34,
+ };
+ paintButton(&widget, .{
+ .variant = .ghost,
+ .proximity_distance = 4,
+ .proximity_x = 40,
+ .proximity_y = -4,
+ }, &renderer);
+
+ const near = sample.pixel(&pixels, width, scale, 60.5, 20.5);
+ const opposite = sample.pixel(&pixels, width, scale, 60.5, 53.5);
+ const centre = sample.pixel(&pixels, width, scale, 60.5, 37.5);
+ const rounded_corner = sample.pixel(&pixels, width, scale, 20.5, 20.5);
+ const outside_clip = sample.pixel(&pixels, width, scale, 30.5, 30.5);
+
+ try std.testing.expect((near >> 16) & 0xff > 0x80);
+ try std.testing.expect(sample.luma(opposite) < sample.luma(0xff808080));
+ try std.testing.expect((near >> 16) & 0xff > (centre >> 16) & 0xff);
+ try std.testing.expectEqual(@as(u32, 0xff808080), rounded_corner);
+ try std.testing.expectEqual(@as(u32, 0xff808080), outside_clip);
+ }
+}
diff --git a/src/ui/paint_widgets.zig b/src/ui/paint_widgets.zig
index 681d03a..cbfb94f 100644
--- a/src/ui/paint_widgets.zig
+++ b/src/ui/paint_widgets.zig
@@ -70,7 +70,13 @@ pub fn paintButton(widget: *const Widget, data: ButtonData, renderer: *Renderer)
.y = widget.computed_y,
.w = widget.computed_width,
.h = widget.computed_height,
- }, button_widget.optionsOf(data), .{ .pointer = data.state, .focused = ui_input.isFocused(widget) });
+ }, button_widget.optionsOf(data), .{
+ .pointer = data.state,
+ .focused = ui_input.isFocused(widget),
+ .proximity_distance = data.proximity_distance,
+ .proximity_x = data.proximity_x,
+ .proximity_y = data.proximity_y,
+ });
}
pub fn paintCheckbox(widget: *const Widget, data: CheckboxData, renderer: *Renderer) void {
diff --git a/src/ui/widgets/button.zig b/src/ui/widgets/button.zig
index 9350966..51304d5 100644
--- a/src/ui/widgets/button.zig
+++ b/src/ui/widgets/button.zig
@@ -4,6 +4,7 @@
// bookkeeping lives in input.zig's dispatcher; this module only moves a single
// button between states and fires its callback.
const chrome = @import("../window_chrome.zig");
+const border_glow = @import("border_glow");
const std = @import("std");
const layout = @import("../layout.zig");
const theme = @import("../theme.zig");
@@ -51,6 +52,9 @@ pub const State = struct {
/// Keyboard focus: a ring inside the button's bounds, so a widget tree's
/// damage still covers it.
focused: bool = false,
+ proximity_distance: f32 = std.math.inf(f32),
+ proximity_x: f32 = 0,
+ proximity_y: f32 = 0,
};
pub const Metrics = struct {
@@ -133,10 +137,11 @@ pub fn paintBackground(r: *Renderer, box: Rect, opts: Options, state: State) voi
const ring = state.focused and !disabled;
const border_w: f32 = if (ring) 2 else if (opts.variant == .secondary) 1 else 0;
const relief: layout.Relief = if (disabled or (opts.variant != .primary and opts.variant != .secondary)) .flat else if (state.pointer == .press or state.selected) t.button_pressed_relief else t.button_relief;
+ const radius = opts.radius orelse if (opts.variant == .chrome) chrome.cornerRadius(t.chrome_round_buttons, box.w, box.h, chrome.control_radius) else m.radius;
if (bg[3] > 0 or border_w > 0) r.fillRect(box.x, box.y, box.w, box.h, .{
.relief = relief,
.color = bg,
- .radius = opts.radius orelse if (opts.variant == .chrome) chrome.cornerRadius(t.chrome_round_buttons, box.w, box.h, chrome.control_radius) else m.radius,
+ .radius = radius,
.border_width = border_w,
.border_color = if (ring) t.controlFocusColor() else if (hovered) t.border else t.border_soft,
});
@@ -158,6 +163,103 @@ pub fn paintBackground(r: *Renderer, box: Rect, opts: Options, state: State) voi
});
}
}
+
+ paintProximityGlow(r, box, radius, state);
+}
+
+/// Paints the pointer-facing red highlight and its opposing dark shade.
+/// Both passes stay inside the button's antialiased rounded shape.
+fn paintProximityGlow(r: *Renderer, box: Rect, radius: f32, state: State) void {
+ const t = r.palette orelse theme.global;
+ if (!t.border_glow_enabled or state.proximity_distance >= std.math.inf(f32) or
+ t.border_glow[3] <= 0 or t.border_glow_reach <= 0 or
+ t.border_glow_length <= 0 or t.border_glow_depth <= 0 or
+ box.w <= 0 or box.h <= 0)
+ return;
+
+ const probe = border_glow.probe(
+ box.w,
+ box.h,
+ radius,
+ state.proximity_x,
+ state.proximity_y,
+ t.border_glow_reach,
+ ) orelse return;
+
+ const s = r.scale;
+ var dx0 = box.x * s;
+ var dy0 = box.y * s;
+ var dx1 = (box.x + box.w) * s;
+ var dy1 = (box.y + box.h) * s;
+ if (r.effectiveClip()) |clip| {
+ dx0 = @max(dx0, clip.x * s);
+ dy0 = @max(dy0, clip.y * s);
+ dx1 = @min(dx1, (clip.x + clip.w) * s);
+ dy1 = @min(dy1, (clip.y + clip.h) * s);
+ }
+
+ const x0: i32 = @intFromFloat(@floor(@max(0, dx0)));
+ const y0: i32 = @intFromFloat(@floor(@max(0, dy0)));
+ const x1: i32 = @intFromFloat(@ceil(@min(@as(f32, @floatFromInt(r.width)), dx1)));
+ const y1: i32 = @intFromFloat(@ceil(@min(@as(f32, @floatFromInt(r.height)), dy1)));
+ if (x0 >= x1 or y0 >= y1) return;
+
+ // The probe point identifies the near edge. At a rounded corner, choose
+ // the dominant normalized axis so the opposing shade has a stable side.
+ const nx = @abs(probe.x - box.w / 2) / @max(box.w / 2, 0.001);
+ const ny = @abs(probe.y - box.h / 2) / @max(box.h / 2, 0.001);
+ const horizontal = nx >= ny;
+ const negative = if (horizontal) probe.x < box.w / 2 else probe.y < box.h / 2;
+ const anchor = if (horizontal) probe.y else probe.x;
+ const length = t.border_glow_length;
+ const depth = t.border_glow_depth;
+ const red = paint_mod.toColor(t.border_glow);
+ const dark = paint_mod.toColor(.{ 0, 0, 0, t.border_glow[3] * 0.35 });
+ const shape = .{
+ .x = box.x * s,
+ .y = box.y * s,
+ .w = box.w * s,
+ .h = box.h * s,
+ .radius = radius * s,
+ };
+
+ r.clean = false;
+ var py = y0;
+ while (py < y1) : (py += 1) {
+ const sy = @as(f32, @floatFromInt(py)) + 0.5;
+ const logical_y = sy / s;
+ const row: usize = @intCast(py * r.width);
+ var px = x0;
+ while (px < x1) : (px += 1) {
+ const sx = @as(f32, @floatFromInt(px)) + 0.5;
+ const logical_x = sx / s;
+ const coverage = paint_mod.sdf.coverage(paint_mod.sdf.sdRoundedBox(
+ sx, sy, shape.x, shape.y, shape.w, shape.h, shape.radius,
+ ));
+ if (coverage <= 0) continue;
+
+ const along = if (horizontal) logical_y - box.y else logical_x - box.x;
+ const along_ratio = (along - anchor) / length;
+ const along_falloff = @exp(-2 * along_ratio * along_ratio);
+ const near_depth = if (horizontal)
+ (if (negative) logical_x - box.x else box.x + box.w - logical_x)
+ else
+ (if (negative) logical_y - box.y else box.y + box.h - logical_y);
+ const far_depth = if (horizontal)
+ (if (negative) box.x + box.w - logical_x else logical_x - box.x)
+ else
+ (if (negative) box.y + box.h - logical_y else logical_y - box.y);
+
+ const near_alpha = probe.near * along_falloff *
+ @exp(-@max(0, near_depth) / depth) * coverage;
+ const far_alpha = probe.near * along_falloff *
+ @exp(-@max(0, far_depth) / depth) * coverage;
+
+ const index = row + @as(usize, @intCast(px));
+ if (near_alpha > 0) paint_mod.blendPixel(&r.pixels[index], red, near_alpha);
+ if (far_alpha > 0) paint_mod.blendPixel(&r.pixels[index], dark, far_alpha);
+ }
+ }
}
pub fn paintContent(r: *Renderer, box: Rect, opts: Options, state: State) void {