From 78c1225e58ef184cd78a4d236b31f36575181721 Mon Sep 17 00:00:00 2001 From: mrk Date: Mon, 30 Dec 2024 15:47:46 +0100 Subject: [PATCH] fix iterm2 sizing issue and image not switching on song change --- src/ui/image/facade.rs | 11 +++++------ src/ui/image/iterm2.rs | 30 ++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/ui/image/facade.rs b/src/ui/image/facade.rs index 7b0e8bf..d2ba129 100644 --- a/src/ui/image/facade.rs +++ b/src/ui/image/facade.rs @@ -55,11 +55,11 @@ impl AlbumArtFacade { pub fn show_default(&mut self) -> Result<()> { self.current_album_art = None; - - let data = Arc::clone(&self.default_album_art); IS_SHOWING.store(true, Ordering::Relaxed); + let data = Arc::clone(&self.default_album_art); log::debug!(bytes = data.len(), area:? = self.last_size; "Displaying default image"); + match &mut self.image_state { ImageState::Kitty(kitty) => kitty.show(data, self.last_size), ImageState::Ueberzug(ueberzug) => ueberzug.show(data, self.last_size), @@ -75,10 +75,11 @@ impl AlbumArtFacade { return Ok(()); }; - let data = Arc::clone(current_album_art); IS_SHOWING.store(true, Ordering::Relaxed); + let data = Arc::clone(current_album_art); log::debug!(bytes = data.len(), area:? = self.last_size; "Displaying current image again",); + match &mut self.image_state { ImageState::Kitty(kitty) => kitty.show(data, self.last_size), ImageState::Ueberzug(ueberzug) => ueberzug.show(data, self.last_size), @@ -89,9 +90,7 @@ impl AlbumArtFacade { } pub fn show(&mut self, data: Vec) -> Result<()> { - if IS_SHOWING.swap(true, Ordering::Relaxed) { - return Ok(()); - } + IS_SHOWING.store(true, Ordering::Relaxed); log::debug!(bytes = data.len(), area:? = self.last_size; "New image received",); let data = Arc::new(data); diff --git a/src/ui/image/iterm2.rs b/src/ui/image/iterm2.rs index 6f934dc..a64b394 100644 --- a/src/ui/image/iterm2.rs +++ b/src/ui/image/iterm2.rs @@ -121,18 +121,27 @@ fn display(w: &mut impl Write, data: EncodedData, area: Rect) -> Result<()> { fn encode(width: u16, height: u16, data: &[u8], max_size_px: Size) -> Result { let start = std::time::Instant::now(); - let (iwidth, iheight) = match get_image_area_size_px(width, height, max_size_px) { + let (area_width, area_height) = match get_image_area_size_px(width, height, max_size_px) { Ok(v) => v, Err(err) => { bail!("Failed to get image size, err: {}", err); } }; - let (len, data) = if get_gif_frames(data)?.is_some() { + let (len, width_px, height_px, data) = if get_gif_frames(data)?.is_some() { log::debug!("encoding animated gif"); - (data.len(), base64::engine::general_purpose::STANDARD.encode(data)) + + // Take smaller of the two dimensions to make the gif stretch over available area and not overflow + let size = area_width.min(area_height).into(); + + ( + data.len(), + size, + size, + base64::engine::general_purpose::STANDARD.encode(data), + ) } else { - let image = match resize_image(data, iwidth, iheight) { + let image = match resize_image(data, area_width, area_height) { Ok(v) => v, Err(err) => { bail!("Failed to resize image, err: {}", err); @@ -141,14 +150,19 @@ fn encode(width: u16, height: u16, data: &[u8], max_size_px: Size) -> Result