Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply forget() to GIF frame. #5444

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3355,16 +3355,30 @@ impl Context {

crate::profile_function!();

if uri.is_empty() {
return;
}

let mut frame_count: usize = 0;
let loaders = self.loaders();

loaders.include.forget(uri);
for loader in loaders.bytes.lock().iter() {
loader.forget(uri);
}
for loader in loaders.image.lock().iter() {
if crate::is_gif_uri(uri) {
frame_count = loader.frame_count(uri).max(frame_count);
}
loader.forget(uri);
}
for loader in loaders.texture.lock().iter() {
if crate::is_gif_uri(uri) {
for frame_index in 0..frame_count {
let frame_uri = crate::encode_gif_uri(uri, frame_index);
loader.forget(&frame_uri);
}
}
loader.forget(uri);
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/egui/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ pub trait ImageLoader {

/// If the loader caches any data, this should return the size of that cache.
fn byte_size(&self) -> usize;

/// Returns the number of frames in the Image.
fn frame_count(&self, uri: &str) -> usize;
}

/// A texture with a known size.
Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/widgets/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ pub fn paint_texture_at(
}

/// gif uris contain the uri & the frame that will be displayed
fn encode_gif_uri(uri: &str, frame_index: usize) -> String {
pub fn encode_gif_uri(uri: &str, frame_index: usize) -> String {
format!("{uri}#{frame_index}")
}

Expand All @@ -815,7 +815,7 @@ pub fn decode_gif_uri(uri: &str) -> Result<(&str, usize), String> {
}

/// checks if uri is a gif file
fn is_gif_uri(uri: &str) -> bool {
pub fn is_gif_uri(uri: &str) -> bool {
uri.ends_with(".gif") || uri.contains(".gif#")
}

Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub use self::{
drag_value::DragValue,
hyperlink::{Hyperlink, Link},
image::{
decode_gif_uri, has_gif_magic_header, paint_texture_at, GifFrameDurations, Image, ImageFit,
ImageOptions, ImageSize, ImageSource,
decode_gif_uri, encode_gif_uri, has_gif_magic_header, is_gif_uri, paint_texture_at,
GifFrameDurations, Image, ImageFit, ImageOptions, ImageSize, ImageSource,
},
image_button::ImageButton,
label::Label,
Expand Down
9 changes: 9 additions & 0 deletions crates/egui_extras/src/loaders/gif_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,13 @@ impl ImageLoader for GifLoader {
})
.sum()
}

fn frame_count(&self, uri: &str) -> usize {
let cache = self.cache.lock();
if let Some(Ok(image)) = cache.get(uri) {
image.frames.len()
} else {
0
}
}
}
4 changes: 4 additions & 0 deletions crates/egui_extras/src/loaders/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ impl ImageLoader for ImageCrateLoader {
})
.sum()
}

fn frame_count(&self, _uri: &str) -> usize {
1
}
}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions crates/egui_extras/src/loaders/svg_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ impl ImageLoader for SvgLoader {
})
.sum()
}

fn frame_count(&self, _uri: &str) -> usize {
1
}
}

#[cfg(test)]
Expand Down
Loading