Markdown pictures: freed with their elements, glyphs whole, failures keep the source, file URLs parsed, Mermaid labels in Ely's ink
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
d5ecc5a69e
commit
ab9a14c692
12 files changed
+417
-126
No files matched your search
@@ -0,0 +1,53 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::{App, Asset, ElementId, RenderImage, Window};
|
||||
|
||||
/// An element's asset; freed on change or when it leaves.
|
||||
pub(crate) struct Held<A: Asset> {
|
||||
source: Option<A::Source>,
|
||||
image: Option<Arc<RenderImage>>,
|
||||
}
|
||||
|
||||
impl<A: Asset> Held<A> {
|
||||
fn free(&mut self, cx: &mut App) {
|
||||
if let Some(source) = self.source.take() {
|
||||
cx.remove_asset::<A>(&source);
|
||||
}
|
||||
if let Some(image) = self.image.take() {
|
||||
// The window may be drawing; free after it.
|
||||
cx.defer(move |cx| cx.drop_image(image, None));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `source` as `A` for element `id`; the previous one freed.
|
||||
pub(crate) fn hold<A: Asset>(
|
||||
id: impl Into<ElementId>,
|
||||
source: &A::Source,
|
||||
image: impl Fn(&A::Output) -> Option<Arc<RenderImage>>,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> Option<A::Output>
|
||||
where
|
||||
A::Source: PartialEq,
|
||||
{
|
||||
let held = window.use_keyed_state(id, cx, |_, cx| {
|
||||
cx.on_release(|held: &mut Held<A>, cx| held.free(cx))
|
||||
.detach();
|
||||
Held {
|
||||
source: None,
|
||||
image: None,
|
||||
}
|
||||
});
|
||||
let loaded = window.use_asset::<A>(source, cx);
|
||||
held.update(cx, |held, cx| {
|
||||
if held.source.as_ref() != Some(source) {
|
||||
held.free(cx);
|
||||
held.source = Some(source.clone());
|
||||
}
|
||||
if let Some(loaded) = &loaded {
|
||||
held.image = image(loaded);
|
||||
}
|
||||
});
|
||||
loaded
|
||||
}
|
||||
@@ -5,6 +5,7 @@ mod divider;
|
||||
mod files;
|
||||
mod focus;
|
||||
mod ghost;
|
||||
mod held;
|
||||
mod icon;
|
||||
mod image;
|
||||
mod layer;
|
||||
@@ -28,6 +29,7 @@ pub(crate) use focus::{
|
||||
};
|
||||
pub use focus::{FocusNext, FocusPrev, FocusRing, FocusScope};
|
||||
pub use ghost::DragGhost;
|
||||
pub(crate) use held::hold;
|
||||
pub use icon::{Icon, IconName};
|
||||
pub use image::Image;
|
||||
pub(crate) use image::{checked_ratio, framed};
|
||||
|
||||
Reference in new issue
Block a user