RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
audio player UI update
oxydizer47f4e056d27a
diff --git a/src/files/AGENTS.md b/src/files/AGENTS.mdindex 50331f9..5b1a370 100644--- a/src/files/AGENTS.md+++ b/src/files/AGENTS.md@@ -264,10 +264,16 @@ See `docs/files-torrents.md` for dependencies, storage and lifecycle.`app_audio_bar.zig` lays out the now-playing bar between toolbar groups, orin an extra row on narrow windows (`contentTop` includes it). It appears forplaying and paused tracks, independently of the current folder; stopped/ended-playback and file choosers have no bar. Controls use the shared buttons and Settings-style slider-widgets and MPRIS through `player_link`; playing progress and slider feedback schedule-repaint deadlines. Hover keys 920–923 damage the bar. `tests/files_audio.py`-covers toolbar seeking, folder navigation, narrow layout, pause/resume and Stop.+playback and file choosers have no bar. Controls are shared circular buttons+(Previous, Play/Pause, Next, Stop, Playlist), the Settings slider through+`ui.paint.paintSlider` (never the shaded variant directly) and MPRIS through+`player_link`. Under 520 px the title and time stack above the slider in a+40 px bar; wider bars put them on one row with the time in a value pill.+Previous/Next step through the playlist snapshot with OpenUri (Previous+restarts a track past 3 s) and are disabled at its ends. Playing progress and+slider feedback schedule repaint deadlines. Hover keys 920–925 damage the bar.+`tests/files_audio.py` covers toolbar seeking, Previous/Next, folder+navigation, wide and narrow layouts, pause/resume and Stop.`app_audio_playlist.zig` owns the playlist popover (click, keyboard and scrolling).The link thread snapshots supported audio files beside the playing track, sorteddiff --git a/src/files/app_audio_bar.zig b/src/files/app_audio_bar.zigindex f107fe5..f30d1f6 100644--- a/src/files/app_audio_bar.zig+++ b/src/files/app_audio_bar.zig@@ -31,26 +31,101 @@ pub fn extraHeight(self: *App) i32 {return if (inlineWidth(self) >= 340) 0 else 44;}+/// Below this width the title and time stack above the slider instead of+/// sharing its row.+const single_row_min = 520;+fn inlineWidth(self: *App) f64 {const right = header.rect(self.w, self.inRepository(), if (self.inRepository()) .git_view else .grid);return right.x - 292;}+/// A stacked bar is 4 px taller, about the toolbar's midline, so the Settings+/// thumb clears the title.pub fn rect(self: *App) ?header.Rect {if (!visible(self)) return null;- if (extraHeight(self) == 0) return .{ .x = 284, .y = 64, .w = inlineWidth(self), .h = 36 };- return .{ .x = 16, .y = @floatFromInt(header.height(self.w, self.inRepository())), .w = @floatFromInt(self.w - 32), .h = 36 };+ if (extraHeight(self) == 0) {+ const w = inlineWidth(self);+ return if (w < single_row_min) .{ .x = 284, .y = 62, .w = w, .h = 40 } else .{ .x = 284, .y = 64, .w = w, .h = 36 };+ }+ const w: f64 = @floatFromInt(self.w - 32);+ return .{ .x = 16, .y = @floatFromInt(header.height(self.w, self.inRepository())), .w = w, .h = if (w < single_row_min) 40 else 36 };+}++/// Controls, in hit-test order; the value is the hover-key offset from 920.+const Id = enum(usize) { play = 0, stop = 1, slider = 2, playlist = 3, previous = 4, next = 5 };+const count = std.enums.values(Id).len;++/// Left to right: Previous, Play, Next, the title, the slider, the time+/// readout, Stop and Playlist. A narrow bar stacks the title and time+/// above the slider.+const Layout = struct {+ stacked: bool,+ title_x: f64,+ title_w: f64,+ slider_x: f64,+ slider_w: f64,+ time_x: f64,+ time_w: f64,+};++const time_size = 13;+const time_sample = "00:00 / 00:00";++fn arrange(r: header.Rect) Layout {+ const title_x = r.x + 112;+ const end = r.x + r.w - 82;+ if (r.w < single_row_min) {+ const time_w: f64 = ui.paint.labelWidth(time_sample, @floatCast(ui.cairo.statusSize()));+ const content_w = @max(0, end - title_x);+ return .{ .stacked = true, .title_x = title_x, .title_w = @max(0, content_w - time_w - 8), .slider_x = title_x + 6, .slider_w = @max(0, content_w - 12), .time_x = end - time_w, .time_w = time_w };+ }+ const pill_w: f64 = @as(f64, ui.paint.labelWidth(time_sample, time_size)) + 22;+ const pill_x = end - pill_w;+ const room = @max(0, pill_x - 14 - title_x);+ const title_w = std.math.clamp(room * 0.36, @min(room, 72), 240);+ const slider_x = title_x + title_w + 14;+ return .{ .stacked = false, .title_x = title_x, .title_w = title_w, .slider_x = slider_x, .slider_w = @max(0, pill_x - 14 - slider_x), .time_x = pill_x, .time_w = pill_w };+}++fn box(r: header.Rect, id: Id) header.Rect {+ const l = arrange(r);+ const small = r.y + (r.h - 28) / 2;+ const large = r.y + (r.h - 32) / 2;+ return switch (id) {+ .previous => .{ .x = r.x + 6, .y = small, .w = 28, .h = 28 },+ .play => .{ .x = r.x + 38, .y = large, .w = 32, .h = 32 },+ .next => .{ .x = r.x + 74, .y = small, .w = 28, .h = 28 },+ .slider => if (l.stacked) .{ .x = l.slider_x, .y = r.y + r.h - 21, .w = l.slider_w, .h = 20 } else .{ .x = l.slider_x, .y = large, .w = l.slider_w, .h = 32 },+ .stop => .{ .x = r.x + r.w - 72, .y = large, .w = 32, .h = 32 },+ .playlist => .{ .x = r.x + r.w - 38, .y = large, .w = 32, .h = 32 },+ };}-fn control(self: *App, id: usize) ?ui.layout.Widget {- const r = rect(self) orelse return null;- const x = if (id == 0) r.x + 6 else if (id == 1) r.x + 42 else if (id == 3) r.x + r.w - 38 else r.x + 84;+fn control(self: *App, id: Id) ?ui.layout.Widget {+ const b = box(rect(self) orelse return null, id);return .{.kind = .{ .slider = .{ .owner = self, .value = fraction(self), .min = 0, .max = 1, .style = .settings, .feel = self.audio_bar.appearance, .on_change = previewChanged } },- .computed_x = @floatCast(x),- .computed_y = @floatCast(r.y + if (id == 2) @as(f64, 20) else 2),- .computed_width = @floatCast(if (id == 2) r.w - 242 else 32),- .computed_height = if (id == 2) 14 else 32,+ .computed_x = @floatCast(b.x),+ .computed_y = @floatCast(b.y),+ .computed_width = @floatCast(b.w),+ .computed_height = @floatCast(b.h),+ };+}++/// Previous restarts a track that has played a while, as players do.+const restart_after_s = 3;++fn elapsedSeconds(self: *App) f64 {+ return @as(f64, fraction(self)) * @as(f64, @floatFromInt(self.audio_state.length_us)) / 1_000_000;+}++fn enabled(self: *App, id: Id) bool {+ return switch (id) {+ .slider => self.audio_state.length_us > 0,+ .next => playlist.neighbour(self, 1) != null,+ .previous => playlist.neighbour(self, -1) != null or elapsedSeconds(self) >= 0.5,+ else => true,};}@@ -71,7 +146,7 @@ fn seek(self: *App, value: f32) void {pub fn motion(self: *App, x: f64, y: f64) bool {if (!self.audio_bar.drag.active) return false;- var widget = control(self, 2) orelse {+ var widget = control(self, .slider) orelse {self.audio_bar.drag.end();return false;};@@ -92,9 +167,9 @@ pub fn release(self: *App) bool {}pub fn step(self: *App, now: i64) void {- const enabled = visible(self) and self.audio_state.length_us > 0;- const hovered = hit(self, self.mouse_x, self.mouse_y) == 2;- if (self.audio_bar.appearance.step(now, false, hovered, self.audio_bar.drag.active, enabled)) {+ const seekable = visible(self) and self.audio_state.length_us > 0;+ const hovered = hit(self, self.mouse_x, self.mouse_y) == @intFromEnum(Id.slider);+ if (self.audio_bar.appearance.step(now, false, hovered, self.audio_bar.drag.active, seekable)) {if (rect(self)) |r| self.addDamage(r);}}@@ -106,22 +181,30 @@ pub fn deadline(self: *App) ?i64 {pub fn hit(self: *App, x: f64, y: f64) ?usize {if (self.dialog != null or self.menu != null or self.drag_origin != null or self.selection_band != null) return null;- for (0..4) |id| {- if (id == 2 and self.audio_state.length_us <= 0) continue;+ for (0..count) |index| {+ const id: Id = @enumFromInt(index);+ if (!enabled(self, id)) continue;const widget = control(self, id) orelse return null;- if (widget.contains(@floatCast(x), @floatCast(y))) return id;+ if (widget.contains(@floatCast(x), @floatCast(y))) return index;}return null;}pub fn click(self: *App, x: f64, y: f64) bool {- const id = hit(self, x, y) orelse return false;+ const index = hit(self, x, y) orelse return false;const link = self.audio_link orelse return false;- switch (id) {- 0 => link.playPause(),- 1 => link.stopPlayback(),- 3 => playlist.toggle(self),- else => {+ switch (@as(Id, @enumFromInt(index))) {+ .play => link.playPause(),+ .stop => link.stopPlayback(),+ .playlist => playlist.toggle(self),+ .next => playlist.skip(self, 1),+ .previous => {+ const elapsed = elapsedSeconds(self);+ if (elapsed >= restart_after_s or playlist.neighbour(self, -1) == null) {+ link.seek(-@as(i64, @intFromFloat(elapsed * 1_000_000)));+ } else playlist.skip(self, -1);+ },+ .slider => {self.audio_bar.preview = fraction(self);self.audio_bar.drag.active = true;_ = motion(self, x, y);@@ -139,31 +222,75 @@ pub fn paint(self: *App, cr: *c.cairo_t) void {c.cairo_clip(cr);const t = ui.theme.global;draw.setSource(cr, t.app_divider);- draw.roundedRect(cr, r.x, r.y, r.w, r.h, 8);+ draw.roundedRect(cr, r.x + 0.5, r.y + 0.5, r.w - 1, r.h - 1, (r.h - 1) / 2);c.cairo_set_line_width(cr, 1);c.cairo_stroke(cr);+ const l = arrange(r);+ const size = if (l.stacked) ui.cairo.statusSize() else ui.cairo.textSize();+ const metrics = draw.fontMetrics(cr, size);+ const baseline = if (l.stacked) r.y + 4 + metrics.ascent else r.y + (r.h + metrics.ascent - metrics.descent) / 2;draw.setSource(cr, t.window_fg);- draw.drawEllipsis(cr, std.fs.path.basename(self.audio_state.file()), r.x + 84, r.y + 15, r.w - 134, ui.cairo.textSize(), true);+ draw.drawEllipsis(cr, std.fs.path.basename(self.audio_state.file()), l.title_x, baseline, l.title_w, size, true);var time_buf: [64]u8 = undefined;const duration: u64 = @intCast(@max(0, @divTrunc(self.audio_state.length_us, 1_000_000)));const elapsed: u64 = @intFromFloat(@as(f64, fraction(self)) * @as(f64, @floatFromInt(duration)));const time = std.fmt.bufPrint(&time_buf, "{d}:{d:0>2} / {d}:{d:0>2}", .{ @divTrunc(elapsed, 60), @mod(elapsed, 60), @divTrunc(duration, 60), @mod(duration, 60) }) catch "";- draw.drawEllipsis(cr, time, r.x + r.w - 150, r.y + 29, 106, ui.cairo.statusSize(), false);- for (0..4) |id| {+ if (l.stacked) {+ const width = draw.measureText(cr, time, size, false);+ draw.drawEllipsis(cr, time, l.time_x + @max(0, l.time_w - width), baseline, l.time_w, size, false);+ }++ const hovered = hit(self, self.mouse_x, self.mouse_y);+ const playing = self.audio_state.status == .playing;+ for (0..count) |index| {+ const id: Id = @enumFromInt(index);var widget = control(self, id).?;- var layer = ui.cairo.Layer.begin(cr, .{ .x = widget.computed_x, .y = widget.computed_y, .w = widget.computed_width, .h = widget.computed_height }) orelse continue;- defer layer.finish();- if (id == 2) {- widget.computed_x = 0;+ if (id == .slider) {+ if (!enabled(self, id) or widget.computed_width < 24) continue;+ // The thumb overhangs the track's ends by half its width.+ const overhang: f32 = 8;+ var layer = ui.cairo.Layer.begin(cr, .{ .x = widget.computed_x - overhang, .y = widget.computed_y, .w = widget.computed_width + 2 * overhang, .h = widget.computed_height }) orelse continue;+ defer layer.finish();+ widget.computed_x = overhang;widget.computed_y = 0;- ui.widgets.slider.paintShaded(&widget, widget.kind.slider, &layer.renderer);+ ui.paint.paintSlider(&widget, widget.kind.slider, &layer.renderer);continue;}- const playing = self.audio_state.status == .playing;+ const b = box(r, id);+ var layer = ui.cairo.Layer.begin(cr, .{ .x = @floatCast(b.x), .y = @floatCast(b.y), .w = @floatCast(b.w), .h = @floatCast(b.h) }) orelse continue;+ defer layer.finish();ui.widgets.button.paint(&layer.renderer, layer.local(), .{- .variant = if (id == 0) .primary else .ghost,- .icon = if (id == 3) .view_list else if (id == 1) .close else if (playing) .pause else .play,- .label = if (id == 3) tr("Playlist") else if (id == 1) tr("Stop") else if (playing) tr("Pause") else tr("Play"),- }, .{ .selected = id == 3 and self.audio_bar.playlist_open, .pointer = if (hit(self, self.mouse_x, self.mouse_y) == id) .hover else .idle });+ .variant = if (id == .play) .primary else .ghost,+ .radius = @floatCast(b.h / 2),+ .icon = switch (id) {+ .previous => .skip_previous,+ .next => .skip_next,+ .stop => .stop,+ .playlist => .view_list,+ else => if (playing) .pause else .play,+ },+ .icon_scale = if (id == .play) 0.42 else 0.5,+ .label = switch (id) {+ .previous => tr("Previous"),+ .next => tr("Next"),+ .stop => tr("Stop"),+ .playlist => tr("Playlist"),+ else => if (playing) tr("Pause") else tr("Play"),+ },+ }, .{+ .selected = id == .playlist and self.audio_bar.playlist_open,+ .pointer = if (!enabled(self, id)) .disabled else if (hovered == index) .hover else .idle,+ });}++ // On one row the time sits in the Settings value pill.+ if (l.stacked) return;+ var pill = ui.widgets.slider.valuePill(time, time_size, t.window_fg);+ pill.kind.text.glow = if (self.audio_bar.drag.active) self.audio_bar.appearance.accent else 0;+ const pill_h: f64 = 26;+ var layer = ui.cairo.Layer.begin(cr, .{ .x = @floatCast(l.time_x), .y = @floatCast(r.y + (r.h - pill_h) / 2), .w = @floatCast(l.time_w), .h = pill_h }) orelse return;+ defer layer.finish();+ pill.computed_width = @floatCast(l.time_w);+ pill.computed_height = @floatCast(pill_h);+ ui.paint.paintPill(&pill, pill.kind.text, &layer.renderer);}diff --git a/src/files/app_audio_playlist.zig b/src/files/app_audio_playlist.zigindex ef604f8..a1793de 100644--- a/src/files/app_audio_playlist.zig+++ b/src/files/app_audio_playlist.zig@@ -64,19 +64,35 @@ pub fn toggle(self: *App) void {self.audio_bar.playlist_wheel = 0;self.cancelWheel();if (self.audio_bar.playlist_open) {- if (self.audio_bar.playlist) |list| {- for (0..list.count()) |i| {- if (self.audio_state.isTrack(list.path(i))) {- self.audio_bar.playlist_selected = i;- break;- }- }- }+ if (current(self)) |i| self.audio_bar.playlist_selected = i;reveal(self);}self.invalidate();}+/// The playing track's place in the snapshot, if it is there.+fn current(self: *App) ?usize {+ const list = self.audio_bar.playlist orelse return null;+ for (0..list.count()) |i| {+ if (self.audio_state.isTrack(list.path(i))) return i;+ }+ return null;+}++/// The snapshot entry `delta` tracks away from the playing one.+pub fn neighbour(self: *App, delta: isize) ?usize {+ const index = current(self) orelse return null;+ const target = @as(isize, @intCast(index)) + delta;+ return if (target >= 0 and target < count(self)) @intCast(target) else null;+}++/// Previous and Next: another track of the snapshot, like a click on its row.+pub fn skip(self: *App, delta: isize) void {+ const index = neighbour(self, delta) orelse return;+ activate(self, index);+ reveal(self);+}+fn activate(self: *App, index: usize) void {const list = self.audio_bar.playlist orelse return;if (index >= list.count()) return;diff --git a/src/files/app_hover.zig b/src/files/app_hover.zigindex c74b889..eaa452d 100644--- a/src/files/app_hover.zig+++ b/src/files/app_hover.zig@@ -15,7 +15,7 @@ const places = App.places;const pin_base = App.pin_base;pub fn hoverBounds(self: *App, key: usize) ?header.Rect {- if (key >= 920 and key < 924) return @import("app_audio_bar.zig").rect(self);+ if (key >= 920 and key < 926) return @import("app_audio_bar.zig").rect(self);if (key >= std.math.maxInt(usize) - 4096) return null;if (key >= 900 and key < 904) return .{ .x = 0, .y = 0, .w = @floatFromInt(self.w), .h = @floatFromInt(self.h) };if (key == 0) return .{ .x = 0, .y = 0, .w = 0, .h = 0 };diff --git a/src/ui/layout.zig b/src/ui/layout.zigindex e4941ef..45c6dc7 100644--- a/src/ui/layout.zig+++ b/src/ui/layout.zig@@ -124,6 +124,10 @@ pub const IconId = enum {trash_can,play,pause,+ /// Media transport: a bar before a leftward triangle, its mirror, and a square.+ skip_previous,+ skip_next,+ stop,view_grid,view_list,sort,diff --git a/src/ui/paint_icons.zig b/src/ui/paint_icons.zigindex 9415bbc..b802af8 100644--- a/src/ui/paint_icons.zig+++ b/src/ui/paint_icons.zig@@ -48,6 +48,15 @@ pub fn iconDistance(id: IconId, px: f32, py: f32, cx: f32, cy: f32, half: f32, t.play => sdf.sdQuad(px, py, cx - half * 0.65, cy - half, cx + half, cy, cx - half * 0.65, cy + half, cx - half * 0.65, cy),// `.play` rotated a quarter turn: same degenerate quad, tip at the top..caret_up => sdf.sdQuad(px, py, cx - half, cy + half * 0.65, cx, cy - half, cx + half, cy + half * 0.65, cx, cy + half * 0.65),+ .skip_next => @min(+ sdf.sdQuad(px, py, cx - half * 0.9, cy - half * 0.9, cx + half * 0.45, cy, cx - half * 0.9, cy + half * 0.9, cx - half * 0.9, cy),+ sdBox(px, py, cx + half * 0.72, cy, half * 0.18, half * 0.9),+ ),+ .skip_previous => @min(+ sdf.sdQuad(px, py, cx + half * 0.9, cy - half * 0.9, cx - half * 0.45, cy, cx + half * 0.9, cy + half * 0.9, cx + half * 0.9, cy),+ sdBox(px, py, cx - half * 0.72, cy, half * 0.18, half * 0.9),+ ),+ .stop => sdRoundedBoxCentered(px, py, cx, cy, half * 0.75, half * 0.75, half * 0.15),.pause => @min(sdBox(px, py, cx - half * 0.45, cy, half * 0.22, half), sdBox(px, py, cx + half * 0.45, cy, half * 0.22, half)),.minimize => sdSegment(px, py, cx - half, cy, cx + half, cy, t),.drag_handle => @min(diff --git a/tests/files_audio.py b/tests/files_audio.pyindex 7fcb54a..fed39eb 100644--- a/tests/files_audio.py+++ b/tests/files_audio.py@@ -170,24 +170,32 @@ def run():click(*found)wait_for(lambda: status() == 's "Playing"', "third click did not resume")# The toolbar follows playback and remains usable while paused.- click(306, 82)+ click(338, 82)wait_for(lambda: status() == 's "Paused"', "toolbar did not pause")- click(460, 93)+ # Narrow inline bar: Previous, Play, Next, then the title and+ # time above the Settings slider (x 402-578), Stop, Playlist.+ click(500, 90)wait_for(lambda: int(mpris("Position").split()[1]) > 10_000_000,"toolbar seek did not move position")# The shared scrollbar thumb also supports dragging outside# its gutter, without resuming a paused track.- move(460, 91)+ move(500, 89)action("pointer_button", {"button": 272, "pressed": True})move(400, 110)action("pointer_button", {"button": 272, "pressed": False})wait_for(lambda: int(mpris("Position").split()[1]) < 10_000_000,"scrollbar drag did not seek backwards")assert status() == 's "Paused"'- click(460, 91)+ click(500, 89)wait_for(lambda: int(mpris("Position").split()[1]) > 10_000_000,"scrollbar click did not seek forwards")shot("toolbar-paused")+ # Previous restarts a track that has played past 3 s; the+ # first track has nothing before it either way.+ click(304, 82)+ wait_for(lambda: int(mpris("Position").split()[1]) < 3_000_000,+ "Previous did not restart the track")+ assert "song.wav" in mpris("Metadata"), mpris("Metadata")# Navigation must not hide the controls for the current track.click(92, 28) # Up to Homeshot("toolbar-other-folder")@@ -208,16 +216,29 @@ def run():wait_for(lambda: "zz-09.wav" in mpris("Metadata"),"playlist keyboard navigation did not reach last track")click(644, 82) # dismiss playlist- click(306, 82)+ # The last track has no Next; Previous early in a track+ # goes back one, and Next returns.+ move(372, 82)+ assert cursor() != "pointer", "Next is enabled on the last track"+ click(304, 82)+ wait_for(lambda: "zz-08.wav" in mpris("Metadata"), "Previous did not go back a track")+ click(372, 82)+ wait_for(lambda: "zz-09.wav" in mpris("Metadata"), "Next did not advance a track")+ click(338, 82)wait_for(lambda: status() == 's "Paused"', "playlist track did not pause")+ # A wide window fits the title and the time pill beside the slider.+ action("set_window_size", {"id": win["id"], "width": 1200, "height": 540})+ wait_for(lambda: box()["width"] == 1200, "Files did not resize")+ time.sleep(.5)+ shot("toolbar-wide")# A narrow window gives the player its own row.action("set_window_size", {"id": win["id"], "width": 560, "height": 500})wait_for(lambda: box()["width"] == 560, "Files did not resize")time.sleep(.5)shot("toolbar-narrow")- click(38, 176)+ click(70, 176)wait_for(lambda: status() == 's "Playing"', "narrow toolbar did not resume")- click(74, 176)+ click(488, 176)wait_for(lambda: status() == "", "toolbar Stop did not exit player")shot("toolbar-stopped")action("set_window_size", {"id": win["id"], "width": 960, "height": 540})@@ -226,9 +247,9 @@ def run():click(28, 28) # Back to Musicclick(*found)wait_for(lambda: status() == 's "Playing"', "play did not restart")- click(306, 82)+ click(338, 82)wait_for(lambda: status() == 's "Paused"', "toolbar did not pause restarted track")- click(306, 82)+ click(338, 82)wait_for(lambda: status() == 's "Playing"', "toolbar did not resume")click(726, 82) # list view: the button sits right of the nametime.sleep(.6)