From 9150b9342d5ced3d5fe7cdd5db415c836dc4a866 Mon Sep 17 00:00:00 2001 From: Lucas Meurer Date: Tue, 23 Sep 2025 10:07:18 +0200 Subject: [PATCH] Make individual egui_extras image loaders public (#7551) This was initially a PR to add kitdiff, but this now lives in it's own crate: https://github.com/rerun-io/kitdiff I needed to make the image loaders public, this way it's possible to compose image loaders together (which allowed me to create a image diff loader that uses two other image loaders). But you can't use the `ctx.try_load_image` since that would deadlock, so you have to store a reference to the other loader in the wrapping loader. --- crates/egui/src/load.rs | 12 +++++++++++- crates/egui_extras/src/lib.rs | 2 +- crates/egui_extras/src/loaders.rs | 16 ++++++++-------- .../loaders/{ehttp_loader.rs => http_loader.rs} | 0 4 files changed, 20 insertions(+), 10 deletions(-) rename crates/egui_extras/src/loaders/{ehttp_loader.rs => http_loader.rs} (100%) diff --git a/crates/egui/src/load.rs b/crates/egui/src/load.rs index 43aa1785..1c74e504 100644 --- a/crates/egui/src/load.rs +++ b/crates/egui/src/load.rs @@ -387,7 +387,7 @@ pub type ImageLoadResult = Result; /// An `ImageLoader` decodes raw bytes into a [`ColorImage`]. /// /// Implementations are expected to cache at least each `URI`. -pub trait ImageLoader { +pub trait ImageLoader: std::any::Any { /// Unique ID of this loader. /// /// To reduce the chance of collisions, include `module_path!()` as part of this ID. @@ -517,6 +517,16 @@ impl TexturePoll { Self::Ready { texture } => Some(texture.id), } } + + #[inline] + pub fn is_pending(&self) -> bool { + matches!(self, Self::Pending { .. }) + } + + #[inline] + pub fn is_ready(&self) -> bool { + matches!(self, Self::Ready { .. }) + } } pub type TextureLoadResult = Result; diff --git a/crates/egui_extras/src/lib.rs b/crates/egui_extras/src/lib.rs index f70ff9ff..16354c44 100644 --- a/crates/egui_extras/src/lib.rs +++ b/crates/egui_extras/src/lib.rs @@ -17,7 +17,7 @@ pub mod syntax_highlighting; #[doc(hidden)] pub mod image; mod layout; -mod loaders; +pub mod loaders; mod sizing; mod strip; mod table; diff --git a/crates/egui_extras/src/loaders.rs b/crates/egui_extras/src/loaders.rs index 03b1abfc..f7360489 100644 --- a/crates/egui_extras/src/loaders.rs +++ b/crates/egui_extras/src/loaders.rs @@ -63,9 +63,9 @@ pub fn install_image_loaders(ctx: &egui::Context) { } #[cfg(feature = "http")] - if !ctx.is_loader_installed(self::ehttp_loader::EhttpLoader::ID) { + if !ctx.is_loader_installed(self::http_loader::EhttpLoader::ID) { ctx.add_bytes_loader(std::sync::Arc::new( - self::ehttp_loader::EhttpLoader::default(), + self::http_loader::EhttpLoader::default(), )); log::trace!("installed EhttpLoader"); } @@ -108,16 +108,16 @@ pub fn install_image_loaders(ctx: &egui::Context) { } #[cfg(not(target_arch = "wasm32"))] -mod file_loader; +pub mod file_loader; #[cfg(feature = "http")] -mod ehttp_loader; +pub mod http_loader; #[cfg(feature = "gif")] -mod gif_loader; +pub mod gif_loader; #[cfg(feature = "image")] -mod image_loader; +pub mod image_loader; #[cfg(feature = "svg")] -mod svg_loader; +pub mod svg_loader; #[cfg(feature = "webp")] -mod webp_loader; +pub mod webp_loader; diff --git a/crates/egui_extras/src/loaders/ehttp_loader.rs b/crates/egui_extras/src/loaders/http_loader.rs similarity index 100% rename from crates/egui_extras/src/loaders/ehttp_loader.rs rename to crates/egui_extras/src/loaders/http_loader.rs