From 03840b1e2436280b49966d88ee73d4f6e7a8466a Mon Sep 17 00:00:00 2001 From: Nicolas Date: Wed, 20 Nov 2024 22:05:22 +0100 Subject: [PATCH] handle image loading on wasm in the old way --- .../egui_extras/src/loaders/image_loader.rs | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/crates/egui_extras/src/loaders/image_loader.rs b/crates/egui_extras/src/loaders/image_loader.rs index d26d64b1587..53b3b6391c6 100644 --- a/crates/egui_extras/src/loaders/image_loader.rs +++ b/crates/egui_extras/src/loaders/image_loader.rs @@ -72,31 +72,43 @@ impl ImageLoader for ImageCrateLoader { return Err(LoadError::NotSupported); } - let uri = uri.to_owned(); - cache.insert(uri.clone(), Poll::Pending); - drop(cache); - - // Do the image parsing on a bg thread - thread::Builder::new() - .name(format!("egui_extras::ImageLoader::load({uri:?})")) - .spawn({ - let ctx = ctx.clone(); - let cache = self.cache.clone(); - - let uri = uri.clone(); - move || { - log::trace!("ImageLoader - started loading {uri:?}"); - let result = crate::image::load_image_bytes(&bytes).map(Arc::new); - log::trace!("ImageLoader - finished loading {uri:?}"); - let prev = cache.lock().insert(uri, Poll::Ready(result)); - assert!(matches!(prev, Some(Poll::Pending))); - - ctx.request_repaint(); - } - }) - .expect("failed to spawn thread"); - - Ok(ImagePoll::Pending { size: None }) + if cfg!(target_arch = "wasm32") { + log::trace!("started loading {uri:?}"); + let result = crate::image::load_image_bytes(&bytes).map(Arc::new); + log::trace!("finished loading {uri:?}"); + cache.insert(uri.into(), std::task::Poll::Ready(result.clone())); + match result { + Ok(image) => Ok(ImagePoll::Ready { image }), + Err(err) => Err(LoadError::Loading(err)), + } + } else { + let uri = uri.to_owned(); + cache.insert(uri.clone(), Poll::Pending); + drop(cache); + + // Do the image parsing on a bg thread + thread::Builder::new() + .name(format!("egui_extras::ImageLoader::load({uri:?})")) + .spawn({ + let ctx = ctx.clone(); + let cache = self.cache.clone(); + + let uri = uri.clone(); + move || { + log::trace!("ImageLoader - started loading {uri:?}"); + let result = + crate::image::load_image_bytes(&bytes).map(Arc::new); + log::trace!("ImageLoader - finished loading {uri:?}"); + let prev = cache.lock().insert(uri, Poll::Ready(result)); + assert!(matches!(prev, Some(Poll::Pending))); + + ctx.request_repaint(); + } + }) + .expect("failed to spawn thread"); + + Ok(ImagePoll::Pending { size: None }) + } } Ok(BytesPoll::Pending { size }) => Ok(ImagePoll::Pending { size }), Err(err) => Err(err),