gpui: Add ImageCache (#27774)
Closes #27414 `ImageCache` is independent of the original image loader and can actively release its cached images to solve the problem of images loaded from the network or files not being released. It has two constructors: - `ImageCache::new`: Manually manage the cache. - `ImageCache::max_items`: Remove the least recently used items when the cache reaches the specified number. When creating an `img` element, you can specify the cache object with `Img::cache`, and the image cache will be managed by `ImageCache`. In the example `crates\gpui\examples\image-gallery.rs`, the `ImageCache::clear` method is actively called when switching a set of images, and the memory will no longer continuously increase. Release Notes: - N/A --------- Co-authored-by: Ben Kunkle <ben@zed.dev>
This commit is contained in:
parent
a50fbc9b5c
commit
abf2b9d7d3
7 changed files with 500 additions and 11 deletions
|
@ -1,5 +1,5 @@
|
|||
use crate::{
|
||||
Action, AnyDrag, AnyElement, AnyTooltip, AnyView, App, AppContext, Arena, Asset,
|
||||
Action, AnyDrag, AnyElement, AnyImageCache, AnyTooltip, AnyView, App, AppContext, Arena, Asset,
|
||||
AsyncWindowContext, AvailableSpace, Background, BorderStyle, Bounds, BoxShadow, Context,
|
||||
Corners, CursorStyle, Decorations, DevicePixels, DispatchActionListener, DispatchNodeId,
|
||||
DispatchTree, DisplayId, Edges, Effect, Entity, EntityId, EventEmitter, FileDropEvent, FontId,
|
||||
|
@ -617,6 +617,7 @@ pub struct Window {
|
|||
pub(crate) element_opacity: Option<f32>,
|
||||
pub(crate) content_mask_stack: Vec<ContentMask<Pixels>>,
|
||||
pub(crate) requested_autoscroll: Option<Bounds<Pixels>>,
|
||||
pub(crate) image_cache_stack: Vec<AnyImageCache>,
|
||||
pub(crate) rendered_frame: Frame,
|
||||
pub(crate) next_frame: Frame,
|
||||
pub(crate) next_hitbox_id: HitboxId,
|
||||
|
@ -933,6 +934,7 @@ impl Window {
|
|||
pending_input_observers: SubscriberSet::new(),
|
||||
prompt: None,
|
||||
client_inset: None,
|
||||
image_cache_stack: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -2857,6 +2859,17 @@ impl Window {
|
|||
result
|
||||
}
|
||||
|
||||
/// Executes the provided function with the specified image cache.
|
||||
pub(crate) fn with_image_cache<F, R>(&mut self, image_cache: AnyImageCache, f: F) -> R
|
||||
where
|
||||
F: FnOnce(&mut Self) -> R,
|
||||
{
|
||||
self.image_cache_stack.push(image_cache);
|
||||
let result = f(self);
|
||||
self.image_cache_stack.pop();
|
||||
result
|
||||
}
|
||||
|
||||
/// Sets an input handler, such as [`ElementInputHandler`][element_input_handler], which interfaces with the
|
||||
/// platform to receive textual input with proper integration with concerns such
|
||||
/// as IME interactions. This handler will be active for the upcoming frame until the following frame is
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue