From 6d379020cb0f21fc1d9f946dc52835ee0cb4b6ac Mon Sep 17 00:00:00 2001 From: acebear17 <121527825+acebear17@users.noreply.github.com> Date: Sun, 29 Nov 2026 00:49:13 +0000 Subject: [PATCH] =?UTF-8?q?Layers:=20the=20Background=20row's=20menu=20sta?= =?UTF-8?q?rts=20with=20Layer=20from=20Background=E2=80=A6=20(#16764)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/panels/background_row_menu_tests.rs | 99 +++++++++++++++++++ crates/ui-egui/src/panels/layer_menu_ui.rs | 17 ++++ crates/ui-egui/src/panels/layers_tree.rs | 3 +- 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 crates/ui-egui/src/panels/background_row_menu_tests.rs diff --git a/crates/ui-egui/src/panels/background_row_menu_tests.rs b/crates/ui-egui/src/panels/background_row_menu_tests.rs new file mode 100644 index 0000000000..422082c587 --- /dev/null +++ b/crates/ui-egui/src/panels/background_row_menu_tests.rs @@ -0,0 +1,99 @@ +//! The Background row's context menu starts with Layer from Background… and a separator, the Layer › New entry +//! (#16764); other rows, a locked pixel layer among them, keep the menu they had. + +use craft::layers::ui::panel::{LayersEvent, MenuTarget}; +use craft::layers::ui::tree::LayerRowKey; +use photocraft_doc::LayerId; +use photocraft_engine::Session; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::panels::layers_tree::{PhotoLayers, row_menu}; + +const FROM_BACKGROUND: &str = "layer.new.layerFromBackground"; + +/// A document with its Background and a pixel layer "Ink" above it; "Ink" is active. +fn setup() -> Box { + let mut app = PhotocraftApp::new(Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 32, "height": 24, "background": "white"})).unwrap(); + app.run("layer.new.layer", json!({"name": "Ink"})).unwrap(); + app +} + +fn background(app: &PhotocraftApp) -> Option { + let st = app.session.active()?; + st.doc.walk().into_iter().map(|(_, _, l)| l.id).find(|id| st.doc.is_background(*id)) +} + +fn active(app: &PhotocraftApp) -> LayerId { + app.session.active().and_then(|st| st.active_layer).expect("a layer is active") +} + +/// The right-clicked row's menu as (value, enabled); a separator is `None`. +fn menu(app: &PhotocraftApp, id: LayerId) -> Vec> { + let st = app.session.active().unwrap(); + let tree = PhotoLayers::new(app, st, &Default::default(), false, None); + row_menu(app, &tree, &MenuTarget::new(Some(LayerRowKey::Layer(id)))) + .iter() + .map(|e| if e.is_separator() { None } else { e.value().map(|v| (v.clone(), e.is_enabled())) }) + .collect() +} + +fn values(menu: &[Option<(String, bool)>]) -> Vec> { + menu.iter().map(|e| e.as_ref().map(|(v, _)| v.clone())).collect() +} + +fn offers(menu: &[Option<(String, bool)>]) -> bool { + menu.iter().flatten().any(|(v, _)| v == FROM_BACKGROUND) +} + +fn last_step(app: &PhotocraftApp) -> Option { + app.session.active().unwrap().history.entries().last().cloned() +} + +#[test] +fn the_background_row_starts_with_layer_from_background_and_a_separator() { + let mut app = setup(); + let ink = values(&menu(&app, active(&app))); + let bg = background(&app).expect("the document has a Background"); + // The right-click selects the row it opens on. + app.run("layer.select", json!({"layer": bg.0, "mode": "replace"})).unwrap(); + let m = menu(&app, bg); + assert_eq!(m.first().cloned().flatten(), Some((FROM_BACKGROUND.to_string(), true)), "first and available: {m:?}"); + assert_eq!(m.get(1), Some(&None), "a separator follows: {m:?}"); + assert_eq!(values(&m)[2..], ink[..], "the rest is the menu another pixel layer gets"); +} + +#[test] +fn other_rows_have_no_layer_from_background() { + let mut app = setup(); + let ink = active(&app); + assert!(!offers(&menu(&app, ink)), "a pixel layer"); + // A pixel layer above the Background with its locks: it is not the Background. + app.run("layer.lockLayers", json!({"transparency": true, "position": true})).unwrap(); + let locked = app.session.active().unwrap().doc.layer(ink).unwrap().clone(); + assert!(locked.locks.transparency && locked.locks.position, "the locks took"); + assert!(!offers(&menu(&app, ink)), "a locked pixel layer"); + app.run("layer.groupLayers", json!({"name": "G"})).unwrap(); + assert!(!offers(&menu(&app, active(&app))), "a group"); +} + +/// Picking the entry converts the Background in the same History step as Layer › New › Layer from Background… +#[test] +fn picking_the_entry_converts_the_background_like_the_layer_menu() { + let mut app = setup(); + let bg = background(&app).unwrap(); + app.run("layer.select", json!({"layer": bg.0, "mode": "replace"})).unwrap(); + let ctx = app.ctx().clone(); + let pick = LayersEvent::menu(MenuTarget::new(Some(LayerRowKey::Layer(bg))), FROM_BACKGROUND); + crate::panels::layers_events::apply_layers_events(&mut app, &ctx, vec![pick]); + assert_eq!(background(&app), None, "the Background became a normal layer"); + assert!(!offers(&menu(&app, bg)), "and its row no longer offers the entry"); + + let mut menu_app = setup(); + let menu_bg = background(&menu_app).unwrap(); + menu_app.run("layer.select", json!({"layer": menu_bg.0, "mode": "replace"})).unwrap(); + crate::menus::invoke(&mut menu_app, &ctx, FROM_BACKGROUND, json!({})).unwrap(); + assert_eq!(background(&menu_app), None); + assert_eq!(last_step(&app), last_step(&menu_app), "the same History step as the Layer menu"); +} diff --git a/crates/ui-egui/src/panels/layer_menu_ui.rs b/crates/ui-egui/src/panels/layer_menu_ui.rs index 0d55e1330a..5aa0be8933 100644 --- a/crates/ui-egui/src/panels/layer_menu_ui.rs +++ b/crates/ui-egui/src/panels/layer_menu_ui.rs @@ -62,6 +62,19 @@ pub(crate) fn layer_count_label(command: &str, multi: bool) -> Option<&'static s Some(if multi { several } else { one }) } +/// The context menu entries for a layer's row: [`entries`], led on the Background's row +/// (`background`, [`photocraft_doc::Document::is_background`]) by Layer from Background… and a +/// separator, the Layer › New entry (#16764). A multi-selection keeps the plain entries. +pub fn row_entries(l: &Layer, multi: bool, has_selection: bool, background: bool) -> Vec { + let mut v: Vec = Vec::new(); + if background && !multi { + v.push(Some((tl!("Layer from Background…"), "layer.new.layerFromBackground"))); + v.push(None); + } + v.extend(entries(l, multi, has_selection)); + v +} + /// 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"))]; @@ -185,3 +198,7 @@ mod tests; #[cfg(test)] #[path = "layer_menu_artboard_group_tests.rs"] mod layer_menu_artboard_group_tests; + +#[cfg(test)] +#[path = "background_row_menu_tests.rs"] +mod background_row_menu_tests; diff --git a/crates/ui-egui/src/panels/layers_tree.rs b/crates/ui-egui/src/panels/layers_tree.rs index 1b30713394..13a08ce3fe 100644 --- a/crates/ui-egui/src/panels/layers_tree.rs +++ b/crates/ui-egui/src/panels/layers_tree.rs @@ -721,7 +721,8 @@ pub(crate) fn row_menu(app: &PhotocraftApp, tree: &PhotoLayers, target: &MenuTar MenuKind::Effects => commands(crate::layer_menu_ui::effects_entries(), false), MenuKind::Layer(id) => { let Some(l) = tree.layer(&id) else { return Vec::new() }; - let mut out = commands(crate::layer_menu_ui::entries(l, target.on_selection, has_selection), target.on_selection); + let background = tree.doc().is_background(id); + let mut out = commands(crate::layer_menu_ui::row_entries(l, target.on_selection, has_selection, background), target.on_selection); out.push(Entry::separator()); out.push(Entry::new(RENAME.to_string(), tl!("Rename Layer…").to_string())); out