Skip to content

Commit

Permalink
handle image loading on wasm in the old way
Browse files Browse the repository at this point in the history
  • Loading branch information
bircni committed Nov 20, 2024
1 parent 10e496f commit 03840b1
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions crates/egui_extras/src/loaders/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 03840b1

Please sign in to comment.