RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
refactor(files): route dialog button hover through interaction surface
Petar Margetić9cb010f1fe87
diff --git a/src/files/app.zig b/src/files/app.zigindex c4d85d4..3733315 100644--- a/src/files/app.zig+++ b/src/files/app.zig@@ -190,6 +190,7 @@ pub const App = struct {toolbar_surface: interaction_surface.Surface = .{},sidebar_surface: interaction_surface.Surface = .{},column_surface: interaction_surface.Surface = .{},+ dialog_surface: interaction_surface.Surface = .{},nav_hover: hover_glide.Glide = .{},item_hover: hover_glide.Glide = .{},// Each path section fades independently, including when crossing a separator.@@ -448,6 +449,7 @@ pub const App = struct {pub const updateToolbarSurface = app_hover.updateToolbarSurface;pub const updateSidebarSurface = app_hover.updateSidebarSurface;pub const updateColumnSurface = app_hover.updateColumnSurface;+ pub const updateDialogSurface = app_hover.updateDialogSurface;pub const hoveredToolbarButton = app_hover.hoveredToolbarButton;pub const toolbarButtonProximity = app_hover.toolbarButtonProximity;pub const damageToolbarProximity = app_hover.damageToolbarProximity;diff --git a/src/files/app_dialogs.zig b/src/files/app_dialogs.zigindex c87d556..a2c7d66 100644--- a/src/files/app_dialogs.zig+++ b/src/files/app_dialogs.zig@@ -16,6 +16,7 @@ const Dialog = @import("app_model.zig").Dialog;pub fn closeDialog(self: *App) void {self.text_dragging = false;+ _ = self.dialog_surface.pointerMotion(null);self.edit_revision += 1;if (self.dialog) |*dlg| {dlg.deletion.deinit();diff --git a/src/files/app_hover.zig b/src/files/app_hover.zigindex 852a5ae..4a9d850 100644--- a/src/files/app_hover.zig+++ b/src/files/app_hover.zig@@ -131,6 +131,88 @@ pub fn updateColumnSurface(self: *App) void {_ = self.column_surface.pointerMotion(null);}+fn dialogSurfaceHit(id: usize, rect: header.Rect) interaction_surface.Hit {+ return .{+ .id = @intCast(id),+ .rect = .{+ .x = @floatCast(rect.x),+ .y = @floatCast(rect.y),+ .w = @floatCast(rect.w),+ .h = @floatCast(rect.h),+ },+ .clip = .{+ .x = @floatCast(rect.x),+ .y = @floatCast(rect.y),+ .w = @floatCast(rect.w),+ .h = @floatCast(rect.h),+ },+ .role = .button,+ };+}++/// The pointer's generic modal or inline rename button.+pub fn updateDialogSurface(self: *App) void {+ const dlg = self.dialog orelse {+ _ = self.dialog_surface.pointerMotion(null);+ return;+ };++ // These dialogs retain their own interaction and hit-testing paths.+ if (dlg.kind == .transfers or dlg.kind == .properties or+ dlg.kind == .open_with or dlg.kind == .trash_confirm)+ {+ _ = self.dialog_surface.pointerMotion(null);+ return;+ }++ var hit: ?interaction_surface.Hit = null;++ if (self.renameCard()) |card| {+ if (card.confirm.contains(self.mouse_x, self.mouse_y)) {+ hit = dialogSurfaceHit(1, card.confirm);+ } else if (card.cancel.contains(self.mouse_x, self.mouse_y)) {+ hit = dialogSurfaceHit(2, card.cancel);+ }+ } else {+ const dw: f64 = @min(440, @as(f64, @floatFromInt(self.w)) - 24);+ const dx = (@as(f64, @floatFromInt(self.w)) - dw) / 2;+ const dy = (@as(f64, @floatFromInt(self.h)) - 130) / 2;++ if (dlg.kind == .conflict) {+ const button_w = (dw - 48) / 3;+ for (0..3) |i| {+ const rect: header.Rect = .{+ .x = dx + 16 + @as(f64, @floatFromInt(i)) * (button_w + 8),+ .y = dy + 84,+ .w = button_w,+ .h = 28,+ };+ if (self.mouse_x >= rect.x and self.mouse_x <= rect.x + rect.w and+ self.mouse_y >= rect.y and self.mouse_y <= rect.y + rect.h)+ {+ hit = dialogSurfaceHit(i + 1, rect);+ break;+ }+ }+ } else {+ const rects = [_]header.Rect{+ .{ .x = dx + dw - 184, .y = dy + 84, .w = 80, .h = 28 },+ .{ .x = dx + dw - 96, .y = dy + 84, .w = 80, .h = 28 },+ };+ for (rects, 0..) |rect, i| {+ if (self.mouse_x >= rect.x and self.mouse_x <= rect.x + rect.w and+ self.mouse_y >= rect.y and self.mouse_y <= rect.y + rect.h)+ {+ hit = dialogSurfaceHit(i + 1, rect);+ break;+ }+ }+ }+ }++ _ = self.dialog_surface.pointerMotion(hit);+}+/// The pointer's sidebar row, unless that is the current place, which/// paints its own fill.pub fn updateSidebarSurface(self: *App) void {@@ -342,6 +424,7 @@ pub fn stepHover(self: *App, now: i64) void {self.updateSidebarSurface();self.updateColumnSurface();+ self.updateDialogSurface();if (self.sidebarWidth() > 0) {const before = self.nav_hover.frame;self.nav_hover.setTarget(now, if (self.hoveredPlace()) |i| .{ .col = 0, .row = @intCast(i) } else null);diff --git a/src/files/app_hover_tests.zig b/src/files/app_hover_tests.zigindex dd3611a..7d032e7 100644--- a/src/files/app_hover_tests.zig+++ b/src/files/app_hover_tests.zig@@ -390,3 +390,87 @@ test "toolbar proximity uses shared button geometry and damages in-button motiontry std.testing.expect(app.paint_damage != null);try std.testing.expect(app.paint_damage.?.contains(x, y));}+++test "dialog surface owns generic modal button hover targets" {+ var app = try testApp();+ defer app.deinit();++ const dw: f64 = @min(440, @as(f64, @floatFromInt(app.w)) - 24);+ const dx = (@as(f64, @floatFromInt(app.w)) - dw) / 2;+ const dy = (@as(f64, @floatFromInt(app.h)) - 130) / 2;++ const generic_kinds = [_]@import("app_model.zig").DialogKind{+ .terminal_close,+ .new_folder,+ .new_file,+ .new_branch,+ };++ for (generic_kinds) |kind| {+ app.dialog = .{ .kind = kind, .title = "Test" };++ app.mouse_x = dx + dw - 144;+ app.mouse_y = dy + 98;+ app.updateDialogSurface();+ try std.testing.expectEqual(@as(u64, 1), app.dialog_surface.hovered.?.id);+ try std.testing.expectEqual(@as(usize, 1), app.hoverKey());++ app.mouse_x = dx + dw - 56;+ app.updateDialogSurface();+ try std.testing.expectEqual(@as(u64, 2), app.dialog_surface.hovered.?.id);+ try std.testing.expectEqual(@as(usize, 2), app.hoverKey());+ }++ app.dialog = .{+ .kind = .conflict,+ .title = "Conflict",+ };++ const button_w = (dw - 48) / 3;+ for (0..3) |i| {+ app.mouse_x = dx + 16 + @as(f64, @floatFromInt(i)) * (button_w + 8) + button_w / 2;+ app.mouse_y = dy + 98;+ app.updateDialogSurface();+ try std.testing.expectEqual(@as(u64, @intCast(i + 1)), app.dialog_surface.hovered.?.id);+ try std.testing.expectEqual(i + 1, app.hoverKey());+ }++ // A specialized dialog keeps its own interaction path.+ app.dialog = .{ .kind = .transfers, .title = "Transfers" };+ app.updateDialogSurface();+ try std.testing.expect(app.dialog_surface.hovered == null);++ // Closing a dialog clears any retained dialog target.+ app.dialog = .{ .kind = .terminal_close, .title = "Close terminal?" };+ app.mouse_x = dx + dw - 144;+ app.mouse_y = dy + 98;+ app.updateDialogSurface();+ try std.testing.expect(app.dialog_surface.hovered != null);+ app.closeDialog();+ try std.testing.expect(app.dialog_surface.hovered == null);+}++test "dialog surface owns inline rename buttons and clears departed targets" {+ var app = try testApp();+ defer app.deinit();++ app.dialog = .{ .kind = .rename, .title = "Rename" };+ const card = app.renameCard().?;++ app.mouse_x = card.confirm.x + card.confirm.w / 2;+ app.mouse_y = card.confirm.y + card.confirm.h / 2;+ app.updateDialogSurface();+ try std.testing.expectEqual(@as(u64, 1), app.dialog_surface.hovered.?.id);+ try std.testing.expectEqual(@as(usize, 1), app.hoverKey());++ app.mouse_x = card.cancel.x + card.cancel.w / 2;+ app.mouse_y = card.cancel.y + card.cancel.h / 2;+ app.updateDialogSurface();+ try std.testing.expectEqual(@as(u64, 2), app.dialog_surface.hovered.?.id);+ try std.testing.expectEqual(@as(usize, 2), app.hoverKey());++ app.handleMotion(-1, -1);+ try std.testing.expect(app.dialog_surface.hovered == null);+ try std.testing.expectEqual(@as(usize, 0), app.hoverKey());+}diff --git a/src/files/app_paint.zig b/src/files/app_paint.zigindex 7b51426..6ed5f59 100644--- a/src/files/app_paint.zig+++ b/src/files/app_paint.zig@@ -217,8 +217,11 @@ pub fn render(self: *App, cr: *c.cairo_t) void {}}-pub fn renderButton(self: *App, cr: *c.cairo_t, x: f64, y: f64, w: f64, h: f64, label_text: []const u8, enabled: bool) void {- const hovered = enabled and self.mouse_x >= x and self.mouse_x <= x + w and self.mouse_y >= y and self.mouse_y <= y + h;+pub fn renderButton(self: *App, cr: *c.cairo_t, x: f64, y: f64, w: f64, h: f64, label_text: []const u8, enabled: bool, target_id: usize) void {+ const hovered = enabled and if (self.dialog_surface.hovered) |target|+ target.id == target_id+ else+ false;var layer = shell_ui.Layer.begin(cr, .{ .x = @floatCast(x), .y = @floatCast(y), .w = @floatCast(w), .h = @floatCast(h) }) orelse return;ui_button.paint(&layer.renderer, layer.local(), .{ .label = label_text }, .{ .pointer = if (!enabled) .disabled else if (hovered) .hover else .idle });layer.finish();@@ -337,7 +340,10 @@ pub fn renderRenameCard(self: *App, cr: *c.cairo_t, card: RenameCard) void {.icon = if (i == 0) .checkmark else .close,.icon_scale = 0.55,.label = if (i == 0) tr("Rename") else tr("Cancel"),- }, .{ .pointer = if (rect.contains(self.mouse_x, self.mouse_y)) .hover else .idle });+ }, .{ .pointer = if (self.dialog_surface.hovered) |target|+ if (target.id == i + 1) .hover else .idle+ else+ .idle });layer.finish();}}@@ -546,24 +552,24 @@ pub fn renderDialog(self: *App, cr: *c.cairo_t) void {if (dlg.kind == .terminal_close) {drawEllipsis(cr, tr("Closing ends this shell and its running jobs."), dx + 16, dy + 56, dw - 32, shell_ui.textSize(), false);- self.renderButton(cr, dx + dw - 184, dy + 84, 80, 28, tr("Close"), true);- self.renderButton(cr, dx + dw - 96, dy + 84, 80, 28, tr("Cancel"), true);+ self.renderButton(cr, dx + dw - 184, dy + 84, 80, 28, tr("Close"), true, 1);+ self.renderButton(cr, dx + dw - 96, dy + 84, 80, 28, tr("Cancel"), true, 2);} else if (dlg.kind == .conflict) {const conflict_str = dlg.conflict_name orelse "";const conflict_msg = i18n.fmt(&buf, tr("File '{value}' already exists in destination."), .{ .value = conflict_str }) catch tr("Conflict");setSource(cr, t.fg);drawEllipsis(cr, conflict_msg, dx + 16, dy + 56, dw - 32, shell_ui.textSize(), false);- self.renderButton(cr, dx + 16, dy + 84, (dw - 48) / 3, 28, tr("Skip (S)"), true);- self.renderButton(cr, dx + 24 + (dw - 48) / 3, dy + 84, (dw - 48) / 3, 28, tr("Rename (R)"), true);- self.renderButton(cr, dx + 32 + 2 * (dw - 48) / 3, dy + 84, (dw - 48) / 3, 28, tr("Cancel (C)"), true);+ self.renderButton(cr, dx + 16, dy + 84, (dw - 48) / 3, 28, tr("Skip (S)"), true, 1);+ self.renderButton(cr, dx + 24 + (dw - 48) / 3, dy + 84, (dw - 48) / 3, 28, tr("Rename (R)"), true, 2);+ self.renderButton(cr, dx + 32 + 2 * (dw - 48) / 3, dy + 84, (dw - 48) / 3, 28, tr("Cancel (C)"), true, 3);} else {self.renderEditor(cr, &self.dialog.?.edit, self.editorRect(), true, false);setSource(cr, t.fg);if (dw >= 420) drawText(cr, tr("Enter confirms · Esc cancels"), dx + 16, dy + 102, shell_ui.textSize(), false);- self.renderButton(cr, dx + dw - 184, dy + 84, 80, 28, tr("OK"), true);- self.renderButton(cr, dx + dw - 96, dy + 84, 80, 28, tr("Cancel"), true);+ self.renderButton(cr, dx + dw - 184, dy + 84, 80, 28, tr("OK"), true, 1);+ self.renderButton(cr, dx + dw - 96, dy + 84, 80, 28, tr("Cancel"), true, 2);}}diff --git a/src/files/app_pointer.zig b/src/files/app_pointer.zigindex d79acd7..17de29c 100644--- a/src/files/app_pointer.zig+++ b/src/files/app_pointer.zig@@ -26,19 +26,7 @@ pub fn hoverKey(self: *App) usize {if (dlg.kind == .properties) return dlg.properties.?.hit(self.w, self.h, x, y);if (dlg.kind == .open_with) return dlg.applications.?.hit(self.w, self.h, x, y);if (dlg.kind == .trash_confirm) return @intFromEnum(dlg.deletion.geometry(self.w, self.h).target(x, y));- const dw: f64 = @min(440, @as(f64, @floatFromInt(self.w)) - 24);- const dx = (@as(f64, @floatFromInt(self.w)) - dw) / 2;- const dy = (@as(f64, @floatFromInt(self.h)) - 130) / 2;- if (y < dy + 84 or y > dy + 112) return 0;- if (dlg.kind == .conflict) {- for (0..3) |i| {- const left = dx + 16 + @as(f64, @floatFromInt(i)) * ((dw - 48) / 3 + 8);- if (x >= left and x <= left + (dw - 48) / 3) return i + 1;- }- } else {- if (x >= dx + dw - 184 and x <= dx + dw - 104) return 1;- if (x >= dx + dw - 96 and x <= dx + dw - 16) return 2;- }+ if (self.dialog_surface.hovered) |target| return @intCast(target.id);return 0;}if (self.menu != null) return 100 + (self.menuHit(x, y) orelse 99);@@ -134,6 +122,7 @@ pub fn handleMotion(self: *App, x: f64, y: f64) void {self.updateToolbarSurface();self.updateSidebarSurface();self.updateColumnSurface();+ self.updateDialogSurface();self.damageToolbarProximity(old_x, old_y, x, y);if (self.selection_band != null) self.updateSelectionBand();if (self.text_dragging) {