fix(ui): Patch ⌥ check reads cached selection bounds; frame test for the hidden outline (#11024)

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 <noreply@anthropic.com>
This commit is contained in:
RickiNanoandClaude Opus 5.5 committed 2026-11-06 17:45:53 +01:00
1 parent 58f08031fb
commit 5da01d72de
3 files changed
+74 -2

No files matched your search

+9
View File
@@ -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.
+21 -2
View File
@@ -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());
@@ -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<egui::Rect> {
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::<Vec<_>>())),
_ => 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]