From fd02fe414c0876d32dbea4253fdce1e4ea169819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Mei=C3=9Fner?= <936176+t-nil@users.noreply.github.com> Date: Sun, 3 Sep 2023 01:25:22 +0200 Subject: [PATCH] - remember and restore previous playback state - add comments --- src/mpris.rs | 6 +++--- src/queue.rs | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/mpris.rs b/src/mpris.rs index 3dbdd9ee..afa2b470 100644 --- a/src/mpris.rs +++ b/src/mpris.rs @@ -263,11 +263,11 @@ impl MprisPlayer { self.event.trigger(); } - #[dbus_interface(property)] - fn randomize(&self) { + /*#[dbus_interface(property)] + fn randomize(&self, _b: bool) { self.queue.randomize(); self.event.trigger(); - } + }*/ #[dbus_interface(property)] fn volume(&self) -> f64 { diff --git a/src/queue.rs b/src/queue.rs index e7af001b..f21a9e0a 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -474,12 +474,26 @@ impl Queue { /// /// This deactivates `Shuffle`. pub fn randomize(&self) { - let mut queue = self.queue.write().unwrap(); - queue.shuffle(&mut thread_rng()); + // stop playlist, because we completely invalidate any playing order + let previous_playback_state = self.cfg.state().playback_state.clone(); + self.stop(); + // deactivate `Shuffle` feature, because it usually wouldn't make sense to use both if self.get_shuffle() == true { self.set_shuffle(false); } + + // permutate the queue Vec + let mut queue = self.queue.write().unwrap(); + queue.shuffle(&mut thread_rng()); + + // resetting playing position to start of queue doesn't seem necessary + // my guess is that stop() does that + + // resume playback if we were playing before + if previous_playback_state == PlaybackState::Playing { + self.toggleplayback(); + } } /// Handle events that are specific to the queue.