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 <noreply@anthropic.com>
This commit is contained in:
Hakan Seven 2026-07-21 10:06:55 +03:00
commit c938d56ac6
2 changed files with 48 additions and 14 deletions

View file

@ -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<crate::app::Message> {
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<crate::app::Message> {
let missing: Vec<std::path::PathBuf> = 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,
)
}
}