Use progress info in position/duration labels

This commit is contained in:
mars 2022-11-15 21:37:03 -07:00
parent 4b05f55725
commit 71a4eb2cf0
1 changed files with 47 additions and 1 deletions

View File

@ -1,7 +1,7 @@
use api::*;
use canary_script::*;
use canary_music_player::{AlbumInfo, PlaybackStatus, TrackInfo};
use canary_music_player::{AlbumInfo, PlaybackStatus, ProgressChanged, TrackInfo};
use crate::widgets::prelude::*;
use button::{RoundButton, RoundButtonStyle};
@ -79,6 +79,7 @@ impl PanelImpl for MusicPlayerPanel {
(Some(widget), AlbumChanged(info)) => widget.update_album(info),
(Some(widget), TrackChanged(info)) => widget.update_track(info),
(Some(widget), PlaybackStatusChanged(status)) => widget.update_playback_status(status),
(Some(widget), ProgressChanged(progress)) => widget.update_progress(progress),
_ => {}
}
}
@ -117,6 +118,9 @@ pub struct MusicPlayerWidget {
art_rect: Rect,
body_rect: Rect,
footer_rect: Rect,
position_secs: f32,
position_dirty: bool,
status: PlaybackStatus,
}
impl Container for MusicPlayerWidget {
@ -131,6 +135,19 @@ impl Container for MusicPlayerWidget {
f(&mut self.duration);
}
fn update(&mut self, dt: f32) {
if let PlaybackStatus::Playing = self.status {
self.position_secs += dt;
self.position_dirty = true;
}
if self.position_dirty {
self.position_dirty = false;
self.position
.set_text(&Self::format_time(self.position_secs));
}
}
fn draw(&mut self, ctx: &DrawContext) {
ctx.draw_partially_rounded_rect(
CornerFlags::TOP,
@ -239,6 +256,22 @@ impl MusicPlayerWidget {
art_rect: Rect::from_xy_size(Vec2::ZERO, Vec2::ZERO),
body_rect: Rect::from_xy_size(Vec2::ZERO, Vec2::ZERO),
footer_rect: Rect::from_xy_size(Vec2::ZERO, Vec2::ZERO),
position_secs: 0.0,
position_dirty: false,
status: PlaybackStatus::Paused,
}
}
pub fn format_time(secs: f32) -> String {
let duration = secs.floor() as usize;
let seconds = duration % 60;
let minutes = (duration / 60) % 60;
let hours = (duration / 60) / 60;
if hours > 0 {
format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
} else {
format!("{:02}:{:02}", minutes, seconds)
}
}
@ -304,7 +337,20 @@ impl MusicPlayerWidget {
);
}
pub fn update_progress(&mut self, progress: ProgressChanged) {
self.position_secs = progress.position;
self.position_dirty = true;
if let Some(length) = progress.length {
self.duration.set_text(&Self::format_time(length));
} else {
self.duration.set_text("--:--");
}
}
pub fn update_playback_status(&mut self, status: PlaybackStatus) {
self.status = status;
let icon = match status {
PlaybackStatus::Playing => "",
PlaybackStatus::Paused => "",