mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 13:20:05 -04:00
Add ImageLoader::has_pending and wait_for_pending_images (#7030)
With kittest it was difficult to wait for images to be loaded before taking a snapshot test. This PR adds `Harness::with_wait_for_pending_images` (true by default) which will cause `Harness::run` to sleep until all images are loaded (or `HarnessBuilder::with_max_steps` is exceeded). It also adds a new ImageLoader::has_pending and BytesLoader::has_pending, which should be implemented if things are loaded / decoded asynchronously. It reverts https://github.com/emilk/egui/pull/6901 which was my previous attempt to fix this (but this didn't work since only the tested crate is compiled with cfg(test) and not it's dependencies)
This commit is contained in:
@@ -125,4 +125,8 @@ impl BytesLoader for EhttpLoader {
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn has_pending(&self) -> bool {
|
||||
self.cache.lock().values().any(|entry| entry.is_pending())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,4 +128,8 @@ impl BytesLoader for FileLoader {
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn has_pending(&self) -> bool {
|
||||
self.cache.lock().values().any(|entry| entry.is_pending())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,9 @@ use egui::{
|
||||
use image::ImageFormat;
|
||||
use std::{mem::size_of, path::Path, sync::Arc, task::Poll};
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::thread;
|
||||
|
||||
type Entry = Poll<Result<Arc<ColorImage>, String>>;
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -73,7 +76,7 @@ impl ImageLoader for ImageCrateLoader {
|
||||
return Err(LoadError::NotSupported);
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_arch = "wasm32", test)))]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[expect(clippy::unnecessary_wraps)] // needed here to match other return types
|
||||
fn load_image(
|
||||
ctx: &egui::Context,
|
||||
@@ -85,7 +88,7 @@ impl ImageLoader for ImageCrateLoader {
|
||||
cache.lock().insert(uri.clone(), Poll::Pending);
|
||||
|
||||
// Do the image parsing on a bg thread
|
||||
std::thread::Builder::new()
|
||||
thread::Builder::new()
|
||||
.name(format!("egui_extras::ImageLoader::load({uri:?})"))
|
||||
.spawn({
|
||||
let ctx = ctx.clone();
|
||||
@@ -113,8 +116,7 @@ impl ImageLoader for ImageCrateLoader {
|
||||
Ok(ImagePoll::Pending { size: None })
|
||||
}
|
||||
|
||||
// Load images on the current thread for tests, so they are less flaky
|
||||
#[cfg(any(target_arch = "wasm32", test))]
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn load_image(
|
||||
_ctx: &egui::Context,
|
||||
uri: &str,
|
||||
@@ -179,6 +181,10 @@ impl ImageLoader for ImageCrateLoader {
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn has_pending(&self) -> bool {
|
||||
self.cache.lock().values().any(|result| result.is_pending())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user