From c938d56ac671c8e8495891a27dad6ef31096971f Mon Sep 17 00:00:00 2001 From: Hakan Seven Date: Tue, 21 Jul 2026 10:06:55 +0300 Subject: [PATCH] fix(start): decode recent-file thumbnails off the boot path load_config synchronously parsed every recent DWG's embedded preview on the main thread before the first frame, so the Start page appeared seconds late on DWG-heavy recents. refresh_recent_thumbs now decodes the missing previews on a background thread and delivers them via RecentThumbsLoaded; the page opens immediately and thumbnails pop in as they resolve. The web build (no threads, no filesystem previews) skips the fetch. Co-Authored-By: Claude Fable 5 --- src/app/recent.rs | 55 +++++++++++++++++++++++++++++++++--------- src/app/update/file.rs | 7 +++--- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/app/recent.rs b/src/app/recent.rs index 2646a5b80..2ccb767fb 100644 --- a/src/app/recent.rs +++ b/src/app/recent.rs @@ -12,23 +12,56 @@ pub(super) const RECENT_MAX: usize = 100; pub(super) const RECENT_DEFAULT: usize = 20; impl OpenCADStudio { - /// Record a freshly opened file at the top of the recents list. - pub(super) fn push_recent(&mut self, path: PathBuf) { + /// Record a freshly opened file at the top of the recents list. Returns + /// the background task that decodes its thumbnail. + pub(super) fn push_recent(&mut self, path: PathBuf) -> iced::Task { self.recent_files.retain(|r| r != &path); self.recent_files.insert(0, path); self.recent_files.truncate(self.recent_limit); - self.refresh_recent_thumbs(); self.save_config(); + self.refresh_recent_thumbs() } - /// Decode any not-yet-cached DWG preview thumbnails for the current recents - /// (cheap preview-only reads). Cached per path; safe to call repeatedly. - /// Call when the recents list changes — never from a `view`. - pub(super) fn refresh_recent_thumbs(&mut self) { - for path in self.recent_files.clone() { - self.recent_thumbs - .entry(path.clone()) - .or_insert_with(|| crate::io::thumbnail::read_handle(&path)); + /// Decode any not-yet-cached DWG preview thumbnails for the current + /// recents on a background thread, delivering them via + /// [`Message::RecentThumbsLoaded`]. The synchronous version of this ran on + /// the boot path and stalled the first frame for as long as it took to + /// parse every recent DWG's preview — the Start page appeared seconds + /// late. Cached per path (a `None` result is cached too); safe to call + /// repeatedly. + pub(super) fn refresh_recent_thumbs(&mut self) -> iced::Task { + let missing: Vec = self + .recent_files + .iter() + .filter(|p| !self.recent_thumbs.contains_key(*p)) + .cloned() + .collect(); + if missing.is_empty() { + return iced::Task::none(); + } + // The web build has no spawnable threads and no filesystem previews — + // recents there simply show without thumbnails. + #[cfg(target_arch = "wasm32")] + { + return iced::Task::none(); + } + #[cfg(not(target_arch = "wasm32"))] + { + let (tx, rx) = iced::futures::channel::oneshot::channel(); + std::thread::spawn(move || { + let thumbs: Vec<_> = missing + .into_iter() + .map(|p| { + let h = crate::io::thumbnail::read_handle(&p); + (p, h) + }) + .collect(); + let _ = tx.send(thumbs); + }); + iced::Task::perform( + async move { rx.await.unwrap_or_default() }, + crate::app::Message::RecentThumbsLoaded, + ) } } diff --git a/src/app/update/file.rs b/src/app/update/file.rs index 36f3d4305..6e4c9d868 100644 --- a/src/app/update/file.rs +++ b/src/app/update/file.rs @@ -296,7 +296,8 @@ impl OpenCADStudio { .clamp(crate::app::recent::RECENT_MIN, crate::app::recent::RECENT_MAX); self.recent_files.truncate(self.recent_limit); self.recent_limit_input = self.recent_limit.to_string(); - self.refresh_recent_thumbs(); + // Thumbnails are decoded by a background task queued at boot + // (`refresh_recent_thumbs`) — never here on the boot path. self.statusbar_config = cfg.statusbar; self.ribbon.set_collapse_mode(cfg.ribbon.collapse); self.plot_dialog = cfg.plot; @@ -396,7 +397,7 @@ pub(super) fn on_open_file(&mut self) -> Task { caches.corrupt_dropped )); } - self.push_recent(path.clone()); + let thumbs_task = self.push_recent(path.clone()); let current_is_empty = { let t = &self.tabs[self.active_tab]; @@ -587,7 +588,7 @@ pub(super) fn on_open_file(&mut self) -> Task { self.tabs[i].dirty = false; self.tabs[i].history = crate::app::document::HistoryState::default(); self.refresh_selected_grips(); - self.drain_pending_open() + Task::batch([thumbs_task, self.drain_pending_open()]) } pub(super) fn on_wblock_save_result_some(&mut self, block_name: String, path: std::path::PathBuf) -> Task {