From 6573a2eb8932664007ad7ad5b82e8f9bb31a5896 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:10:07 +0100 Subject: [PATCH] Patch tool: canvas right-click menu inside and outside the selection (#12122) Inside the selection the Patch tool opens Use Pattern to Patch Selection (greyed: no pattern patch yet), Use Patch as Source / Destination (they set the options bar's mode), Color Range... and Load Selection... (greyed until a selection is saved). Outside it opens the Lasso's selection menu, and with no selection the selection tools' no-selection menu. --- crates/ui-egui/src/canvas/canvas_tool_menu.rs | 59 ++++++++- .../canvas_tool_menu/patch_menu_tests.rs | 116 ++++++++++++++++++ 2 files changed, 169 insertions(+), 6 deletions(-) create mode 100644 crates/ui-egui/src/canvas/canvas_tool_menu/patch_menu_tests.rs diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu.rs b/crates/ui-egui/src/canvas/canvas_tool_menu.rs index ff45431f9a..810aec8d19 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu.rs @@ -691,6 +691,32 @@ pub const COUNT_MENU: &[Row] = &[ /// Right-click with the Content-Aware Move tool inside an active selection (#12194). pub const CAM_INSIDE_SELECTION_MENU: &[Row] = &[Some(("Color Range…", "select.colorRange")), Some(("Load Selection…", "select.loadSelection"))]; +/// The Patch tool's rows (#12122). Use Pattern stays greyed: the Patch tool has no pattern mode yet. +pub const PATCH_USE_PATTERN: &str = "canvasMenu.patch.usePattern"; +pub const PATCH_SOURCE: &str = "canvasMenu.patch.source"; +pub const PATCH_DESTINATION: &str = "canvasMenu.patch.destination"; + +/// Right-click with the Patch tool inside the active selection, in the reference application's +/// order (#12122). Outside it the Patch tool opens the Lasso's selection menu. +pub const PATCH_INSIDE_SELECTION_MENU: &[Row] = &[ + Some(("Use Pattern to Patch Selection", PATCH_USE_PATTERN)), + None, + Some(("Use Patch as Source", PATCH_SOURCE)), + Some(("Use Patch as Destination", PATCH_DESTINATION)), + None, + Some(("Color Range…", "select.colorRange")), + Some(("Load Selection…", "select.loadSelection")), +]; + +/// The options bar's Patch mode a row sets: `source` or `destination`. +fn patch_mode(command: &str) -> Option<&'static str> { + match command { + PATCH_SOURCE => Some("source"), + PATCH_DESTINATION => Some("destination"), + _ => None, + } +} + /// The selection menu as measured in the reference application with the Red Eye tool (#12577), /// the Object Selection tool (#12866) and the Horizontal Type tool away from text (#12042): no /// Distort or Perspective, and Delete and Fill Selection after the fills. @@ -1001,9 +1027,16 @@ fn sampler_menu(menu: &CanvasToolMenu) -> bool { menu.tool == Tool::ColorSampler && menu.kind == MenuKind::Tool } -/// Is `menu` the Content-Aware Move tool's menu opened inside the active selection (#12194)? +/// Do `tool`'s menu rows depend on whether the right-click is inside the selection: Content-Aware +/// Move (#12194) and Patch (#12122)? +fn splits_at_selection(tool: Tool) -> bool { + matches!(tool, Tool::ContentAwareMove | Tool::Patch) +} + +/// Is `menu` the Content-Aware Move (#12194) or Patch (#12122) tool's menu opened inside the +/// active selection? pub fn inside_selection(menu: &CanvasToolMenu) -> bool { - menu.tool == Tool::ContentAwareMove && menu.kind == MenuKind::Tool && menu.has_selection && menu.inside_selection + splits_at_selection(menu.tool) && menu.kind == MenuKind::Tool && menu.has_selection && menu.inside_selection } /// Content-Aware Move's canvas context menu requires an open document and leaves Bitmap, Duotone @@ -1068,6 +1101,8 @@ pub fn applies(tool: Tool) -> bool { // Sample Size and Copy Color (#11747). | Tool::Eyedropper | Tool::ContentAwareMove + // The patch rows inside the selection, the selection menu outside it (#12122). + | Tool::Patch // The layer or selection menu, where measured (#12577). | Tool::RedEye ) || path_menu(tool) @@ -1234,7 +1269,7 @@ pub fn rows(menu: &CanvasToolMenu) -> &'static [Row] { } else if menu.tool == Tool::Eyedropper { EYEDROPPER_MENU } else if inside_selection(menu) { - CAM_INSIDE_SELECTION_MENU + if menu.tool == Tool::Patch { PATCH_INSIDE_SELECTION_MENU } else { CAM_INSIDE_SELECTION_MENU } } else { selection_rows(menu.has_selection) } @@ -1497,8 +1532,12 @@ pub fn entry_enabled(app: &PhotocraftApp, menu: &CanvasToolMenu, command: &str) if sampler_menu(menu) { return command == SAMPLER_DELETE && menu.sampler.is_some() && sampler_current(app, menu); } - // Inside the Content-Aware Move selection menu (#12194), Load Selection… stays greyed until a - // selection channel (non-spot alpha channel) has been saved. + // The Patch mode rows set the options bar's mode; Use Pattern waits for a pattern patch (#12122). + if menu.tool == Tool::Patch && (patch_mode(command).is_some() || command == PATCH_USE_PATTERN) { + return inside_selection(menu) && patch_mode(command).is_some(); + } + // Inside the Content-Aware Move (#12194) and Patch (#12122) selection menus, Load Selection… + // stays greyed until a selection channel (non-spot alpha channel) has been saved. if inside_selection(menu) && command == "select.loadSelection" { return app.session.active().is_some_and(|st| st.doc.channels.iter().any(|c| c.spot.is_none())) && crate::menus::is_enabled(app, command); } @@ -1716,7 +1755,7 @@ fn capture(app: &mut PhotocraftApp, menu: &mut CanvasToolMenu, doc: [f64; 2], po // measured configuration heads the menu with the entry (#12915). menu.point = point.filter(|(target, hit)| pen_point_allowed(app, *target, hit)).and_then(|(target, hit)| point_at(app, target, hit)); } - menu.inside_selection = menu.tool == Tool::ContentAwareMove && selected_at(app, doc); + menu.inside_selection = splits_at_selection(menu.tool) && selected_at(app, doc); if menu.tool == Tool::ColorSampler { menu.sampler = crate::analysis::sampler_at(app, doc); // PLAN: assumed name `analysis::sampler_at` (tools-w53) menu.sampler_at = menu.sampler.and_then(|i| Some((app.session.active_index()?, *app.session.active()?.color_samplers.get(i)?))); @@ -1772,6 +1811,11 @@ pub fn choose(app: &mut PhotocraftApp, ctx: &Context, command: &str) { app.run(&id, params).map(|_| ()) } MenuKind::Tool if is_eyedropper(&menu) => choose_eyedropper(app, ctx, &menu, command), + // A Patch mode row sets the options bar's Source / Destination choice (#12122). + MenuKind::Tool if patch_mode(command).is_some() => { + app.ui.tool_options.patch_mode = patch_mode(command).unwrap_or("source").into(); + Ok(()) + } // Photoshop's Make Work Path… asks for the tolerance first. MenuKind::Tool if command == "select.toWorkPath" => { pen_dialogs::open(app, command, ""); @@ -2038,6 +2082,9 @@ mod cam_menu_tests; #[cfg(test)] pub(crate) mod tests; +#[cfg(test)] +mod patch_menu_tests; + #[cfg(test)] #[path = "../task_bar_pasteboard_entry_tests.rs"] mod task_bar_pasteboard_entry_tests; diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/patch_menu_tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/patch_menu_tests.rs new file mode 100644 index 0000000000..e82ffa66ec --- /dev/null +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/patch_menu_tests.rs @@ -0,0 +1,116 @@ +//! #12122: the Patch tool's canvas right-click opens the patch rows inside the selection, the +//! Lasso's selection menu outside it and the selection tools' no-selection menu without one. + +use egui::Modifiers; +use egui_kittest::Harness; +use serde_json::{Value, json}; + +use super::tests::{any_dialog, app, harness, open_here, pick, right_click, screen_of}; +use super::{NO_SELECTION_MENU, PATCH_DESTINATION, PATCH_SOURCE, PATCH_USE_PATTERN, SELECTION_MENU, available}; +use crate::PhotocraftApp; +use crate::state::Tool; + +/// A 32 × 32 document with the selection 4..20 square and `tool` selected. +fn selected(tool: Tool) -> Box { + let mut app = app(); + app.run("select.rect", json!({"x": 4, "y": 4, "width": 16, "height": 16})).unwrap(); + app.ui.tool = tool; + app +} + +/// Right-click document point `doc` with `tool`: the open menu's entries as `ui.inspect` lists them. +fn entries_at(h: &mut Harness<'static, Box>, tool: Tool, doc: [f32; 2]) -> Vec { + h.state_mut().ui.canvas_tool_menu = None; + h.state_mut().ui.tool = tool; + let at = screen_of(h.state(), doc); + right_click(h, at, Modifiers::NONE); + let inspected = crate::control::inspect(h.state(), &h.ctx); + assert_eq!(inspected["canvasToolMenu"]["tool"], json!(format!("{tool:?}")), "{tool:?} opens its menu at {doc:?}"); + inspected["canvasToolMenu"]["entries"].as_array().cloned().unwrap_or_default() +} + +/// The entries' labels, a separator as `None`. +fn shown(entries: &[Value]) -> Vec> { + entries.iter().map(|e| if e["separator"] == json!(true) { None } else { e["label"].as_str().map(String::from) }).collect() +} + +fn enabled(entries: &[Value], label: &str) -> bool { + entries.iter().find(|e| e["label"] == json!(label)).unwrap_or_else(|| panic!("{label} is listed"))["enabled"] == json!(true) +} + +#[test] +fn inside_the_selection_the_patch_rows_show_in_the_reference_order() { + let mut h = harness(selected(Tool::Patch)); + let entries = entries_at(&mut h, Tool::Patch, [12.0, 12.0]); + let want = [ + Some("Use Pattern to Patch Selection"), + None, + Some("Use Patch as Source"), + Some("Use Patch as Destination"), + None, + Some("Color Range…"), + Some("Load Selection…"), + ]; + assert_eq!(shown(&entries), want.map(|l| l.map(String::from))); + assert!(!enabled(&entries, "Use Pattern to Patch Selection"), "no pattern patch exists yet: greyed"); + assert!(enabled(&entries, "Use Patch as Source") && enabled(&entries, "Use Patch as Destination")); + assert!(enabled(&entries, "Color Range…")); + assert!(!enabled(&entries, "Load Selection…"), "no selection saved: greyed"); + + h.state_mut().run("select.saveSelection", json!({"name": "Alpha 1"})).unwrap(); + let entries = entries_at(&mut h, Tool::Patch, [12.0, 12.0]); + assert!(enabled(&entries, "Load Selection…"), "a saved selection makes Load Selection… live"); +} + +#[test] +fn outside_the_selection_the_patch_tool_opens_the_lassos_menu() { + let mut h = harness(selected(Tool::Patch)); + let lasso = entries_at(&mut h, Tool::Lasso, [26.0, 26.0]); + let patch = entries_at(&mut h, Tool::Patch, [26.0, 26.0]); + assert_eq!(shown(&patch), shown(&lasso)); + assert_eq!(patch.iter().map(|e| &e["enabled"]).collect::>(), lasso.iter().map(|e| &e["enabled"]).collect::>()); + assert_eq!(patch.len(), SELECTION_MENU.len(), "the selection menu, nothing added"); +} + +#[test] +fn without_a_selection_the_patch_tool_opens_the_no_selection_menu() { + let mut app = app(); + app.ui.tool = Tool::Patch; + let mut h = harness(app); + let entries = entries_at(&mut h, Tool::Patch, [12.0, 12.0]); + let want: Vec<_> = NO_SELECTION_MENU.iter().map(|row| row.map(|(label, _)| label.to_string())).collect(); + assert_eq!(shown(&entries), want); +} + +#[test] +fn the_mode_rows_set_the_options_bar_mode_and_nothing_else() { + let mut app = selected(Tool::Patch); + assert_eq!(app.ui.tool_options.patch_mode, "source"); + let journal = app.session.journal.len(); + for (row, mode) in [(PATCH_DESTINATION, "destination"), (PATCH_SOURCE, "source")] { + assert!(open_here(&mut app, Tool::Patch, [12.0, 12.0])); + pick(&mut app, row); + assert!(app.ui.canvas_tool_menu.is_none(), "choosing a row closes the menu"); + assert_eq!(app.ui.tool_options.patch_mode, mode); + } + assert_eq!(app.session.journal.len(), journal, "the mode rows run no command"); + assert_eq!(app.ui.tool, Tool::Patch); + + // Use Pattern is greyed, so choosing it does nothing. + assert!(open_here(&mut app, Tool::Patch, [12.0, 12.0])); + let menu = app.ui.canvas_tool_menu.clone().unwrap(); + assert!(!available(&app, &menu, PATCH_USE_PATTERN)); + // Outside the selection the mode rows aren't offered. + assert!(open_here(&mut app, Tool::Patch, [26.0, 26.0])); + let menu = app.ui.canvas_tool_menu.clone().unwrap(); + assert!(!available(&app, &menu, PATCH_DESTINATION)); +} + +#[test] +fn color_range_opens_its_dialog() { + let mut app = selected(Tool::Patch); + assert!(open_here(&mut app, Tool::Patch, [12.0, 12.0])); + pick(&mut app, "select.colorRange"); + assert!(app.ui.canvas_tool_menu.is_none()); + assert!(any_dialog(&app), "Color Range… opens the Color Range dialog"); +}