mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 22:00:03 -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,3 +1,5 @@
|
||||
#![allow(deprecated)]
|
||||
|
||||
use egui::{mutex::Mutex, TextureFilter, TextureOptions};
|
||||
|
||||
#[cfg(feature = "svg")]
|
||||
@@ -8,6 +10,9 @@ pub use usvg::FitTo;
|
||||
/// Load once, and save somewhere in your app state.
|
||||
///
|
||||
/// Use the `svg` and `image` features to enable more constructors.
|
||||
///
|
||||
/// ⚠ This type is deprecated: Consider using [`egui::Image`] instead.
|
||||
#[deprecated = "consider using `egui::Image` instead"]
|
||||
pub struct RetainedImage {
|
||||
debug_name: String,
|
||||
|
||||
@@ -186,7 +191,7 @@ impl RetainedImage {
|
||||
// We need to convert the SVG to a texture to display it:
|
||||
// Future improvement: tell backend to do mip-mapping of the image to
|
||||
// make it look smoother when downsized.
|
||||
ui.image(self.texture_id(ui.ctx()), desired_size)
|
||||
ui.raw_image((self.texture_id(ui.ctx()), desired_size))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ mod table;
|
||||
#[cfg(feature = "chrono")]
|
||||
pub use crate::datepicker::DatePickerButton;
|
||||
|
||||
#[allow(deprecated)]
|
||||
pub use crate::image::RetainedImage;
|
||||
pub(crate) use crate::layout::StripLayout;
|
||||
pub use crate::sizing::Size;
|
||||
@@ -63,6 +64,7 @@ pub(crate) use profiling_scopes::*;
|
||||
|
||||
/// Log an error with either `log` or `eprintln`
|
||||
macro_rules! log_err {
|
||||
($fmt: literal) => {$crate::log_err!($fmt,)};
|
||||
($fmt: literal, $($arg: tt)*) => {{
|
||||
#[cfg(feature = "log")]
|
||||
log::error!($fmt, $($arg)*);
|
||||
@@ -77,6 +79,7 @@ pub(crate) use log_err;
|
||||
|
||||
/// Panic in debug builds, log otherwise.
|
||||
macro_rules! log_or_panic {
|
||||
($fmt: literal) => {$crate::log_or_panic!($fmt,)};
|
||||
($fmt: literal, $($arg: tt)*) => {{
|
||||
if cfg!(debug_assertions) {
|
||||
panic!($fmt, $($arg)*);
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -5,52 +5,60 @@ use egui::{
|
||||
};
|
||||
use std::{sync::Arc, task::Poll};
|
||||
|
||||
type Entry = Poll<Result<Arc<[u8]>, String>>;
|
||||
#[derive(Clone)]
|
||||
struct File {
|
||||
bytes: Arc<[u8]>,
|
||||
mime: Option<String>,
|
||||
}
|
||||
|
||||
impl File {
|
||||
fn from_response(uri: &str, response: ehttp::Response) -> Result<Self, String> {
|
||||
if !response.ok {
|
||||
match response.text() {
|
||||
Some(response_text) => {
|
||||
return Err(format!(
|
||||
"failed to load {uri:?}: {} {} {response_text}",
|
||||
response.status, response.status_text
|
||||
))
|
||||
}
|
||||
None => {
|
||||
return Err(format!(
|
||||
"failed to load {uri:?}: {} {}",
|
||||
response.status, response.status_text
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mime = response.content_type().map(|v| v.to_owned());
|
||||
let bytes = response.bytes.into();
|
||||
|
||||
Ok(File { bytes, mime })
|
||||
}
|
||||
}
|
||||
|
||||
type Entry = Poll<Result<File, String>>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct EhttpLoader {
|
||||
cache: Arc<Mutex<HashMap<String, Entry>>>,
|
||||
}
|
||||
|
||||
impl EhttpLoader {
|
||||
pub const ID: &str = egui::generate_loader_id!(EhttpLoader);
|
||||
}
|
||||
|
||||
const PROTOCOLS: &[&str] = &["http://", "https://"];
|
||||
|
||||
fn starts_with_one_of(s: &str, prefixes: &[&str]) -> bool {
|
||||
prefixes.iter().any(|prefix| s.starts_with(prefix))
|
||||
}
|
||||
|
||||
fn get_image_bytes(
|
||||
uri: &str,
|
||||
response: Result<ehttp::Response, String>,
|
||||
) -> Result<Arc<[u8]>, String> {
|
||||
let response = response?;
|
||||
if !response.ok {
|
||||
match response.text() {
|
||||
Some(response_text) => {
|
||||
return Err(format!(
|
||||
"failed to load {uri:?}: {} {} {response_text}",
|
||||
response.status, response.status_text
|
||||
))
|
||||
}
|
||||
None => {
|
||||
return Err(format!(
|
||||
"failed to load {uri:?}: {} {}",
|
||||
response.status, response.status_text
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let Some(content_type) = response.content_type() else {
|
||||
return Err(format!("failed to load {uri:?}: no content-type header found"));
|
||||
};
|
||||
if !content_type.starts_with("image/") {
|
||||
return Err(format!("failed to load {uri:?}: expected content-type starting with \"image/\", found {content_type:?}"));
|
||||
}
|
||||
|
||||
Ok(response.bytes.into())
|
||||
}
|
||||
|
||||
impl BytesLoader for EhttpLoader {
|
||||
fn id(&self) -> &str {
|
||||
Self::ID
|
||||
}
|
||||
|
||||
fn load(&self, ctx: &egui::Context, uri: &str) -> BytesLoadResult {
|
||||
if !starts_with_one_of(uri, PROTOCOLS) {
|
||||
return Err(LoadError::NotSupported);
|
||||
@@ -59,9 +67,10 @@ impl BytesLoader for EhttpLoader {
|
||||
let mut cache = self.cache.lock();
|
||||
if let Some(entry) = cache.get(uri).cloned() {
|
||||
match entry {
|
||||
Poll::Ready(Ok(bytes)) => Ok(BytesPoll::Ready {
|
||||
Poll::Ready(Ok(file)) => Ok(BytesPoll::Ready {
|
||||
size: None,
|
||||
bytes: Bytes::Shared(bytes),
|
||||
bytes: Bytes::Shared(file.bytes),
|
||||
mime: file.mime,
|
||||
}),
|
||||
Poll::Ready(Err(err)) => Err(LoadError::Custom(err)),
|
||||
Poll::Pending => Ok(BytesPoll::Pending { size: None }),
|
||||
@@ -77,7 +86,14 @@ impl BytesLoader for EhttpLoader {
|
||||
let ctx = ctx.clone();
|
||||
let cache = self.cache.clone();
|
||||
move |response| {
|
||||
let result = get_image_bytes(&uri, response);
|
||||
let result = match response {
|
||||
Ok(response) => File::from_response(&uri, response),
|
||||
Err(err) => {
|
||||
// Log details; return summary
|
||||
crate::log_err!("Failed to load {uri:?}: {err}");
|
||||
Err(format!("Failed to load {uri:?}"))
|
||||
}
|
||||
};
|
||||
crate::log_trace!("finished loading {uri:?}");
|
||||
let prev = cache.lock().insert(uri, Poll::Ready(result));
|
||||
assert!(matches!(prev, Some(Poll::Pending)));
|
||||
@@ -93,12 +109,18 @@ impl BytesLoader for EhttpLoader {
|
||||
let _ = self.cache.lock().remove(uri);
|
||||
}
|
||||
|
||||
fn forget_all(&self) {
|
||||
self.cache.lock().clear();
|
||||
}
|
||||
|
||||
fn byte_size(&self) -> usize {
|
||||
self.cache
|
||||
.lock()
|
||||
.values()
|
||||
.map(|entry| match entry {
|
||||
Poll::Ready(Ok(bytes)) => bytes.len(),
|
||||
Poll::Ready(Ok(file)) => {
|
||||
file.bytes.len() + file.mime.as_ref().map_or(0, |m| m.len())
|
||||
}
|
||||
Poll::Ready(Err(err)) => err.len(),
|
||||
_ => 0,
|
||||
})
|
||||
|
||||
@@ -5,7 +5,13 @@ use egui::{
|
||||
};
|
||||
use std::{sync::Arc, task::Poll, thread};
|
||||
|
||||
type Entry = Poll<Result<Arc<[u8]>, String>>;
|
||||
#[derive(Clone)]
|
||||
struct File {
|
||||
bytes: Arc<[u8]>,
|
||||
mime: Option<String>,
|
||||
}
|
||||
|
||||
type Entry = Poll<Result<File, String>>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FileLoader {
|
||||
@@ -13,9 +19,17 @@ pub struct FileLoader {
|
||||
cache: Arc<Mutex<HashMap<String, Entry>>>,
|
||||
}
|
||||
|
||||
impl FileLoader {
|
||||
pub const ID: &str = egui::generate_loader_id!(FileLoader);
|
||||
}
|
||||
|
||||
const PROTOCOL: &str = "file://";
|
||||
|
||||
impl BytesLoader for FileLoader {
|
||||
fn id(&self) -> &str {
|
||||
Self::ID
|
||||
}
|
||||
|
||||
fn load(&self, ctx: &egui::Context, uri: &str) -> BytesLoadResult {
|
||||
// File loader only supports the `file` protocol.
|
||||
let Some(path) = uri.strip_prefix(PROTOCOL) else {
|
||||
@@ -26,9 +40,10 @@ impl BytesLoader for FileLoader {
|
||||
if let Some(entry) = cache.get(path).cloned() {
|
||||
// `path` has either begun loading, is loaded, or has failed to load.
|
||||
match entry {
|
||||
Poll::Ready(Ok(bytes)) => Ok(BytesPoll::Ready {
|
||||
Poll::Ready(Ok(file)) => Ok(BytesPoll::Ready {
|
||||
size: None,
|
||||
bytes: Bytes::Shared(bytes),
|
||||
bytes: Bytes::Shared(file.bytes),
|
||||
mime: file.mime,
|
||||
}),
|
||||
Poll::Ready(Err(err)) => Err(LoadError::Custom(err)),
|
||||
Poll::Pending => Ok(BytesPoll::Pending { size: None }),
|
||||
@@ -51,7 +66,12 @@ impl BytesLoader for FileLoader {
|
||||
let uri = uri.to_owned();
|
||||
move || {
|
||||
let result = match std::fs::read(&path) {
|
||||
Ok(bytes) => Ok(bytes.into()),
|
||||
Ok(bytes) => Ok(File {
|
||||
bytes: bytes.into(),
|
||||
mime: mime_guess::from_path(&path)
|
||||
.first_raw()
|
||||
.map(|v| v.to_owned()),
|
||||
}),
|
||||
Err(err) => Err(err.to_string()),
|
||||
};
|
||||
let prev = cache.lock().insert(path, Poll::Ready(result));
|
||||
@@ -70,12 +90,18 @@ impl BytesLoader for FileLoader {
|
||||
let _ = self.cache.lock().remove(uri);
|
||||
}
|
||||
|
||||
fn forget_all(&self) {
|
||||
self.cache.lock().clear();
|
||||
}
|
||||
|
||||
fn byte_size(&self) -> usize {
|
||||
self.cache
|
||||
.lock()
|
||||
.values()
|
||||
.map(|entry| match entry {
|
||||
Poll::Ready(Ok(bytes)) => bytes.len(),
|
||||
Poll::Ready(Ok(file)) => {
|
||||
file.bytes.len() + file.mime.as_ref().map_or(0, |m| m.len())
|
||||
}
|
||||
Poll::Ready(Err(err)) => err.len(),
|
||||
_ => 0,
|
||||
})
|
||||
|
||||
@@ -13,7 +13,11 @@ pub struct ImageCrateLoader {
|
||||
cache: Mutex<HashMap<String, Entry>>,
|
||||
}
|
||||
|
||||
fn is_supported(uri: &str) -> bool {
|
||||
impl ImageCrateLoader {
|
||||
pub const ID: &str = egui::generate_loader_id!(ImageCrateLoader);
|
||||
}
|
||||
|
||||
fn is_supported_uri(uri: &str) -> bool {
|
||||
let Some(ext) = Path::new(uri).extension().and_then(|ext| ext.to_str()) else {
|
||||
// `true` because if there's no extension, assume that we support it
|
||||
return true
|
||||
@@ -22,9 +26,23 @@ fn is_supported(uri: &str) -> bool {
|
||||
ext != "svg"
|
||||
}
|
||||
|
||||
fn is_unsupported_mime(mime: &str) -> bool {
|
||||
mime.contains("svg")
|
||||
}
|
||||
|
||||
impl ImageLoader for ImageCrateLoader {
|
||||
fn id(&self) -> &str {
|
||||
Self::ID
|
||||
}
|
||||
|
||||
fn load(&self, ctx: &egui::Context, uri: &str, _: SizeHint) -> ImageLoadResult {
|
||||
if !is_supported(uri) {
|
||||
// three stages of guessing if we support loading the image:
|
||||
// 1. URI extension
|
||||
// 2. Mime from `BytesPoll::Ready`
|
||||
// 3. image::guess_format
|
||||
|
||||
// (1)
|
||||
if !is_supported_uri(uri) {
|
||||
return Err(LoadError::NotSupported);
|
||||
}
|
||||
|
||||
@@ -36,7 +54,14 @@ impl ImageLoader for ImageCrateLoader {
|
||||
}
|
||||
} else {
|
||||
match ctx.try_load_bytes(uri) {
|
||||
Ok(BytesPoll::Ready { bytes, .. }) => {
|
||||
Ok(BytesPoll::Ready { bytes, mime, .. }) => {
|
||||
// (2 and 3)
|
||||
if mime.as_deref().is_some_and(is_unsupported_mime)
|
||||
|| image::guess_format(&bytes).is_err()
|
||||
{
|
||||
return Err(LoadError::NotSupported);
|
||||
}
|
||||
|
||||
crate::log_trace!("started loading {uri:?}");
|
||||
let result = crate::image::load_image_bytes(&bytes).map(Arc::new);
|
||||
crate::log_trace!("finished loading {uri:?}");
|
||||
@@ -56,6 +81,10 @@ impl ImageLoader for ImageCrateLoader {
|
||||
let _ = self.cache.lock().remove(uri);
|
||||
}
|
||||
|
||||
fn forget_all(&self) {
|
||||
self.cache.lock().clear();
|
||||
}
|
||||
|
||||
fn byte_size(&self) -> usize {
|
||||
self.cache
|
||||
.lock()
|
||||
@@ -74,11 +103,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn check_support() {
|
||||
assert!(is_supported("https://test.png"));
|
||||
assert!(is_supported("test.jpeg"));
|
||||
assert!(is_supported("http://test.gif"));
|
||||
assert!(is_supported("test.webp"));
|
||||
assert!(is_supported("file://test"));
|
||||
assert!(!is_supported("test.svg"));
|
||||
assert!(is_supported_uri("https://test.png"));
|
||||
assert!(is_supported_uri("test.jpeg"));
|
||||
assert!(is_supported_uri("http://test.gif"));
|
||||
assert!(is_supported_uri("test.webp"));
|
||||
assert!(is_supported_uri("file://test"));
|
||||
assert!(!is_supported_uri("test.svg"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,10 @@ pub struct SvgLoader {
|
||||
cache: Mutex<HashMap<(String, SizeHint), Entry>>,
|
||||
}
|
||||
|
||||
impl SvgLoader {
|
||||
pub const ID: &str = egui::generate_loader_id!(SvgLoader);
|
||||
}
|
||||
|
||||
fn is_supported(uri: &str) -> bool {
|
||||
let Some(ext) = Path::new(uri).extension().and_then(|ext| ext.to_str()) else { return false };
|
||||
|
||||
@@ -20,6 +24,10 @@ fn is_supported(uri: &str) -> bool {
|
||||
}
|
||||
|
||||
impl ImageLoader for SvgLoader {
|
||||
fn id(&self) -> &str {
|
||||
Self::ID
|
||||
}
|
||||
|
||||
fn load(&self, ctx: &egui::Context, uri: &str, size_hint: SizeHint) -> ImageLoadResult {
|
||||
if !is_supported(uri) {
|
||||
return Err(LoadError::NotSupported);
|
||||
@@ -39,7 +47,7 @@ impl ImageLoader for SvgLoader {
|
||||
Ok(BytesPoll::Ready { bytes, .. }) => {
|
||||
crate::log_trace!("started loading {uri:?}");
|
||||
let fit_to = match size_hint {
|
||||
SizeHint::Original => usvg::FitTo::Original,
|
||||
SizeHint::Scale(factor) => usvg::FitTo::Zoom(factor.into_inner()),
|
||||
SizeHint::Width(w) => usvg::FitTo::Width(w),
|
||||
SizeHint::Height(h) => usvg::FitTo::Height(h),
|
||||
SizeHint::Size(w, h) => usvg::FitTo::Size(w, h),
|
||||
@@ -63,6 +71,10 @@ impl ImageLoader for SvgLoader {
|
||||
self.cache.lock().retain(|(u, _), _| u != uri);
|
||||
}
|
||||
|
||||
fn forget_all(&self) {
|
||||
self.cache.lock().clear();
|
||||
}
|
||||
|
||||
fn byte_size(&self) -> usize {
|
||||
self.cache
|
||||
.lock()
|
||||
|
||||
Reference in New Issue
Block a user