diff --git a/src/player/current.rs b/src/player/current.rs index e28f7ca..6986bbb 100644 --- a/src/player/current.rs +++ b/src/player/current.rs @@ -23,6 +23,10 @@ impl Metadata { } } + pub fn set_cover(&mut self, cover: DynamicImage) { + self.cover = cover.thumbnail(200, 200); + } + pub fn update_metadata(&mut self, song: Song) { self.name = song.title; self.artist = song.artist; diff --git a/src/player/player.rs b/src/player/player.rs index e304686..e9c0fac 100644 --- a/src/player/player.rs +++ b/src/player/player.rs @@ -1,10 +1,11 @@ use std::{sync::mpsc::TryRecvError, thread, time::Duration}; use crossterm::event::{poll, read, Event, KeyCode}; +use image::DynamicImage; use crate::{ ssonic::{response::Song, APIEvent}, - utils::Error, + utils::{default_cover, Error}, }; use super::{errors::PlayerError, Player, PlayerEvent}; @@ -42,6 +43,7 @@ impl Player { PlayerEvent::UserQuit => return Err(Box::new(PlayerError::UserQuit)), PlayerEvent::AddSongList(list) => self.playlist.append(list), PlayerEvent::PlayNext => self.play_next()?, + PlayerEvent::UpdateCover(cover) => self.tui_root.update_cover(cover), _ => unimplemented!(), } } @@ -73,6 +75,9 @@ impl Player { }; if let Some(cover_id) = song.cover_art.clone() { self.api_chan.send(APIEvent::FetchCoverArt(cover_id))?; + } else { + self.player_chan_in + .send(PlayerEvent::UpdateCover(default_cover()))?; } self.tui_root.metadata.update_metadata(song); diff --git a/src/player/tui.rs b/src/player/tui.rs index 5a64924..cd04709 100644 --- a/src/player/tui.rs +++ b/src/player/tui.rs @@ -1,3 +1,4 @@ +use image::DynamicImage; use layout::Flex; use ratatui::{ layout::{Constraint, Layout}, @@ -45,7 +46,8 @@ impl Root { }) } fn render_info(&mut self, area: Rect, buf: &mut Buffer) { - let layout_info = Layout::horizontal([Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)]); + let layout_info = + Layout::horizontal([Constraint::Ratio(1, 3), Constraint::Ratio(2, 3)]).margin(5); let [cover, track_info] = layout_info.areas(area); self.render_cover(cover, buf); self.render_track_info(track_info, buf); @@ -56,8 +58,8 @@ impl Root { StatefulImage::new(None).render(area, buf, &mut self.image_state); } fn render_track_info(&self, area: Rect, buf: &mut Buffer) { - let layout_track = Layout::vertical([Constraint::Min(20), Constraint::Min(10)]); - let [title, artist, _] = layout_track.areas(area); + let layout_track = Layout::vertical([Constraint::Max(2), Constraint::Max(1)]); + let [title, artist] = layout_track.areas(area); self.render_title(title, buf); self.render_artist(artist, buf); } @@ -72,6 +74,12 @@ impl Root { Paragraph::new(self.metadata.artist.clone().unwrap()).render(area, buf); } } + pub fn update_cover(&mut self, cover: DynamicImage) { + self.metadata.set_cover(cover); + self.image_state = self + .proto_picker + .new_resize_protocol(self.metadata.cover.clone()); + } } impl Player { diff --git a/src/utils.rs b/src/utils.rs index 0cfc390..54ac3bf 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -18,5 +18,7 @@ pub fn restore() { const DEFAULT_COVER_RAW: &'static [u8] = include_bytes!("cover_default.png"); pub fn default_cover() -> DynamicImage { - image::load_from_memory(DEFAULT_COVER_RAW).unwrap() + image::load_from_memory(DEFAULT_COVER_RAW) + .unwrap() + .thumbnail(200, 200) }