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.
This commit is contained in:
+84
@@ -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<Target> {
|
||||
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<Song>) {
|
||||
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 {
|
||||
|
||||
@@ -96,6 +96,8 @@ fn step(ui: slint::Weak<AppWindow>, steps: Vec<String>, 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
|
||||
|
||||
+36
-2
@@ -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<Held> = None;
|
||||
let mut south: Option<SouthPress> = 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<Action> {
|
||||
|
||||
fn button_action(b: Button) -> Option<Action> {
|
||||
Some(match b {
|
||||
Button::South => Action::Activate,
|
||||
Button::East => Action::Back,
|
||||
Button::West => Action::PlayPause,
|
||||
Button::North => Action::Queue,
|
||||
|
||||
+9
-1
@@ -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")],
|
||||
|
||||
@@ -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<Vec<Song>> {
|
||||
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<AlbumDetail> {
|
||||
#[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() {
|
||||
|
||||
+2
-1
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
<svg width="64" height="64" xmlns="http://www.w3.org/2000/svg">
|
||||
<defs>
|
||||
<mask id="glyph">
|
||||
<rect width="64" height="64" fill="#FFFFFF"/>
|
||||
<path fill="none" stroke="#000000" stroke-width="3.5" stroke-linejoin="round" d="M32 21 L42 32 L36.5 32 L36.5 42 L27.5 42 L27.5 32 L22 32 Z"/>
|
||||
</mask>
|
||||
</defs>
|
||||
<path stroke="none" fill="#FFFFFF" mask="url(#glyph)" d="M16 8 L48 8 Q56 8 56 16 L56 48 Q56 56 48 56 L16 56 Q8 56 8 48 L8 16 Q8 8 16 8"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 465 B |
Reference in New Issue
Block a user