From 58f08031fb80ea5b8d67dc1f82035ade25229ddf Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:26:51 +0100 Subject: [PATCH] =?UTF-8?q?fix(ui):=20Patch=20tool=20hides=20the=20selecti?= =?UTF-8?q?on=20it=20replaces=20and=20shows=20+/=E2=88=92=20badges=20(#110?= =?UTF-8?q?24)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A new-selection Patch or Content-Aware Move lasso now hides the old marching ants from the press on, as the Polygonal and Magnetic Lasso do; before, the old outline stayed beside the new one until the release. Shift, Alt and Shift+Alt keep it visible. Floating pieces, Select and Mask's preview and other documents keep their ants. Both tools' cursors show the Lasso's + / − / × badge for Shift / Alt / Shift+Alt, worked out as the gesture does, and none over the selection without modifiers, where a press drags the patch. document_selection_mode now scans the selection only when Alt could change the answer, since the canvas asks every frame during a drag. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/canvas.rs | 6 +- crates/ui-egui/src/canvas/tool_feedback.rs | 14 ++++- crates/ui-egui/src/tools/retouch_ui.rs | 11 ++++ crates/ui-egui/src/tools/retouch_ui/tests.rs | 60 ++++++++++++++++++++ 4 files changed, 88 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 2acba730f4..d07bc441e3 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -247,7 +247,11 @@ fn paint_selection_outline(app: &mut PhotocraftApp, input: SelectionPaintInput<' Some(Some((proxy, k))) => (proxy.selection.as_ref(), (*k).max(1)), }; // Selection outline: true boundary, animated marching ants (cached per revision). - if let Some(sel) = outlined.filter(|_| app.ui.view.shows(app.ui.view.show.selection_edges) && !polygon_replaces_selection(app)) { + // A Patch lasso drag replaces the active document's selection, not a floating piece or Select + // and Mask's refined preview; other documents keep their ants. + let patch_replaces = + || refined.is_none() && floated.is_none() && app.session.active_index() == Some(input.idx) && crate::retouch_ui::patch_replaces_selection(app); + if let Some(sel) = outlined.filter(|_| app.ui.view.shows(app.ui.view.show.selection_edges) && !polygon_replaces_selection(app) && !patch_replaces()) { // Trace at display resolution over the visible part only; key by the mask's tile identity // (not the document revision) so unrelated edits don't re-trace it. The step is in // device pixels per document pixel (`View::zoom`, not the point zoom), so a 1 px diff --git a/crates/ui-egui/src/canvas/tool_feedback.rs b/crates/ui-egui/src/canvas/tool_feedback.rs index ce6cbb9e49..d18f16dd09 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); - let selected = app.session.active().and_then(|st| st.doc.selection.as_ref()).is_some_and(|s| !s.content_bounds().is_empty()); - if m.alt && !selected && matches!(mode, "subtract" | "intersect") && selection_mode(tool, app.ui.selection_mode, egui::Modifiers::NONE) == "replace" { + // 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()); + if m.alt && matches!(mode, "subtract" | "intersect") && selection_mode(tool, app.ui.selection_mode, egui::Modifiers::NONE) == "replace" && !selected() { "replace" } else { mode @@ -117,6 +118,15 @@ pub fn badge(app: &PhotocraftApp, tool: Tool, m: egui::Modifiers) -> Option 0.0 } +/// A Patch or Content-Aware Move lasso that makes a new selection is being drawn: the selection it +/// replaces is hidden from the press on, as in Photoshop, rather than shown beside the new outline +/// until the release (#11024). Adding, subtracting and intersecting keep it visible. +pub fn patch_replaces_selection(app: &PhotocraftApp) -> bool { + app.drag.as_ref().is_some_and(|d| { + matches!(d.tool, Tool::Patch | Tool::ContentAwareMove) + && !patch_drags_selection(app, d.start, d.modifiers) + && crate::canvas::selection_mode(app, d.modifiers) == "replace" + }) +} + /// Whole-pixel offset for a drag from `start` to `end`, limited so the dragged outline stays on the /// canvas (the engine rejects a patch or move that leaves it). pub fn patch_offset(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2]) -> [i32; 2] { diff --git a/crates/ui-egui/src/tools/retouch_ui/tests.rs b/crates/ui-egui/src/tools/retouch_ui/tests.rs index 4a084bc80a..2616aaef23 100644 --- a/crates/ui-egui/src/tools/retouch_ui/tests.rs +++ b/crates/ui-egui/src/tools/retouch_ui/tests.rs @@ -377,6 +377,66 @@ fn patch_tool_lassoes_then_drags_the_patch() { assert!(px[0] > 0.95 && px[1] > 0.95 && px[2] > 0.95, "blemish patched with the white background: {px:?}"); } +/// #11024: a new Patch (or Content-Aware Move) lasso hides the selection it replaces from the +/// press on, and the release leaves only the new area selected. ⇧ keeps the old one and adds. +#[test] +fn a_new_patch_lasso_hides_and_replaces_the_old_selection() { + let lasso = |app: &mut PhotocraftApp, x0: f64, m: egui::Modifiers, release: bool| { + tool_event(app, ToolEvent::Down { x: x0, y: 20.0, pressure: 1.0 }, m); + for [x, y] in [[x0 + 16.0, 20.0], [x0 + 16.0, 40.0], [x0, 40.0]] { + tool_event(app, ToolEvent::Move { x, y, pressure: 1.0 }, m); + } + if release { + tool_event(app, ToolEvent::Up { x: x0, y: 40.0 }, m); + } + }; + let selected = |app: &PhotocraftApp, x: i32| app.session.active().unwrap().doc.selection.as_ref().is_some_and(|s| s.sample_channel(x, 30, 0) > 0.0); + for tool in [Tool::Patch, Tool::ContentAwareMove] { + let mut app = app(); + app.ui.tool = tool; + lasso(&mut app, 10.0, egui::Modifiers::NONE, true); + assert!(selected(&app, 18), "{tool:?}: the first lasso made a selection"); + assert!(!patch_replaces_selection(&app), "{tool:?}: nothing is being drawn"); + + lasso(&mut app, 60.0, egui::Modifiers::NONE, false); + assert!(patch_replaces_selection(&app), "{tool:?}: the old outline is hidden while the new one is drawn"); + tool_event(&mut app, ToolEvent::Up { x: 60.0, y: 40.0 }, egui::Modifiers::NONE); + assert!(!patch_replaces_selection(&app), "{tool:?}: the new selection shows after the release"); + assert!(selected(&app, 68) && !selected(&app, 18), "{tool:?}: the new area replaced the old one"); + + lasso(&mut app, 10.0, egui::Modifiers::SHIFT, false); + assert!(!patch_replaces_selection(&app), "{tool:?}: ⇧ adds, so the old outline stays"); + tool_event(&mut app, ToolEvent::Up { x: 10.0, y: 40.0 }, egui::Modifiers::SHIFT); + assert!(selected(&app, 68) && selected(&app, 18), "{tool:?}: ⇧ added to the selection"); + } +} + +/// #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] +fn patch_cursor_shows_the_selection_mode_badge() { + use crate::tool_feedback::{Badge, badge}; + let (none, shift, alt) = (egui::Modifiers::NONE, egui::Modifiers::SHIFT, egui::Modifiers::ALT); + for tool in [Tool::Patch, Tool::ContentAwareMove] { + let mut app = app(); + app.ui.tool = tool; + app.hover_doc = Some([80.0, 30.0]); + assert_eq!(badge(&app, tool, shift), Some(Badge::Add), "{tool:?}"); + assert_eq!(badge(&app, tool, alt), None, "{tool:?}: nothing selected, so ⌥ has nothing to subtract"); + assert_eq!(badge(&app, tool, none), None, "{tool:?}: a new selection has no badge"); + + app.run("select.rect", json!({"x": 10, "y": 20, "width": 20, "height": 20})).unwrap(); + assert_eq!(badge(&app, tool, shift), Some(Badge::Add), "{tool:?}"); + assert_eq!(badge(&app, tool, alt), Some(Badge::Subtract), "{tool:?}"); + assert_eq!(badge(&app, tool, egui::Modifiers::SHIFT | egui::Modifiers::ALT), Some(Badge::Intersect), "{tool:?}"); + app.ui.selection_mode = 1; + assert_eq!(badge(&app, tool, none), Some(Badge::Add), "{tool:?}: the options-bar mode outside the selection"); + app.hover_doc = Some([20.0, 30.0]); + assert_eq!(badge(&app, tool, none), None, "{tool:?}: a press over the selection drags the patch"); + assert_eq!(badge(&app, tool, shift), Some(Badge::Add), "{tool:?}: ⇧ over the selection adds"); + } +} + #[test] fn patch_tool_content_aware_fills_from_where_it_is_dropped() { let mut app = app();