Skip to content

Commit

Permalink
Use the more explicit egui_extras::install_image_loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Sep 14, 2023
1 parent 324787f commit 10fd9f6
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion crates/egui/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! will get you up and running quickly with its reasonable default implementations of the traits described below.
//!
//! 1. Add [`egui_extras`](https://crates.io/crates/egui_extras/) as a dependency with the `all_loaders` feature.
//! 2. Add a call to [`egui_extras::loaders::install`](https://docs.rs/egui_extras/latest/egui_extras/loaders/fn.install.html)
//! 2. Add a call to [`egui_extras::install_image_loaders`](https://docs.rs/egui_extras/latest/egui_extras/loaders/fn.install.html)
//! in your app's setup code.
//! 3. Use [`Ui::image`][`crate::ui::Ui::image`] with some [`ImageSource`][`crate::ImageSource`].
//!
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,7 @@ impl Ui {
/// Show an image available at the given `uri`.
///
/// ⚠ This will do nothing unless you install some image loaders first!
/// The easiest way to do this is via [`egui_extras::loaders::install`](https://docs.rs/egui_extras/latest/egui_extras/loaders/fn.install.html).
/// The easiest way to do this is via [`egui_extras::install_image_loaders`](https://docs.rs/egui_extras/latest/egui_extras/loaders/fn.install.html).
///
/// The loaders handle caching image data, sampled textures, etc. across frames, so calling this is immediate-mode safe.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_app/src/wrap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub struct WrapApp {

impl WrapApp {
pub fn new(_cc: &eframe::CreationContext<'_>) -> Self {
egui_extras::loaders::install(&_cc.egui_ctx);
egui_extras::install_image_loaders(&_cc.egui_ctx);

#[allow(unused_mut)]
let mut slf = Self {
Expand Down
14 changes: 8 additions & 6 deletions crates/egui_extras/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ file = ["dep:mime_guess"]
## Add support for loading images via HTTP.
http = ["dep:ehttp"]

## Add support for loading images with the [`image`](https://docs.rs/image) crate.
##
## You also need to ALSO opt-in to the image formats you want to support, like so:
## ```toml
## image = { version = "0.24", features = ["jpeg", "png"] }
## ```
image = ["dep:image"]

## Enable profiling with the [`puffin`](https://docs.rs/puffin) crate.
##
## Only enabled on native, because of the low resolution (1ms) of clocks in browsers.
Expand Down Expand Up @@ -71,12 +79,6 @@ chrono = { version = "0.4", optional = true, default-features = false, features
## Enable this when generating docs.
document-features = { version = "0.2", optional = true }

## Add support for loading images with the [`image`](https://docs.rs/image) crate.
##
## You also need to ALSO opt-in to the image formats you want to support, like so:
## ```toml
## image = { version = "0.24", features = ["jpeg", "png"] }
## ```
image = { version = "0.24", optional = true, default-features = false }

# file feature
Expand Down
2 changes: 2 additions & 0 deletions crates/egui_extras/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub use crate::sizing::Size;
pub use crate::strip::*;
pub use crate::table::*;

pub use loaders::install_image_loaders;

// ---------------------------------------------------------------------------

mod profiling_scopes {
Expand Down
23 changes: 13 additions & 10 deletions crates/egui_extras/src/loaders.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// TODO: automatic cache eviction

/// Installs the default set of loaders.
/// Installs a set of image loaders.
///
/// - `file` loader on non-Wasm targets
/// - `http` loader (with the `http` feature)
/// - `image` loader (with the `image` feature)
/// - `svg` loader with the `svg` feature
/// Calling this enables the use of [`egui::Image`] and [`egui::Ui::image`].
///
/// ⚠ This will do nothing and you won't see any images unless you also enable some feature flags on `egui_extras`:
///
/// - `file` feature: `file://` loader on non-Wasm targets
/// - `http` feature: `http(s)://` loader
/// - `image` feature: Loader of png, jpeg etc using the [`image`] crate
/// - `svg` feature: `.svg` loader
///
/// Calling this multiple times on the same [`egui::Context`] is safe.
/// It will never install duplicate loaders.
///
/// ⚠ This will do nothing and you won't see any images unless you enable some features:
///
/// - If you just want to be able to load `file://` and `http://` URIs, enable the `all_loaders` feature.
/// - The supported set of image formats is configured by adding the [`image`](https://crates.io/crates/image)
/// crate as your direct dependency, and enabling features on it:
Expand Down Expand Up @@ -40,7 +42,8 @@
/// It will attempt to load `http://` and `https://` URIs, and infer the content type from the `Content-Type` header.
///
/// The `image` loader is an [`ImageLoader`][`egui::load::ImageLoader`].
/// It will attempt to load any URI with any extension other than `svg`. It will also load any URI without an extension.
/// It will attempt to load any URI with any extension other than `svg`.
/// It will also try to load any URI without an extension.
/// The content type specified by [`BytesPoll::Ready::mime`][`egui::load::BytesPoll::Ready::mime`] always takes precedence.
/// This means that even if the URI has a `png` extension, and the `png` image format is enabled, if the content type is
/// not one of the supported and enabled image formats, the loader will return [`LoadError::NotSupported`][`egui::load::LoadError::NotSupported`],
Expand All @@ -52,7 +55,7 @@
/// and must include `svg` for it to be considered supported. For example, `image/svg+xml` would be loaded by the `svg` loader.
///
/// See [`egui::load`] for more information about how loaders work.
pub fn install(ctx: &egui::Context) {
pub fn install_image_loaders(ctx: &egui::Context) {
#[cfg(all(not(target_arch = "wasm32"), feature = "file"))]
if !ctx.is_loader_installed(self::file_loader::FileLoader::ID) {
ctx.add_bytes_loader(std::sync::Arc::new(self::file_loader::FileLoader::default()));
Expand Down Expand Up @@ -87,7 +90,7 @@ pub fn install(ctx: &egui::Context) {
not(feature = "image"),
not(feature = "svg")
))]
log::warn!("`loaders::install` was called, but no loaders are enabled");
log::warn!("`install_image_loaders` was called, but no loaders are enabled");

let _ = ctx;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/images/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() -> Result<(), eframe::Error> {
options,
Box::new(|cc| {
// The following call is needed to load images when using `ui.image` and `egui::Image`:
egui_extras::loaders::install(&cc.egui_ctx);
egui_extras::install_image_loaders(&cc.egui_ctx);
Box::<MyApp>::default()
}),
)
Expand Down

0 comments on commit 10fd9f6

Please sign in to comment.