RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
refactor(files): route sidebar hover through interaction surface
Petar Margetića1cceb78bee6
diff --git a/src/files/app.zig b/src/files/app.zigindex 2ee45fa..c9afcf4 100644--- a/src/files/app.zig+++ b/src/files/app.zig@@ -188,6 +188,7 @@ pub const App = struct {// damages only what changed; the painters draw `frame`, never the pointer.toolbar_hover: ToolbarHover = .{},toolbar_surface: interaction_surface.Surface = .{},+ sidebar_surface: interaction_surface.Surface = .{},nav_hover: hover_glide.Glide = .{},item_hover: hover_glide.Glide = .{},// Each path section fades independently, including when crossing a separator.@@ -444,6 +445,7 @@ pub const App = struct {pub const toolbarButtonVisible = app_hover.toolbarButtonVisible;pub const toolbarButtonEnabled = app_hover.toolbarButtonEnabled;pub const updateToolbarSurface = app_hover.updateToolbarSurface;+ pub const updateSidebarSurface = app_hover.updateSidebarSurface;pub const hoveredToolbarButton = app_hover.hoveredToolbarButton;pub const toolbarButtonProximity = app_hover.toolbarButtonProximity;pub const damageToolbarProximity = app_hover.damageToolbarProximity;diff --git a/src/files/app_hover.zig b/src/files/app_hover.zigindex 382cfdb..fd3f6ee 100644--- a/src/files/app_hover.zig+++ b/src/files/app_hover.zig@@ -4,6 +4,7 @@ const std = @import("std");const App = @import("app.zig").App;const anim = @import("ui").anim;const header = @import("header.zig");+const interaction_surface = @import("ui").interaction_surface;const hover_glide = @import("ui").hover_glide;const button_widget = @import("ui").widgets.button;const ui_theme = @import("ui").theme;@@ -92,20 +93,70 @@ pub fn clipToViewport(self: *App, rect: header.Rect) header.Rect {/// The pointer's sidebar row, unless that is the current place, which/// paints its own fill.+pub fn updateSidebarSurface(self: *App) void {+ if (self.dialog != null or self.menu != null or self.sidebar_resize != null or+ self.overSidebarDivider(self.mouse_x, self.mouse_y))+ {+ _ = self.sidebar_surface.pointerMotion(null);+ return;+ }++ const index = self.placeAt(self.mouse_x, self.mouse_y) orelse {+ _ = self.sidebar_surface.pointerMotion(null);+ return;+ };+ const eject = self.ejectAt(self.mouse_x, self.mouse_y);+ const key: usize = if (eject) |k| 700 + k else switch (self.rowKind(index)) {+ .builtin => |b| 300 + b,+ .pin => |j| 800 + j,+ .device => |k| 600 + k,+ };+ const bounds = if (eject != null) self.ejectRect(index) else self.placeRect(index);++ _ = self.sidebar_surface.pointerMotion(.{+ .id = @intCast(key),+ .rect = .{+ .x = @floatCast(bounds.x),+ .y = @floatCast(bounds.y),+ .w = @floatCast(bounds.w),+ .h = @floatCast(bounds.h),+ },+ .clip = .{+ .x = 0,+ .y = @floatFromInt(self.contentTop()),+ .w = @floatFromInt(self.sidebarWidth()),+ .h = @floatFromInt(self.h - self.contentTop() - self.sidebarFooter()),+ },+ .role = .interactive,+ });+}++/// The pointer's sidebar row, unless that is the current place, which+/// paints its own fill. The Surface retains the host's existing row identity.pub fn hoveredPlace(self: *App) ?usize {if (self.dialog != null or self.menu != null) return null;if (self.sidebar_resize != null or self.overSidebarDivider(self.mouse_x, self.mouse_y)) return null;- const width: f64 = @floatFromInt(self.sidebarWidth());- if (self.mouse_x < 0 or self.mouse_x >= width) return null;- if (self.sidebarScrollbar()) |bar| if (self.mouse_x >= @as(f64, bar.track.x)) return null;- if (self.mouse_y < @as(f64, @floatFromInt(self.contentTop())) or self.mouse_y >= @as(f64, @floatFromInt(self.h - self.sidebarFooter()))) return null;- for (0..self.rowCount()) |i| {- if (!self.placeVisible(i)) continue;- const row = self.placeRect(i);- if (self.mouse_y < row.y or self.mouse_y >= row.y + row.h) continue;- return if (self.placeActive(i)) null else i;+ if (self.sidebarScrollbar()) |bar| {+ if (self.mouse_x >= @as(f64, bar.track.x)) return null;}- return null;++ const target = self.sidebar_surface.hovered orelse return null;+ const key: usize = @intCast(target.id);+ const index = if (key >= 300 and key < 300 + places.len)+ key - 300+ else if (key >= 800 and key < 800 + pins_mod.max) blk: {+ const pin = key - 800;+ if (pin >= self.pins.items.items.len) return null;+ break :blk pin_base + pin;+ } else if (key >= 600 and key < 700)+ self.deviceBase() + key - 600+ else if (key >= 700 and key < 800)+ self.deviceBase() + key - 700+ else+ return null;++ if (index >= self.rowCount() or !self.placeVisible(index)) return null;+ return if (self.placeActive(index)) null else index;}/// The pointer's item, unless it is selected, which paints its own fill.@@ -249,6 +300,7 @@ pub fn damageToolbarProximity(self: *App, old_x: f64, old_y: f64, new_x: f64, nepub fn stepHover(self: *App, now: i64) void {const pixel = anim.rasterPixelQuantum(@floatFromInt(self.scale));+ self.updateSidebarSurface();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 9bd97ed..2047b1a 100644--- a/src/files/app_hover_tests.zig+++ b/src/files/app_hover_tests.zig@@ -220,6 +220,7 @@ test "sidebar hover glides between places and leaves the current one to its ownvar y = first.y;while (y < next.y + next.h) : (y += 1) {app.mouse_y = y;+ app.updateSidebarSurface();try std.testing.expect(app.hoveredPlace() != null);}}@@ -244,6 +245,32 @@ test "sidebar hover glides between places and leaves the current one to its owntry std.testing.expectEqual(app.placeRect(4).y, box.y);}+test "sidebar surface owns place hover and clears modal and departed targets" {+ var app = try testApp();+ defer app.deinit();+ app.loading = false;++ app.handleMotion(70, @as(f64, @floatFromInt(app.placeY(1))) + 10);+ try std.testing.expectEqual(@as(u64, 301), app.sidebar_surface.hovered.?.id);+ try std.testing.expectEqual(@as(?usize, 1), app.hoveredPlace());++ // The current place keeps its identity for pointer damage, but paints+ // its own selected fill instead of receiving the hover glide.+ app.handleMotion(70, @as(f64, @floatFromInt(app.placeY(0))) + 10);+ try std.testing.expectEqual(@as(u64, 300), app.sidebar_surface.hovered.?.id);+ try std.testing.expectEqual(@as(?usize, null), app.hoveredPlace());++ app.menu = .background;+ app.updateSidebarSurface();+ try std.testing.expect(app.sidebar_surface.hovered == null);+ try std.testing.expectEqual(@as(?usize, null), app.hoveredPlace());++ app.menu = null;+ app.handleMotion(-1, -1);+ try std.testing.expect(app.sidebar_surface.hovered == null);+ try std.testing.expectEqual(@as(?usize, null), app.hoveredPlace());+}+test "a grid that reflows forgets its item highlight" {const before = anim.currentSettings();defer anim.applySettings(before);diff --git a/src/files/app_pointer.zig b/src/files/app_pointer.zigindex 34212cb..74281fc 100644--- a/src/files/app_pointer.zig+++ b/src/files/app_pointer.zig@@ -60,18 +60,7 @@ pub fn hoverKey(self: *App) usize {if (i < 4 and self.listColumn(i).contains(x, y)) return 400 + i;}}- if (x >= 0 and x < @as(f64, @floatFromInt(self.sidebarWidth())) and y >= @as(f64, @floatFromInt(self.contentTop())) and y < @as(f64, @floatFromInt(self.h - self.sidebarFooter()))) {- if (self.ejectAt(x, y)) |k| return 700 + k;- for (0..self.rowCount()) |i| {- if (!self.placeVisible(i)) continue;- const row = self.placeRect(i);- if (y >= row.y and y < row.y + row.h) return switch (self.rowKind(i)) {- .builtin => |b| 300 + b,- .pin => |j| 800 + j,- .device => |k| 600 + k,- };- }- }+ if (self.sidebar_surface.hovered) |target| return @intCast(target.id);if (self.inlineTorrentHit(x, y)) |hit| return std.math.maxInt(usize) - 3000 - hit.index * 2 - @intFromBool(hit.toggle);if (self.itemAt(x, y)) |i| {if (self.sizeButtonRect(i)) |rect| {@@ -145,6 +134,7 @@ pub fn handleMotion(self: *App, x: f64, y: f64) void {self.mouse_x = x;self.mouse_y = y;self.updateToolbarSurface();+ self.updateSidebarSurface();self.damageToolbarProximity(old_x, old_y, x, y);if (self.selection_band != null) self.updateSelectionBand();if (self.text_dragging) {