diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu.rs b/crates/ui-egui/src/canvas/canvas_tool_menu.rs index 2b8f6bcabe8..aa904401a60 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu.rs @@ -750,6 +750,20 @@ 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) +} + +/// 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) @@ -871,7 +885,16 @@ fn inside_corners(corners: &[craft::draw::ui::corner::CornerPoint], p: [f64; 2]) /// 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. The Add Anchor Point menu names the selected anchors (#12710). +/// 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 = several_layers_selected(app); + if let Some(label) = crate::layer_menu_ui::layer_count_label(command, multi) { + return label; + } + } if menu.tool == Tool::AddAnchorPoint { return add_anchor_row_label(app, menu, label, command); } @@ -1424,6 +1447,16 @@ 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 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_pixel_layers_selected(app) + { + return false; + } if !(path_menu(menu.tool) || shape_tool_menu(menu.tool)) { return crate::menus::is_enabled(app, command); } @@ -1848,6 +1881,12 @@ pub fn entries(app: &PhotocraftApp, menu: &CanvasToolMenu) -> Vec> "Stroke Subpath…" => tl!("Stroke Subpath…"), "Stroke Subpaths…" => tl!("Stroke Subpaths…"), "Rasterize Vector Mask" => tl!("Rasterize Vector Mask"), + // `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), }; out.push(Entry::new(command.to_string(), text.to_string()).enabled(entry_enabled(app, menu, command)).checked(mode_checked(app, menu, command))); @@ -1956,6 +1995,9 @@ mod delete_anchor_menu_tests; #[cfg(test)] mod convert_point_menu_tests; +#[cfg(test)] +mod layer_plural_tests; + #[cfg(test)] mod object_selection_menu_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..935fe827868 --- /dev/null +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/layer_plural_tests.rs @@ -0,0 +1,198 @@ +//! #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) -> (Box, 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; + let ctx = app.ctx().clone(); + (app, ctx, [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, 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. + 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() + .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, 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}"); + } + // 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. +#[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, 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, 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"); + } +} + +/// 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"); +} diff --git a/crates/ui-egui/src/panels/layer_menu_ui.rs b/crates/ui-egui/src/panels/layer_menu_ui.rs index e15158af73d..b0608c09ec7 100644 --- a/crates/ui-egui/src/panels/layer_menu_ui.rs +++ b/crates/ui-egui/src/panels/layer_menu_ui.rs @@ -48,12 +48,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"))); @@ -72,7 +89,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(_) => {