Replace music player SetPosition message with relative Seek

This commit is contained in:
mars 2022-11-25 17:48:36 -07:00
parent 448435eb8c
commit 2bef47a65a
3 changed files with 9 additions and 9 deletions

View File

@ -104,6 +104,6 @@ pub enum OutMsg {
/// Sets the volume. Values are clamped to 0.0 to 1.0.
SetVolume { volume: f32 },
/// Set the current track position in seconds.
SetPosition { position: f32 },
/// Seeks the current track's position in seconds.
Seek { offset: f32 },
}

View File

@ -111,10 +111,8 @@ async fn on_message(
OutMsg::Stop => player.stop().await?,
OutMsg::Previous => player.previous().await?,
OutMsg::Next => player.next().await?,
OutMsg::SetPosition { position } => {
let current = player.position().await?;
let next = (position * 1_000_000.0) as i64; // Seconds to microseconds
let offset = next - current;
OutMsg::Seek { offset } => {
let offset = (offset * 1_000_000.0) as i64; // Seconds to microseconds
player.seek(offset).await?;
}
_ => {}

View File

@ -149,14 +149,14 @@ impl Container for MusicPlayerWidget {
Some(position * self.duration_secs)
} else if self.position_updating {
let position = self.slider.get_position() * self.duration_secs;
let msg = OutMsg::SetPosition { position };
let offset = position - self.position_secs;
let msg = OutMsg::Seek { offset };
self.send_message(&msg);
self.position_secs = position;
self.position_updating = false;
Some(position)
} else if let PlaybackStatus::Playing = self.status {
self.position_secs += dt;
self.slider
.set_position(self.position_secs / self.duration_secs);
Some(self.position_secs)
} else if self.position_dirty {
self.position_dirty = false;
@ -168,6 +168,8 @@ impl Container for MusicPlayerWidget {
if let Some(position) = position_display {
self.position_dirty = false;
self.position.set_text(&Self::format_time(position));
self.slider
.set_position(self.position_secs / self.duration_secs);
}
if self.previous.was_clicked() {