Skip to content

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

refactor(files): route item hover through interaction surface

Petar Margetiće0b9d74cfef0
diff --git a/src/files/app.zig b/src/files/app.zig
index 3733315..d5eb105 100644
--- a/src/files/app.zig
+++ b/src/files/app.zig
@@ -191,6 +191,7 @@ pub const App = struct {
sidebar_surface: interaction_surface.Surface = .{},
column_surface: interaction_surface.Surface = .{},
dialog_surface: interaction_surface.Surface = .{},
+ item_surface: interaction_surface.Surface = .{},
nav_hover: hover_glide.Glide = .{},
item_hover: hover_glide.Glide = .{},
// Each path section fades independently, including when crossing a separator.
@@ -450,6 +451,7 @@ pub const App = struct {
pub const updateSidebarSurface = app_hover.updateSidebarSurface;
pub const updateColumnSurface = app_hover.updateColumnSurface;
pub const updateDialogSurface = app_hover.updateDialogSurface;
+ pub const updateItemSurface = app_hover.updateItemSurface;
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.zig
index 4a9d850..e0b81e6 100644
--- a/src/files/app_hover.zig
+++ b/src/files/app_hover.zig
@@ -253,6 +253,53 @@ pub fn updateSidebarSurface(self: *App) void {
});
}
+/// The pointer's item card, using Files' existing itemAt geometry.
+pub fn updateItemSurface(self: *App) void {
+ if (self.dialog != null or self.menu != null or self.transfer_view.active) {
+ _ = self.item_surface.pointerMotion(null);
+ return;
+ }
+
+ const index = self.itemAt(self.mouse_x, self.mouse_y) orelse {
+ _ = self.item_surface.pointerMotion(null);
+ return;
+ };
+
+ const per_row: usize = @intCast(self.columns());
+ const col: i32 = @intCast(index % per_row);
+ const row: i32 = @intCast(index / per_row);
+ const cell = self.cellWidth();
+ const rect: header.Rect = .{
+ .x = @floatFromInt(self.sidebarWidth() + 20 + col * cell),
+ .y = @floatFromInt(self.viewTop() + self.itemInset() - self.scroll_y + row * self.rowHeight()),
+ .w = @floatFromInt(cell - 20),
+ .h = @floatFromInt(self.cardHeight()),
+ };
+ const clip: header.Rect = .{
+ .x = @floatFromInt(self.sidebarWidth() + 20),
+ .y = @floatFromInt(self.viewTop()),
+ .w = @floatFromInt(@max(0, self.w - self.sidebarWidth() - 20 - self.scrollbarGutter())),
+ .h = @floatFromInt(@max(0, self.contentBottom() - self.viewTop())),
+ };
+
+ _ = self.item_surface.pointerMotion(.{
+ .id = @intCast(index),
+ .rect = .{
+ .x = @floatCast(rect.x),
+ .y = @floatCast(rect.y),
+ .w = @floatCast(rect.w),
+ .h = @floatCast(rect.h),
+ },
+ .clip = .{
+ .x = @floatCast(clip.x),
+ .y = @floatCast(clip.y),
+ .w = @floatCast(clip.w),
+ .h = @floatCast(clip.h),
+ },
+ .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 {
@@ -283,8 +330,10 @@ pub fn hoveredPlace(self: *App) ?usize {
/// The pointer's item, unless it is selected, which paints its own fill.
pub fn hoveredItem(self: *App) ?usize {
- if (self.dialog != null or self.menu != null) return null;
- const idx = self.itemAt(self.mouse_x, self.mouse_y) orelse return null;
+ if (self.dialog != null or self.menu != null or self.transfer_view.active) return null;
+ const target = self.item_surface.hovered orelse return null;
+ const idx: usize = @intCast(target.id);
+ if (idx >= self.items.items.len) return null;
return if (self.items.items[idx].selected) null else idx;
}
@@ -422,6 +471,7 @@ pub fn damageToolbarProximity(self: *App, old_x: f64, old_y: f64, new_x: f64, ne
pub fn stepHover(self: *App, now: i64) void {
const pixel = anim.rasterPixelQuantum(@floatFromInt(self.scale));
+ self.updateItemSurface();
self.updateSidebarSurface();
self.updateColumnSurface();
self.updateDialogSurface();
diff --git a/src/files/app_hover_tests.zig b/src/files/app_hover_tests.zig
index 7d032e7..84472bd 100644
--- a/src/files/app_hover_tests.zig
+++ b/src/files/app_hover_tests.zig
@@ -119,6 +119,8 @@ test "item hover glides between cells and rests without repaints or wakeups" {
// It fades in where the pointer landed.
const first = pointOnItem(&app, 0);
app.handleMotion(first[0], first[1]);
+ try std.testing.expectEqual(@as(u64, 0), app.item_surface.hovered.?.id);
+ try std.testing.expectEqual(@as(usize, 1000), app.hoverKey());
app.dirty = false;
app.stepHover(1000);
try std.testing.expect(app.hover_active);
@@ -158,6 +160,8 @@ test "item hover glides between cells and rests without repaints or wakeups" {
// A selected item paints its own fill: the highlight fades out.
app.items.items[5].selected = true;
+ try std.testing.expectEqual(@as(u64, 5), app.item_surface.hovered.?.id);
+ try std.testing.expectEqual(@as(usize, 1005), app.hoverKey());
app.stepHover(12100);
app.stepHover(15000);
try std.testing.expectEqual(@as(f32, 0), app.item_hover.frame.alpha);
@@ -181,6 +185,7 @@ test "item hover glides between cells and rests without repaints or wakeups" {
app.menu = .background;
app.stepHover(21100);
app.stepHover(24000);
+ try std.testing.expect(app.item_surface.hovered == null);
try std.testing.expectEqual(@as(f32, 0), app.item_hover.frame.alpha);
}
diff --git a/src/files/app_pointer.zig b/src/files/app_pointer.zig
index 17de29c..ec902a4 100644
--- a/src/files/app_pointer.zig
+++ b/src/files/app_pointer.zig
@@ -48,20 +48,23 @@ pub fn hoverKey(self: *App) usize {
}
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| {
- if (rect.contains(x, y)) {
- if (self.folder_sizes.get(self.items.items[i].path)) |sz| {
- switch (sz) {
- .running => return 2_000_000 + i,
- .done => {},
+ if (self.item_surface.hovered) |target| {
+ const i: usize = @intCast(target.id);
+ if (i < self.items.items.len) {
+ if (self.sizeButtonRect(i)) |rect| {
+ if (rect.contains(x, y)) {
+ if (self.folder_sizes.get(self.items.items[i].path)) |sz| {
+ switch (sz) {
+ .running => return 2_000_000 + i,
+ .done => {},
+ }
+ } else {
+ return 2_000_000 + i;
}
- } else {
- return 2_000_000 + i;
}
}
+ return 1000 + i;
}
- return 1000 + i;
}
return 0;
}
@@ -123,6 +126,7 @@ pub fn handleMotion(self: *App, x: f64, y: f64) void {
self.updateSidebarSurface();
self.updateColumnSurface();
self.updateDialogSurface();
+ self.updateItemSurface();
self.damageToolbarProximity(old_x, old_y, x, y);
if (self.selection_band != null) self.updateSelectionBand();
if (self.text_dragging) {