Skip to content

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

Add sidebar accordion header interaction

Mel5d83f08dfc99
diff --git a/src/files/app.zig b/src/files/app.zig
index 63098f3..b8c98f5 100644
--- a/src/files/app.zig
+++ b/src/files/app.zig
@@ -795,6 +795,7 @@ pub const App = struct {
pub const drawPackPlaceIcon = app_sidebar_paint.drawPackPlaceIcon;
pub const renderSidebar = app_sidebar_paint.renderSidebar;
pub const sidebarLayout = app_sidebar_paint.sidebarLayout;
+ pub const sidebarHeaderAt = app_sidebar_paint.sidebarHeaderAt;
pub const renderPinDrop = app_sidebar_paint.renderPinDrop;
pub const fileOpsSidebarHeight = app_sidebar_paint.fileOpsSidebarHeight;
pub const fileOpsSidebarHit = app_sidebar_paint.fileOpsSidebarHit;
diff --git a/src/files/app_pointer.zig b/src/files/app_pointer.zig
index a8ee0fa..90e30d2 100644
--- a/src/files/app_pointer.zig
+++ b/src/files/app_pointer.zig
@@ -503,6 +503,15 @@ pub fn handleButton(self: *App, button: u32, pressed: bool) void {
return;
}
}
+ if (button == 0x110) {
+ if (self.sidebarHeaderAt(x, y)) |section| {
+ self.sidebar_section = section;
+ self.sidebar_section_selected = true;
+ self.clampSidebarScroll();
+ self.invalidate();
+ return;
+ }
+ }
if (self.fileOpsSidebarClick(x, y)) return;
if (self.torrentSidebarHit(x, y)) {
self.openTransfers();
diff --git a/src/files/app_sidebar_paint.zig b/src/files/app_sidebar_paint.zig
index 7ef8d4a..76d1df5 100644
--- a/src/files/app_sidebar_paint.zig
+++ b/src/files/app_sidebar_paint.zig
@@ -82,6 +82,24 @@ pub fn sidebarLayout(self: *App) app_model.SidebarLayout {
);
}
+/// Returns the visible accordion header under the pointer. Card bodies are
+/// deliberately excluded so section selection cannot steal their controls.
+pub fn sidebarHeaderAt(self: *App, x: f64, y: f64) ?app_model.SidebarSection {
+ if (self.sidebarWidth() <= 16 or
+ x < 8 or x >= @as(f64, @floatFromInt(self.sidebarWidth() - 8)) or
+ y < @as(f64, @floatFromInt(self.contentTop())) or
+ y >= @as(f64, @floatFromInt(self.h - self.sidebarFooter()))) return null;
+
+ const layout = self.sidebarLayout();
+ for (layout.cards, 0..) |card, i| {
+ if (!card.visible) continue;
+ const top: f64 = @floatFromInt(card.y);
+ const bottom = top + @as(f64, @floatFromInt(card.header_height));
+ if (y >= top and y < bottom) return @enumFromInt(i);
+ }
+ return null;
+}
+
/// DEVICES: a heading and one row per volume, dimmed while unmounted, with
/// an eject button on the mounted ones.
pub fn renderDevices(self: *App, cr: *c.cairo_t, content_width: f64) void {
diff --git a/src/files/app_sidebar_tests.zig b/src/files/app_sidebar_tests.zig
index bed938c..de03da9 100644
--- a/src/files/app_sidebar_tests.zig
+++ b/src/files/app_sidebar_tests.zig
@@ -63,6 +63,41 @@ test "sidebar layout uses shared headers and only expanded visible body" {
try std.testing.expectEqual(@as(i32, 152), expanded_transfers.height());
}
+test "sidebar header clicks select sections without collapsing the active card" {
+ var app = try testApp();
+ defer app.deinit();
+
+ try std.testing.expectEqual(app_model.SidebarSection.places, app.sidebar_section);
+ try std.testing.expect(!app.sidebar_section_selected);
+
+ const layout = app.sidebarLayout();
+ const disks = layout.cards[@intFromEnum(app_model.SidebarSection.disks)];
+ try std.testing.expect(disks.visible);
+ const x: f64 = 32;
+ const y: f64 = @floatFromInt(disks.y + @divTrunc(disks.header_height, 2));
+
+ try std.testing.expectEqual(app_model.SidebarSection.disks, app.sidebarHeaderAt(x, y).?);
+ app.handleMotion(x, y);
+ app.handleButton(0x110, true);
+
+ try std.testing.expectEqual(app_model.SidebarSection.disks, app.sidebar_section);
+ try std.testing.expect(app.sidebar_section_selected);
+ try std.testing.expect(app.sidebarLayout().cards[@intFromEnum(app_model.SidebarSection.disks)].expanded);
+
+ // Recompute its position after Places collapses. Clicking the active
+ // header is idempotent, not a collapse toggle.
+ const selected_disks = app.sidebarLayout().cards[@intFromEnum(app_model.SidebarSection.disks)];
+ const selected_y: f64 = @floatFromInt(selected_disks.y + @divTrunc(selected_disks.header_height, 2));
+ app.handleMotion(x, selected_y);
+ app.handleButton(0x110, true);
+ try std.testing.expectEqual(app_model.SidebarSection.disks, app.sidebar_section);
+ try std.testing.expect(app.sidebarLayout().cards[@intFromEnum(app_model.SidebarSection.disks)].expanded);
+
+ const body_y: f64 = @floatFromInt(selected_disks.y + selected_disks.header_height + 1);
+ try std.testing.expectEqual(@as(?app_model.SidebarSection, null), app.sidebarHeaderAt(x, body_y));
+ try std.testing.expectEqual(@as(?app_model.SidebarSection, null), app.sidebarHeaderAt(-1, selected_y));
+}
+
test "list column resize pushes following columns and scrolls in Files and chooser" {
for ([_]bool{ false, true }) |chooser| {
var app = try testApp();