RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
Improve transfers sidebar progress UI
Melc50cdb04049c
diff --git a/src/files/app.zig b/src/files/app.zigindex 478c705..8b38695 100644--- a/src/files/app.zig+++ b/src/files/app.zig@@ -124,6 +124,7 @@ pub const App = struct {non_git_list_view: ?bool = null,/// Each sidebar section expands independently. Places is open by default.sidebar_expanded: [4]bool = .{ true, false, false, false },+ transfers_were_active: bool = false,git_expanded: bool = true,/// The "Git View" checkbox: off shows a repository like any other folder.git_view: bool = true,@@ -275,6 +276,9 @@ pub const App = struct {job_cut_revision: ?usize = null,job_progress: usize = std.math.maxInt(usize),job_transferred_bytes: u64 = 0,+ job_rate_sample_bytes: u64 = 0,+ job_rate_sample_at: i64 = 0,+ job_bytes_per_second: u64 = 0,job_state: ops_mod.JobState = .idle,status_notice: ?[]u8 = null,status_notice_until: i64 = 0,@@ -801,6 +805,7 @@ pub const App = struct {pub const fileOpsSidebarHover = app_sidebar_paint.fileOpsSidebarHover;pub const fileOpsSidebarClick = app_sidebar_paint.fileOpsSidebarClick;pub const renderFileOpsSidebar = app_sidebar_paint.renderFileOpsSidebar;+pub const renderFileOpsTip = app_sidebar_paint.renderFileOpsTip;// app_sidebar_paint.zigpub const sidebarExtent = app_sidebar_paint.sidebarExtent;@@ -815,6 +820,7 @@ pub const App = struct {pub const inlineTorrentHit = app_torrent_rows.inlineTorrentHit;pub const inlineTorrentClick = app_torrent_rows.inlineTorrentClick;pub const renderTorrentTip = app_torrent_rows.renderTorrentTip;+pub const renderTorrentSidebarTip = app_torrents.renderTorrentSidebarTip;pub const toggleTorrent = app_torrents.toggleTorrent;pub const openTorrentDetails = app_torrents.openTorrentDetails;pub const openTransfers = app_torrents.openTransfers;diff --git a/src/files/app_draw.zig b/src/files/app_draw.zigindex 7923186..5401b1b 100644--- a/src/files/app_draw.zig+++ b/src/files/app_draw.zig@@ -76,6 +76,36 @@ pub fn drawProgressBar(c.cairo_fill(cr);}+/// Shared compact row for file and torrent transfers.+pub fn drawTransferProgressRow(+ cr: *c.cairo_t,+ area: anytype,+ label: []const u8,+ fraction: ?f64,+ foreground: [4]f32,+ accent: [4]f32,+ size: f64,+) void {+ var percent_buf: [32]u8 = undefined;+ const percent_text = if (fraction) |value|+ std.fmt.bufPrint(&percent_buf, "{d}%", .{@as(u64, @intFromFloat(@min(1.0, @max(0.0, value)) * 100))}) catch ""+ else+ "—";++ setSource(cr, foreground);+ drawEllipsis(cr, label, area.x + 12, area.y + 19, area.w - 112, size, false);+ drawText(cr, percent_text, area.x + area.w - 88, area.y + 19, size, true);+ drawProgressBar(+ cr,+ area.x + 12,+ area.y + area.h - 9,+ area.w - 24,+ fraction,+ withAlpha(foreground, 0.14),+ accent,+ );+}+pub fn drawSpinner(cr: *c.cairo_t, cx: f64, cy: f64, r: f64) void {const angle = @as(f64, @floatFromInt(@mod(nowMs(), 1000))) / 1000.0 * 2.0 * std.math.pi;c.cairo_save(cr);diff --git a/src/files/app_listing.zig b/src/files/app_listing.zigindex 2623e4a..505fcbd 100644--- a/src/files/app_listing.zig+++ b/src/files/app_listing.zig@@ -165,6 +165,28 @@ pub fn sync(self: *App) void {}if (self.job_runner) |runner| {const progress = runner.pollProgress();+ const sample_at = nowMs();+ if (progress.state == .running or progress.state == .paused or progress.state == .waiting_conflict) {+ if (self.job_rate_sample_at == 0) {+ self.job_rate_sample_at = sample_at;+ self.job_rate_sample_bytes = progress.transferred_bytes;+ self.job_bytes_per_second = 0;+ } else if (sample_at - self.job_rate_sample_at >= 500) {+ const elapsed = @max(1, sample_at - self.job_rate_sample_at);+ const transferred = progress.transferred_bytes -| self.job_rate_sample_bytes;+ self.job_bytes_per_second = if (progress.state == .running)+ @intFromFloat(@as(f64, @floatFromInt(transferred)) * 1000.0 / @as(f64, @floatFromInt(elapsed)))+ else+ 0;+ self.job_rate_sample_at = sample_at;+ self.job_rate_sample_bytes = progress.transferred_bytes;+ self.invalidate();+ }+ } else {+ self.job_rate_sample_at = 0;+ self.job_rate_sample_bytes = 0;+ self.job_bytes_per_second = 0;+ }if (progress.state == .waiting_conflict) {if (self.dialog == null or self.dialog.?.kind != .conflict) {self.openConflictDialog(progress.conflict_item_name);@@ -205,6 +227,25 @@ pub fn sync(self: *App) void {self.job_state = progress.state;self.job_transferred_bytes = progress.transferred_bytes;}++ const file_ops_active = self.job_state == .running or+ self.job_state == .paused or+ self.job_state == .waiting_conflict;+ const torrent_active = for (self.torrents.items()) |transfer| {+ if (transfer.@"error".len == 0 and+ !transfer.finished and+ !(transfer.pending and transfer.metadata))+ {+ break true;+ }+ } else false;+ const transfers_active = file_ops_active or torrent_active;+ if (transfers_active and !self.transfers_were_active) {+ self.sidebar_expanded[@intFromEnum(@import("app_model.zig").SidebarSection.transfers)] = true;+ self.invalidate();+ }+ self.transfers_were_active = transfers_active;+if (self.status_notice != null and nowMs() >= self.status_notice_until) {self.allocator.free(self.status_notice.?);self.status_notice = null;diff --git a/src/files/app_paint.zig b/src/files/app_paint.zigindex 7d9fb48..a1a5cb7 100644--- a/src/files/app_paint.zig+++ b/src/files/app_paint.zig@@ -6,7 +6,6 @@ const tr = i18n.tr;const App = @import("app.zig").App;const c = @import("c.zig").api;const recent = @import("recent.zig");-const ops_mod = @import("ops.zig");const deletion = @import("../delete_dialog.zig");const header = @import("header.zig");const shell_ui = @import("ui").cairo;@@ -210,6 +209,10 @@ pub fn render(self: *App, cr: *c.cairo_t) void {if (!self.audio_bar.playlist_open and !self.transfer_view.active and self.menu == null and self.dialog == null and self.selection_band == null) self.renderFilenameTip(cr);if (self.menu != null) self.renderMenu(cr);+ if (self.menu == null and self.dialog == null) {+ self.renderFileOpsTip(cr);+ self.renderTorrentSidebarTip(cr);+ }if (!self.audio_bar.playlist_open and self.menu == null and self.dialog == null) self.renderTorrentTip(cr);@import("app_audio_playlist.zig").paint(self, cr);@@ -454,22 +457,10 @@ pub fn renderStatusBar(self: *App, cr: *c.cairo_t, h_f: f64) void {var status_buf: [256]u8 = undefined;var status_text: []const u8 = "";- var prog: ?ops_mod.ProgressInfo = null;- if (self.job_runner) |runner| {- const p = runner.pollProgress();- if (p.state == .running or p.state == .waiting_conflict) {- prog = p;- }- }-if (self.transfer_view.active) {setSource(cr, if (self.torrents.message().len > 0) ui_theme.global.danger else ui_theme.global.window_fg);const count = self.torrents.items().len;status_text = if (self.torrents.message().len > 0) self.torrents.message() else if (self.torrents.retry_at != null) tr("Connecting…") else i18n.fmt(&status_buf, i18n.trn("{count} transfer · Downloads continue after closing Files.", "{count} transfers · Downloads continue after closing Files.", count), .{ .count = count }) catch tr("Transfers");- } else if (prog) |p| {- setSource(cr, accent);- const pattern = if (self.job_runner.?.kind == .extract or self.job_runner.?.kind == .open_archive_member) tr("Extracting {current}/{total}: {name}... (Esc to cancel)") else tr("Transferring {current}/{total}: {name}... (Esc to cancel)");- status_text = i18n.fmt(&status_buf, pattern, .{ .current = p.processed_items + 1, .total = p.total_items, .name = p.current_name }) catch tr("Transferring...");} else if (self.status_notice != null and nowMs() < self.status_notice_until) {if (self.status_is_error) {setSource(cr, ui_theme.global.danger);diff --git a/src/files/app_sidebar_paint.zig b/src/files/app_sidebar_paint.zigindex d54c1bf..993664c 100644--- a/src/files/app_sidebar_paint.zig+++ b/src/files/app_sidebar_paint.zig@@ -23,6 +23,7 @@ const drawText = @import("app_draw.zig").drawText;const fontMetrics = @import("app_draw.zig").fontMetrics;const sidebar_controls = @import("app_sidebar_controls.zig");const deviceLabel = App.deviceLabel;+const transferTip = @import("app_transfer_tip.zig");/// A quiet surface separating the sidebar's four functional sections.fn drawSidebarCard(cr: *c.cairo_t, x: f64, y: f64, w: f64, h: f64) void {@@ -137,7 +138,7 @@ pub fn sidebarLayout(self: *App) app_model.SidebarLayout {return app_model.SidebarLayout.init(top,.{ places_body, disks_body, git_body, transfers_body },- .{ true, true, self.repository() != null, self.torrentSidebarHeight() > 0 },+ .{ true, true, self.repository() != null, self.torrentSidebarHeight() > 0 or self.fileOpsSidebarHeight() > 0 },self.sidebar_expanded,);}@@ -540,26 +541,14 @@ pub fn renderFileOpsSidebar(self: *App, cr: *c.cairo_t) void {elsenull;- var percent_buf: [32]u8 = undefined;- const percent_text = if (fraction) |value|- std.fmt.bufPrint(&percent_buf, "{d}%", .{@as(u64, @intFromFloat(value * 100))}) catch ""- else- "—";- drawEllipsis(cr, if (progress.current_name.len > 0) progress.current_name else tr("Preparing…"),- area.x + 12, area.y + 19, area.w - 112, shell_ui.statusSize(), false);- drawText(cr, percent_text, area.x + area.w - 88, area.y + 19, shell_ui.statusSize(), true);-- const track_x = area.x + 12;- const track_y = area.y + area.h - 9;- const track_w = area.w - 24;- @import("app_draw.zig").drawProgressBar(+ @import("app_draw.zig").drawTransferProgressRow(cr,- track_x,- track_y,- track_w,+ area,+ if (progress.current_name.len > 0) progress.current_name else tr("Preparing…"),fraction,- withAlpha(ui_theme.global.sidebarForeground(), 0.14),+ ui_theme.global.sidebarForeground(),palette.accent,+ shell_ui.statusSize(),);const can_pause = (progress.kind == .copy or progress.kind == .move) and@@ -586,3 +575,17 @@ pub fn renderSidebarScrollbar(self: *App, cr: *c.cairo_t) void {const bar = self.sidebarScrollbar() orelse return;shell_ui.drawScrollbar(cr, scrollbar.look(bar, self.sidebar_scroll_appearance, self.scrollbar_width, ui_theme.shellPalette()));}+pub fn renderFileOpsTip(self: *App, cr: *c.cairo_t) void {+ if (self.hoverKey() != std.math.maxInt(usize) - 2002) return;++ var speed_buf: [96]u8 = undefined;+ var bytes_buf: [96]u8 = undefined;+ const speed = i18n.fmt(&speed_buf, tr("{rate} KiB/s"), .{+ .rate = @divTrunc(self.job_bytes_per_second, 1024),+ }) catch "";+ const transferred = i18n.fmt(&bytes_buf, tr("{value} transferred"), .{+ .value = @divTrunc(self.job_transferred_bytes, 1024),+ }) catch "";+ const details = [_][]const u8{ speed, transferred };+ transferTip.render(cr, self.w, self.h, self.mouse_x, self.mouse_y, tr("File transfer"), &details);+}diff --git a/src/files/app_torrents.zig b/src/files/app_torrents.zigindex 2bfe49e..49bb5b2 100644--- a/src/files/app_torrents.zig+++ b/src/files/app_torrents.zig@@ -14,6 +14,7 @@ const Transfer = @import("torrents.zig").Transfer;const ops_mod = @import("ops.zig");const Rect = ui.Rect;const draw = @import("app_draw.zig");+const transferTip = @import("app_transfer_tip.zig");const sidebar_controls = @import("app_sidebar_controls.zig");pub const View = struct {@@ -121,7 +122,7 @@ pub fn confirmTorrent(self: *App) void {} else self.setStatusNotice(self.torrents.message(), true);}pub fn torrentSidebarHeight(self: *App) i32 {- return if (enabled and self.chooser == null) 76 else 0;+ return if (enabled and self.chooser == null and self.torrents.items().len > 0) 52 else 0;}fn sidebarBox(self: *App) Rect {const card = self.sidebarLayout().cards[@intFromEnum(@import("app_model.zig").SidebarSection.transfers)];@@ -142,12 +143,12 @@ pub fn torrentSidebarHover(self: *App, x: f64, y: f64) usize {if (self.torrents.items().len > 0) {const area = sidebarBox(self);const t = self.torrents.items()[0];- const remove_rect = sidebar_controls.buttonRect(area, 26, 34);+ const remove_rect = sidebar_controls.buttonRect(area, 26, 2);if (sidebar_controls.contains(remove_rect, x, y)) {return std.math.maxInt(usize) - 2011;}if (!t.pending) {- const pause_rect = sidebar_controls.buttonRect(area, 54, 34);+ const pause_rect = sidebar_controls.buttonRect(area, 54, 2);if (sidebar_controls.contains(pause_rect, x, y)) {return std.math.maxInt(usize) - 2010;}@@ -161,7 +162,7 @@ pub fn torrentSidebarClick(self: *App, x: f64, y: f64) bool {const area = sidebarBox(self);const t = self.torrents.items()[0];const id = t.id;- const remove_rect = sidebar_controls.buttonRect(area, 26, 34);+ const remove_rect = sidebar_controls.buttonRect(area, 26, 2);if (sidebar_controls.contains(remove_rect, x, y)) {if (!self.torrents.send(.{ .op = "remove", .id = id })) {self.setStatusNotice(self.torrents.message(), true);@@ -173,7 +174,7 @@ pub fn torrentSidebarClick(self: *App, x: f64, y: f64) bool {return true;}if (!t.pending) {- const pause_rect = sidebar_controls.buttonRect(area, 54, 34);+ const pause_rect = sidebar_controls.buttonRect(area, 54, 2);if (sidebar_controls.contains(pause_rect, x, y)) {const op: []const u8 = if (t.paused) "resume" else "pause";if (!self.torrents.send(.{ .op = op, .id = id })) {@@ -190,32 +191,31 @@ pub fn torrentSidebarClick(self: *App, x: f64, y: f64) bool {pub fn renderTorrentSidebar(self: *App, cr: *c.cairo_t) void {if (!enabled) return;if (self.chooser != null) return;+ if (self.torrents.items().len == 0) return;const area = sidebarBox(self);- var layer = ui.Layer.begin(cr, .{ .x = area.x, .y = area.y, .w = area.w, .h = area.h }) orelse return;- var palette = theme.shellPalette();- palette.fg = theme.global.sidebarForeground();- palette.button_font_size = ui.textSize();- layer.renderer.palette = palette;- var active: usize = 0;- var rate: i64 = 0;- for (self.torrents.items()) |t| {- if (!t.paused) active += 1;- rate += t.download_rate;- }- var buf: [96]u8 = undefined;- const label = if (self.torrents.retry_at != null) tr("Connecting…") else if (self.torrents.message().len > 0) tr("Transfers needs attention") else i18n.fmt(&buf, tr("{value} active · {rate} KiB/s"), .{ .value = active, .rate = @divTrunc(rate, 1024) }) catch tr("Transfers");- layer.renderer.drawText(12, 20, area.w - 24, 24, .{ .content = label, .font_size = ui.statusSize(), .color = palette.fg });+ var label: []const u8 = tr("Transfers");var fraction: ?f64 = null;var pause_icon: ?sidebar_controls.Icon = null;if (self.torrents.items().len > 0) {const t = self.torrents.items()[0];- layer.renderer.drawText(12, 46, area.w - 88, 24, .{ .content = t.name, .font_size = ui.statusSize(), .color = palette.fg });- fraction = @as(f64, @floatFromInt(std.math.clamp(t.progress, 0, 1000000))) / 1000000.0;+ label = t.name;+ if (!t.pending) {+ fraction = @as(f64, @floatFromInt(std.math.clamp(t.progress, 0, 1000000))) / 1000000.0;+ }if (!t.pending) pause_icon = if (t.paused) .play else .pause;}- layer.finish();+ const palette = theme.shellPalette();+ draw.drawTransferProgressRow(+ cr,+ area,+ label,+ fraction,+ theme.global.sidebarForeground(),+ palette.accent,+ ui.statusSize(),+ );if (pause_icon) |icon| {- const pause_rect = sidebar_controls.buttonRect(area, 54, 34);+ const pause_rect = sidebar_controls.buttonRect(area, 54, 2);sidebar_controls.render(cr,pause_rect,@@ -224,7 +224,7 @@ pub fn renderTorrentSidebar(self: *App, cr: *c.cairo_t) void {);}if (self.torrents.items().len > 0) {- const remove_rect = sidebar_controls.buttonRect(area, 26, 34);+ const remove_rect = sidebar_controls.buttonRect(area, 26, 2);sidebar_controls.render(cr,remove_rect,@@ -232,17 +232,6 @@ pub fn renderTorrentSidebar(self: *App, cr: *c.cairo_t) void {sidebar_controls.contains(remove_rect, self.mouse_x, self.mouse_y),);}- if (fraction) |value| {- draw.drawProgressBar(- cr,- area.x + 12,- area.y + 72,- area.w - 24,- value,- draw.withAlpha(theme.global.sidebarForeground(), 0.14),- palette.accent,- );- }}fn box(self: *App) Rect {const w: f32 = @min(720, @as(f32, @floatFromInt(self.w)) - 24);@@ -932,3 +921,19 @@ pub fn openTorrentDetails(self: *App, id: []const u8) void {return;};}+pub fn renderTorrentSidebarTip(self: *App, cr: *c.cairo_t) void {+ if (self.hoverKey() != std.math.maxInt(usize) - 2001) return;+ if (self.torrents.items().len == 0) return;+ const t = self.torrents.items()[0];++ var download_buf: [96]u8 = undefined;+ var upload_buf: [96]u8 = undefined;+ const download = i18n.fmt(&download_buf, tr("Download: {rate} KiB/s"), .{+ .rate = @divTrunc(t.download_rate, 1024),+ }) catch "";+ const upload = i18n.fmt(&upload_buf, tr("Upload: {rate} KiB/s"), .{+ .rate = @divTrunc(t.upload_rate, 1024),+ }) catch "";+ const details = [_][]const u8{ download, upload };+ transferTip.render(cr, self.w, self.h, self.mouse_x, self.mouse_y, t.name, &details);+}diff --git a/src/files/app_transfer_tip.zig b/src/files/app_transfer_tip.zignew file mode 100644index 0000000..b3adc81--- /dev/null+++ b/src/files/app_transfer_tip.zig@@ -0,0 +1,51 @@+//! Shared tooltip frame and layout for transfer details.+const std = @import("std");+const ui = @import("ui").cairo;+const theme = @import("ui").theme;+const frame = @import("ui").widgets.dialog;+const c = @import("c.zig").api;++pub fn render(+ cr: *c.cairo_t,+ window_w: i32,+ window_h: i32,+ mouse_x: f64,+ mouse_y: f64,+ title: []const u8,+ details: []const []const u8,+) void {+ const width: f32 = @min(320, @as(f32, @floatFromInt(window_w)) - 16);+ const height: f32 = 18 + @as(f32, @floatFromInt(details.len + 1)) * 22;+ if (width <= 0 or @as(f32, @floatFromInt(window_h)) < height + 16) return;++ const max_x = @max(8, @as(f32, @floatFromInt(window_w)) - width - 8);+ const x = std.math.clamp(@as(f32, @floatCast(mouse_x + 14)), 8, max_x);+ const below = @as(f32, @floatCast(mouse_y + 14));+ const y = std.math.clamp(+ if (below + height <= @as(f32, @floatFromInt(window_h)) - 8)+ below+ else+ @as(f32, @floatCast(mouse_y)) - height - 14,+ 8,+ @as(f32, @floatFromInt(window_h)) - height - 8,+ );++ var layer = ui.Layer.begin(cr, .{ .x = x, .y = y, .w = width, .h = height }) orelse return;+ defer layer.finish();+ var palette = theme.shellPalette();+ palette.fg = theme.global.window_fg;+ layer.renderer.palette = palette;+ frame.paintFrame(&layer.renderer, layer.local());+ layer.renderer.drawText(12, 9, width - 24, 22, .{+ .content = title,+ .font_size = ui.textSize(),+ .color = palette.fg,+ });+ for (details, 0..) |detail, i| {+ layer.renderer.drawText(12, 31 + @as(f32, @floatFromInt(i)) * 22, width - 24, 22, .{+ .content = detail,+ .font_size = ui.statusSize(),+ .color = palette.fg,+ });+ }+}