Skip to content

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

feat(pdf): add proximity button glow

Petar Margetić1c7c472f2c62
diff --git a/src/pdf/app.zig b/src/pdf/app.zig
index 1889400..f82be3f 100644
--- a/src/pdf/app.zig
+++ b/src/pdf/app.zig
@@ -355,6 +355,7 @@ pub const App = struct {
// app_pointer.zig
pub const handleMotion = app_pointer.handleMotion;
+ pub const buttonProximity = app_pointer.buttonProximity;
pub const handleButton = app_pointer.handleButton;
pub const clearSelectionText = app_pointer.clearSelectionText;
pub const updateSelection = app_pointer.updateSelection;
diff --git a/src/pdf/app_paint.zig b/src/pdf/app_paint.zig
index 0204576..7313d8d 100644
--- a/src/pdf/app_paint.zig
+++ b/src/pdf/app_paint.zig
@@ -4,6 +4,7 @@ const std = @import("std");
const i18n = @import("i18n");
const tr = i18n.tr;
const App = @import("app.zig").App;
+const Action = @import("app.zig").Action;
const c = @import("c.zig");
const document = @import("document.zig");
const layout = @import("layout.zig");
@@ -393,16 +394,16 @@ pub fn renderToolbar(self: *App, cr: *c.api.cairo_t) void {
const w = item.w;
const h = toolbar_button_h;
switch (item.act) {
- .toggle_sidebar => self.renderButtonIcon(cr, x, y, w, h, .view_grid, tr("Toggle sidebar")),
- .prev_page => self.renderButtonText(cr, x, y, w, h, "◀"),
- .next_page => self.renderButtonText(cr, x, y, w, h, "▶"),
- .zoom_out => self.renderButtonText(cr, x, y, w, h, "−"),
- .zoom_in => self.renderButtonText(cr, x, y, w, h, "+"),
- .fit_width => self.renderToggle(cr, x, y, w, h, tr("Fit width"), self.zoom_mode == .fit_width),
- .fit_page => self.renderToggle(cr, x, y, w, h, tr("Fit page"), self.zoom_mode == .fit_page),
- .rotate => self.renderButtonIcon(cr, x, y, w, h, .rotate, tr("Rotate")),
- .find_toggle => self.renderButtonText(cr, x, y, w, h, if (self.search_open) tr("Find ▲") else tr("Find")),
- .print => self.renderButtonIcon(cr, x, y, w, h, .print, tr("Print")),
+ .toggle_sidebar => self.renderButtonIcon(cr, x, y, w, h, .view_grid, tr("Toggle sidebar"), item.act),
+ .prev_page => self.renderButtonText(cr, x, y, w, h, "◀", item.act),
+ .next_page => self.renderButtonText(cr, x, y, w, h, "▶", item.act),
+ .zoom_out => self.renderButtonText(cr, x, y, w, h, "−", item.act),
+ .zoom_in => self.renderButtonText(cr, x, y, w, h, "+", item.act),
+ .fit_width => self.renderToggle(cr, x, y, w, h, tr("Fit width"), self.zoom_mode == .fit_width, item.act),
+ .fit_page => self.renderToggle(cr, x, y, w, h, tr("Fit page"), self.zoom_mode == .fit_page, item.act),
+ .rotate => self.renderButtonIcon(cr, x, y, w, h, .rotate, tr("Rotate"), item.act),
+ .find_toggle => self.renderButtonText(cr, x, y, w, h, if (self.search_open) tr("Find ▲") else tr("Find"), item.act),
+ .print => self.renderButtonIcon(cr, x, y, w, h, .print, tr("Print"), item.act),
else => {},
}
}
@@ -535,33 +536,47 @@ pub fn renderDialog(self: *App, cr: *c.api.cairo_t) void {
}
}
- self.renderButtonText(cr, dx + dw - 180, dy + dh - 46, 80, 32, tr("Cancel"));
+ self.renderButtonText(cr, dx + dw - 180, dy + dh - 46, 80, 32, tr("Cancel"), null);
const btn_label = if (self.dialog == .password) tr("Unlock") else tr("OK");
- self.renderButtonText(cr, dx + dw - 90, dy + dh - 46, 70, 32, btn_label);
+ self.renderButtonText(cr, dx + dw - 90, dy + dh - 46, 70, 32, btn_label, null);
}
-pub fn renderButtonText(self: *App, cr: *c.api.cairo_t, x: f64, y: f64, w: f64, h: f64, label: []const u8) void {
+pub fn renderButtonText(self: *App, cr: *c.api.cairo_t, x: f64, y: f64, w: f64, h: f64, label: []const u8, action: ?Action) void {
+ const proximity: @import("ui").widgets.button.ProximitySample = if (action) |act| self.buttonProximity(act, self.px, self.py) else .{ .distance = std.math.inf(f32), .x = 0, .y = 0 };
const is_hover = self.px >= x and self.px <= x + w and self.py >= y and self.py <= y + h;
var layer = shell_ui.Layer.begin(cr, .{ .x = @floatCast(x), .y = @floatCast(y), .w = @floatCast(w), .h = @floatCast(h) }) orelse return;
defer layer.finish();
ui_button.paint(&layer.renderer, layer.local(), .{
.variant = .secondary,
.label = label,
- }, .{ .pointer = if (is_hover) .hover else .idle });
+ }, .{
+ .pointer = if (is_hover) .hover else .idle,
+ .proximity_distance = proximity.distance,
+ .proximity_x = proximity.x,
+ .proximity_y = proximity.y,
+ });
}
/// A text button shown selected while its mode is active.
-pub fn renderToggle(self: *App, cr: *c.api.cairo_t, x: f64, y: f64, w: f64, h: f64, label: []const u8, selected: bool) void {
+pub fn renderToggle(self: *App, cr: *c.api.cairo_t, x: f64, y: f64, w: f64, h: f64, label: []const u8, selected: bool, action: Action) void {
+ const proximity = self.buttonProximity(action, self.px, self.py);
const is_hover = self.px >= x and self.px <= x + w and self.py >= y and self.py <= y + h;
var layer = shell_ui.Layer.begin(cr, .{ .x = @floatCast(x), .y = @floatCast(y), .w = @floatCast(w), .h = @floatCast(h) }) orelse return;
defer layer.finish();
ui_button.paint(&layer.renderer, layer.local(), .{
.variant = .ghost,
.label = label,
- }, .{ .pointer = if (is_hover) .hover else .idle, .selected = selected });
+ }, .{
+ .pointer = if (is_hover) .hover else .idle,
+ .selected = selected,
+ .proximity_distance = proximity.distance,
+ .proximity_x = proximity.x,
+ .proximity_y = proximity.y,
+ });
}
-pub fn renderButtonIcon(self: *App, cr: *c.api.cairo_t, x: f64, y: f64, w: f64, h: f64, icon: IconId, tooltip: []const u8) void {
+pub fn renderButtonIcon(self: *App, cr: *c.api.cairo_t, x: f64, y: f64, w: f64, h: f64, icon: IconId, tooltip: []const u8, action: Action) void {
+ const proximity = self.buttonProximity(action, self.px, self.py);
const is_hover = self.px >= x and self.px <= x + w and self.py >= y and self.py <= y + h;
var layer = shell_ui.Layer.begin(cr, .{ .x = @floatCast(x), .y = @floatCast(y), .w = @floatCast(w), .h = @floatCast(h) }) orelse return;
defer layer.finish();
@@ -569,5 +584,10 @@ pub fn renderButtonIcon(self: *App, cr: *c.api.cairo_t, x: f64, y: f64, w: f64,
.variant = .ghost,
.icon = icon,
.label = tooltip,
- }, .{ .pointer = if (is_hover) .hover else .idle });
+ }, .{
+ .pointer = if (is_hover) .hover else .idle,
+ .proximity_distance = proximity.distance,
+ .proximity_x = proximity.x,
+ .proximity_y = proximity.y,
+ });
}
diff --git a/src/pdf/app_pointer.zig b/src/pdf/app_pointer.zig
index 17597cc..a1b4453 100644
--- a/src/pdf/app_pointer.zig
+++ b/src/pdf/app_pointer.zig
@@ -7,15 +7,82 @@ const App = @import("app.zig").App;
const layout = @import("layout.zig");
const selection = @import("selection.zig");
const scrollbar = @import("ui").widgets.scrollbar;
+const button_widget = @import("ui").widgets.button;
+const ui_theme = @import("ui").theme;
+const Action = @import("app.zig").Action;
const a = @import("app.zig").a;
const toolbar_buttons = @import("app.zig").toolbar_buttons;
const toolbar_button_y = @import("app.zig").toolbar_button_y;
const toolbar_button_h = @import("app.zig").toolbar_button_h;
+pub fn buttonProximity(
+ self: *const App,
+ action: Action,
+ x: f64,
+ y: f64,
+) button_widget.ProximitySample {
+ const absent = button_widget.ProximitySample{
+ .distance = std.math.inf(f32),
+ .x = 0,
+ .y = 0,
+ };
+ if (self.dialog != .none) return absent;
+
+ for (toolbar_buttons) |item| {
+ if (item.act != action) continue;
+
+ return button_widget.sampleProximity(
+ .{
+ .x = @floatCast(item.x),
+ .y = @floatCast(toolbar_button_y),
+ .w = @floatCast(item.w),
+ .h = @floatCast(toolbar_button_h),
+ },
+ @floatCast(x),
+ @floatCast(y),
+ .{
+ .x = 0,
+ .y = 0,
+ .w = @floatFromInt(self.w),
+ .h = @floatCast(self.toolbarRect().h),
+ },
+ );
+ }
+
+ return absent;
+}
+
+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)
+ return false;
+
+ for (toolbar_buttons) |item| {
+ const before = self.buttonProximity(item.act, old_x, old_y);
+ const after = self.buttonProximity(item.act, new_x, new_y);
+ if (@min(before.distance, after.distance) <= reach) return true;
+ }
+
+ return false;
+}
+
pub fn handleMotion(self: *App, px: f64, py: f64) void {
+ const old_x = self.px;
+ const old_y = self.py;
self.px = px;
self.py = py;
+ if (toolbarProximityChanged(self, old_x, old_y, px, py)) {
+ self.dirty = true;
+ }
+
if (self.hbar_drag.active) {
if (self.hbar()) |bar| self.scroll_x = self.hbar_drag.offsetAt(bar, @floatCast(px), @floatCast(py));
self.dirty = true;
@@ -351,6 +418,35 @@ pub fn cancelDrag(self: *App) void {
self.selection_dragging = false;
}
+test "PDF toolbar proximity samples buttons and detects redraws" {
+ 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;
+
+ var app: App = undefined;
+ app.w = 700;
+ app.dialog = .none;
+
+ const button = toolbar_buttons[1];
+ const x = button.x + button.w / 2;
+ const y = toolbar_button_y - 4;
+
+ const sample = buttonProximity(&app, button.act, x, y);
+ try std.testing.expectApproxEqAbs(@as(f32, 4), sample.distance, 0.001);
+ try std.testing.expectApproxEqAbs(@as(f32, 16), sample.x, 0.001);
+ try std.testing.expectApproxEqAbs(@as(f32, -4), sample.y, 0.001);
+
+ try std.testing.expect(toolbarProximityChanged(&app, x, y, 650, 500));
+ try std.testing.expect(!toolbarProximityChanged(&app, 650, 500, 650, 500));
+
+ app.dialog = .information;
+ const hidden = buttonProximity(&app, button.act, x, y);
+ try std.testing.expect(std.math.isInf(hidden.distance));
+ try std.testing.expect(!toolbarProximityChanged(&app, x, y, 650, 500));
+}
+
pub fn cursorName(self: *const App) [*:0]const u8 {
if (self.hover_link != null) return "pointer";
if (self.hover_text or self.selection_dragging) return "text";
diff --git a/src/pdf/client_input.zig b/src/pdf/client_input.zig
index eb3c3e5..c3fd56b 100644
--- a/src/pdf/client_input.zig
+++ b/src/pdf/client_input.zig
@@ -21,6 +21,8 @@ pub fn pointerEvent(_: *wl.Pointer, event: wl.Pointer.Event, self: *Client) void
.leave => {
self.inside = false;
self.app.cancelDrag();
+ self.app.handleMotion(-1, -1);
+ self.app.dirty = true;
},
.motion => |ev| {
self.handlePointerMotion(ev.surface_x.toDouble(), ev.surface_y.toDouble());
diff --git a/src/pdf_tests.zig b/src/pdf_tests.zig
index 1d8faa0..b424cfa 100644
--- a/src/pdf_tests.zig
+++ b/src/pdf_tests.zig
@@ -6,6 +6,10 @@ const selection = @import("pdf/selection.zig");
const cache = @import("pdf/cache.zig");
const worker = @import("pdf/worker.zig");
+test {
+ _ = @import("pdf/app_pointer.zig");
+}
+
const testing = std.testing;
const a = testing.allocator;