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:
Sunli 2025-04-23 04:30:21 +08:00 committed by GitHub
parent a50fbc9b5c
commit abf2b9d7d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 500 additions and 11 deletions

View file

@ -1518,7 +1518,7 @@ impl Image {
cx: &mut App,
) -> Option<Arc<RenderImage>> {
ImageSource::Image(self)
.use_data(window, cx)
.use_data(None, window, cx)
.and_then(|result| result.ok())
}