Skip to content

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

feat(images): add proximity button glow

Petar Margetić2dbf7fa07afe
diff --git a/src/images/app.zig b/src/images/app.zig
index 551d3e4..c2fcaa7 100644
--- a/src/images/app.zig
+++ b/src/images/app.zig
@@ -145,6 +145,7 @@ pub const App = struct {
pub const canvas = app_input.canvas;
pub const buttonRect = app_input.buttonRect;
+ pub const buttonProximity = app_input.buttonProximity;
pub const hit = app_input.hit;
pub const clamp = app_input.clamp;
pub const zoom = app_input.zoom;
diff --git a/src/images/app_input.zig b/src/images/app_input.zig
index e25f9de..86051af 100644
--- a/src/images/app_input.zig
+++ b/src/images/app_input.zig
@@ -9,6 +9,8 @@ const cache_mod = @import("cache.zig");
const Rect = view.Rect;
const edit = @import("edit.zig");
const scrollbar = @import("ui").widgets.scrollbar;
+const button_widget = @import("ui").widgets.button;
+const ui_theme = @import("ui").theme;
const a = std.heap.c_allocator;
const app_mod = @import("app.zig");
const App = app_mod.App;
@@ -26,6 +28,59 @@ pub fn buttonRect(self: *const App, action: Action) Rect {
return .{ .x = 12 + @as(f64, @floatFromInt(n % columns)) * width, .y = 8 + @as(f64, @floatFromInt(n / columns)) * 40, .w = width - 4, .h = 36 };
}
+pub fn buttonProximity(
+ self: *const App,
+ action: Action,
+ x: f64,
+ y: f64,
+) button_widget.ProximitySample {
+ const rect = self.buttonRect(action);
+ const absent = button_widget.ProximitySample{
+ .distance = std.math.inf(f32),
+ .x = @floatCast(x - rect.x),
+ .y = @floatCast(y - rect.y),
+ };
+ if (self.dialog != .none or self.operation != null or !self.enabled(action))
+ return absent;
+
+ return button_widget.sampleProximity(
+ .{
+ .x = @floatCast(rect.x),
+ .y = @floatCast(rect.y),
+ .w = @floatCast(rect.w),
+ .h = @floatCast(rect.h),
+ },
+ @floatCast(x),
+ @floatCast(y),
+ .{
+ .x = 0,
+ .y = 0,
+ .w = @floatFromInt(self.w),
+ .h = @floatCast(self.toolbarBottom()),
+ },
+ );
+}
+
+fn toolbarProximityChanged(
+ self: *const App,
+ old_x: f64,
+ old_y: f64,
+ new_x: f64,
+ new_y: f64,
+) bool {
+ const reach = ui_theme.global.border_glow_reach;
+ if (!ui_theme.global.button_hover_glow_enabled or reach <= 0 or
+ self.dialog != .none or self.operation != null)
+ return false;
+
+ for (std.meta.tags(Action)) |action| {
+ const before = self.buttonProximity(action, old_x, old_y);
+ const after = self.buttonProximity(action, new_x, new_y);
+ if (@min(before.distance, after.distance) <= reach) return true;
+ }
+ return false;
+}
+
pub fn hit(self: *const App, x: f64, y: f64) ?Action {
inline for (std.meta.tags(Action)) |action| if (self.buttonRect(action).contains(x, y)) return action;
return null;
@@ -163,6 +218,8 @@ pub fn handleKey(self: *App, sym: u32, utf8: []const u8) void {
pub fn handleMotion(self: *App, x: f64, y: f64) void {
if (self.closed) return;
+ const old_x = self.px;
+ const old_y = self.py;
if (self.dialog == .trash) {
if (self.deletion.motion(self.w, self.h, x, y)) self.dirty = true;
const g = self.deletion.geometry(self.w, self.h);
@@ -173,6 +230,7 @@ pub fn handleMotion(self: *App, x: f64, y: f64) void {
self.hover = hover;
self.dirty = true;
}
+ if (toolbarProximityChanged(self, old_x, old_y, x, y)) self.dirty = true;
if (self.operation != null or (self.dialog != .none and !self.filename_drag)) {
self.px = x;
self.py = y;
@@ -313,3 +371,41 @@ pub fn revealSelected(self: *App) void {
const start = self.thumbStart();
self.strip_start = if (pos < start) pos else if (pos >= start + self.thumbCount()) pos + 1 - self.thumbCount() else start;
}
+
+test "Images toolbar proximity drives redraws outside direct hover" {
+ const paths = [_][:0]const u8{"/nonexistent/image.png"};
+ var app: App = .{
+ .paths = &paths,
+ .selected = 0,
+ .loader = undefined,
+ .thumbnail_loader = undefined,
+ };
+
+ const saved_theme = ui_theme.global;
+ defer ui_theme.global = saved_theme;
+ ui_theme.global.button_hover_glow_enabled = true;
+ ui_theme.global.border_glow_reach = 56;
+
+ const rect = app.buttonRect(.back);
+ const x = rect.x + rect.w / 2;
+ const y = rect.y - 4;
+ const sample = buttonProximity(&app, .back, x, y);
+
+ try std.testing.expectApproxEqAbs(@as(f32, 4), sample.distance, 0.001);
+ try std.testing.expectApproxEqAbs(@as(f32, @floatCast(rect.w / 2)), sample.x, 0.001);
+ try std.testing.expectApproxEqAbs(@as(f32, -4), sample.y, 0.001);
+
+ const disabled = buttonProximity(&app, .previous, x, y);
+ try std.testing.expect(std.math.isInf(disabled.distance));
+
+ app.px = 500;
+ app.py = 500;
+ app.dirty = false;
+ app.handleMotion(x, y);
+ try std.testing.expect(app.dirty);
+ try std.testing.expect(app.hover == null);
+
+ app.dirty = false;
+ app.handleMotion(500, 500);
+ try std.testing.expect(app.dirty);
+}
diff --git a/src/images/app_render.zig b/src/images/app_render.zig
index 4b36f29..4f98b62 100644
--- a/src/images/app_render.zig
+++ b/src/images/app_render.zig
@@ -47,9 +47,13 @@ pub fn render(self: *App, cr: *c.cairo_t) void {
.{ .variant = .ghost, .label = label }
else
.{ .variant = .ghost, .icon = glyph(act), .label = label };
+ const proximity = self.buttonProximity(act, self.px, self.py);
var layer = shell_ui.Layer.begin(cr, shellRect(r)) orelse continue;
ui_button.paint(&layer.renderer, layer.local(), opts, .{
.pointer = if (!self.enabled(act) or self.operation != null) .disabled else if (self.hover == act) .hover else .idle,
+ .proximity_distance = proximity.distance,
+ .proximity_x = proximity.x,
+ .proximity_y = proximity.y,
});
layer.finish();
}
diff --git a/src/images/main.zig b/src/images/main.zig
index d27376b..6a79514 100644
--- a/src/images/main.zig
+++ b/src/images/main.zig
@@ -371,6 +371,7 @@ pub const Client = struct {
.leave => {
self.inside = false;
self.app.cancelDrag();
+ self.app.handleMotion(-1, -1);
},
.motion => |ev| {
self.handlePointerMotion(ev.surface_x.toDouble(), ev.surface_y.toDouble());