From 8947ad041163123f64cc0497fe8de5f066d60066 Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Thu, 12 Nov 2026 09:55:37 +0100 Subject: [PATCH 1/3] ui: canvas layer menu reads Duplicate/Delete/Rasterize Layers with several layers selected (#12358) The Layers panel menu already switches Duplicate Layer..., Delete Layer and Rasterize Layer to the plural when several layers are selected, but the canvas tools' layer menu (the Count tool's right-click menu, among others) showed its static table's singular labels whatever the selection. Move the rule into `layer_menu_ui::layer_count_label(command, multi)`, used by the Layers panel's `entries` (same labels as before) and by the canvas menu's `row_label`, which feeds both the drawn menu and `ui.inspect`. The canvas menu counts the selected layers only for those three rows. Command ids and order don't change, and the new labels are spelled out for `cargo xtask i18n-coverage`. Tests (layer_plural_tests.rs): two selected layers give the plural labels in the measured order, one gives the singular, the labels match the Layers panel's, and Delete Layers still deletes both layers. The plural and panel-match tests fail without the fix. Fixes #12358 --- crates/ui-egui/src/canvas/canvas_tool_menu.rs | 20 ++- .../canvas_tool_menu/layer_plural_tests.rs | 160 ++++++++++++++++++ crates/ui-egui/src/panels/layer_menu_ui.rs | 26 ++- 3 files changed, 202 insertions(+), 4 deletions(-) create mode 100644 crates/ui-egui/src/canvas/canvas_tool_menu/layer_plural_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 d600445e757..310266a7c27 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu.rs @@ -627,8 +627,17 @@ pub const PEN_MENU: &[Row] = &[ /// The label a row shows, untranslated. The Pen's transform row names what it acts on, as in /// Photoshop (#12913): the points of the path being drawn, or nothing when the document has no /// path at all. A path that exists but isn't active, and the Path Selection and Direct Selection -/// menus, keep the table's label. +/// menus, keep the table's label. The layer rows that act on every selected layer (Duplicate, +/// Delete, Rasterize) read in the plural with several layers selected, as in the Layers panel +/// menu (#12358). pub(crate) fn row_label(app: &PhotocraftApp, menu: &CanvasToolMenu, label: &'static str, command: &str) -> &'static str { + // Only these rows count the selected layers (the menu asks for every row each frame). + if crate::layer_menu_ui::layer_count_label(command, false).is_some() { + let multi = app.session.active().is_some_and(|st| st.selected_layers().len() > 1); + if let Some(label) = crate::layer_menu_ui::layer_count_label(command, multi) { + return label; + } + } if command != "path.transform" || menu.tool != Tool::Pen { return label; } @@ -1780,6 +1789,12 @@ pub fn show(app: &mut PhotocraftApp, ctx: &Context) { let text = match row_label(app, &menu, label, command) { // Spelled out so `cargo xtask i18n-coverage` lists the new label. "Free Transform Points" => tl!("Free Transform Points"), + // `layer_menu_ui::layer_count_label`'s labels, likewise (#12358). + "Duplicate Layer…" => tl!("Duplicate Layer…"), + "Delete Layer" => tl!("Delete Layer"), + "Duplicate Layers…" => tl!("Duplicate Layers…"), + "Delete Layers" => tl!("Delete Layers"), + "Rasterize Layers" => tl!("Rasterize Layers"), key => tl!(key), }; let item = egui::Button::selectable(mode_checked(app, &menu, command), text).min_size(egui::vec2(200.0, 20.0)); @@ -1838,3 +1853,6 @@ mod delete_anchor_menu_tests; #[cfg(test)] mod convert_point_menu_tests; + +#[cfg(test)] +mod layer_plural_tests; diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/layer_plural_tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/layer_plural_tests.rs new file mode 100644 index 00000000000..6c5a4c2a64c --- /dev/null +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/layer_plural_tests.rs @@ -0,0 +1,160 @@ +//! #12358: with several layers selected, the canvas layer menu's Duplicate, Delete and Rasterize +//! rows read in the plural, as the Layers panel menu does; with one layer selected they stay +//! singular. The labels come from the panel's rule and the command ids don't change. + +use serde_json::{Value, json}; + +use super::count_tests::{call, right_click}; +use super::*; + +/// The rows whose label names one layer or several, with their singular and plural labels. +const COUNTED: [(&str, &str, &str); 3] = [ + ("layer.duplicate", "Duplicate Layer…", "Duplicate Layers…"), + ("layer.delete", "Delete Layer", "Delete Layers"), + ("layer.rasterize.layer", "Rasterize Layer", "Rasterize Layers"), +]; + +/// The Count tool's layer menu ids in the reference order (#11772), "―" for separators. +const WANT: [&str; 17] = [ + "layer.layerStyle.blendingOptions", + "―", + "layer.duplicate", + "layer.delete", + "layer.new.groupFromLayers", + "―", + "layer.smartObjects.convertToSmartObject", + "layer.smartObjects.convertToLayers", + "―", + "layer.rasterize.layer", + "layer.rasterize.layerStyle", + "―", + "edit.freeTransform", + "―", + "select.colorRange", + "select.loadSelection", + "select.reselect", +]; + +/// A 64 × 48 document with two pixel layers above the background, the top one active; with +/// `both`, the other one is selected too. The Count tool is current. +fn app(both: bool) -> (PhotocraftApp, egui::Context, [u64; 2]) { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + let layer = |r: Value| r["layer"].as_u64().unwrap(); + let ink = layer(app.run("layer.new.layer", json!({"name": "Ink"})).unwrap()); + let paint = layer(app.run("layer.new.layer", json!({"name": "Paint"})).unwrap()); + if both { + app.run("layer.select", json!({"layer": ink, "mode": "add"})).unwrap(); + } + app.sync_views(); + app.ui.tool = Tool::Count; + (app, egui::Context::default(), [ink, paint]) +} + +/// The open menu's `ui.inspect` entries as (id, label, enabled), "―" for separators. +fn inspected(app: &mut PhotocraftApp, ctx: &egui::Context) -> Vec<(String, String, bool)> { + let r = call(app, ctx, "ui.inspect", json!({})); + let entries = r.pointer("/result/canvasToolMenu/entries").and_then(Value::as_array).cloned().unwrap_or_default(); + entries + .iter() + .map(|e| match e["id"].as_str() { + Some(id) => (id.to_string(), e["label"].as_str().unwrap_or_default().to_string(), e["enabled"].as_bool().unwrap_or_default()), + None => ("―".to_string(), String::new(), false), + }) + .collect() +} + +fn selected(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().selected_layers().len() +} + +#[test] +fn with_two_layers_selected_the_layer_rows_read_in_the_plural() { + let (mut app, ctx, _) = app(true); + assert_eq!(selected(&app), 2); + right_click(&mut app, &ctx, 50.0, 40.0); + let entries = inspected(&mut app, &ctx); + assert_eq!(entries.iter().map(|e| e.0.as_str()).collect::>(), WANT, "the command ids and their order don't change"); + for (id, _, plural) in COUNTED { + let label = entries.iter().find(|e| e.0 == id).map(|e| e.1.as_str()); + assert_eq!(label, Some(plural), "{id}"); + } + let labels: Vec<&str> = entries.iter().filter(|e| e.0 != "―").map(|e| e.1.as_str()).collect(); + assert_eq!( + labels, + [ + "Blending Options…", + "Duplicate Layers…", + "Delete Layers", + "Group from Layers…", + "Convert to Smart Object", + "Convert to Layers", + "Rasterize Layers", + "Rasterize Layer Style", + "Free Transform", + "Color Range…", + "Load Selection…", + "Reselect", + ], + "the measured list" + ); + // Greyed in the reference with two pixel layers and no pixel selection; the plural rows act on + // the whole selection and stay available. Blending Options, Color Range… and Load Selection… + // (also greyed there) are left out: the rows keep following `menus::is_enabled`, which enables + // them here, and #12358 changes labels only. + let greyed = ["layer.smartObjects.convertToLayers", "layer.rasterize.layer", "layer.rasterize.layerStyle"]; + let enabled = ["layer.duplicate", "layer.delete", "layer.new.groupFromLayers", "layer.smartObjects.convertToSmartObject", "edit.freeTransform"]; + let wrong: Vec<(&str, Option)> = greyed + .iter() + .map(|id| (*id, false)) + .chain(enabled.iter().map(|id| (*id, true))) + .filter_map(|(id, want)| { + let got = entries.iter().find(|e| e.0 == id).map(|e| e.2); + (got != Some(want)).then_some((id, got)) + }) + .collect(); + assert!(wrong.is_empty(), "rows enabled otherwise than in the reference: {wrong:?}"); +} + +#[test] +fn with_one_layer_selected_the_layer_rows_stay_singular() { + let (mut app, ctx, _) = app(false); + assert_eq!(selected(&app), 1); + right_click(&mut app, &ctx, 50.0, 40.0); + let entries = inspected(&mut app, &ctx); + assert_eq!(entries.iter().map(|e| e.0.as_str()).collect::>(), WANT); + for (id, singular, _) in COUNTED { + let label = entries.iter().find(|e| e.0 == id).map(|e| e.1.as_str()); + assert_eq!(label, Some(singular), "{id}"); + } +} + +/// The canvas rows and the Layers panel menu take their labels from the same rule. +#[test] +fn the_labels_match_the_layers_panel_menu() { + for multi in [false, true] { + let (mut app, ctx, [_, paint]) = app(multi); + right_click(&mut app, &ctx, 50.0, 40.0); + let entries = inspected(&mut app, &ctx); + let st = app.session.active().unwrap(); + let layer = st.doc.layer(photocraft_doc::LayerId(paint)).unwrap(); + let panel = crate::layer_menu_ui::entries(layer, multi, false); + for (id, _, _) in COUNTED { + let canvas = entries.iter().find(|e| e.0 == id).map(|e| e.1.as_str()); + let panel = panel.iter().flatten().find(|e| e.1 == id).map(|e| e.0); + assert_eq!(canvas, panel, "{id}, several layers: {multi}"); + } + } +} + +/// Delete Layers still runs `layer.delete`, which removes every selected layer. +#[test] +fn delete_layers_still_deletes_the_selected_layers() { + let (mut app, ctx, [ink, paint]) = app(true); + right_click(&mut app, &ctx, 50.0, 40.0); + assert_eq!(call(&mut app, &ctx, "ui.context.choose", json!({"id": "layer.delete"}))["ok"], true); + let st = app.session.active().unwrap(); + for id in [ink, paint] { + assert!(st.doc.layer(photocraft_doc::LayerId(id)).is_none(), "layer {id} deleted"); + } +} diff --git a/crates/ui-egui/src/panels/layer_menu_ui.rs b/crates/ui-egui/src/panels/layer_menu_ui.rs index 6158ba89972..0e6a57a919f 100644 --- a/crates/ui-egui/src/panels/layer_menu_ui.rs +++ b/crates/ui-egui/src/panels/layer_menu_ui.rs @@ -49,12 +49,29 @@ pub fn single_layer_only(id: &str) -> bool { matches!(id, "layer.layerStyle.blendingOptions" | "layer.layerStyle.copyLayerStyle") || (id.starts_with("layer.layerMask.") && !adds_mask) } +/// The untranslated label of a layer row that names one layer or several: Duplicate, Delete and +/// Rasterize act on every selected layer, so with several selected (`multi`) they read in the +/// plural, as in Photoshop. `None` for other commands. Shared by this menu and the canvas tools' +/// layer menu (#12358). +pub(crate) fn layer_count_label(command: &str, multi: bool) -> Option<&'static str> { + let (one, several) = match command { + "layer.duplicate" => ("Duplicate Layer…", "Duplicate Layers…"), + "layer.delete" => ("Delete Layer", "Delete Layers"), + "layer.rasterize.layer" => ("Rasterize Layer", "Rasterize Layers"), + _ => return None, + }; + Some(if multi { several } else { one }) +} + /// The context menu entries for a layer (Photoshop 2026 order, trimmed to the layer kind). pub fn entries(l: &Layer, multi: bool, has_selection: bool) -> Vec { let mut v: Vec = vec![Some((tl!("Blending Options…"), "layer.layerStyle.blendingOptions"))]; v.push(None); - v.push(Some((if multi { tl!("Duplicate Layers…") } else { tl!("Duplicate Layer…") }, "layer.duplicate"))); - v.push(Some((if multi { tl!("Delete Layers") } else { tl!("Delete Layer") }, "layer.delete"))); + for id in ["layer.duplicate", "layer.delete"] { + if let Some(label) = layer_count_label(id, multi) { + v.push(Some((tl!(label), id))); + } + } // Groups the clicked layer, or the whole selection when several layers are selected, after // Photoshop's New Group dialog. v.push(Some((tl!("Group from Layers…"), "layer.new.groupFromLayers"))); @@ -73,7 +90,10 @@ pub fn entries(l: &Layer, multi: bool, has_selection: bool) -> Vec { // One item for the whole selection, whatever the clicked layer is: Rasterize acts on every // selected layer. Edit Contents and Convert to Layers work on one layer, so they are not // offered here. - v.push(Some(("Rasterize Layers", "layer.rasterize.layer"))); + let id = "layer.rasterize.layer"; + if let Some(label) = layer_count_label(id, multi) { + v.push(Some((label, id))); + } } else { match &l.content { LayerContent::Text(_) => { From 55e9b409bc1accbd1ab02334088455080ccf8a72 Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:02:34 +0100 Subject: [PATCH 2/3] ui: Count tool layer menu greys Blending Options, Color Range and Load Selection with several layers selected (#12358) The reference greys these three rows in the Count tool's canvas layer menu with two pixel layers selected: they work on one layer. The other rows of the measured list already matched. The rule is limited to the Count tool, the measured case; the other tools' layer menus keep `menus::is_enabled`. The plural test now asserts every measured grey row and fails without the change; the one-layer test checks the three rows keep `menus::is_enabled`. --- crates/ui-egui/src/canvas/canvas_tool_menu.rs | 16 +++++++++++++++- .../canvas_tool_menu/layer_plural_tests.rs | 18 ++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu.rs b/crates/ui-egui/src/canvas/canvas_tool_menu.rs index bb211cf445f..c8d87b2e0e9 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu.rs @@ -626,6 +626,11 @@ pub const PEN_MENU: &[Row] = &[ Some(("Disable Symmetry Path", "paint.symmetryDisable")), ]; +/// Several layers are selected in the front document (#12358). +fn several_layers_selected(app: &PhotocraftApp) -> bool { + app.session.active().is_some_and(|st| st.selected_layers().len() > 1) +} + /// The label a row shows, untranslated. The Pen's transform row names what it acts on, as in /// Photoshop (#12913): the points of the path being drawn, or nothing when the document has no /// path at all. A path that exists but isn't active, and the Path Selection and Direct Selection @@ -635,7 +640,7 @@ pub const PEN_MENU: &[Row] = &[ pub(crate) fn row_label(app: &PhotocraftApp, menu: &CanvasToolMenu, label: &'static str, command: &str) -> &'static str { // Only these rows count the selected layers (the menu asks for every row each frame). if crate::layer_menu_ui::layer_count_label(command, false).is_some() { - let multi = app.session.active().is_some_and(|st| st.selected_layers().len() > 1); + let multi = several_layers_selected(app); if let Some(label) = crate::layer_menu_ui::layer_count_label(command, multi) { return label; } @@ -1243,6 +1248,15 @@ pub fn entry_enabled(app: &PhotocraftApp, menu: &CanvasToolMenu, command: &str) 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); } + // #12358, measured on the Count tool's layer menu: with several layers selected, Blending + // Options…, Color Range… and Load Selection… work on one layer, so they are greyed. The other + // tools' layer menus weren't measured with several layers and keep `menus::is_enabled`. + if menu.tool == Tool::Count + && matches!(command, "layer.layerStyle.blendingOptions" | "select.colorRange" | "select.loadSelection") + && several_layers_selected(app) + { + return false; + } if menu.transform || !path_menu(menu.tool) { return crate::menus::is_enabled(app, command); } diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/layer_plural_tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/layer_plural_tests.rs index 6c5a4c2a64c..bc0fc4fa85b 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu/layer_plural_tests.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/layer_plural_tests.rs @@ -99,10 +99,15 @@ fn with_two_layers_selected_the_layer_rows_read_in_the_plural() { "the measured list" ); // Greyed in the reference with two pixel layers and no pixel selection; the plural rows act on - // the whole selection and stay available. Blending Options, Color Range… and Load Selection… - // (also greyed there) are left out: the rows keep following `menus::is_enabled`, which enables - // them here, and #12358 changes labels only. - let greyed = ["layer.smartObjects.convertToLayers", "layer.rasterize.layer", "layer.rasterize.layerStyle"]; + // the whole selection and stay available. + let greyed = [ + "layer.layerStyle.blendingOptions", + "layer.smartObjects.convertToLayers", + "layer.rasterize.layer", + "layer.rasterize.layerStyle", + "select.colorRange", + "select.loadSelection", + ]; let enabled = ["layer.duplicate", "layer.delete", "layer.new.groupFromLayers", "layer.smartObjects.convertToSmartObject", "edit.freeTransform"]; let wrong: Vec<(&str, Option)> = greyed .iter() @@ -127,6 +132,11 @@ fn with_one_layer_selected_the_layer_rows_stay_singular() { let label = entries.iter().find(|e| e.0 == id).map(|e| e.1.as_str()); assert_eq!(label, Some(singular), "{id}"); } + // With one layer the rows greyed only for several layers keep following `menus::is_enabled`. + for id in ["layer.layerStyle.blendingOptions", "select.colorRange", "select.loadSelection"] { + let enabled = entries.iter().find(|e| e.0 == id).map(|e| e.2); + assert_eq!(enabled, Some(crate::menus::is_enabled(&app, id)), "{id}"); + } } /// The canvas rows and the Layers panel menu take their labels from the same rule. From 9abf147684802b117c23fea549d3bf83914a87ea Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:37:36 +0100 Subject: [PATCH 3/3] ui: grey the Count tool's one-layer rows only with several pixel layers selected (#12358) --- crates/ui-egui/src/canvas/canvas_tool_menu.rs | 18 ++++++++++--- .../canvas_tool_menu/layer_plural_tests.rs | 27 +++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu.rs b/crates/ui-egui/src/canvas/canvas_tool_menu.rs index 2fa4dba674a..5eb20e9a9ec 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu.rs @@ -753,6 +753,15 @@ fn several_layers_selected(app: &PhotocraftApp) -> bool { app.session.active().is_some_and(|st| st.selected_layers().len() > 1) } +/// Several layers are selected in the front document, every one a pixel layer: the case #12358 +/// measured on the Count tool's layer menu. +fn several_pixel_layers_selected(app: &PhotocraftApp) -> bool { + app.session.active().is_some_and(|st| { + let selected = st.selected_layers(); + selected.len() > 1 && selected.iter().all(|id| st.doc.layer(*id).is_some_and(|l| matches!(l.content, LayerContent::Raster(_)))) + }) +} + /// Is a path with at least two points being drawn with the Pen? pub(crate) fn pen_pending(app: &PhotocraftApp) -> bool { app.ui.pen.as_ref().is_some_and(|p| p.draft.knots.len() >= 2) @@ -1372,12 +1381,13 @@ pub fn entry_enabled(app: &PhotocraftApp, menu: &CanvasToolMenu, command: &str) 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); } - // #12358, measured on the Count tool's layer menu: with several layers selected, Blending - // Options…, Color Range… and Load Selection… work on one layer, so they are greyed. The other - // tools' layer menus weren't measured with several layers and keep `menus::is_enabled`. + // #12358, measured on the Count tool's layer menu: with several pixel layers selected, + // Blending Options…, Color Range… and Load Selection… work on one layer, so they are greyed. + // Other layer kinds and the other tools' layer menus weren't measured with several layers and + // keep `menus::is_enabled`. if menu.tool == Tool::Count && matches!(command, "layer.layerStyle.blendingOptions" | "select.colorRange" | "select.loadSelection") - && several_layers_selected(app) + && several_pixel_layers_selected(app) { return false; } diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/layer_plural_tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/layer_plural_tests.rs index 472a2015b61..935fe827868 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu/layer_plural_tests.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/layer_plural_tests.rs @@ -169,3 +169,30 @@ fn delete_layers_still_deletes_the_selected_layers() { assert!(st.doc.layer(photocraft_doc::LayerId(id)).is_none(), "layer {id} deleted"); } } + +/// The greying is limited to the measured case, several pixel layers: with two shape layers +/// selected, Blending Options…, Color Range… and Load Selection… follow `menus::is_enabled`. +#[test] +fn with_two_shape_layers_selected_the_one_layer_rows_follow_the_menu_bar() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + let shape = |app: &mut PhotocraftApp, rect: [u32; 4]| { + app.run("shape.create", json!({"kind": "rect", "rect": rect, "fill": "#3070c0"})).unwrap()["layer"].as_u64().unwrap() + }; + let first = shape(&mut app, [4, 4, 10, 10]); + shape(&mut app, [30, 20, 10, 10]); + app.run("layer.select", json!({"layer": first, "mode": "add"})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::Count; + let ctx = app.ctx().clone(); + assert_eq!(selected(&app), 2); + right_click(&mut app, 50.0, 40.0); + let entries = inspected(&mut app, &ctx); + assert!(!entries.is_empty(), "the Count tool's layer menu is open"); + let rows = ["layer.layerStyle.blendingOptions", "select.colorRange", "select.loadSelection"]; + for id in rows { + let enabled = entries.iter().find(|e| e.0 == id).map(|e| e.2); + assert_eq!(enabled, Some(crate::menus::is_enabled(&app, id)), "{id}"); + } + assert!(rows.iter().any(|id| crate::menus::is_enabled(&app, id)), "the menu bar enables one of them, so the case tells the rules apart"); +}