fix(ui): Patch tool hides the selection it replaces and shows +/− badges (#11024)

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 <[email protected]>
This commit is contained in:
RickiNanoandClaude Opus 5.5 committed 2026-11-06 17:45:53 +01:00
1 parent b837027c41
commit 58f08031fb
4 files changed
+88 -3

No files matched your search

+5 -1
View File
@@ -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
+12 -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);
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<Badg
if let Some(p) = app.hover_doc.filter(|_| tool == Tool::Pen) {
return crate::anchor_tools::pen_auto(app, tool, m, p).map(|(t, ..)| if t == Tool::AddAnchorPoint { Badge::Add } else { Badge::Subtract });
}
// Patch and Content-Aware Move lasso their area in the Lasso's mode (⇧ adds, ⌥ subtracts,
// #11024). Over the selection without them a press drags the patch, which combines nothing.
if matches!(tool, Tool::Patch | Tool::ContentAwareMove) {
let at = app.drag.as_ref().filter(|d| d.tool == tool).map(|d| d.start).or(app.hover_doc);
if at.is_some_and(|p| crate::retouch_ui::patch_drags_selection(app, p, m)) {
return None;
}
return Badge::from_mode(document_selection_mode(app, Tool::Lasso, m));
}
if !is_selection_tool(tool) {
return None;
}
+11
View File
@@ -313,6 +313,17 @@ pub fn patch_drags_selection(app: &PhotocraftApp, at: [f64; 2], mods: egui::Modi
sel.sample_channel(at[0].floor() as i32, at[1].floor() as i32, 0) > 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] {
@@ -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();