yazi/yazi-core/src/tab/preview.rs
三咲雅 misaki masa 58f1013348
Some checks failed
Cachix / Publish Flake (push) Has been cancelled
Check / clippy (push) Has been cancelled
Check / rustfmt (push) Has been cancelled
Check / stylua (push) Has been cancelled
Draft / build-unix (gcc-aarch64-linux-gnu, ubuntu-latest, aarch64-unknown-linux-gnu) (push) Has been cancelled
Draft / build-unix (gcc-i686-linux-gnu, ubuntu-latest, i686-unknown-linux-gnu) (push) Has been cancelled
Draft / build-unix (gcc-riscv64-linux-gnu, ubuntu-latest, riscv64gc-unknown-linux-gnu) (push) Has been cancelled
Draft / build-unix (gcc-sparc64-linux-gnu, ubuntu-latest, sparc64-unknown-linux-gnu) (push) Has been cancelled
Draft / build-unix (macos-latest, aarch64-apple-darwin) (push) Has been cancelled
Draft / build-unix (macos-latest, x86_64-apple-darwin) (push) Has been cancelled
Draft / build-unix (ubuntu-latest, x86_64-unknown-linux-gnu) (push) Has been cancelled
Draft / build-windows (windows-latest, aarch64-pc-windows-msvc) (push) Has been cancelled
Draft / build-windows (windows-latest, x86_64-pc-windows-msvc) (push) Has been cancelled
Draft / build-musl (aarch64-unknown-linux-musl) (push) Has been cancelled
Draft / build-musl (x86_64-unknown-linux-musl) (push) Has been cancelled
Draft / build-snap (amd64, ubuntu-latest) (push) Has been cancelled
Draft / build-snap (arm64, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (macos-latest) (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
Test / test (windows-latest) (push) Has been cancelled
Draft / snap (push) Has been cancelled
Draft / draft (push) Has been cancelled
Draft / nightly (push) Has been cancelled
feat: trash bin (#4144)
2026-07-23 01:59:39 +08:00

70 lines
1.9 KiB
Rust

use tokio::task::JoinHandle;
use yazi_adapter::ADAPTOR;
use yazi_config::{LAYOUT, YAZI};
use yazi_fs::file::File;
use yazi_macro::render;
use yazi_runner::{RUNNER, previewer::{PeekError, PeekJob}};
use yazi_shared::{pool::Symbol, url::UrlBuf};
use crate::{AppProxy, Highlighter, MgrProxy, tab::PreviewLock};
#[derive(Default)]
pub struct Preview {
pub lock: Option<PreviewLock>,
pub skip: usize,
handle: Option<JoinHandle<()>>,
}
impl Preview {
pub fn go(&mut self, file: File, mime: Symbol<str>, force: bool) {
if mime.is_empty() {
return; // Wait till mimetype is resolved to avoid flickering
} else if !force && self.same_lock(&file, &mime) {
return;
}
let Some(previewer) = YAZI.plugin.previewers.matches(&file, &mime) else {
return self.reset();
};
self.abort();
let job = PeekJob { previewer, file, mime, skip: self.skip };
self.handle = Some(tokio::spawn(async move {
let mut rx = RUNNER.peek(&job).await;
match rx.recv().await.unwrap_or(Err(PeekError::Cancelled)) {
Ok(()) | Err(PeekError::Cancelled) => {}
Err(PeekError::ShouldSync) => AppProxy::plugin_peek(job),
Err(e) => MgrProxy::update_peeked_error(job, e.to_string()),
}
}));
}
pub fn abort(&mut self) {
self.handle.take().map(|ct| ct.abort());
Highlighter::abort();
}
pub fn reset(&mut self) {
self.abort();
ADAPTOR.image_hide().ok();
render!(self.lock.take().is_some())
}
pub fn reset_image(&mut self) {
self.abort();
ADAPTOR.image_hide().ok();
}
pub fn same_url(&self, url: &UrlBuf) -> bool { matches!(&self.lock, Some(l) if l.url == *url) }
pub fn same_file(&self, file: &File, mime: &str) -> bool {
self.same_url(&file.url)
&& matches!(&self.lock , Some(l) if l.cha.hits(file.cha) && l.mime == mime && *l.area == LAYOUT.get().preview)
}
pub fn same_lock(&self, file: &File, mime: &str) -> bool {
self.same_file(file, mime) && matches!(&self.lock, Some(l) if l.skip == self.skip)
}
}