From e11d95daa55b6e01914f22741625f54bf3c3d2ec Mon Sep 17 00:00:00 2001 From: Ansho Rei Date: Thu, 5 Nov 2026 19:04:08 +0100 Subject: [PATCH] feat: shuffle play an album, artist or playlist with a long press Holding South on a row, or shift+s on the keyboard, plays its tracks in random order with track shuffle on, so turning shuffle off goes back to their own order. On a song row it shuffles the page the song is on. An artist plays every track of every album, fetched a few albums at a time. South now acts on release, so a long press can mean something else. A long press on a row with nothing to shuffle activates as before. --- src/app.rs | 84 ++++++++++++++++++++++++++ src/devscript.rs | 2 + src/input.rs | 38 +++++++++++- src/prompts.rs | 10 ++- src/subsonic.rs | 25 ++++++++ ui/app.slint | 3 +- ui/prompts/keyboard/keyboard_shift.svg | 9 +++ 7 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 ui/prompts/keyboard/keyboard_shift.svg diff --git a/src/app.rs b/src/app.rs index fed4903..37e47e1 100644 --- a/src/app.rs +++ b/src/app.rs @@ -764,6 +764,13 @@ impl App { } pub fn action(&mut self, a: Action) { + // A long press of the controller's activate button shuffles what is + // selected, and activates wherever there is nothing to shuffle. + let a = match a { + Action::HoldActivate if self.selected_shuffle_target().is_some() => Action::Shuffle, + Action::HoldActivate => Action::Activate, + a => a, + }; match a { Action::Quit => { let _ = slint::quit_event_loop(); @@ -863,6 +870,7 @@ impl App { self.move_sel(a) } Action::Activate => self.activate(), + Action::Shuffle => self.shuffle_selected(), Action::Back => self.back(), Action::Queue => match self.selected_setting() { Some(Setting::Server(i)) => self.remove_server(i), @@ -1818,6 +1826,18 @@ impl App { if matches!(target, Some(Target::Song(_) | Target::Album(_) | Target::Playlist(_))) { h(Control::Queue, "Add to queue"); } + if matches!( + target, + Some(Target::Song(_) | Target::Album(_) | Target::Artist(_) | Target::Playlist(_)) + ) { + h( + Control::ShufflePlay, + match self.prompts { + Prompts::Keyboard => "Shuffle", + Prompts::Pad(_) => "Hold: Shuffle", + }, + ); + } h(Control::PlayPause, "Play / Pause"); h(Control::Sections, "Sections"); h(Control::NowPlaying, "Now playing"); @@ -2089,6 +2109,70 @@ impl App { }); } + /// What shuffle play would play from the selected row, if anything. + fn selected_shuffle_target(&self) -> Option { + if self.screen != Screen::Browse || self.area != Area::Content { + return None; + } + let page = self.current_page()?; + let target = &page.entries.get(page.sel)?.target; + matches!( + target, + Target::Song(_) | Target::Album(_) | Target::Artist(_) | Target::Playlist(_) + ) + .then(|| target.clone()) + } + + /// Plays the selected album, artist or playlist shuffled, or the page a + /// selected song is on. + fn shuffle_selected(&mut self) { + let (Some(target), Some(client)) = (self.selected_shuffle_target(), self.client.clone()) else { + return; + }; + let failed = |app: &mut App, e: anyhow::Error| app.toast(format!("Could not shuffle: {e:#}")); + match target { + Target::Song(_) => { + let songs = self.current_page().map(|p| p.songs.clone()).unwrap_or_default(); + self.shuffle_play(songs); + } + Target::Album(id) => self.spawn( + async move { Ok(client.album(&id).await?.song) }, + move |app, r| match r { + Ok(songs) => app.shuffle_play(songs), + Err(e) => failed(app, e), + }, + ), + Target::Playlist(id) => { + self.spawn( + async move { Ok(client.playlist(&id).await?.entry) }, + move |app, r| match r { + Ok(songs) => app.shuffle_play(songs), + Err(e) => failed(app, e), + }, + ) + } + Target::Artist(id) => self.spawn(async move { client.artist_songs(&id).await }, move |app, r| match r { + Ok(songs) => app.shuffle_play(songs), + Err(e) => failed(app, e), + }), + Target::Setting(_) => {} + } + } + + /// Replaces the queue with `songs` in random order, with track shuffle on + /// so turning it off goes back to their own order. + fn shuffle_play(&mut self, songs: Vec) { + if songs.is_empty() { + self.toast("Nothing to shuffle".into()); + return; + } + let perm = shuffle::tracks(&mut self.rng, songs.len(), 0); + self.play_songs(shuffle::apply(&songs, &perm), 0); + self.order = perm; + self.shuffle = Mode::Tracks; + self.render_hints(); + } + fn enqueue_selected(&mut self) { let Some(page) = self.current_page() else { return }; let Some(entry) = page.entries.get(page.sel) else { diff --git a/src/devscript.rs b/src/devscript.rs index 363b053..95fd957 100644 --- a/src/devscript.rs +++ b/src/devscript.rs @@ -96,6 +96,8 @@ fn step(ui: slint::Weak, steps: Vec, i: usize) { "volume-down" => Action::VolumeDown, "mute" => Action::ToggleMute, "cycle-shuffle" => Action::CycleShuffle, + "shuffle" => Action::Shuffle, + "hold-activate" => Action::HoldActivate, other => { log::warn!("unknown dev script step {other}"); Action::None diff --git a/src/input.rs b/src/input.rs index efa11ee..2ba8190 100644 --- a/src/input.rs +++ b/src/input.rs @@ -27,6 +27,8 @@ const REPEAT_DELAY: Duration = Duration::from_millis(380); const REPEAT_SLOW: Duration = Duration::from_millis(110); const REPEAT_FAST: Duration = Duration::from_millis(45); const FAST_AFTER: Duration = Duration::from_millis(1500); +/// How long South is held before it counts as a long press. +const LONG_PRESS: Duration = Duration::from_millis(500); const STICK_ON: f32 = 0.55; const STICK_OFF: f32 = 0.35; @@ -37,6 +39,14 @@ enum Source { RightStick, } +/// South acts on release, so a long press can mean something else. +struct SouthPress { + family: Family, + since: Instant, + /// Sent as a long press already, so the release sends nothing. + long: bool, +} + struct Held { source: Source, action: Action, @@ -66,6 +76,7 @@ pub fn spawn_gamepad(emit: impl Fn(PadEvent) + Send + 'static) { } emit(PadEvent::Present(present(&gilrs, &mut families))); let mut held: Option = None; + let mut south: Option = None; loop { while let Some(ev) = gilrs.next_event_blocking(Some(Duration::from_millis(15))) { if matches!(ev.event, EventType::Connected | EventType::Disconnected) { @@ -77,6 +88,20 @@ pub fn spawn_gamepad(emit: impl Fn(PadEvent) + Send + 'static) { } let family = families.family(&gilrs, ev.id); match ev.event { + EventType::ButtonPressed(Button::South, _) => { + south = Some(SouthPress { + family, + since: Instant::now(), + long: false, + }); + } + EventType::ButtonReleased(Button::South, _) => { + if let Some(p) = south.take() + && !p.long + { + emit(PadEvent::Action(Action::Activate, p.family)); + } + } EventType::ButtonPressed(b, _) => { if let Some(a) = dpad_action(b) { held = Some(start_hold(Source::DPad, a, family)); @@ -108,10 +133,20 @@ pub fn spawn_gamepad(emit: impl Fn(PadEvent) + Send + 'static) { _ => {} } } - EventType::Disconnected => held = None, + EventType::Disconnected => { + held = None; + south = None; + } _ => {} } } + if let Some(p) = south.as_mut() + && !p.long + && p.since.elapsed() >= LONG_PRESS + { + p.long = true; + emit(PadEvent::Action(Action::HoldActivate, p.family)); + } if let Some(h) = held.as_mut() { let now = Instant::now(); if now >= h.next { @@ -216,7 +251,6 @@ fn dpad_action(b: Button) -> Option { fn button_action(b: Button) -> Option { Some(match b { - Button::South => Action::Activate, Button::East => Action::Back, Button::West => Action::PlayPause, Button::North => Action::Queue, diff --git a/src/prompts.rs b/src/prompts.rs index d90b9b3..c6d69af 100644 --- a/src/prompts.rs +++ b/src/prompts.rs @@ -38,6 +38,8 @@ pub enum Control { /// Cycles the shuffle mode on now playing: the queue button on a /// controller, a key of its own on the keyboard. Shuffle, + /// Shuffle play on a row: a long press of activate on a controller. + ShufflePlay, /// Steam + X brings up Steam's on-screen keyboard. Nothing to show for a /// real keyboard. OnScreenKeyboard, @@ -45,7 +47,7 @@ pub enum Control { #[cfg(test)] impl Control { - pub const ALL: [Control; 13] = [ + pub const ALL: [Control; 14] = [ Control::Activate, Control::Back, Control::PlayPause, @@ -58,6 +60,7 @@ impl Control { Control::Volume, Control::Mute, Control::Shuffle, + Control::ShufflePlay, Control::OnScreenKeyboard, ]; } @@ -86,10 +89,12 @@ pub fn icons(prompts: Prompts, control: Control) -> &'static [&'static [u8]] { Volume => &[icon!("keyboard", "keyboard_minus"), icon!("keyboard", "keyboard_plus")], Mute => &[icon!("keyboard", "keyboard_m")], Shuffle => &[icon!("keyboard", "keyboard_s")], + ShufflePlay => &[icon!("keyboard", "keyboard_shift"), icon!("keyboard", "keyboard_s")], OnScreenKeyboard => &[], }, Prompts::Pad(Family::Xbox) => match control { Activate => &[icon!("xbox", "xbox_button_color_a")], + ShufflePlay => &[icon!("xbox", "xbox_button_color_a")], Back => &[icon!("xbox", "xbox_button_color_b")], PlayPause => &[icon!("xbox", "xbox_button_color_x")], Queue => &[icon!("xbox", "xbox_button_color_y")], @@ -105,6 +110,7 @@ pub fn icons(prompts: Prompts, control: Control) -> &'static [&'static [u8]] { }, Prompts::Pad(Family::PlayStation) => match control { Activate => &[icon!("playstation", "playstation_button_color_cross")], + ShufflePlay => &[icon!("playstation", "playstation_button_color_cross")], Back => &[icon!("playstation", "playstation_button_color_circle")], PlayPause => &[icon!("playstation", "playstation_button_color_square")], Queue => &[icon!("playstation", "playstation_button_color_triangle")], @@ -131,6 +137,7 @@ pub fn icons(prompts: Prompts, control: Control) -> &'static [&'static [u8]] { // By position, as gilrs reports buttons: the bottom face button is B on a Switch. Prompts::Pad(Family::Switch) => match control { Activate => &[icon!("switch", "switch_button_b")], + ShufflePlay => &[icon!("switch", "switch_button_b")], Back => &[icon!("switch", "switch_button_a")], PlayPause => &[icon!("switch", "switch_button_y")], Queue => &[icon!("switch", "switch_button_x")], @@ -149,6 +156,7 @@ pub fn icons(prompts: Prompts, control: Control) -> &'static [&'static [u8]] { }, Prompts::Pad(Family::SteamDeck) => match control { Activate => &[icon!("steamdeck", "steamdeck_button_a")], + ShufflePlay => &[icon!("steamdeck", "steamdeck_button_a")], Back => &[icon!("steamdeck", "steamdeck_button_b")], PlayPause => &[icon!("steamdeck", "steamdeck_button_x")], Queue => &[icon!("steamdeck", "steamdeck_button_y")], diff --git a/src/subsonic.rs b/src/subsonic.rs index 2fb6543..b9f457c 100644 --- a/src/subsonic.rs +++ b/src/subsonic.rs @@ -212,6 +212,28 @@ impl Client { Ok(r.artist) } + /// Every song on the artist's albums, album by album. A few albums are + /// fetched at a time, so an artist with dozens does not flood the server. + pub async fn artist_songs(&self, id: &str) -> Result> { + let albums = self.artist(id).await?.album; + let limit = std::sync::Arc::new(tokio::sync::Semaphore::new(4)); + let tasks: Vec<_> = albums + .into_iter() + .map(|a| { + let (client, limit) = (self.clone(), limit.clone()); + tokio::spawn(async move { + let _permit = limit.acquire_owned().await?; + anyhow::Ok(client.album(&a.id).await?.song) + }) + }) + .collect(); + let mut songs = Vec::new(); + for t in tasks { + songs.extend(t.await??); + } + Ok(songs) + } + pub async fn album(&self, id: &str) -> Result { #[derive(Deserialize)] struct R { @@ -605,6 +627,9 @@ mod tests { assert!(!artists.is_empty(), "no artists"); let artist = c.artist(&artists[0].id).await.unwrap(); println!("artist: {} with {} albums", artist.name, artist.album.len()); + let songs = c.artist_songs(&artists[0].id).await.unwrap(); + assert!(!songs.is_empty(), "artist has no songs"); + println!("artist songs: {}", songs.len()); let playlists = c.playlists().await.unwrap(); if let Some(p) = playlists.first() { diff --git a/ui/app.slint b/ui/app.slint index b743b41..98f059b 100644 --- a/ui/app.slint +++ b/ui/app.slint @@ -9,7 +9,7 @@ export enum Action { none, up, down, left, right, page-up, page-down, activate, back, play-pause, next, prev, queue, section-next, section-prev, now-playing, search, seek-forward, seek-back, toggle-fullscreen, quit, - volume-up, volume-down, toggle-mute, cycle-shuffle, + volume-up, volume-down, toggle-mute, cycle-shuffle, shuffle, hold-activate, } export enum Screen { login, browse, now-playing } @@ -1306,6 +1306,7 @@ export component AppWindow inherits Window { if (e.text == "-") { root.action(Action.volume-down); return accept; } if (e.text == "m") { root.action(Action.toggle-mute); return accept; } if (e.text == "s") { root.action(Action.cycle-shuffle); return accept; } + if (e.text == "S") { root.action(Action.shuffle); return accept; } reject } diff --git a/ui/prompts/keyboard/keyboard_shift.svg b/ui/prompts/keyboard/keyboard_shift.svg new file mode 100644 index 0000000..ba71414 --- /dev/null +++ b/ui/prompts/keyboard/keyboard_shift.svg @@ -0,0 +1,9 @@ + + + + + + + + +