From a94251b616ede3523a07b30351db541016d3e15a Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 24 Nov 2026 13:56:11 +0000 Subject: [PATCH] Refresh Filter Gallery thumbnails for each open layer --- crates/ui-egui/src/dialogs/gallery_ui.rs | 25 ++++++-- .../ui-egui/src/dialogs/gallery_ui/tests.rs | 64 +++++++++++++++++++ 2 files changed, 83 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/dialogs/gallery_ui.rs b/crates/ui-egui/src/dialogs/gallery_ui.rs index 79de658bc8e..e7be29e6409 100644 --- a/crates/ui-egui/src/dialogs/gallery_ui.rs +++ b/crates/ui-egui/src/dialogs/gallery_ui.rs @@ -139,12 +139,14 @@ pub(crate) struct PcGallerySource { background: [f32; 4], /// The middle of the layer, about 4× the thumbnail size, sampled down to the thumbnail size. thumb_src: Surface, + /// Keeps the shared thumbnail cache from reusing pictures from an earlier Gallery. + thumb_revision: Revision, catalog: PcCatalog, preview: SharedPicture, } impl PcGallerySource { - fn new(src: Surface, canvas: Rect, target: Value, colours: [[f32; 4]; 2]) -> Self { + fn new(src: Surface, canvas: Rect, target: Value, colours: [[f32; 4]; 2], thumb_revision: Revision) -> Self { let cb = src.content_bounds().intersect(&canvas); let cb = if cb.is_empty() { canvas } else { cb }; let k = ((cb.width() as usize / (THUMB[0] * 4)).min(cb.height() as usize / (THUMB[1] * 4))).max(1) as u32; @@ -152,7 +154,17 @@ impl PcGallerySource { let (cx, cy) = ((cb.x0 + cb.x1) / 2, (cb.y0 + cb.y1) / 2); let thumb_src = sample(&src, Rect::new(cx - tw / 2, cy - th / 2, cx - tw / 2 + tw, cy - th / 2 + th), k); let preview = SharedPicture::new(PixelSize::new(canvas.width().max(1), canvas.height().max(1))); - PcGallerySource { src: Arc::new(src), canvas, target, foreground: colours[0], background: colours[1], thumb_src, catalog: PcCatalog, preview } + PcGallerySource { + src: Arc::new(src), + canvas, + target, + foreground: colours[0], + background: colours[1], + thumb_src, + thumb_revision, + catalog: PcCatalog, + preview, + } } /// The command params for `effects`: what OK runs and what the preview renders. @@ -213,9 +225,9 @@ impl FilterGallerySource for PcGallerySource { } impl ThumbSource for PcGallerySource { - /// The crop and colours are fixed while the gallery is open, so a filter's thumbnail never changes. - fn revision(&self, key: &GalleryThumb, _request: &ThumbRequest) -> Revision { - Revision::of(&key.filter) + /// All filters use the same immutable source, and each filter has its own cache key. + fn revision(&self, _key: &GalleryThumb, _request: &ThumbRequest) -> Revision { + self.thumb_revision } fn render(&self, key: &GalleryThumb, _request: &ThumbRequest) -> ThumbRender { @@ -501,7 +513,8 @@ fn open_on( session.effects = effects; } let size = PixelSize::new(canvas.width().max(1), canvas.height().max(1)); - let source = Arc::new(PcGallerySource::new(src, canvas, target, colours)); + let thumb_revision = Revision::new(app.texture_serials.mint()); + let source = Arc::new(PcGallerySource::new(src, canvas, target, colours, thumb_revision)); let workspace = FilterGalleryWorkspace::new(FilterGalleryOpen::new(size, layer_name, session)); let shared: Arc = source.clone(); diff --git a/crates/ui-egui/src/dialogs/gallery_ui/tests.rs b/crates/ui-egui/src/dialogs/gallery_ui/tests.rs index 78b949c2367..8c2cb18f109 100644 --- a/crates/ui-egui/src/dialogs/gallery_ui/tests.rs +++ b/crates/ui-egui/src/dialogs/gallery_ui/tests.rs @@ -408,3 +408,67 @@ fn glow_color_keeps_exact_floats_until_a_new_pick_and_stores_linear() { }; assert!(near(&recorded(h.state())[..3], &[0.21586, 0.05127, 1.0], 1e-3), "{:?}", recorded(h.state())); } + +type GalleryHarness = egui_kittest::Harness<'static, Box>; + +fn open_gallery(mut app: Box) -> GalleryHarness { + let ctx = app.ctx().clone(); + crate::menus::invoke(&mut app, &ctx, KEY, json!({})).unwrap(); + let mut gallery = egui_kittest::Harness::builder().with_context(ctx).with_size(egui::vec2(1500.0, 950.0)).with_pixels_per_point(1.25).build_ui_state( + |ui, app: &mut Box| { + app.framework.dialogs.run(ui); + }, + app, + ); + gallery.run_steps(4); + gallery +} + +fn painted_thumbnails(gallery: &GalleryHarness) -> Vec<[usize; 2]> { + let output = gallery.output(); + let painted = gallery.ctx.tessellate(output.shapes.clone(), output.pixels_per_point); + let drawn: std::collections::HashSet<_> = painted + .iter() + .filter_map(|item| match &item.primitive { + egui::epaint::Primitive::Mesh(mesh) => Some(mesh.texture_id), + _ => None, + }) + .collect(); + gallery + .ctx + .tex_manager() + .read() + .allocated() + .filter(|(id, meta)| drawn.contains(id) && meta.name.contains("filterGallery.thumbs")) + .map(|(_, meta)| meta.size) + .collect() +} + +#[test] +fn gallery_browser_paints_filter_thumbnails() { + let mut app = app(); + app.run("edit.fill", json!({"color": "#3366cc"})).unwrap(); + let gallery = open_gallery(app); + let artistic = GalleryFilter::ALL.iter().filter(|f| f.category() == GalleryFilter::ColoredPencil.category()).count(); + let thumbs = painted_thumbnails(&gallery); + assert_eq!(thumbs.len(), artistic, "every filter in the open Artistic category is painted"); + assert!(thumbs.iter().all(|size| size[0] > 1 && size[1] > 1)); +} + +#[test] +fn gallery_replaces_blank_thumbnails_when_reopened_on_paint() { + let mut app = app(); + app.run("layer.new.layer", json!({})).unwrap(); + let mut gallery = open_gallery(app); + let blank = painted_thumbnails(&gallery); + assert!(!blank.is_empty() && blank.iter().all(|size| *size == [1, 1]), "the first layer has no pixels"); + let ctx = gallery.ctx.clone(); + ui(gallery.state_mut(), json!({"cancel": true})).unwrap(); + take(gallery.state_mut(), &ctx); + gallery.state_mut().run("edit.fill", json!({"color": "#3366cc"})).unwrap(); + crate::menus::invoke(gallery.state_mut(), &ctx, KEY, json!({})).unwrap(); + gallery.run_steps(4); + let painted = painted_thumbnails(&gallery); + assert_eq!(painted.len(), blank.len(), "every Artistic tile has a picture"); + assert!(painted.iter().all(|size| size[0] > 1 && size[1] > 1), "reopening still paints blank previews: {painted:?}"); +}