From 5da01d72dea2c42e99ba8430b65518cef24908b3 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Wed, 4 Nov 2026 21:52:02 +0100 Subject: [PATCH] =?UTF-8?q?fix(ui):=20Patch=20=E2=8C=A5=20check=20reads=20?= =?UTF-8?q?cached=20selection=20bounds;=20frame=20test=20for=20the=20hidde?= =?UTF-8?q?n=20outline=20(#11024)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review follow-up on #11115: - document_selection_mode's ⌥ "anything selected" check reads the bounds the selection outline caches (PhotocraftApp::known_bounds, same key as cached_bounds) instead of scanning the selection every frame; a changed selection is still scanned. - a_new_patch_lasso_paints_no_ants_on_the_old_selection paints the canvas mid-lasso and checks no ants sit on the replaced selection (⇧ keeps them), so removing the paint_selection_outline guard now fails a test. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/app/thumbs.rs | 9 ++++ crates/ui-egui/src/canvas/tool_feedback.rs | 23 +++++++++- crates/ui-egui/src/tools/retouch_ui/tests.rs | 44 ++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/app/thumbs.rs b/crates/ui-egui/src/app/thumbs.rs index 36cf9834f0..1af1f0b9ec 100644 --- a/crates/ui-egui/src/app/thumbs.rs +++ b/crates/ui-egui/src/app/thumbs.rs @@ -203,6 +203,15 @@ impl PhotocraftApp { b } + /// [`Self::cached_bounds`] where only a shared borrow is at hand: the cached box if `surface` + /// hasn't changed since, else a scan (not cached). + pub fn known_bounds(&self, key: u64, surface: &photocraft_raster::Surface) -> photocraft_geom::Rect { + match self.bounds_cache.get(&key) { + Some((r, b)) if *r == surface_fingerprint(surface) => *b, + _ => surface.content_bounds(), + } + } + /// The box Properties › Transform shows for `layer` (X, Y, W, H): its pixels' bounds, but a /// soft shape's path and stroke, whose Density and Feather render past them (#5846), as its /// transform frame does. diff --git a/crates/ui-egui/src/canvas/tool_feedback.rs b/crates/ui-egui/src/canvas/tool_feedback.rs index d18f16dd09..6c9c890571 100644 --- a/crates/ui-egui/src/canvas/tool_feedback.rs +++ b/crates/ui-egui/src/canvas/tool_feedback.rs @@ -91,8 +91,9 @@ pub fn selection_mode(tool: Tool, bar: u8, m: egui::Modifiers) -> &'static str { /// only draws a marquee from its centre). An options-bar Subtract or Intersect is kept (#1106). pub fn document_selection_mode(app: &PhotocraftApp, tool: Tool, m: egui::Modifiers) -> &'static str { let mode = selection_mode(tool, app.ui.selection_mode, m); - // Scanned last: the canvas asks every frame while a Patch lasso is drawn or hovered. - let selected = || app.session.active().and_then(|st| st.doc.selection.as_ref()).is_some_and(|s| !s.content_bounds().is_empty()); + // Asked last, and from the bounds the selection outline caches (same key): the canvas asks + // every frame while a Patch lasso is drawn or hovered with ⌥. + let selected = || app.session.active().is_some_and(|st| st.doc.selection.as_ref().is_some_and(|s| !app.known_bounds(u64::MAX - st.doc.id.0, s).is_empty())); if m.alt && matches!(mode, "subtract" | "intersect") && selection_mode(tool, app.ui.selection_mode, egui::Modifiers::NONE) == "replace" && !selected() { "replace" } else { @@ -235,6 +236,24 @@ mod tests { .collect() } + /// ⌥'s "is anything selected" reads the bounds the selection outline caches, as the canvas + /// asks every frame, and scans only a selection that changed since. + #[test] + fn alt_reads_the_cached_selection_bounds() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + let alt = egui::Modifiers::ALT; + app.run("file.new", serde_json::json!({"width": 40, "height": 30})).unwrap(); + app.run("select.rect", serde_json::json!({"x": 5, "y": 5, "width": 10, "height": 10})).unwrap(); + assert_eq!(document_selection_mode(&app, Tool::Lasso, alt), "subtract", "nothing cached: scanned"); + let st = app.session.active().unwrap(); + let (key, fingerprint) = (u64::MAX - st.doc.id.0, crate::surface_fingerprint(st.doc.selection.as_ref().unwrap())); + // An empty box cached for this very selection is believed, so nothing is scanned. + app.bounds_cache.insert(key, (fingerprint, photocraft_geom::Rect::EMPTY)); + assert_eq!(document_selection_mode(&app, Tool::Lasso, alt), "replace", "the cached bounds are used"); + app.run("select.rect", serde_json::json!({"x": 20, "y": 5, "width": 10, "height": 10})).unwrap(); + assert_eq!(document_selection_mode(&app, Tool::Lasso, alt), "subtract", "a new selection is scanned again"); + } + #[test] fn badge_follows_modifiers_and_the_options_bar() { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); diff --git a/crates/ui-egui/src/tools/retouch_ui/tests.rs b/crates/ui-egui/src/tools/retouch_ui/tests.rs index 2616aaef23..c97cf3454b 100644 --- a/crates/ui-egui/src/tools/retouch_ui/tests.rs +++ b/crates/ui-egui/src/tools/retouch_ui/tests.rs @@ -411,6 +411,50 @@ fn a_new_patch_lasso_hides_and_replaces_the_old_selection() { } } +/// The screen boxes of the marching ants painted in one canvas frame (meshes of black and white). +fn painted_ants(app: &mut PhotocraftApp) -> Vec { + let ctx = egui::Context::default(); + PhotocraftApp::setup_context(&ctx, crate::theme::ThemeKind::ALL[0]); + let rect = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(300.0, 200.0)); + let view = crate::state::View { zoom: 2.0, center: [50.0, 30.0], doc_size: [100, 60], fit_pending: false, ..Default::default() }; + let mut output = ctx.run_ui(egui::RawInput { screen_rect: Some(rect), ..Default::default() }, |ui| { + crate::canvas::canvas_view(app, ui, 0, rect, view.clone(), true); + }); + output.textures_delta.clear(); + let black_and_white = + |m: &egui::Mesh| m.vertices.iter().any(|v| v.color == egui::Color32::BLACK) && m.vertices.iter().any(|v| v.color == egui::Color32::WHITE); + output + .shapes + .iter() + .filter_map(|s| match &s.shape { + egui::Shape::Mesh(m) if black_and_white(m) => Some(egui::Rect::from_points(&m.vertices.iter().map(|v| v.pos).collect::>())), + _ => None, + }) + .collect() +} + +/// #11024 on the canvas: while a new Patch lasso is drawn, no ants are painted where the selection +/// it replaces is, only along the lasso; a ⇧ lasso, which adds, keeps them. +#[test] +fn a_new_patch_lasso_paints_no_ants_on_the_old_selection() { + for tool in [Tool::Patch, Tool::ContentAwareMove] { + for (m, kept) in [(egui::Modifiers::NONE, false), (egui::Modifiers::SHIFT, true)] { + let mut app = app(); + app.run("select.rect", json!({"x": 10, "y": 20, "width": 16, "height": 20})).unwrap(); + app.sync_views(); + app.ui.tool = tool; + let old = painted_ants(&mut app).into_iter().reduce(|a, b| a.union(b)).expect("the selection has its ants"); + tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 20.0, pressure: 1.0 }, m); + for [x, y] in [[76.0, 20.0], [76.0, 40.0], [60.0, 40.0]] { + tool_event(&mut app, ToolEvent::Move { x, y, pressure: 1.0 }, m); + } + let ants = painted_ants(&mut app); + assert!(ants.iter().any(|a| !a.intersects(old)), "{tool:?} {m:?}: the lasso being drawn has its ants: {ants:?}"); + assert_eq!(ants.iter().any(|a| a.intersects(old)), kept, "{tool:?} {m:?}: ants on the old selection at {old:?}: {ants:?}"); + } + } +} + /// #11024: the Patch and Content-Aware Move cursors show + with ⇧ and − with ⌥, like the Lasso's, /// but nothing over the selection without them, where a press drags the patch. #[test]