SAO UI music player sends Prev/Play/Next events on button presses

This commit is contained in:
mars 2022-11-16 21:11:34 -07:00
parent 0cc55b80a8
commit 32cf5e3359
1 changed files with 22 additions and 3 deletions

View File

@ -1,7 +1,7 @@
use api::*;
use canary_script::*;
use canary_music_player::{AlbumInfo, PlaybackStatus, ProgressChanged, TrackInfo};
use canary_music_player::{AlbumInfo, PlaybackStatus, ProgressChanged, TrackInfo, OutMsg};
use crate::widgets::prelude::*;
use button::{RoundButton, RoundButtonStyle};
@ -57,7 +57,7 @@ impl PanelImpl for MusicPlayerPanel {
use InMsg::*;
match (self.widget.as_mut(), msg) {
(Some(_), Disconnected) => self.widget = None,
(None, Connected) => self.widget = Some(MusicPlayerWidget::new()),
(None, Connected) => self.widget = Some(MusicPlayerWidget::new(self.panel)),
(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),
@ -106,6 +106,7 @@ impl Default for MusicPlayerStyle {
}
pub struct MusicPlayerWidget {
panel: Panel,
artist: Offset<Label>,
album: Offset<Label>,
track: Offset<Label>,
@ -146,6 +147,18 @@ impl Container for MusicPlayerWidget {
self.position
.set_text(&Self::format_time(self.position_secs));
}
if self.previous.was_clicked() {
self.send_message(&OutMsg::Previous);
}
if self.play.was_clicked() {
self.send_message(&OutMsg::PlayPause);
}
if self.next.was_clicked() {
self.send_message(&OutMsg::Next);
}
}
fn draw(&mut self, ctx: &DrawContext) {
@ -168,7 +181,7 @@ impl Container for MusicPlayerWidget {
}
impl MusicPlayerWidget {
pub fn new() -> Self {
pub fn new(panel: Panel) -> Self {
let style = MusicPlayerStyle::default();
let display_font = Font::new(crate::DISPLAY_FONT);
let content_font = Font::new(crate::CONTENT_FONT);
@ -244,6 +257,7 @@ impl MusicPlayerWidget {
let next = RoundButton::new(secondary_button, Some(next_text));
Self {
panel,
artist: make_body_label("Artist"),
album: make_body_label("Album"),
track: make_body_label("Track"),
@ -262,6 +276,11 @@ impl MusicPlayerWidget {
}
}
pub fn send_message(&self, msg: &OutMsg) {
let msg = serde_json::to_vec(msg).unwrap();
self.panel.send_message(&msg);
}
pub fn format_time(secs: f32) -> String {
let duration = secs.floor() as usize;
let seconds = duration % 60;