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),