Merge pull request #13005 from rmsff/fix/object-selection-canvas-menu-12866

Object Selection menu: the reference rows with and without a selection
This commit is contained in:
David Mann authored and GitHub committed 2026-11-13 02:02:19 +01:00
commit 006ebfcfb1
5 files changed
+215 -10

No files matched your search

+10 -5
View File
@@ -518,8 +518,9 @@ pub const NO_SELECTION_MENU: &[Row] = &[
];
/// Photoshop's generic layer menu, in its order for a pixel layer without a selection: the Count
/// tool's canvas menu on a marker or off one (#11772), and the Red Eye tool's without a selection. Each row is enabled as the Layers panel
/// menu enables it for the active layer (`menus::is_enabled`) and runs like the menu item.
/// tool's canvas menu on a marker or off one (#11772), and the Red Eye and Object Selection
/// (#12866) tools' without a selection. Each row is enabled as the Layers panel menu enables it
/// for the active layer (`menus::is_enabled`) and runs like the menu item.
pub const COUNT_MENU: &[Row] = &[
Some(("Blending Options…", "layer.layerStyle.blendingOptions")),
None,
@@ -543,8 +544,9 @@ 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 selection menu as measured in the reference application with the Red Eye tool (#12577):
/// no Distort or Perspective, and Delete and Fill Selection after the fills.
/// The selection menu as measured in the reference application with the Red Eye tool (#12577) and
/// the Object Selection tool (#12866): no Distort or Perspective, and Delete and Fill Selection
/// after the fills.
pub const MEASURED_SELECTION_MENU: &[Row] = selection_menu![
None,
Some(("Fill…", "edit.fill")),
@@ -851,7 +853,7 @@ pub fn rows(menu: &CanvasToolMenu) -> &'static [Row] {
SAMPLER_MENU
} else if menu.tool == Tool::Count {
COUNT_MENU
} else if menu.tool == Tool::RedEye {
} else if matches!(menu.tool, Tool::RedEye | Tool::ObjectSelection) {
if menu.has_selection { MEASURED_SELECTION_MENU } else { COUNT_MENU }
} else if menu.tool == Tool::Eyedropper {
EYEDROPPER_MENU
@@ -1838,3 +1840,6 @@ mod delete_anchor_menu_tests;
#[cfg(test)]
mod convert_point_menu_tests;
#[cfg(test)]
mod object_selection_menu_tests;
@@ -0,0 +1,192 @@
//! #12866: the Object Selection tool's canvas menu lists the reference application's rows: with a
//! selection, no Distort or Perspective and Delete and Fill Selection after the fills; with none,
//! the layer menu, its rows greyed as measured over the Background, a type and a shape layer. The
//! marquees', lassos' and Magic Wand's menus are unchanged.
use egui::{Context, Modifiers};
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
use serde_json::{Value, json};
use super::tests::{app, harness, labels, right_click};
use super::{COUNT_MENU, NO_SELECTION_MENU, SELECTION_MENU, available, open, rows};
use crate::PhotocraftApp;
use crate::state::Tool;
/// A pixel layer filled red, active, with `select` run first when given.
fn pixel_layer(select: &[(&str, Value)]) -> PhotocraftApp {
let mut app = app();
app.run("layer.new.layer", json!({})).unwrap();
app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();
for (id, params) in select {
app.run(id, params.clone()).unwrap();
}
app.ui.tool = Tool::ObjectSelection;
app
}
/// Right-click the middle of the canvas: the open menu's entries as `ui.inspect` lists them.
fn right_click_entries(h: &mut Harness<'static, PhotocraftApp>) -> Vec<Value> {
let center = h.state().last_canvas_rect.center();
right_click(h, center, Modifiers::NONE);
let inspected = crate::control::inspect(h.state(), &h.ctx);
assert_eq!(inspected["canvasToolMenu"]["tool"], json!("ObjectSelection"));
inspected["canvasToolMenu"]["entries"].as_array().cloned().unwrap_or_default()
}
/// The entries' labels, a separator as `None`.
fn shown(entries: &[Value]) -> Vec<Option<String>> {
entries.iter().map(|e| if e["separator"] == json!(true) { None } else { e["label"].as_str().map(String::from) }).collect()
}
/// The issue's notation: rows joined by " · ", separators written " ― ".
fn menu(spec: &str) -> Vec<Option<String>> {
spec.split(" ― ")
.enumerate()
.flat_map(|(i, group)| (i > 0).then_some(None).into_iter().chain(group.split(" · ").map(|label| Some(label.to_owned()))))
.collect()
}
#[test]
fn with_a_selection_the_menu_lists_the_reference_rows() {
let mut h = harness(pixel_layer(&[("select.rect", json!({"x": 4, "y": 4, "width": 24, "height": 24}))]));
let entries = right_click_entries(&mut h);
assert_eq!(
shown(&entries),
menu(
"Deselect · Select Inverse · Feather… · Select and Mask… ― Save Selection… · Make Work Path… ― Layer Via Copy · Layer Via Cut · \
New Layer… ― Free Transform · Transform Selection ― Fill… · Stroke… · Content-Aware Fill… · Delete and Fill Selection ― \
Last Filter ― Fade…"
)
);
// A row the menu had before runs its command, as before.
let before = h.state().session.journal.len();
h.get_by_label("Select Inverse").click();
h.run_steps(2);
assert!(h.state().ui.canvas_tool_menu.is_none());
assert!(h.state().session.journal[before..].iter().any(|(id, _)| id == "select.inverse"));
}
#[test]
fn with_no_selection_the_menu_lists_the_layer_rows() {
let mut h = harness(pixel_layer(&[("select.rect", json!({"x": 4, "y": 4, "width": 24, "height": 24})), ("select.deselect", json!({}))]));
let entries = right_click_entries(&mut h);
assert_eq!(
shown(&entries),
menu(
"Blending Options… ― Duplicate Layer… · Delete Layer · Group from Layers… ― Convert to Smart Object · Convert to Layers ― \
Rasterize Layer · Rasterize Layer Style ― Free Transform ― Color Range… · Load Selection… · Reselect"
)
);
// Over a plain pixel layer these have nothing to act on and grey out.
for label in ["Convert to Layers", "Rasterize Layer", "Rasterize Layer Style"] {
let entry = entries.iter().find(|e| e["label"] == json!(label));
assert_eq!(entry.map(|e| &e["enabled"]), Some(&json!(false)), "{label} is greyed out");
}
h.get_by_label("Reselect").click();
h.run_steps(2);
assert!(h.state().ui.canvas_tool_menu.is_none());
assert!(h.state().session.active().unwrap().doc.selection.is_some(), "Reselect brings the selection back");
assert_eq!(h.state().session.journal.last().map(|(id, _)| id.as_str()), Some("select.reselect"));
}
/// The rows greyed without a selection, as measured over each layer kind (#12866). Load
/// Selection… and Reselect follow the document, not the layer, and are left out.
const GREYED: [(&str, &[&str]); 3] = [
("Background", &["Blending Options…", "Group from Layers…", "Convert to Layers", "Rasterize Layer", "Rasterize Layer Style", "Free Transform"]),
("Type", &["Convert to Layers", "Rasterize Layer Style"]),
("Shape", &["Convert to Layers", "Rasterize Layer Style"]),
];
#[test]
fn background_type_and_shape_layers_get_the_layer_menu_greyed_as_measured() {
for (kind, greyed) in GREYED {
let mut app = app();
// The Background stays active under a second layer, so Delete Layer has one to keep.
let background = app.session.active().and_then(|st| st.active_layer).unwrap();
app.run("layer.new.layer", json!({})).unwrap();
match kind {
"Background" => app.session.set_active_layer(Some(background)),
"Type" => {
app.run("type.create", json!({"x": 4, "y": 20, "text": "A", "size": 12})).unwrap();
}
_ => {
app.run("shape.create", json!({"kind": "rect", "rect": [4, 4, 20, 20], "fill": "#ff0000"})).unwrap();
}
}
let st = app.session.active().unwrap();
let layer = st.doc.layer(st.active_layer.unwrap()).unwrap();
assert_eq!(if st.doc.is_background(layer.id) { "Background" } else { layer.content.kind_name() }, kind);
app.ui.tool = Tool::ObjectSelection;
assert!(open(&mut app, &Context::default(), Tool::ObjectSelection, [10.0, 10.0]), "{kind}");
let menu = app.ui.canvas_tool_menu.clone().unwrap();
assert_eq!(labels(rows(&menu)), labels(COUNT_MENU), "{kind}");
let wrong: Vec<_> = rows(&menu)
.iter()
.flatten()
.filter(|(label, id)| !matches!(*label, "Load Selection…" | "Reselect") && available(&app, &menu, id) == greyed.contains(label))
.map(|(label, _)| *label)
.collect();
assert!(wrong.is_empty(), "{kind}: enabled or greyed unlike the reference: {wrong:?}");
}
}
/// The reference application greys Group from Layers… whenever the Background is selected, also
/// beside a pixel layer that is the active one.
#[test]
fn group_from_layers_is_greyed_with_the_background_among_the_selected_layers() {
let mut app = pixel_layer(&[]);
let st = app.session.active().unwrap();
let pixel = st.active_layer.unwrap();
let background = st.doc.layers.iter().map(|l| l.id).find(|id| st.doc.is_background(*id)).unwrap();
assert!(open(&mut app, &Context::default(), Tool::ObjectSelection, [10.0, 10.0]));
let menu = app.ui.canvas_tool_menu.clone().unwrap();
assert!(available(&app, &menu, "layer.new.groupFromLayers"), "a pixel layer alone can be grouped");
app.run("layer.select", json!({"layer": background.0, "mode": "add"})).unwrap();
app.session.set_active_layer(Some(pixel));
assert_eq!(app.session.active().unwrap().selected_layers().len(), 2);
assert!(open(&mut app, &Context::default(), Tool::ObjectSelection, [10.0, 10.0]));
let menu = app.ui.canvas_tool_menu.clone().unwrap();
assert!(!available(&app, &menu, "layer.new.groupFromLayers"), "the Background is selected too");
}
#[test]
fn every_object_selection_row_runs_a_known_command() {
let mut app = pixel_layer(&[]);
let items = crate::menus::menu_items(&app);
// Selected first, then deselected.
for id in ["select.all", "select.deselect"] {
app.run(id, json!({})).unwrap();
assert!(open(&mut app, &Context::default(), Tool::ObjectSelection, [10.0, 10.0]));
let menu = app.ui.canvas_tool_menu.clone().unwrap();
for (label, id) in rows(&menu).iter().flatten() {
assert!(items.iter().any(|i| i.id == *id) || photocraft_engine::commands::find(id).is_some(), "{label}: unknown command {id}");
}
}
}
#[test]
fn the_other_selection_tools_keep_their_menus() {
let tools = [
Tool::RectMarquee,
Tool::EllipseMarquee,
Tool::SingleRowMarquee,
Tool::SingleColumnMarquee,
Tool::Lasso,
Tool::PolygonLasso,
Tool::MagneticLasso,
Tool::MagicWand,
];
for tool in tools {
for (selection, menu) in [(true, SELECTION_MENU), (false, NO_SELECTION_MENU)] {
let mut app = app();
if selection {
app.run("select.all", json!({})).unwrap();
}
app.ui.tool = tool;
assert!(open(&mut app, &Context::default(), tool, [10.0, 10.0]), "{tool:?}");
let open_rows = rows(app.ui.canvas_tool_menu.as_ref().unwrap());
assert_eq!(labels(open_rows), labels(menu), "{tool:?}, selection {selection}");
}
}
}
@@ -387,7 +387,7 @@ fn transform_menu_follows_photoshop_order() {
}
}
fn labels(rows: &[Row]) -> Vec<Option<&str>> {
pub(super) fn labels(rows: &[Row]) -> Vec<Option<&str>> {
rows.iter().map(|row| row.map(|(label, _)| label)).collect()
}
+8 -1
View File
@@ -27,6 +27,8 @@ fn background_blocks(id: &str, has_selection: bool) -> bool {
"layer.createClippingMask"
| "layer.releaseClippingMask"
| "layer.mergeDown"
// The Background can't go into a group (#12866, measured on the canvas menu).
| "layer.new.groupFromLayers"
| "layer.lockLayers"
| "layer.maskAllObjects"
| "edit.puppetWarp"
@@ -104,6 +106,11 @@ fn selection_targets(id: &str) -> Option<Targets> {
/// runs on all of them is greyed only when none of them can take it (the active layer alone must
/// not decide). Everything else, and a single selected layer, is judged by the active layer.
pub fn disabled_for_selection(doc: &Document, selected: &[LayerId], active: Option<&Layer>, id: &str) -> bool {
// The reference application refuses Group from Layers whenever the Background is among the
// selected layers, not only when it is the active one.
if id == "layer.new.groupFromLayers" && selected.iter().filter_map(|l| doc.layer(*l)).any(|l| crate::doc_props_ui::is_background(doc, l)) {
return true;
}
let Some(rule) = selection_targets(id).filter(|_| selected.len() > 1) else { return disabled_for(doc, active, id) };
let layers = selected.iter().filter_map(|l| doc.layer(*l));
let can = |l: &Layer| !disabled_for(doc, Some(l), id) && (!matches!(rule, Targets::WithoutMask) || l.mask.is_none());
@@ -186,7 +193,7 @@ pub fn disabled(app: &crate::PhotocraftApp, id: &str) -> bool {
return true;
}
// Only these commands depend on the selection; skip walking the layers for the rest.
if selection_targets(id).is_some() {
if selection_targets(id).is_some() || id == "layer.new.groupFromLayers" {
return disabled_for_selection(&st.doc, &st.selected_layers(), active, id);
}
disabled_for(&st.doc, active, id)
@@ -63,9 +63,10 @@ const NO_SELECTION: [&str; 17] = [
"Reselect",
];
/// The reference also greys Group from Layers… and Load Selection…, but their menu-bar twins are
/// live here (as in the Count tool's menu), and the rows follow the twins.
const NO_SELECTION_GREY: [&str; 5] = ["Blending Options…", "Convert to Layers", "Rasterize Layer", "Rasterize Layer Style", "Free Transform"];
/// The reference also greys Load Selection…, but its menu-bar twin is live here (as in the Count
/// tool's menu), and the row follows the twin.
const NO_SELECTION_GREY: [&str; 6] =
["Blending Options…", "Group from Layers…", "Convert to Layers", "Rasterize Layer", "Rasterize Layer Style", "Free Transform"];
const IN_SELECTION: [&str; 23] = [
"Deselect",