Handle cover updates

This commit is contained in:
Muaz Ahmad 2024-11-29 12:19:52 +05:00
parent ee4e024d31
commit 120d991360
4 changed files with 24 additions and 5 deletions

View file

@ -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) { pub fn update_metadata(&mut self, song: Song) {
self.name = song.title; self.name = song.title;
self.artist = song.artist; self.artist = song.artist;

View file

@ -1,10 +1,11 @@
use std::{sync::mpsc::TryRecvError, thread, time::Duration}; use std::{sync::mpsc::TryRecvError, thread, time::Duration};
use crossterm::event::{poll, read, Event, KeyCode}; use crossterm::event::{poll, read, Event, KeyCode};
use image::DynamicImage;
use crate::{ use crate::{
ssonic::{response::Song, APIEvent}, ssonic::{response::Song, APIEvent},
utils::Error, utils::{default_cover, Error},
}; };
use super::{errors::PlayerError, Player, PlayerEvent}; use super::{errors::PlayerError, Player, PlayerEvent};
@ -42,6 +43,7 @@ impl Player {
PlayerEvent::UserQuit => return Err(Box::new(PlayerError::UserQuit)), PlayerEvent::UserQuit => return Err(Box::new(PlayerError::UserQuit)),
PlayerEvent::AddSongList(list) => self.playlist.append(list), PlayerEvent::AddSongList(list) => self.playlist.append(list),
PlayerEvent::PlayNext => self.play_next()?, PlayerEvent::PlayNext => self.play_next()?,
PlayerEvent::UpdateCover(cover) => self.tui_root.update_cover(cover),
_ => unimplemented!(), _ => unimplemented!(),
} }
} }
@ -73,6 +75,9 @@ impl Player {
}; };
if let Some(cover_id) = song.cover_art.clone() { if let Some(cover_id) = song.cover_art.clone() {
self.api_chan.send(APIEvent::FetchCoverArt(cover_id))?; 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); self.tui_root.metadata.update_metadata(song);

View file

@ -1,3 +1,4 @@
use image::DynamicImage;
use layout::Flex; use layout::Flex;
use ratatui::{ use ratatui::{
layout::{Constraint, Layout}, layout::{Constraint, Layout},
@ -45,7 +46,8 @@ impl Root {
}) })
} }
fn render_info(&mut self, area: Rect, buf: &mut Buffer) { 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); let [cover, track_info] = layout_info.areas(area);
self.render_cover(cover, buf); self.render_cover(cover, buf);
self.render_track_info(track_info, buf); self.render_track_info(track_info, buf);
@ -56,8 +58,8 @@ impl Root {
StatefulImage::new(None).render(area, buf, &mut self.image_state); StatefulImage::new(None).render(area, buf, &mut self.image_state);
} }
fn render_track_info(&self, area: Rect, buf: &mut Buffer) { fn render_track_info(&self, area: Rect, buf: &mut Buffer) {
let layout_track = Layout::vertical([Constraint::Min(20), Constraint::Min(10)]); let layout_track = Layout::vertical([Constraint::Max(2), Constraint::Max(1)]);
let [title, artist, _] = layout_track.areas(area); let [title, artist] = layout_track.areas(area);
self.render_title(title, buf); self.render_title(title, buf);
self.render_artist(artist, buf); self.render_artist(artist, buf);
} }
@ -72,6 +74,12 @@ impl Root {
Paragraph::new(self.metadata.artist.clone().unwrap()).render(area, buf); 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 { impl Player {

View file

@ -18,5 +18,7 @@ pub fn restore() {
const DEFAULT_COVER_RAW: &'static [u8] = include_bytes!("cover_default.png"); const DEFAULT_COVER_RAW: &'static [u8] = include_bytes!("cover_default.png");
pub fn default_cover() -> DynamicImage { pub fn default_cover() -> DynamicImage {
image::load_from_memory(DEFAULT_COVER_RAW).unwrap() image::load_from_memory(DEFAULT_COVER_RAW)
.unwrap()
.thumbnail(200, 200)
} }