Skip to content

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

Add controls to torrent sidebar

Melc921495ac617
diff --git a/src/files/app.zig b/src/files/app.zig
index 39f2b72..478c705 100644
--- a/src/files/app.zig
+++ b/src/files/app.zig
@@ -828,6 +828,8 @@ pub const App = struct {
pub const confirmTorrent = app_torrents.confirmTorrent;
pub const torrentSidebarHeight = app_torrents.torrentSidebarHeight;
pub const torrentSidebarHit = app_torrents.torrentSidebarHit;
+ pub const torrentSidebarHover = app_torrents.torrentSidebarHover;
+ pub const torrentSidebarClick = app_torrents.torrentSidebarClick;
pub const renderTorrentSidebar = app_torrents.renderTorrentSidebar;
pub const transfersHover = app_torrents.transfersHover;
pub const transfersClick = app_torrents.transfersClick;
diff --git a/src/files/app_pointer.zig b/src/files/app_pointer.zig
index 474c63e..635e714 100644
--- a/src/files/app_pointer.zig
+++ b/src/files/app_pointer.zig
@@ -23,7 +23,10 @@ pub fn hoverKey(self: *App) usize {
const file_ops_hover = self.fileOpsSidebarHover(self.mouse_x, self.mouse_y);
if (file_ops_hover != 0) return file_ops_hover;
}
- if (self.dialog == null and self.torrentSidebarHit(self.mouse_x, self.mouse_y)) return std.math.maxInt(usize) - 2001;
+ if (self.dialog == null) {
+ const torrent_hover = self.torrentSidebarHover(self.mouse_x, self.mouse_y);
+ if (torrent_hover != 0) return torrent_hover;
+ }
const x = self.mouse_x;
const y = self.mouse_y;
if (self.dialog) |dlg| {
@@ -518,10 +521,7 @@ pub fn handleButton(self: *App, button: u32, pressed: bool) void {
}
}
if (self.fileOpsSidebarClick(x, y)) return;
- if (self.torrentSidebarHit(x, y)) {
- self.openTransfers();
- return;
- }
+ if (self.torrentSidebarClick(x, y)) return;
if (self.ejectAt(x, y)) |k| {
self.ejectDevice(k);
return;
diff --git a/src/files/app_sidebar_controls.zig b/src/files/app_sidebar_controls.zig
new file mode 100644
index 0000000..14a3fee
--- /dev/null
+++ b/src/files/app_sidebar_controls.zig
@@ -0,0 +1,49 @@
+//! Shared icon buttons for compact sidebar controls.
+
+const c = @import("c.zig").api;
+const ui = @import("ui").cairo;
+const theme = @import("ui").theme;
+const draw = @import("app_draw.zig");
+const Rect = ui.Rect;
+
+pub const Icon = enum {
+ pause,
+ play,
+ close,
+};
+
+/// `right_inset` positions the button's left edge from the area's right edge.
+pub fn buttonRect(area: Rect, right_inset: f32, top_inset: f32) Rect {
+ return .{
+ .x = area.x + area.w - right_inset,
+ .y = area.y + top_inset,
+ .w = 24,
+ .h = 24,
+ };
+}
+
+pub fn contains(rect: Rect, x: f64, y: f64) bool {
+ return x >= rect.x and x < rect.x + rect.w and
+ y >= rect.y and y < rect.y + rect.h;
+}
+
+pub fn render(cr: *c.cairo_t, rect: Rect, icon: Icon, hovered: bool) void {
+ if (hovered) {
+ draw.roundedRect(cr, rect.x, rect.y, rect.w, rect.h, 6);
+ draw.setSource(cr, theme.global.app_item_hover);
+ c.cairo_fill(cr);
+ }
+
+ if (ui.Layer.begin(cr, rect)) |icon_layer| {
+ var layer = icon_layer;
+ layer.renderer.drawIcon(4, 4, 16, 16, .{
+ .id = switch (icon) {
+ .pause => .pause,
+ .play => .play,
+ .close => .close,
+ },
+ .color = theme.global.app_icon,
+ });
+ layer.finish();
+ }
+}
diff --git a/src/files/app_sidebar_paint.zig b/src/files/app_sidebar_paint.zig
index 75ea065..3d41d51 100644
--- a/src/files/app_sidebar_paint.zig
+++ b/src/files/app_sidebar_paint.zig
@@ -21,6 +21,7 @@ const roundedRect = @import("app_draw.zig").roundedRect;
const drawEllipsis = @import("app_draw.zig").drawEllipsis;
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;
/// A quiet surface separating the sidebar's four functional sections.
@@ -461,16 +462,15 @@ fn fileOpsBox(self: *App) shell_ui.Rect {
}
fn fileOpsPauseRect(area: shell_ui.Rect) shell_ui.Rect {
- return .{ .x = area.x + area.w - 54, .y = area.y + 2, .w = 24, .h = 24 };
+ return sidebar_controls.buttonRect(area, 54, 2);
}
fn fileOpsCancelRect(area: shell_ui.Rect) shell_ui.Rect {
- return .{ .x = area.x + area.w - 26, .y = area.y + 2, .w = 24, .h = 24 };
+ return sidebar_controls.buttonRect(area, 26, 2);
}
fn fileOpsRectContains(rect: shell_ui.Rect, x: f64, y: f64) bool {
- return x >= rect.x and x < rect.x + rect.w and
- y >= rect.y and y < rect.y + rect.h;
+ return sidebar_controls.contains(rect, x, y);
}
pub fn fileOpsSidebarHeight(self: *App) i32 {
@@ -566,34 +566,20 @@ pub fn renderFileOpsSidebar(self: *App, cr: *c.cairo_t) void {
(progress.state == .running or progress.state == .paused);
if (can_pause) {
const rect = fileOpsPauseRect(area);
- if (fileOpsRectContains(rect, self.mouse_x, self.mouse_y)) {
- roundedRect(cr, rect.x, rect.y, rect.w, rect.h, 6);
- setSource(cr, ui_theme.global.app_item_hover);
- c.cairo_fill(cr);
- }
- if (shell_ui.Layer.begin(cr, rect)) |icon_layer| {
- var layer = icon_layer;
- layer.renderer.drawIcon(4, 4, 16, 16, .{
- .id = if (progress.state == .paused) .play else .pause,
- .color = ui_theme.global.app_icon,
- });
- layer.finish();
- }
+ sidebar_controls.render(
+ cr,
+ rect,
+ if (progress.state == .paused) .play else .pause,
+ fileOpsRectContains(rect, self.mouse_x, self.mouse_y),
+ );
}
const cancel_rect = fileOpsCancelRect(area);
- if (fileOpsRectContains(cancel_rect, self.mouse_x, self.mouse_y)) {
- roundedRect(cr, cancel_rect.x, cancel_rect.y, cancel_rect.w, cancel_rect.h, 6);
- setSource(cr, ui_theme.global.app_item_hover);
- c.cairo_fill(cr);
- }
- if (shell_ui.Layer.begin(cr, cancel_rect)) |icon_layer| {
- var layer = icon_layer;
- layer.renderer.drawIcon(4, 4, 16, 16, .{
- .id = .close,
- .color = ui_theme.global.app_icon,
- });
- layer.finish();
- }
+ sidebar_controls.render(
+ cr,
+ cancel_rect,
+ .close,
+ fileOpsRectContains(cancel_rect, self.mouse_x, self.mouse_y),
+ );
}
pub fn renderSidebarScrollbar(self: *App, cr: *c.cairo_t) void {
diff --git a/src/files/app_torrents.zig b/src/files/app_torrents.zig
index 6859c28..2bfe49e 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 sidebar_controls = @import("app_sidebar_controls.zig");
pub const View = struct {
active: bool = false,
@@ -136,6 +137,56 @@ pub fn torrentSidebarHit(self: *App, x: f64, y: f64) bool {
if (self.chooser != null or self.sidebarWidth() == 0 or y < @as(f64, @floatFromInt(self.contentTop())) or y >= @as(f64, @floatFromInt(self.h - self.sidebarFooter()))) return false;
return contains(sidebarBox(self), @floatCast(x), @floatCast(y));
}
+pub fn torrentSidebarHover(self: *App, x: f64, y: f64) usize {
+ if (!self.torrentSidebarHit(x, y)) return 0;
+ 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);
+ 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);
+ if (sidebar_controls.contains(pause_rect, x, y)) {
+ return std.math.maxInt(usize) - 2010;
+ }
+ }
+ }
+ return std.math.maxInt(usize) - 2001;
+}
+pub fn torrentSidebarClick(self: *App, x: f64, y: f64) bool {
+ if (!self.torrentSidebarHit(x, y)) return false;
+ if (self.torrents.items().len > 0) {
+ const area = sidebarBox(self);
+ const t = self.torrents.items()[0];
+ const id = t.id;
+ const remove_rect = sidebar_controls.buttonRect(area, 26, 34);
+ if (sidebar_controls.contains(remove_rect, x, y)) {
+ if (!self.torrents.send(.{ .op = "remove", .id = id })) {
+ self.setStatusNotice(self.torrents.message(), true);
+ } else {
+ self.transfer_view.selected[0] = 0;
+ self.transfer_view.row = null;
+ }
+ self.invalidate();
+ return true;
+ }
+ if (!t.pending) {
+ const pause_rect = sidebar_controls.buttonRect(area, 54, 34);
+ 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 })) {
+ self.setStatusNotice(self.torrents.message(), true);
+ }
+ self.invalidate();
+ return true;
+ }
+ }
+ }
+ self.openTransfers();
+ return true;
+}
pub fn renderTorrentSidebar(self: *App, cr: *c.cairo_t) void {
if (!enabled) return;
if (self.chooser != null) return;
@@ -155,12 +206,32 @@ pub fn renderTorrentSidebar(self: *App, cr: *c.cairo_t) void {
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 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 - 24, 24, .{ .content = t.name, .font_size = ui.statusSize(), .color = palette.fg });
+ 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;
+ if (!t.pending) pause_icon = if (t.paused) .play else .pause;
}
layer.finish();
+ if (pause_icon) |icon| {
+ const pause_rect = sidebar_controls.buttonRect(area, 54, 34);
+ sidebar_controls.render(
+ cr,
+ pause_rect,
+ icon,
+ sidebar_controls.contains(pause_rect, self.mouse_x, self.mouse_y),
+ );
+ }
+ if (self.torrents.items().len > 0) {
+ const remove_rect = sidebar_controls.buttonRect(area, 26, 34);
+ sidebar_controls.render(
+ cr,
+ remove_rect,
+ .close,
+ sidebar_controls.contains(remove_rect, self.mouse_x, self.mouse_y),
+ );
+ }
if (fraction) |value| {
draw.drawProgressBar(
cr,