perf: clear only limited cells when hiding images (#369)

This commit is contained in:
三咲雅 · Misaki Masa 2023-11-15 08:52:56 +08:00 committed by GitHub
parent f038391ef5
commit 24e92d09e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 12 deletions

View File

@ -4,7 +4,7 @@ use ratatui::{prelude::Rect, widgets::{Block, Padding}};
use serde::{Deserialize, Serialize};
use yazi_shared::Term;
use crate::THEME;
use crate::{PREVIEW, THEME};
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(try_from = "Vec<u16>")]
@ -38,22 +38,34 @@ impl TryFrom<Vec<u16>> for ManagerLayout {
impl ManagerLayout {
pub fn preview_rect(&self) -> Rect {
let WindowSize { columns, rows, .. } = Term::size();
let (top, right, bottom, left) = THEME.manager.preview_offset;
let width = (columns * self.preview) as f64 / self.all as f64;
let width = if width.fract() > 0.5 { width.ceil() as u16 } else { width.floor() as u16 };
let w = (columns * self.preview) as f64 / self.all as f64;
let w = if w.fract() > 0.5 { w.ceil() as u16 } else { w.floor() as u16 };
let offset = THEME.manager.preview_offset;
Block::default().padding(Padding::new(offset.3, offset.1, offset.0, offset.2)).inner(Rect {
x: columns.saturating_sub(width),
y: 0,
width,
height: rows,
})
Rect {
x: left.saturating_add(columns - w),
y: top,
width: w.saturating_sub(left + right),
height: rows.saturating_sub(top + bottom),
}
}
#[inline]
pub fn preview_height(&self) -> usize { self.preview_rect().height as usize }
pub fn image_rect(&self) -> Rect {
let mut rect = self.preview_rect();
if PREVIEW.max_width == 0 || PREVIEW.max_height == 0 {
return rect;
}
if let Some((w, h)) = Term::ratio() {
rect.width = rect.width.min((PREVIEW.max_width as f64 / w).ceil() as u16);
rect.height = rect.height.min((PREVIEW.max_height as f64 / h).ceil() as u16);
}
rect
}
pub fn folder_rect(&self) -> Rect {
let WindowSize { columns, rows, .. } = Term::size();

View File

@ -153,7 +153,7 @@ impl Preview {
pub fn reset<F: FnOnce(&PreviewLock) -> bool>(&mut self, f: F) -> bool {
self.handle.take().map(|h| h.abort());
Highlighter::abort();
ADAPTOR.image_hide(MANAGER.layout.preview_rect()).ok();
ADAPTOR.image_hide(MANAGER.layout.image_rect()).ok();
let Some(ref lock) = self.lock else {
return false;

View File

@ -42,7 +42,7 @@ impl Provider {
}
pub(super) async fn image(path: &Path) -> Result<PreviewData, PeekError> {
ADAPTOR.image_show(path, MANAGER.layout.preview_rect()).await?;
ADAPTOR.image_show(path, MANAGER.layout.image_rect()).await?;
Ok(PreviewData::Image)
}