Skip to content

Commit

Permalink
fix iterm2 sizing issue and image not switching on song change
Browse files Browse the repository at this point in the history
  • Loading branch information
mierak committed Dec 30, 2024
1 parent 1b1f98e commit 78c1225
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
11 changes: 5 additions & 6 deletions src/ui/image/facade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -89,9 +90,7 @@ impl AlbumArtFacade {
}

pub fn show(&mut self, data: Vec<u8>) -> 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);
Expand Down
30 changes: 22 additions & 8 deletions src/ui/image/iterm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EncodedData> {
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);
Expand All @@ -141,14 +150,19 @@ fn encode(width: u16, height: u16, data: &[u8], max_size_px: Size) -> Result<Enc
let Ok(jpg) = jpg_encode(&image) else {
bail!("Failed to encode image as jpg")
};
(jpg.len(), base64::engine::general_purpose::STANDARD.encode(&jpg))
(
jpg.len(),
image.width(),
image.height(),
base64::engine::general_purpose::STANDARD.encode(&jpg),
)
};

log::debug!(compressed_bytes = data.len(), image_bytes = len, elapsed:? = start.elapsed(); "encoded data");
log::debug!(compressed_bytes = data.len(), image_bytes = len, elapsed:? = start.elapsed(), area_width, area_height ; "encoded data");
Ok(EncodedData {
content: data,
size: len,
width: u32::from(iwidth),
height: u32::from(iheight),
width: width_px,
height: height_px,
})
}

0 comments on commit 78c1225

Please sign in to comment.