mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
Improved texture loading (#3315)
* rework loading around `Arc<Loaders>` * use `Bytes` instead of splitting api * remove unwraps in `texture_handle` * make `FileLoader` optional under `file` feature * hide http load error stack trace from UI * implement image fit * support more image sources * center spinner if we know size ahead of time * allocate final size for spinner * improve image format guessing * remove `ui.image`, `Image`, add `RawImage` * deprecate `RetainedImage` * `image2` -> `image` * add viewer example * update `examples/image` + remove `svg` and `download_image` exapmles * fix lints and tests * fix doc link * add image controls to `images` example * add more `From` str-like types * add api to forget all images * fix max size * do not scale original size unless necessary * fix doc link * add more docs for `Image` and `RawImage` * make paint_at `pub` * update `ImageButton` to use new `Image` API * fix double rendering * `SizeHint::Original` -> `Scale` + remove `Option` wrapper * Update crates/egui/src/load.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * remove special `None` value for `forget` * Update crates/egui/src/load.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * add more examples to `ui.image` + add `include_image` macro * Update crates/egui/src/ui.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * update `menu_image_button` to use `ImageSource` * `OrderedFloat::get` -> `into_inner` * derive `Eq` on `SizedTexture` * add `id` to loaders + `is_installed` check * move `images` to demo + simplify `images` example * log trace when installing loaders * fix lint * fix doc link * add more documentation * more `egui_extras::loaders` docs * Update examples/images/src/main.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * update `images` example screenshots + readme * remove unused `rfd` from `images` example * Update crates/egui_extras/src/loaders/ehttp_loader.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * add `must_use` on `Image` and `RawImage` * document `loaders::install` multiple call safety * Update crates/egui_extras/Cargo.toml Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * reshuffle `is_loader_installed` * make `include_image` produce `ImageSource` + update docs * update `include_image` docs * remove `None` mentions from loader `forget` * inline `From` texture id + size for `SizedTexture` * add warning about statically known path * change image load error + use in image button * add `.size()` to `Image` * Update crates/egui_demo_app/Cargo.toml Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * add explanations to image viewer ui --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
@@ -1,41 +1,87 @@
|
||||
// TODO: automatic cache eviction
|
||||
|
||||
/// Installs the default set of loaders:
|
||||
/// Installs the default set of loaders.
|
||||
///
|
||||
/// - `file` loader on non-Wasm targets
|
||||
/// - `http` loader (with the `ehttp` feature)
|
||||
/// - `http` loader (with the `http` feature)
|
||||
/// - `image` loader (with the `image` feature)
|
||||
/// - `svg` loader with the `svg` feature
|
||||
///
|
||||
/// ⚠ 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://` images, enable the `all-loaders` feature.
|
||||
/// Calling this multiple times on the same [`egui::Context`] is safe.
|
||||
/// It will never install duplicate loaders.
|
||||
///
|
||||
/// ⚠ The supported set of image formats is configured by adding the [`image`](https://crates.io/crates/image)
|
||||
/// ⚠ 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:
|
||||
///
|
||||
/// ```toml,ignore
|
||||
/// image = { version = "0.24", features = ["jpeg", "png"] }
|
||||
/// ```
|
||||
///
|
||||
/// ⚠ You have to configure both the supported loaders in `egui_extras` _and_ the supported image formats
|
||||
/// in `image` to get any output!
|
||||
///
|
||||
/// ## Loader-specific information
|
||||
///
|
||||
/// ⚠ The exact way bytes, images, and textures are loaded is subject to change,
|
||||
/// but the supported protocols and file extensions are not.
|
||||
///
|
||||
/// The `file` loader is a [`BytesLoader`][`egui::load::BytesLoader`].
|
||||
/// It will attempt to load `file://` URIs, and infer the content type from the extension.
|
||||
/// The path will be passed to [`std::fs::read`] after trimming the `file://` prefix,
|
||||
/// and is resolved the same way as with `std::fs::read(path)`:
|
||||
/// - Relative paths are relative to the current working directory
|
||||
/// - Absolute paths are left as is.
|
||||
///
|
||||
/// The `http` loader is a [`BytesLoader`][`egui::load::BytesLoader`].
|
||||
/// 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.
|
||||
/// 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`],
|
||||
/// allowing a different loader to attempt to load the image.
|
||||
///
|
||||
/// The `svg` loader is an [`ImageLoader`][`egui::load::ImageLoader`].
|
||||
/// It will attempt to load any URI with an `svg` extension. It will _not_ attempt to load a URI without an extension.
|
||||
/// The content type specified by [`BytesPoll::Ready::mime`][`egui::load::BytesPoll::Ready::mime`] always takes precedence,
|
||||
/// 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) {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
ctx.add_bytes_loader(std::sync::Arc::new(self::file_loader::FileLoader::default()));
|
||||
#[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()));
|
||||
crate::log_trace!("installed FileLoader");
|
||||
}
|
||||
|
||||
#[cfg(feature = "http")]
|
||||
ctx.add_bytes_loader(std::sync::Arc::new(
|
||||
self::ehttp_loader::EhttpLoader::default(),
|
||||
));
|
||||
if !ctx.is_loader_installed(self::ehttp_loader::EhttpLoader::ID) {
|
||||
ctx.add_bytes_loader(std::sync::Arc::new(
|
||||
self::ehttp_loader::EhttpLoader::default(),
|
||||
));
|
||||
crate::log_trace!("installed EhttpLoader");
|
||||
}
|
||||
|
||||
#[cfg(feature = "image")]
|
||||
ctx.add_image_loader(std::sync::Arc::new(
|
||||
self::image_loader::ImageCrateLoader::default(),
|
||||
));
|
||||
if !ctx.is_loader_installed(self::image_loader::ImageCrateLoader::ID) {
|
||||
ctx.add_image_loader(std::sync::Arc::new(
|
||||
self::image_loader::ImageCrateLoader::default(),
|
||||
));
|
||||
crate::log_trace!("installed ImageCrateLoader");
|
||||
}
|
||||
|
||||
#[cfg(feature = "svg")]
|
||||
ctx.add_image_loader(std::sync::Arc::new(self::svg_loader::SvgLoader::default()));
|
||||
if !ctx.is_loader_installed(self::svg_loader::SvgLoader::ID) {
|
||||
ctx.add_image_loader(std::sync::Arc::new(self::svg_loader::SvgLoader::default()));
|
||||
crate::log_trace!("installed SvgLoader");
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
target_arch = "wasm32",
|
||||
any(target_arch = "wasm32", not(feature = "file")),
|
||||
not(feature = "http"),
|
||||
not(feature = "image"),
|
||||
not(feature = "svg")
|
||||
|
||||
Reference in New Issue
Block a user