Skip to content

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

Add sidebar accordion state and geometry

Meld2c762f4965d
diff --git a/src/files/app.zig b/src/files/app.zig
index f7cd23a..76d7dad 100644
--- a/src/files/app.zig
+++ b/src/files/app.zig
@@ -122,6 +122,11 @@ pub const App = struct {
show_hidden: bool = false,
list_view: bool = false,
non_git_list_view: ?bool = null,
+ /// The sidebar accordion selection. Git's branch-list expansion remains
+ /// independent in `git_expanded`.
+ sidebar_section: app_model.SidebarSection = .places,
+ /// Set only after an explicit section choice; later activity may respect it.
+ sidebar_section_selected: bool = false,
git_expanded: bool = true,
/// The "Git View" checkbox: off shows a repository like any other folder.
git_view: bool = true,
diff --git a/src/files/app_model.zig b/src/files/app_model.zig
index 54907d9..534c9c7 100644
--- a/src/files/app_model.zig
+++ b/src/files/app_model.zig
@@ -93,6 +93,64 @@ pub const FileAction = enum {
};
pub const Sort = enum { name, name_desc, size, modified, type, size_asc, modified_asc, type_desc };
+/// The four first-class sections in the Files sidebar, in display order.
+pub const SidebarSection = enum(u2) {
+ places,
+ disks,
+ git,
+ transfers,
+};
+
+/// Shared geometry for one sidebar accordion card.
+pub const SidebarCardGeometry = struct {
+ y: i32,
+ header_height: i32,
+ body_height: i32,
+ visible: bool,
+ expanded: bool,
+
+ pub fn height(self: SidebarCardGeometry) i32 {
+ if (!self.visible) return 0;
+ return self.header_height + (if (self.expanded) self.body_height else 0);
+ }
+};
+
+/// Vertical layout contract shared by all sidebar cards.
+pub const SidebarLayout = struct {
+ pub const header_height: i32 = 32;
+ pub const gap: i32 = 8;
+
+ cards: [4]SidebarCardGeometry,
+
+ /// Positions visible cards in section order. Hidden cards consume no
+ /// space; only the selected section receives its body height.
+ pub fn init(
+ top: i32,
+ body_heights: [4]i32,
+ visible: [4]bool,
+ expanded_section: SidebarSection,
+ ) SidebarLayout {
+ var result: SidebarLayout = .{ .cards = undefined };
+ var y = top;
+
+ for (0..4) |i| {
+ const section: SidebarSection = @enumFromInt(i);
+ const expanded = visible[i] and section == expanded_section;
+ const card: SidebarCardGeometry = .{
+ .y = y,
+ .header_height = header_height,
+ .body_height = body_heights[i],
+ .visible = visible[i],
+ .expanded = expanded,
+ };
+ result.cards[i] = card;
+ if (visible[i]) y += card.height() + gap;
+ }
+
+ return result;
+ }
+};
+
pub const SizeState = union(enum) {
running: *dirsize.Job,
done: struct {
diff --git a/src/files/app_sidebar_tests.zig b/src/files/app_sidebar_tests.zig
index 5cf6738..7b40c9e 100644
--- a/src/files/app_sidebar_tests.zig
+++ b/src/files/app_sidebar_tests.zig
@@ -10,10 +10,59 @@ const pins_mod = @import("pins.zig");
const MenuKind = @import("app_model.zig").MenuKind;
const FileAction = @import("app_model.zig").FileAction;
const Sort = @import("app_model.zig").Sort;
+const app_model = @import("app_model.zig");
const app_tests = @import("app_tests.zig");
const testApp = app_tests.testApp;
const testItems = app_tests.testItems;
+test "sidebar accordion defaults to Places without changing Git branch expansion" {
+ 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);
+ try std.testing.expect(app.git_expanded);
+}
+
+test "sidebar layout uses shared headers and only expanded visible body" {
+ const body_heights = [_]i32{ 80, 40, 64, 120 };
+ const visible = [_]bool{ true, true, false, true };
+
+ const places_expanded = app_model.SidebarLayout.init(
+ 100,
+ body_heights,
+ visible,
+ .places,
+ );
+ const places = places_expanded.cards[@intFromEnum(app_model.SidebarSection.places)];
+ const disks = places_expanded.cards[@intFromEnum(app_model.SidebarSection.disks)];
+ const git = places_expanded.cards[@intFromEnum(app_model.SidebarSection.git)];
+ const transfers = places_expanded.cards[@intFromEnum(app_model.SidebarSection.transfers)];
+
+ try std.testing.expectEqual(@as(i32, 100), places.y);
+ try std.testing.expectEqual(@as(i32, 112), places.height());
+ try std.testing.expectEqual(@as(i32, 220), disks.y);
+ try std.testing.expectEqual(@as(i32, 32), disks.height());
+ try std.testing.expect(!git.visible);
+ try std.testing.expectEqual(@as(i32, 260), transfers.y);
+ try std.testing.expectEqual(@as(i32, 32), transfers.height());
+
+ const transfers_expanded = app_model.SidebarLayout.init(
+ 100,
+ body_heights,
+ visible,
+ .transfers,
+ );
+ const collapsed_places = transfers_expanded.cards[@intFromEnum(app_model.SidebarSection.places)];
+ const collapsed_disks = transfers_expanded.cards[@intFromEnum(app_model.SidebarSection.disks)];
+ const expanded_transfers = transfers_expanded.cards[@intFromEnum(app_model.SidebarSection.transfers)];
+
+ try std.testing.expectEqual(@as(i32, 32), collapsed_places.height());
+ try std.testing.expectEqual(@as(i32, 140), collapsed_disks.y);
+ try std.testing.expectEqual(@as(i32, 180), expanded_transfers.y);
+ try std.testing.expectEqual(@as(i32, 152), expanded_transfers.height());
+}
+
test "list column resize pushes following columns and scrolls in Files and chooser" {
for ([_]bool{ false, true }) |chooser| {
var app = try testApp();