diff --git a/yazi-adaptor/src/adaptor.rs b/yazi-adaptor/src/adaptor.rs index e6d2674d..1d721641 100644 --- a/yazi-adaptor/src/adaptor.rs +++ b/yazi-adaptor/src/adaptor.rs @@ -1,4 +1,4 @@ -use std::{env, path::Path, sync::Arc}; +use std::{env, path::Path, sync::{atomic::Ordering, Arc}}; use anyhow::{anyhow, Result}; use ratatui::prelude::Rect; @@ -160,25 +160,13 @@ impl Adaptor { pub(super) fn start(self) { Ueberzug::start(self); } pub async fn image_show(self, path: &Path, rect: Rect) -> Result<(u32, u32)> { - let size = match self { + match self { Self::Kitty => Kitty::image_show(path, rect).await, Self::KittyOld => KittyOld::image_show(path, rect).await, Self::Iterm2 => Iterm2::image_show(path, rect).await, Self::Sixel => Sixel::image_show(path, rect).await, _ => Ueberzug::image_show(path, rect).await, - }?; - - let shown = Term::ratio() - .map(|(r1, r2)| Rect { - x: rect.x, - y: rect.y, - width: (size.0 as f64 / r1).ceil() as u16, - height: (size.1 as f64 / r2).ceil() as u16, - }) - .unwrap_or(rect); - - SHOWN.store(Some(Arc::new(shown))); - Ok(size) + } } pub fn image_hide(self) -> Result<()> { @@ -195,6 +183,20 @@ impl Adaptor { } } + #[inline] + pub(super) fn shown_store(rect: Rect, size: (u32, u32)) { + SHOWN.store(Some(Arc::new( + Term::ratio() + .map(|(r1, r2)| Rect { + x: rect.x, + y: rect.y, + width: (size.0 as f64 / r1).ceil() as u16, + height: (size.1 as f64 / r2).ceil() as u16, + }) + .unwrap_or(rect), + ))); + } + #[inline] pub(super) fn needs_ueberzug(self) -> bool { !matches!(self, Self::Kitty | Self::KittyOld | Self::Iterm2 | Self::Sixel) diff --git a/yazi-adaptor/src/iterm2.rs b/yazi-adaptor/src/iterm2.rs index 4f38e54e..9dd60c0a 100644 --- a/yazi-adaptor/src/iterm2.rs +++ b/yazi-adaptor/src/iterm2.rs @@ -18,6 +18,7 @@ impl Iterm2 { let b = Self::encode(img).await?; Adaptor::Iterm2.image_hide()?; + Adaptor::shown_store(rect, size); Term::move_lock(stdout().lock(), (rect.x, rect.y), |stdout| { stdout.write_all(&b)?; Ok(size) diff --git a/yazi-adaptor/src/kitty.rs b/yazi-adaptor/src/kitty.rs index 66cb4e4b..a799739d 100644 --- a/yazi-adaptor/src/kitty.rs +++ b/yazi-adaptor/src/kitty.rs @@ -318,6 +318,7 @@ impl Kitty { let b = Self::encode(img).await?; Adaptor::Kitty.image_hide()?; + Adaptor::shown_store(rect, size); Term::move_lock(stdout().lock(), (rect.x, rect.y), |stdout| { stdout.write_all(&b)?; diff --git a/yazi-adaptor/src/kitty_old.rs b/yazi-adaptor/src/kitty_old.rs index e2861c3b..96e1cf7d 100644 --- a/yazi-adaptor/src/kitty_old.rs +++ b/yazi-adaptor/src/kitty_old.rs @@ -18,6 +18,7 @@ impl KittyOld { let b = Self::encode(img).await?; Adaptor::KittyOld.image_hide()?; + Adaptor::shown_store(rect, size); Term::move_lock(stdout().lock(), (rect.x, rect.y), |stdout| { stdout.write_all(&b)?; Ok(size) diff --git a/yazi-adaptor/src/sixel.rs b/yazi-adaptor/src/sixel.rs index c14591f1..86ca0ffd 100644 --- a/yazi-adaptor/src/sixel.rs +++ b/yazi-adaptor/src/sixel.rs @@ -17,6 +17,7 @@ impl Sixel { let b = Self::encode(img).await?; Adaptor::Sixel.image_hide()?; + Adaptor::shown_store(rect, size); Term::move_lock(stdout().lock(), (rect.x, rect.y), |stdout| { stdout.write_all(&b)?; Ok(size) diff --git a/yazi-adaptor/src/ueberzug.rs b/yazi-adaptor/src/ueberzug.rs index 669b0294..c491e441 100644 --- a/yazi-adaptor/src/ueberzug.rs +++ b/yazi-adaptor/src/ueberzug.rs @@ -44,6 +44,7 @@ impl Ueberzug { pub(super) async fn image_show(path: &Path, rect: Rect) -> Result<(u32, u32)> { if let Some(tx) = &*DEMON { tx.send(Some((path.to_path_buf(), rect)))?; + Adaptor::shown_store(rect, (0, 0)); } else { bail!("uninitialized ueberzug"); } diff --git a/yazi-core/src/manager/tabs.rs b/yazi-core/src/manager/tabs.rs index cb0e3d6d..410699ca 100644 --- a/yazi-core/src/manager/tabs.rs +++ b/yazi-core/src/manager/tabs.rs @@ -34,8 +34,10 @@ impl Tabs { return; } - // We must reset it before changing the tab index. - self.active_mut().preview.reset_image(); + // Reset the preview of the previous active tab + if let Some(active) = self.items.get_mut(self.idx) { + active.preview.reset_image(); + } self.idx = idx; Manager::_refresh();