Use progress info in position/duration labels
This commit is contained in:
parent
4b05f55725
commit
71a4eb2cf0
|
@ -1,7 +1,7 @@
|
||||||
use api::*;
|
use api::*;
|
||||||
use canary_script::*;
|
use canary_script::*;
|
||||||
|
|
||||||
use canary_music_player::{AlbumInfo, PlaybackStatus, TrackInfo};
|
use canary_music_player::{AlbumInfo, PlaybackStatus, ProgressChanged, TrackInfo};
|
||||||
|
|
||||||
use crate::widgets::prelude::*;
|
use crate::widgets::prelude::*;
|
||||||
use button::{RoundButton, RoundButtonStyle};
|
use button::{RoundButton, RoundButtonStyle};
|
||||||
|
@ -79,6 +79,7 @@ impl PanelImpl for MusicPlayerPanel {
|
||||||
(Some(widget), AlbumChanged(info)) => widget.update_album(info),
|
(Some(widget), AlbumChanged(info)) => widget.update_album(info),
|
||||||
(Some(widget), TrackChanged(info)) => widget.update_track(info),
|
(Some(widget), TrackChanged(info)) => widget.update_track(info),
|
||||||
(Some(widget), PlaybackStatusChanged(status)) => widget.update_playback_status(status),
|
(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,
|
art_rect: Rect,
|
||||||
body_rect: Rect,
|
body_rect: Rect,
|
||||||
footer_rect: Rect,
|
footer_rect: Rect,
|
||||||
|
position_secs: f32,
|
||||||
|
position_dirty: bool,
|
||||||
|
status: PlaybackStatus,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Container for MusicPlayerWidget {
|
impl Container for MusicPlayerWidget {
|
||||||
|
@ -131,6 +135,19 @@ impl Container for MusicPlayerWidget {
|
||||||
f(&mut self.duration);
|
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) {
|
fn draw(&mut self, ctx: &DrawContext) {
|
||||||
ctx.draw_partially_rounded_rect(
|
ctx.draw_partially_rounded_rect(
|
||||||
CornerFlags::TOP,
|
CornerFlags::TOP,
|
||||||
|
@ -239,6 +256,22 @@ impl MusicPlayerWidget {
|
||||||
art_rect: Rect::from_xy_size(Vec2::ZERO, Vec2::ZERO),
|
art_rect: Rect::from_xy_size(Vec2::ZERO, Vec2::ZERO),
|
||||||
body_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),
|
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) {
|
pub fn update_playback_status(&mut self, status: PlaybackStatus) {
|
||||||
|
self.status = status;
|
||||||
|
|
||||||
let icon = match status {
|
let icon = match status {
|
||||||
PlaybackStatus::Playing => "契",
|
PlaybackStatus::Playing => "契",
|
||||||
PlaybackStatus::Paused => "",
|
PlaybackStatus::Paused => "",
|
||||||
|
|
Loading…
Reference in New Issue