Skip to content

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

Add transfer destination tree sidebar

Mel6d24054c4551
diff --git a/src/files/app.zig b/src/files/app.zig
index 90a2c5a..c872897 100644
--- a/src/files/app.zig
+++ b/src/files/app.zig
@@ -37,6 +37,7 @@ const app_scroll = @import("app_scroll.zig");
const app_torrents = @import("app_torrents.zig");
const app_torrent_rows = @import("app_torrent_rows.zig");
const app_transfer_rows = @import("app_transfer_rows.zig");
+const app_transfer_tree = @import("app_transfer_tree.zig");
const app_git_sidebar = @import("app_git_sidebar.zig");
const app_opening = @import("app_opening.zig");
const app_audio = @import("app_audio.zig");
@@ -287,6 +288,7 @@ pub const App = struct {
job_bytes_per_second: u64 = 0,
job_state: ops_mod.JobState = .idle,
transfer_rows_dirty: bool = false,
+ transfer_tree_state: app_transfer_tree.State,
status_notice: ?[]u8 = null,
status_notice_until: i64 = 0,
status_is_error: bool = false,
@@ -336,6 +338,7 @@ pub const App = struct {
.home_dir = home_dir,
.items_arena = std.heap.ArenaAllocator.init(allocator),
.job_runner = ops_mod.JobRunner.init(allocator) catch null,
+ .transfer_tree_state = app_transfer_tree.State.init(allocator),
.folder_sizes = std.StringHashMap(SizeState).init(allocator),
.thumbs_enabled = !thumb_decode.disabled(environ),
.single_click_open = @import("settings.zig").singleClickOpen(),
@@ -389,6 +392,7 @@ pub const App = struct {
if (self.preview_return_fd) |fd| _ = c.close(fd);
if (self.editor_path) |path| self.allocator.free(path);
if (self.job_runner) |runner| runner.deinit();
+ self.transfer_tree_state.deinit();
self.clearFolderSizes();
self.folder_sizes.deinit();
if (self.dirsize_pool) |p| p.deinit();
@@ -820,6 +824,9 @@ pub const renderFileOpsTip = app_sidebar_paint.renderFileOpsTip;
// app_sidebar_paint.zig
pub const sidebarExtent = app_sidebar_paint.sidebarExtent;
+ pub const transferTreeBox = app_sidebar_paint.transferTreeBox;
+ pub const transferTreeClick = app_sidebar_paint.transferTreeClick;
+ pub const renderTransferTree = app_sidebar_paint.renderTransferTree;
pub const startTorrents = app_torrents.startTorrents;
pub const stepTorrents = app_torrents.stepTorrents;
diff --git a/src/files/app_listing.zig b/src/files/app_listing.zig
index 2a704da..573ca42 100644
--- a/src/files/app_listing.zig
+++ b/src/files/app_listing.zig
@@ -325,6 +325,7 @@ pub fn sync(self: *App) void {
if (progress.state == .finished) {
if (self.job_was_running) {
self.job_was_running = false;
+ self.transfer_tree_state.markDirty();
self.setStatusNotice(progress.status_message, progress.is_error);
if (self.clipboard_cut and !self.clipboard_is_text and self.job_cut_revision == self.clipboard_revision) {
var i: usize = 0;
@@ -356,6 +357,11 @@ pub fn sync(self: *App) void {
{
self.transfer_rows_dirty = true;
}
+ if (state_changed or
+ ((runner.kind == .copy or runner.kind == .move) and processed_changed))
+ {
+ self.transfer_tree_state.markDirty();
+ }
if (state_changed) {
self.invalidate();
} else if (processed_changed or bytes_changed or file_path_changed or
diff --git a/src/files/app_pointer.zig b/src/files/app_pointer.zig
index 216b3c3..7e7d0a7 100644
--- a/src/files/app_pointer.zig
+++ b/src/files/app_pointer.zig
@@ -531,6 +531,7 @@ pub fn handleButton(self: *App, button: u32, pressed: bool) void {
}
}
if (self.fileOpsSidebarClick(x, y)) return;
+ if (self.transferTreeClick(x, y)) return;
if (self.torrentSidebarClick(x, y)) return;
if (self.ejectAt(x, y)) |k| {
self.ejectDevice(k);
diff --git a/src/files/app_sidebar_paint.zig b/src/files/app_sidebar_paint.zig
index e01b3cc..714f997 100644
--- a/src/files/app_sidebar_paint.zig
+++ b/src/files/app_sidebar_paint.zig
@@ -134,13 +134,23 @@ pub fn sidebarLayout(self: *App) app_model.SidebarLayout {
0;
const torrent_height = self.torrentSidebarHeight();
const file_ops_height = self.fileOpsSidebarHeight();
+ if (self.transfer_tree_state.dirty) {
+ if (self.job_runner) |runner| {
+ self.transfer_tree_state.rebuild(runner) catch {};
+ }
+ }
+ const transfer_tree_height: i32 = if (self.transfer_tree_state.tree) |*tree|
+ @intCast(self.transfer_tree_state.visibleCount(tree) * 25 + 8)
+ else
+ 0;
const transfers_body = torrent_height +
- (if (file_ops_height > 0) @as(i32, app_model.SidebarLayout.gap) + file_ops_height else 0);
+ (if (file_ops_height > 0) @as(i32, app_model.SidebarLayout.gap) + file_ops_height else 0) +
+ (if (transfer_tree_height > 0) @as(i32, app_model.SidebarLayout.gap) + transfer_tree_height else 0);
return app_model.SidebarLayout.init(
top,
.{ places_body, disks_body, git_body, transfers_body },
- .{ true, true, self.repository() != null, torrent_height > 0 or file_ops_height > 0 },
+ .{ true, true, self.repository() != null, torrent_height > 0 or file_ops_height > 0 or transfer_tree_height > 0 },
self.sidebar_expanded,
);
}
@@ -453,6 +463,7 @@ pub fn renderSidebar(self: *App, cr: *c.cairo_t) void {
if (transfers_card.expanded) {
self.renderTorrentSidebar(cr);
self.renderFileOpsSidebar(cr);
+ self.renderTransferTree(cr);
}
}
@@ -641,3 +652,112 @@ pub fn renderFileOpsTip(self: *App, cr: *c.cairo_t) void {
};
transferTip.render(cr, self.w, self.h, self.mouse_x, self.mouse_y, title, &details);
}
+
+pub fn transferTreeBox(self: *App) shell_ui.Rect {
+ const card = self.sidebarLayout().cards[@intFromEnum(app_model.SidebarSection.transfers)];
+ const file_ops_height = self.fileOpsSidebarHeight();
+ const top = card.y + card.header_height + self.torrentSidebarHeight() +
+ (if (file_ops_height > 0) app_model.SidebarLayout.gap + file_ops_height else 0) +
+ app_model.SidebarLayout.gap;
+ const height: i32 = if (self.transfer_tree_state.tree) |*tree|
+ (if (tree.nodes.items.len > 0) @intCast(self.transfer_tree_state.visibleCount(tree) * 25 + 8) else 0)
+ else
+ 0;
+ return .{
+ .x = 12,
+ .y = @floatFromInt(top),
+ .w = @floatCast(@max(0, self.sidebarContentWidth() - 24)),
+ .h = @floatFromInt(height),
+ };
+}
+
+pub fn transferTreeClick(self: *App, x: f64, y: f64) bool {
+ const area = transferTreeBox(self);
+ if (area.h <= 0 or x < area.x or x >= area.x + area.w or y < area.y or y >= area.y + area.h) return false;
+
+ const tree = if (self.transfer_tree_state.tree) |*value| value else return true;
+ const row = @as(usize, @intFromFloat((y - area.y - 4) / 25));
+ var visible_index: usize = 0;
+ for (tree.nodes.items, 0..) |node, index| {
+ if (!self.transfer_tree_state.nodeVisible(tree, index)) continue;
+ if (visible_index == row) {
+ if (node.kind == .directory) {
+ self.transfer_tree_state.toggleExpanded(node.path) catch {};
+ self.clampSidebarScroll();
+ self.invalidate();
+ }
+ return true;
+ }
+ visible_index += 1;
+ }
+ return true;
+}
+
+pub fn renderTransferTree(self: *App, cr: *c.cairo_t) void {
+ const tree = if (self.transfer_tree_state.tree) |*value| value else return;
+ if (tree.nodes.items.len == 0) return;
+
+ const area = transferTreeBox(self);
+ if (area.w < 100 or area.h <= 0) return;
+ const progress = if (self.job_runner) |runner| runner.pollProgress() else null;
+ const active_source = if (progress) |value|
+ (if (value.current_file_path_len > 0) value.current_file_path[0..value.current_file_path_len] else "")
+ else
+ "";
+ const active_bytes = if (progress) |value| value.current_file_bytes else 0;
+ const active_size = if (progress) |value| value.current_file_size else 0;
+ const palette = ui_theme.shellPalette();
+
+ c.cairo_save(cr);
+ defer c.cairo_restore(cr);
+ c.cairo_rectangle(cr, area.x, area.y, area.w, area.h);
+ c.cairo_clip(cr);
+
+ var row: usize = 0;
+ for (tree.nodes.items, 0..) |node, index| {
+ if (!self.transfer_tree_state.nodeVisible(tree, index)) continue;
+ const y = area.y + 4 + @as(f64, @floatFromInt(row * 25));
+ row += 1;
+
+ var depth: usize = 0;
+ var parent = node.parent_index;
+ while (parent) |parent_index| {
+ depth += 1;
+ parent = tree.nodes.items[parent_index].parent_index;
+ }
+ const indent = @as(f64, @floatFromInt(@min(depth, 8))) * 12;
+ const aggregate = self.transfer_tree_state.aggregateProgress(
+ tree,
+ index,
+ active_source,
+ active_bytes,
+ active_size,
+ );
+ const active = aggregate.active;
+ const glyph: []const u8 = if (node.kind == .directory)
+ (if (self.transfer_tree_state.isExpanded(node.path)) "▾" else "▸")
+ else switch (node.state) {
+ .completed => "✓",
+ .failed => "!",
+ .copying => "◌",
+ else => "·",
+ };
+
+ setSource(cr, if (active) palette.accent else withAlpha(ui_theme.global.sidebarForeground(), 0.82));
+ drawText(cr, glyph, area.x + 5 + indent, y + 17, shell_ui.statusSize(), false);
+ setSource(cr, withAlpha(ui_theme.global.sidebarForeground(), if (node.state == .pending) 0.62 else 0.9));
+ drawEllipsis(cr, node.name, area.x + 22 + indent, y + 17, area.w - 30 - indent, shell_ui.statusSize(), false);
+
+ if (aggregate.total_bytes > 0) {
+ const bar_width = @max(0, area.w - 32 - indent);
+ roundedRect(cr, area.x + 22 + indent, y + 21, bar_width, 2, 1);
+ setSource(cr, withAlpha(palette.accent, 0.22));
+ c.cairo_fill(cr);
+ if (aggregate.completed_bytes > 0) {
+ roundedRect(cr, area.x + 22 + indent, y + 21, bar_width * aggregate.fraction(), 2, 1);
+ setSource(cr, if (active) palette.accent else withAlpha(palette.accent, 0.8));
+ c.cairo_fill(cr);
+ }
+ }
+ }
+}
diff --git a/src/files/app_transfer_tree.zig b/src/files/app_transfer_tree.zig
new file mode 100644
index 0000000..25a6b0c
--- /dev/null
+++ b/src/files/app_transfer_tree.zig
@@ -0,0 +1,215 @@
+//! Cached UI state for the transfer destination tree.
+
+const std = @import("std");
+const Allocator = std.mem.Allocator;
+const ops = @import("ops.zig");
+const projection = @import("transfer_tree.zig");
+
+pub const State = struct {
+ allocator: Allocator,
+ tree: ?projection.Tree = null,
+ expanded_paths: std.StringHashMap(void),
+ dirty: bool = true,
+
+ pub fn init(allocator: Allocator) State {
+ return .{
+ .allocator = allocator,
+ .expanded_paths = std.StringHashMap(void).init(allocator),
+ };
+ }
+
+ pub fn deinit(self: *State) void {
+ if (self.tree) |*tree| tree.deinit();
+
+ var paths = self.expanded_paths.keyIterator();
+ while (paths.next()) |path| self.allocator.free(path.*);
+ self.expanded_paths.deinit();
+ }
+
+ pub fn markDirty(self: *State) void {
+ self.dirty = true;
+ }
+
+ /// Replaces the cached projection from one consistent manifest snapshot.
+ pub fn rebuild(self: *State, runner: *ops.JobRunner) !void {
+ var snapshot = try runner.snapshotTransferManifest(self.allocator);
+ defer snapshot.deinit();
+
+ var next = try projection.build(self.allocator, snapshot.entries.items);
+ errdefer next.deinit();
+
+ if (self.tree) |*old| old.deinit();
+ self.tree = next;
+ self.dirty = false;
+ }
+
+ pub fn isExpanded(self: *const State, path: []const u8) bool {
+ return self.expanded_paths.contains(path);
+ }
+
+ pub const AggregateProgress = struct {
+ total_bytes: u64 = 0,
+ completed_bytes: u64 = 0,
+ active: bool = false,
+ failed: bool = false,
+
+ pub fn fraction(self: AggregateProgress) f64 {
+ if (self.total_bytes == 0) return 0;
+ return @min(1.0, @as(f64, @floatFromInt(self.completed_bytes)) /
+ @as(f64, @floatFromInt(self.total_bytes)));
+ }
+ };
+
+ fn hiddenState(state: ops.TransferEntryState) bool {
+ return state == .skipped or state == .cancelled;
+ }
+
+ /// A node is visible only when every directory ancestor is expanded.
+ pub fn nodeVisible(self: *const State, tree: *const projection.Tree, index: usize) bool {
+ if (hiddenState(tree.nodes.items[index].state)) return false;
+ var parent = tree.nodes.items[index].parent_index;
+ while (parent) |parent_index| {
+ const ancestor = tree.nodes.items[parent_index];
+ if (hiddenState(ancestor.state) or ancestor.kind != .directory or
+ !self.isExpanded(ancestor.path)) return false;
+ parent = ancestor.parent_index;
+ }
+ return true;
+ }
+
+ pub fn visibleCount(self: *const State, tree: *const projection.Tree) usize {
+ var count: usize = 0;
+ for (tree.nodes.items, 0..) |_, index| {
+ if (self.nodeVisible(tree, index)) count += 1;
+ }
+ return count;
+ }
+
+ fn isWithinNode(tree: *const projection.Tree, candidate: usize, ancestor: usize) bool {
+ var parent = tree.nodes.items[candidate].parent_index;
+ while (parent) |parent_index| {
+ if (parent_index == ancestor) return true;
+ parent = tree.nodes.items[parent_index].parent_index;
+ }
+ return candidate == ancestor;
+ }
+
+ /// Computes file-byte progress for a node and all descendant files.
+ /// The active path is the manifest source path.
+ pub fn aggregateProgress(
+ _: *const State,
+ tree: *const projection.Tree,
+ index: usize,
+ active_source_path: []const u8,
+ active_bytes: u64,
+ active_size: u64,
+ ) AggregateProgress {
+ var result: AggregateProgress = .{};
+ for (tree.nodes.items, 0..) |node, candidate| {
+ if (!isWithinNode(tree, candidate, index) or
+ node.kind == .directory or hiddenState(node.state)) continue;
+
+ result.total_bytes +|= node.size;
+ if (node.state == .failed) result.failed = true;
+ if (node.state == .completed) {
+ result.completed_bytes +|= node.size;
+ } else if (active_source_path.len > 0 and
+ std.mem.eql(u8, node.source_path, active_source_path))
+ {
+ result.active = true;
+ result.completed_bytes +|= @min(
+ active_bytes,
+ if (node.size > 0) node.size else active_size,
+ );
+ }
+ }
+ result.completed_bytes = @min(result.completed_bytes, result.total_bytes);
+ return result;
+ }
+
+ pub fn toggleExpanded(self: *State, path: []const u8) !void {
+ if (self.expanded_paths.fetchRemove(path)) |removed| {
+ self.allocator.free(removed.key);
+ } else {
+ const owned_path = try self.allocator.dupe(u8, path);
+ errdefer self.allocator.free(owned_path);
+ try self.expanded_paths.put(owned_path, {});
+ }
+ }
+};
+
+test "transfer tree expansion is keyed by destination path" {
+ var state = State.init(std.testing.allocator);
+ defer state.deinit();
+
+ try std.testing.expect(!state.isExpanded("/destination/renamed"));
+
+ try state.toggleExpanded("/destination/renamed");
+ try std.testing.expect(state.isExpanded("/destination/renamed"));
+ try std.testing.expect(!state.isExpanded("/destination/other"));
+
+ try state.toggleExpanded("/destination/renamed");
+ try std.testing.expect(!state.isExpanded("/destination/renamed"));
+}
+
+test "transfer tree state owns expansion paths" {
+ var state = State.init(std.testing.allocator);
+ defer state.deinit();
+
+ const path = try std.testing.allocator.dupe(u8, "/destination/folder");
+ try state.expanded_paths.put(path, {});
+
+ try std.testing.expect(state.isExpanded("/destination/folder"));
+}
+
+test "transfer tree aggregates descendant file progress" {
+ const allocator = std.testing.allocator;
+ var manifest = ops.TransferManifest.init(allocator);
+ defer manifest.deinit();
+
+ try manifest.append("/source", "", "/destination/source", 0, .directory, 0);
+ try manifest.append("/source/active.bin", "active.bin", "/destination/source/active.bin", 10, .file, 0);
+ try manifest.append("/source/done.bin", "done.bin", "/destination/source/done.bin", 20, .file, 0);
+ try manifest.append("/source/skipped.bin", "skipped.bin", "/destination/source/skipped.bin", 30, .file, 0);
+
+ manifest.entries.items[1].state = .copying;
+ manifest.entries.items[2].state = .completed;
+ manifest.entries.items[3].state = .skipped;
+
+ var tree = try projection.build(allocator, manifest.entries.items);
+ defer tree.deinit();
+
+ const aggregate = State.aggregateProgress(&tree, 0, "/source/active.bin", 4, 10);
+ try std.testing.expectEqual(@as(u64, 30), aggregate.total_bytes);
+ try std.testing.expectEqual(@as(u64, 24), aggregate.completed_bytes);
+ try std.testing.expect(aggregate.active);
+ try std.testing.expect(!aggregate.failed);
+ try std.testing.expectApproxEqAbs(@as(f64, 0.8), aggregate.fraction(), 0.0001);
+}
+
+test "transfer tree hides skipped and cancelled entries" {
+ const allocator = std.testing.allocator;
+ var manifest = ops.TransferManifest.init(allocator);
+ defer manifest.deinit();
+
+ try manifest.append("/source", "", "/destination/source", 0, .directory, 0);
+ try manifest.append("/source/visible", "visible", "/destination/source/visible", 10, .file, 0);
+ try manifest.append("/source/skipped", "skipped", "/destination/source/skipped", 10, .file, 0);
+ try manifest.append("/source/cancelled", "cancelled", "/destination/source/cancelled", 10, .file, 0);
+
+ manifest.entries.items[2].state = .skipped;
+ manifest.entries.items[3].state = .cancelled;
+
+ var tree = try projection.build(allocator, manifest.entries.items);
+ defer tree.deinit();
+
+ var state = State.init(allocator);
+ defer state.deinit();
+ try state.toggleExpanded("/destination/source");
+
+ try std.testing.expect(state.nodeVisible(&tree, 0));
+ try std.testing.expect(state.nodeVisible(&tree, 1));
+ try std.testing.expect(!state.nodeVisible(&tree, 2));
+ try std.testing.expect(!state.nodeVisible(&tree, 3));
+ try std.testing.expectEqual(@as(usize, 2), state.visibleCount(&tree));
+}
diff --git a/src/files/transfer_tree.zig b/src/files/transfer_tree.zig
index 96b4cfa..ffb10ca 100644
--- a/src/files/transfer_tree.zig
+++ b/src/files/transfer_tree.zig
@@ -6,6 +6,7 @@ const Allocator = std.mem.Allocator;
pub const Node = struct {
path: []u8,
+ source_path: []u8,
name: []u8,
parent_index: ?usize,
entry_index: usize,
@@ -22,6 +23,7 @@ pub const Tree = struct {
pub fn deinit(self: *Tree) void {
for (self.nodes.items) |node| {
self.allocator.free(node.path);
+ self.allocator.free(node.source_path);
self.allocator.free(node.name);
}
self.nodes.deinit(self.allocator);
@@ -77,12 +79,15 @@ pub fn build(allocator: Allocator, entries: []const ops.TransferEntry) !Tree {
const name = std.fs.path.basename(destination);
const owned_path = try allocator.dupe(u8, destination);
errdefer allocator.free(owned_path);
+ const owned_source_path = try allocator.dupe(u8, entry.source_path);
+ errdefer allocator.free(owned_source_path);
const owned_name = try allocator.dupe(u8, if (name.len == 0) destination else name);
errdefer allocator.free(owned_name);
const node_index = tree.nodes.items.len;
try tree.nodes.append(allocator, .{
.path = owned_path,
+ .source_path = owned_source_path,
.name = owned_name,
.parent_index = null,
.entry_index = entry_index,
@@ -144,6 +149,7 @@ test "transfer tree projects renamed roots, nested entries and independent roots
const file = tree.nodes.items[1];
try std.testing.expectEqualStrings("file.txt", file.name);
+ try std.testing.expectEqualStrings("/source-a/nested/file.txt", file.source_path);
try std.testing.expectEqual(ops.TransferEntryKind.file, file.kind);
try std.testing.expectEqual(@as(u64, 42), file.size);
try std.testing.expectEqual(ops.TransferEntryState.copying, file.state);