From 0f106b591c7be48508821313a6b2a9566f8dca62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Sun, 18 Aug 2024 23:30:32 +0800 Subject: [PATCH] perf: new `image_delay` option debounces image previews to avoid lag caused by terminal image decoding during fast scrolling (#1512) --- yazi-config/preset/yazi.toml | 1 + yazi-config/src/preview/preview.rs | 4 ++++ yazi-plugin/preset/plugins/font.lua | 11 +++++------ yazi-plugin/preset/plugins/image.lua | 3 ++- yazi-plugin/preset/plugins/magick.lua | 11 +++++------ yazi-plugin/preset/plugins/pdf.lua | 11 +++++------ yazi-plugin/preset/plugins/video.lua | 11 +++++------ 7 files changed, 27 insertions(+), 25 deletions(-) diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index d21cfb07..e11aa703 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -21,6 +21,7 @@ tab_size = 2 max_width = 600 max_height = 900 cache_dir = "" +image_delay = 30 image_filter = "triangle" image_quality = 75 sixel_fraction = 15 diff --git a/yazi-config/src/preview/preview.rs b/yazi-config/src/preview/preview.rs index 6f4be1c8..901d2e54 100644 --- a/yazi-config/src/preview/preview.rs +++ b/yazi-config/src/preview/preview.rs @@ -15,6 +15,7 @@ pub struct Preview { pub cache_dir: PathBuf, + pub image_delay: u8, pub image_filter: String, pub image_quality: u8, pub sixel_fraction: u8, @@ -63,6 +64,8 @@ impl FromStr for Preview { cache_dir: Option, + #[validate(range(min = 0, max = 100))] + image_delay: u8, image_filter: String, #[validate(range(min = 50, max = 90))] image_quality: u8, @@ -87,6 +90,7 @@ impl FromStr for Preview { cache_dir, + image_delay: preview.image_delay, image_filter: preview.image_filter, image_quality: preview.image_quality, sixel_fraction: preview.sixel_fraction, diff --git a/yazi-plugin/preset/plugins/font.lua b/yazi-plugin/preset/plugins/font.lua index f94f6248..b0b648dc 100644 --- a/yazi-plugin/preset/plugins/font.lua +++ b/yazi-plugin/preset/plugins/font.lua @@ -3,15 +3,14 @@ local TEXT = "ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\nabcdefghijklm\nnopqrstuvwxyz\n123456 local M = {} function M:peek() - local cache = ya.file_cache(self) - if not cache then + local start, cache = os.clock(), ya.file_cache(self) + if not cache or self:preload() ~= 1 then return end - if self:preload() == 1 then - ya.image_show(cache, self.area) - ya.preview_widgets(self, {}) - end + ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock())) + ya.image_show(cache, self.area) + ya.preview_widgets(self, {}) end function M:seek() end diff --git a/yazi-plugin/preset/plugins/image.lua b/yazi-plugin/preset/plugins/image.lua index 1a6a7707..a5a04b41 100644 --- a/yazi-plugin/preset/plugins/image.lua +++ b/yazi-plugin/preset/plugins/image.lua @@ -1,11 +1,12 @@ local M = {} function M:peek() - local url = ya.file_cache(self) + local start, url = os.clock(), ya.file_cache(self) if not url or not fs.cha(url) then url = self.file.url end + ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock())) ya.image_show(url, self.area) ya.preview_widgets(self, {}) end diff --git a/yazi-plugin/preset/plugins/magick.lua b/yazi-plugin/preset/plugins/magick.lua index dbe1aa9d..75586c14 100644 --- a/yazi-plugin/preset/plugins/magick.lua +++ b/yazi-plugin/preset/plugins/magick.lua @@ -1,15 +1,14 @@ local M = {} function M:peek() - local cache = ya.file_cache(self) - if not cache then + local start, cache = os.clock(), ya.file_cache(self) + if not cache or self:preload() ~= 1 then return end - if self:preload() == 1 then - ya.image_show(cache, self.area) - ya.preview_widgets(self, {}) - end + ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock())) + ya.image_show(cache, self.area) + ya.preview_widgets(self, {}) end function M:seek() end diff --git a/yazi-plugin/preset/plugins/pdf.lua b/yazi-plugin/preset/plugins/pdf.lua index de4f4ef8..b8a66a84 100644 --- a/yazi-plugin/preset/plugins/pdf.lua +++ b/yazi-plugin/preset/plugins/pdf.lua @@ -1,15 +1,14 @@ local M = {} function M:peek() - local cache = ya.file_cache(self) - if not cache then + local start, cache = os.clock(), ya.file_cache(self) + if not cache or self:preload() ~= 1 then return end - if self:preload() == 1 then - ya.image_show(cache, self.area) - ya.preview_widgets(self, {}) - end + ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock())) + ya.image_show(cache, self.area) + ya.preview_widgets(self, {}) end function M:seek(units) diff --git a/yazi-plugin/preset/plugins/video.lua b/yazi-plugin/preset/plugins/video.lua index 50ea9528..3bcf15c4 100644 --- a/yazi-plugin/preset/plugins/video.lua +++ b/yazi-plugin/preset/plugins/video.lua @@ -1,15 +1,14 @@ local M = {} function M:peek() - local cache = ya.file_cache(self) - if not cache then + local start, cache = os.clock(), ya.file_cache(self) + if not cache or self:preload() ~= 1 then return end - if self:preload() == 1 then - ya.image_show(cache, self.area) - ya.preview_widgets(self, {}) - end + ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock())) + ya.image_show(cache, self.area) + ya.preview_widgets(self, {}) end function M:seek(units)