Merge pull request #12240 from ElNeMeR70/fix/content-aware-move-context-menu-12194

Open selection context menus on right-click with Content-Aware Move tool
This commit is contained in:
David Mann authored and GitHub committed 2026-11-11 02:00:35 +01:00
commit 927cd5aecb
3 files changed
+204 -4

No files matched your search

+61 -4
View File
@@ -3,7 +3,7 @@
//! pasteboard (outside the image), a guide and a pending crop.
use egui::Context;
use photocraft_doc::LayerContent;
use photocraft_doc::{ColorMode, LayerContent};
use photocraft_engine::prefs::{self, CanvasColor};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
@@ -59,6 +59,9 @@ pub struct CanvasToolMenu {
/// the Sample Size, which its Copy Color rows put on the clipboard. `None` over transparency.
#[serde(default)]
pub color: Option<[f32; 3]>,
/// Opened with the Content-Aware Move tool inside the active selection (#12194).
#[serde(default)]
pub inside_selection: bool,
}
/// A misspelled word in the type layer being edited (`edit.checkSpelling`'s `list` entry).
@@ -319,6 +322,7 @@ pub fn open_crop(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32;
sampler: None,
sampler_at: None,
color: None,
inside_selection: false,
});
crate::canvas_popup::mark_open(ctx, menu_id());
true
@@ -435,6 +439,9 @@ pub const NO_SELECTION_MENU: &[Row] = &[
Some(("Load Selection…", "select.loadSelection")),
];
/// 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 Eyedropper's copy rows (#11747).
pub const EYEDROPPER_COPY_HTML: &str = "canvasMenu.eyedropper.copyHtml";
pub const EYEDROPPER_COPY_HEX: &str = "canvasMenu.eyedropper.copyHex";
@@ -567,8 +574,26 @@ fn sampler_menu(menu: &CanvasToolMenu) -> bool {
menu.tool == Tool::ColorSampler && !menu.transform && !menu.pasteboard && !menu.guide && menu.crop.is_none() && menu.anchor.is_none()
}
/// Tools whose plain canvas right-click opens a menu: the selection tools' actions, the path
/// menu and the Color Sampler's.
/// Is `menu` the Content-Aware Move tool's menu opened inside the active selection (#12194)?
pub fn inside_selection(menu: &CanvasToolMenu) -> bool {
menu.tool == Tool::ContentAwareMove
&& menu.has_selection
&& menu.inside_selection
&& !menu.transform
&& !menu.pasteboard
&& !menu.guide
&& menu.crop.is_none()
&& menu.anchor.is_none()
}
/// Content-Aware Move's canvas context menu requires an open document and leaves Bitmap, Duotone
/// and Multichannel documents on their existing no-menu path (#12194).
fn cam_allowed(app: &PhotocraftApp) -> bool {
app.session.active().is_some_and(|s| !matches!(s.doc.mode, ColorMode::Bitmap | ColorMode::Duotone | ColorMode::Multichannel))
}
/// Tools whose plain canvas right-click opens a menu: the selection tools' actions, the
/// Content-Aware Move menu, the path menu and the Color Sampler's.
pub fn applies(tool: Tool) -> bool {
matches!(
tool,
@@ -587,6 +612,7 @@ pub fn applies(tool: Tool) -> bool {
| Tool::ColorSampler
// Sample Size and Copy Color (#11747).
| Tool::Eyedropper
| Tool::ContentAwareMove
) || path_menu(tool)
}
@@ -640,6 +666,8 @@ pub fn rows(menu: &CanvasToolMenu) -> &'static [Row] {
SAMPLER_MENU
} else if menu.tool == Tool::Eyedropper {
EYEDROPPER_MENU
} else if inside_selection(menu) {
CAM_INSIDE_SELECTION_MENU
} else {
selection_rows(menu.has_selection)
}
@@ -785,6 +813,7 @@ pub fn open_transform(app: &mut PhotocraftApp, ctx: &Context, pos: [f32; 2]) ->
sampler: None,
sampler_at: None,
color: None,
inside_selection: false,
});
crate::canvas_popup::mark_open(ctx, menu_id());
true
@@ -842,7 +871,7 @@ pub fn pasteboard_at(app: &PhotocraftApp, tool: Tool, doc: [f64; 2]) -> bool {
let Some(st) = app.session.active() else { return false };
let (w, h) = (f64::from(st.doc.size.width), f64::from(st.doc.size.height));
let outside = doc.iter().all(|v| v.is_finite()) && (doc[0] < 0.0 || doc[1] < 0.0 || doc[0] >= w || doc[1] >= h);
outside && !path_menu(tool) && !crate::paint_mouse::has_brush_picker(tool) && !crate::paint_mouse::right_erases(app, tool) && app.ui.text_edit.is_none()
outside && !path_menu(tool) && !crate::paint_mouse::has_brush_picker(tool) && !crate::paint_mouse::right_erases(app, tool) && !editing_text(app, tool)
}
/// Right-click on the pasteboard: its colour menu at screen point `pos`.
@@ -869,6 +898,7 @@ pub fn open_pasteboard(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos:
sampler: None,
sampler_at: None,
color: None,
inside_selection: false,
});
crate::canvas_popup::mark_open(ctx, menu_id());
true
@@ -903,6 +933,7 @@ pub fn open_guide(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, mods: egui
sampler: None,
sampler_at: None,
color: None,
inside_selection: false,
});
crate::canvas_popup::mark_open(ctx, menu_id());
true
@@ -962,6 +993,11 @@ 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.
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);
}
if menu.transform || !path_menu(menu.tool) {
return crate::menus::is_enabled(app, command);
}
@@ -1016,12 +1052,29 @@ pub fn open_at(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32; 2]
if !(applies(tool) || text || type_layer) {
return false;
}
// Without an active document or in Bitmap/Duotone/Multichannel modes, Content-Aware Move opens
// no menu (#12194); release any non-held control-channel HUD that would otherwise stay open.
if tool == Tool::ContentAwareMove && !cam_allowed(app) {
crate::color_hud::release(&mut app.ui);
return false;
}
app.ui.brush_picker = None;
app.ui.layer_menu = None;
let has_selection = app.session.active().is_some_and(|s| s.doc.selection.is_some());
let has_path = crate::vector_ui::active_path_name(app).is_some() || app.ui.pen.as_ref().is_some_and(|p| p.knots.len() >= 2);
let path_name = crate::vector_ui::active_path_name(app);
let spelling = if text { doc.and_then(|d| spelling_at(app, d)) } else { None };
let inside_selection = tool == Tool::ContentAwareMove
&& doc.is_some_and(|d| {
d.iter().all(|v| v.is_finite())
&& d[0] >= 0.0
&& d[1] >= 0.0
&& app.session.active().is_some_and(|s| {
d[0] < f64::from(s.doc.size.width)
&& d[1] < f64::from(s.doc.size.height)
&& s.doc.selection.as_ref().is_some_and(|sel| sel.sample_channel(d[0].floor() as i32, d[1].floor() as i32, 0) >= 0.5)
})
});
let sampler = if tool == Tool::ColorSampler { doc.and_then(|d| crate::analysis_ui::sampler_at(app, d)) } else { None };
let sampler_at = sampler.and_then(|i| Some((app.session.active_index()?, *app.session.active()?.color_samplers.get(i)?)));
// The Eyedropper reads the colour now, under the right-click: all layers, with the Sample
@@ -1044,6 +1097,7 @@ pub fn open_at(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32; 2]
sampler,
sampler_at,
color,
inside_selection,
});
crate::canvas_popup::mark_open(ctx, menu_id());
true
@@ -1074,6 +1128,7 @@ pub fn open_view(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32;
sampler: None,
sampler_at: None,
color: None,
inside_selection: false,
});
crate::canvas_popup::mark_open(ctx, menu_id());
true
@@ -1489,6 +1544,8 @@ pub fn menu_rect_if_open(app: &PhotocraftApp, ctx: &Context) -> Option<egui::Rec
Some(egui::Rect::from_min_size(pos, size))
}
#[cfg(test)]
mod cam_menu_tests;
#[cfg(test)]
mod tests;
@@ -0,0 +1,142 @@
use super::*;
use egui::{Modifiers, PointerButton, Pos2, vec2};
use egui_kittest::Harness;
fn app() -> PhotocraftApp {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 32, "height": 32})).unwrap();
app.ui.tool = Tool::ContentAwareMove;
app
}
fn harness(mut app: PhotocraftApp) -> Harness<'static, PhotocraftApp> {
app.sync_views();
let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state(
|ui, app: &mut PhotocraftApp| {
let ctx = ui.ctx().clone();
if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
return;
}
egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui));
},
app,
);
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::default());
h.run_steps(4);
h
}
fn right_click(h: &mut Harness<'static, PhotocraftApp>, p: Pos2) {
h.event(egui::Event::PointerMoved(p));
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: true, modifiers: Modifiers::NONE });
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: false, modifiers: Modifiers::NONE });
h.run_steps(2);
}
#[test]
fn content_aware_move_outside_and_without_selection_matches_lasso_menus() {
let mut h = harness(app());
assert!(applies(Tool::ContentAwareMove));
let center = h.state().last_canvas_rect.center();
// With no selection, a canvas right-click opens NO_SELECTION_MENU, matching Tool::Lasso.
right_click(&mut h, center);
let menu = h.state().ui.canvas_tool_menu.clone().expect("no-selection menu opened");
assert!(!menu.has_selection && !inside_selection(&menu) && menu.sampler.is_none());
assert_eq!(rows(&menu), NO_SELECTION_MENU);
let ctx = h.ctx.clone();
choose(h.state_mut(), &ctx, "select.all");
assert!(h.state().session.active().unwrap().doc.selection.is_some());
// Outside an active selection, a canvas right-click opens SELECTION_MENU, matching Tool::Lasso.
h.state_mut().run("select.rect", json!({"x": 2, "y": 2, "width": 6, "height": 6})).unwrap();
right_click(&mut h, center);
let menu = h.state().ui.canvas_tool_menu.clone().expect("outside-selection menu opened");
assert!(menu.has_selection && !inside_selection(&menu) && menu.sampler.is_none());
assert_eq!(rows(&menu), SELECTION_MENU);
choose(h.state_mut(), &ctx, "select.deselect");
assert!(h.state().session.active().unwrap().doc.selection.is_none());
}
#[test]
fn content_aware_move_inside_selection_opens_color_range_and_load_selection_only() {
let mut app = app();
app.run("select.rect", json!({"x": 4, "y": 4, "width": 24, "height": 24})).unwrap();
let mut h = harness(app);
let center = h.state().last_canvas_rect.center();
right_click(&mut h, center);
let menu = h.state().ui.canvas_tool_menu.clone().expect("inside-selection menu opened");
assert!(menu.has_selection && inside_selection(&menu) && menu.sampler.is_none() && menu.sampler_at.is_none());
assert_eq!(rows(&menu), &[Some(("Color Range…", "select.colorRange")), Some(("Load Selection…", "select.loadSelection"))]);
// Color Range… is enabled; Load Selection… is greyed out when no selection channel is saved.
assert!(entry_enabled(h.state(), &menu, "select.colorRange"));
assert!(!entry_enabled(h.state(), &menu, "select.loadSelection"));
assert!(!available(h.state(), &menu, "select.deselect"), "full selection menu rows are not available inside selection");
// A spot channel does not count as a saved selection channel.
let ctx = h.ctx.clone();
h.state_mut().run("channel.newSpot", json!({"name": "Spot 1"})).unwrap();
assert!(!entry_enabled(h.state(), &menu, "select.loadSelection"));
// Once a selection channel is saved, Load Selection… becomes enabled and renders cleanly with no samplers.
h.state_mut().run("select.saveSelection", json!({"name": "Alpha 1"})).unwrap();
assert!(open_at(h.state_mut(), &ctx, Tool::ContentAwareMove, [10.0, 10.0], Some([16.0, 16.0])));
h.run_steps(1);
let menu = h.state().ui.canvas_tool_menu.clone().unwrap();
assert!(entry_enabled(h.state(), &menu, "select.loadSelection"));
// Even with a saved channel, Load Selection… greys out when menus::is_enabled blocks it (e.g. a pending crop).
h.state_mut().ui.tool = Tool::Crop;
h.state_mut().ui.crop_rect = Some([0.0, 0.0, 16.0, 16.0]);
h.state_mut().crop.default_frame = false;
assert!(!entry_enabled(h.state(), &menu, "select.loadSelection"));
h.state_mut().ui.crop_rect = None;
h.state_mut().ui.tool = Tool::ContentAwareMove;
assert!(entry_enabled(h.state(), &menu, "select.loadSelection"));
// Choosing Color Range… opens the Color Range dialog.
choose(h.state_mut(), &ctx, "select.colorRange");
assert!(!h.state().ui.dialogs.is_empty());
h.state_mut().ui.dialogs.clear();
// Choosing Load Selection… opens the Load Selection dialog.
assert!(open_at(h.state_mut(), &ctx, Tool::ContentAwareMove, [10.0, 10.0], Some([16.0, 16.0])));
choose(h.state_mut(), &ctx, "select.loadSelection");
assert!(!h.state().ui.dialogs.is_empty());
}
#[test]
fn content_aware_move_boundary_modes_and_overlay_guards() {
let ctx = Context::default();
let mut empty = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
empty.ui.tool = Tool::ContentAwareMove;
crate::color_hud::open(&mut empty, [12.0, 12.0], false);
assert!(!open_at(&mut empty, &ctx, Tool::ContentAwareMove, [10.0, 10.0], Some([8.0, 8.0])));
assert!(empty.ui.color_hud.is_none());
let mut app = app();
app.run("select.rect", json!({"x": 4, "y": 4, "width": 8, "height": 8})).unwrap();
// Boundary, negative, and non-finite coordinates.
for (pt, expected) in [([4.0, 4.0], true), ([11.9, 11.9], true), ([3.9, 4.0], false), ([12.0, 12.0], false), ([-1.0, 4.0], false), ([f64::NAN, 4.0], false)]
{
assert!(open_at(&mut app, &ctx, Tool::ContentAwareMove, [10.0, 10.0], Some(pt)));
let m = app.ui.canvas_tool_menu.clone().unwrap();
assert_eq!(inside_selection(&m), expected, "point {pt:?}");
}
// Transform/pasteboard/guide/crop/anchor menus are not replaced by CAM_INSIDE_SELECTION_MENU.
let mut tmenu = app.ui.canvas_tool_menu.clone().unwrap();
tmenu.transform = true;
assert!(!inside_selection(&tmenu) && rows(&tmenu) == TRANSFORM_MENU);
// Excluded color modes return false and release a non-held color HUD.
for mode in [ColorMode::Bitmap, ColorMode::Duotone, ColorMode::Multichannel] {
std::sync::Arc::make_mut(&mut app.session.active_mut().unwrap().doc).mode = mode;
crate::color_hud::open(&mut app, [12.0, 12.0], false);
assert!(!open_at(&mut app, &ctx, Tool::ContentAwareMove, [10.0, 10.0], Some([6.0, 6.0])));
assert!(app.ui.color_hud.is_none());
}
}
@@ -59,6 +59,7 @@ fn type_layers_offer_free_transform_but_not_distort_or_perspective() {
sampler: None,
sampler_at: None,
color: None,
inside_selection: false,
};
assert!(crate::canvas_tool_menu::available(&app, &menu, "edit.freeTransform"));
assert!(!crate::canvas_tool_menu::available(&app, &menu, "edit.transform.distort"));