Libraries panel: graphic and pattern thumbnails come from the framework's thumbnail store (Thumbnails with a ThumbSource), decoded a few a frame and kept per item and size, as the Symbols panel's are (#9232)

This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-25 06:00:27 +07:00
1 parent 8e7a6142fc
commit 9d050d2b01
2 files changed
+75 -14

No files matched your search

+46 -14
View File
@@ -30,7 +30,8 @@ use craft::ui::menu::{Menu, MenuItem, PanelRow};
use craft::ui::notices::{Notice, NoticeAction, NoticeLayout};
use craft::ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind};
use craft::ui::pickers::Select;
use craft::ui::primitives::{ContentPainterCx, IconSpec, Revision, TextureStore, TextureStoreOptions, fit_rect};
use craft::ui::primitives::textures::{Priority, Revision, ThumbPaint, ThumbRender, ThumbRequest, ThumbSource, ThumbnailOptions, Thumbnails};
use craft::ui::primitives::{ContentPainterCx, IconSpec};
use craft::ui::runtime::access::PopupKind;
use craft::ui::runtime::commands::{CommandState, CommandStates};
use craft::ui::runtime::memory::Remember as _;
@@ -38,7 +39,7 @@ use craft::ui::sections::{DropAction, FooterAction, FooterBar, Panel, Section, S
use craft::ui::style::DataColor;
use craft::ui::text::TextField;
use craft::ui::tile_grid::{CaptionPlace, Tile, TileGrid};
use egui::{Color32, ColorImage, CornerRadius, Pos2, Rect, Response, Shape, Ui, pos2};
use egui::{Color32, ColorImage, CornerRadius, Rect, Response, Shape, Ui};
use serde_json::{Value, json};
use vectorcraft_color::{GradientPaint, Paint};
use vectorcraft_doc::resample::{Resample, fitted};
@@ -894,26 +895,49 @@ fn paint_gradient(cx: &ContentPainterCx<'_>, rect: Rect, paint: &GradientPaint)
cx.ramp(rect, CornerRadius::ZERO, &ramp, Axis::X);
}
/// The PNG thumbnail `png` (base64) of a graphic or pattern on white in `rect`, stored as a
/// texture under `key` (the library and the item's kind and key) at the size it shows.
// PLAN: a tile's and a row's thumbnail take a painter, not a component, so the picture goes through `Picture`'s own store here rather than `Picture::new`.
/// The largest side of a graphic or pattern thumbnail, in pixels.
const MAX_THUMB_PX: u32 = 256;
/// A graphic's or pattern's thumbnail: the library, the item's kind and key, and the side in pixels
/// it is asked at, so a tile and a row of the same item each keep their own picture.
type GraphicThumb = (String, String, u32);
/// Makes the picture of one item's PNG thumbnail `png` (base64).
struct GraphicPicture<'a> {
png: &'a str,
}
impl ThumbSource<GraphicThumb> for GraphicPicture<'_> {
/// A changed item has another thumbnail length.
fn revision(&self, _: &GraphicThumb, _: &ThumbRequest) -> Revision {
Revision::of(&self.png.len())
}
/// Decoded on the UI thread, which the store limits to a few decodes a pass.
fn render(&self, _: &GraphicThumb, request: &ThumbRequest) -> ThumbRender {
ThumbRender::Ready(thumbnail_image(self.png, request.side))
}
}
/// The PNG thumbnail `png` (base64) of a graphic or pattern on white in `rect`, from the panel's
/// store of thumbnails under `key` (the library and the item's kind and key) at the size it shows.
fn paint_graphic(cx: &ContentPainterCx<'_>, rect: Rect, key: &(String, String), png: &str) {
let ctx = cx.painter.ctx().clone();
// As many pixels as the tile shows.
let px = rect.width().max(rect.height()) * cx.painter.pixels_per_point();
let px = if px.is_finite() { px.ceil().clamp(8.0, 256.0) as u32 } else { 256 };
let store = TextureStore::<(String, String, u32)>::in_context(&ctx, "library-thumbnails", TextureStoreOptions::thumbnails());
// A changed item has another thumbnail length.
let texture = store.get_or_put(&ctx, (key.0.clone(), key.1.clone(), px), Revision::of(&png.len()), || thumbnail_image(png, px));
let ctx = cx.painter.ctx();
let thumbs = Thumbnails::<GraphicThumb>::in_context(ctx, "library-thumbnails", ThumbnailOptions::new().max_side(MAX_THUMB_PX));
let request = ThumbRequest::for_cell(rect.width().max(rect.height()), cx.pixels_per_point());
let key = (key.0.clone(), key.1.clone(), request.side.min(MAX_THUMB_PX));
let state = thumbs.state(ctx, &GraphicPicture { png }, &key, &request, Priority::VISIBLE);
// The whole cell is white, around a picture of another shape too.
cx.data_shapes([Shape::rect_filled(rect, 0.0, Color32::WHITE)]);
let whole = Rect::from_min_max(Pos2::ZERO, pos2(1.0, 1.0));
cx.picture(texture.id, fit_rect(rect, texture.size_vec2()), whole, 0.0);
thumbs.paint(cx, rect, &state, &ThumbPaint::default().paper(DataColor::new(Color32::WHITE)));
}
/// The PNG thumbnail `png` (base64) at most `px` pixels a side, shrunk smoothly: the GPU draws it
/// without mipmaps, so a texture much larger than its tile would break thin strokes up. One clear
/// pixel when it does not decode.
fn thumbnail_image(png: &str, px: u32) -> ColorImage {
#[cfg(test)]
counters::DECODED.with(|c| c.set(c.get() + 1));
vectorcraft_format::base64_decode(png)
.and_then(|png| craft_image_codecs::decode_as(craft_image_codecs::Format::Png, &png).ok())
.and_then(|img| image::RgbaImage::from_raw(img.width(), img.height(), img.to_rgba8()).map(image::DynamicImage::ImageRgba8))
@@ -931,6 +955,14 @@ fn shrunk_thumbnail(img: image::DynamicImage, px: u32) -> image::RgbaImage {
img.into_rgba8()
}
/// Graphic and pattern thumbnails decoded on this test thread.
#[cfg(test)]
pub(crate) mod counters {
thread_local! {
pub(crate) static DECODED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
}
/// Import a library file (`library.import`'s params), saying so in the status bar.
pub(crate) fn import(app: &mut VectorcraftApp, p: Value) -> Result<Value, String> {
let r = app.run("library.import", p)?;
@@ -526,3 +526,32 @@ fn a_shrunk_thumbnail_keeps_its_color_beside_clear_pixels() {
assert!(p[3] > 0 && [200, 30, 30].iter().zip(p.0).all(|(&want, c)| m(c, p[3]).abs_diff(m(want, p[3])) <= 1), "{p:?}");
}
}
/// Graphic thumbnails decoded while the panel draws one frame.
fn decoded(panel: &mut Harness) -> usize {
super::counters::DECODED.with(|c| c.set(0));
panel.frame();
super::counters::DECODED.with(std::cell::Cell::get)
}
/// A library of many graphics decodes their thumbnails at most 16 a frame, through the framework's
/// thumbnail store, and decodes none again once they show.
#[test]
fn graphic_thumbnails_decode_a_few_a_frame_and_once() {
let mut app = app();
app.run("library.create", json!({"name": "Brand"})).unwrap();
for i in 0..60 {
art(&mut app, f64::from(i), Some(&format!("Mark {i}")));
}
// The harness draws two frames as it opens.
super::counters::DECODED.with(|c| c.set(0));
let mut panel = Harness::new(app, show);
let opening = super::counters::DECODED.with(std::cell::Cell::get);
assert!(opening > 0 && opening <= 32, "{opening} decoded in the first two frames");
let mut later = 0;
for _ in 0..8 {
later += decoded(&mut panel);
}
assert!(later > 0, "the rest decode in later frames");
assert_eq!(decoded(&mut panel), 0, "a shown thumbnail is not decoded again");
}