From 29a3c7ece02206f7ee88f0902a55c07578f6d8ed Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Wed, 25 Nov 2026 18:00:24 +0100 Subject: [PATCH] =?UTF-8?q?Layers:=20with=20the=20Background=20active,=20t?= =?UTF-8?q?he=20panel=20menu=20starts=20with=20Layer=20from=20Background?= =?UTF-8?q?=E2=80=A6=20(#16765)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Layers panel menu now opens with Layer from Background… and a separator while the Background is the active layer; the row runs layer.new.layerFromBackground as Layer › New › Layer from Background… does. With any other layer active the menu is unchanged. --- .../flyout_layer_from_background_tests.rs | 73 +++++++++++++++++++ crates/ui-egui/src/panels/layers_panel_ui.rs | 11 ++- 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 crates/ui-egui/src/panels/flyout_layer_from_background_tests.rs diff --git a/crates/ui-egui/src/panels/flyout_layer_from_background_tests.rs b/crates/ui-egui/src/panels/flyout_layer_from_background_tests.rs new file mode 100644 index 00000000000..e856daae178 --- /dev/null +++ b/crates/ui-egui/src/panels/flyout_layer_from_background_tests.rs @@ -0,0 +1,73 @@ +//! The Layers panel menu's Layer from Background… (#16765): with the Background active the menu starts with it and a +//! separator, and it converts the Background as Layer › New › Layer from Background… does; with another layer active +//! the menu is unchanged. +use craft::keymap::CommandRef; +use craft::ui::menu::PanelRow; +use serde_json::json; + +use super::panel_rows; +use crate::PhotocraftApp; + +const FROM_BACKGROUND: &str = "layer.new.layerFromBackground"; + +/// A document with its Background and a layer "Ink" above it; "Ink" is active. +fn doc_app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 32, "height": 24})).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)).map(|id| id.0) +} + +fn select_background(app: &mut PhotocraftApp) { + let id = background(app).expect("the document has a Background"); + app.run("layer.select", json!({"layer": id})).unwrap(); +} + +/// The panel menu's rows, by command id; a separator is `None`. +fn row_ids(app: &PhotocraftApp) -> Vec> { + panel_rows(app) + .into_iter() + .filter_map(|row| match row { + PanelRow::Command(c) => Some(Some(c.id.clone())), + PanelRow::Separator => Some(None), + _ => None, + }) + .collect() +} + +#[test] +fn with_the_background_active_the_menu_starts_with_layer_from_background_and_a_separator() { + let mut app = doc_app(); + let others = row_ids(&app); + select_background(&mut app); + let ids = row_ids(&app); + assert_eq!(ids.first().cloned().flatten().as_deref(), Some(FROM_BACKGROUND), "{ids:?}"); + assert_eq!(ids.get(1), Some(&None), "a separator follows: {ids:?}"); + assert_eq!(ids[2..], others[..], "the rest of the menu is the one another layer gets"); +} + +#[test] +fn with_another_layer_active_the_menu_has_no_layer_from_background() { + let app = doc_app(); + let ids = row_ids(&app); + assert!(!ids.iter().flatten().any(|id| id == FROM_BACKGROUND), "{ids:?}"); + assert_eq!(ids.first().cloned().flatten().as_deref(), Some("layer.new.layer"), "New Layer… stays first: {ids:?}"); +} + +#[test] +fn the_entry_converts_the_background_like_the_layer_menu_entry() { + let mut app = doc_app(); + select_background(&mut app); + assert!(crate::menus::is_enabled(&app, FROM_BACKGROUND), "the entry is available"); + // A picked menu row fires its command, which runs as the menu item does. + let ctx = app.ctx().clone(); + crate::shortcut_dispatch::run_fired(&mut app, &ctx, &[crate::blend_keys::test_fired(CommandRef::new(FROM_BACKGROUND))]); + assert!(!app.ui.status_error, "{}", app.ui.status); + assert_eq!(background(&app), None, "the Background became a normal layer"); + assert!(!row_ids(&app).iter().flatten().any(|id| id == FROM_BACKGROUND), "and the entry leaves the menu"); +} diff --git a/crates/ui-egui/src/panels/layers_panel_ui.rs b/crates/ui-egui/src/panels/layers_panel_ui.rs index 17988d94e98..910a71c209e 100644 --- a/crates/ui-egui/src/panels/layers_panel_ui.rs +++ b/crates/ui-egui/src/panels/layers_panel_ui.rs @@ -127,7 +127,10 @@ fn report(app: &mut PhotocraftApp, result: Result<(), String>) { pub(crate) fn panel_rows(app: &PhotocraftApp) -> Vec { let command = |id: &str| PanelRow::Command(CommandRef::new(id)); let any_hidden = app.session.active().is_some_and(|st| st.selected_layers().iter().any(|id| st.doc.layer(*id).is_some_and(|l| !l.visible))); - let mut rows = vec![ + // With the Background active the menu starts with Layer from Background…, the Layer › New entry (#16765). + let background_active = app.session.active().is_some_and(|st| st.active_layer.is_some_and(|id| st.doc.is_background(id))); + let mut rows = if background_active { vec![command("layer.new.layerFromBackground"), PanelRow::Separator] } else { Vec::new() }; + rows.extend([ command("layer.new.layer"), command("layer.new.group"), // Duplicate Layer… opens the Duplicate Layer dialog, like Layer › Duplicate Layer… (#14615). @@ -142,7 +145,7 @@ pub(crate) fn panel_rows(app: &PhotocraftApp) -> Vec { PanelRow::Separator, command("layer.new.artboard"), PanelRow::Separator, - ]; + ]); let words = Env::of_root(app.ctx()).words::(); rows.extend(craft::layers::ui::panel::panel_rows(&words)); rows @@ -323,6 +326,10 @@ pub(crate) fn display_reorder(app: &PhotocraftApp, index: usize) -> Option<(std: #[path = "layers_menu_duplicate_tests.rs"] mod layers_menu_duplicate_tests; +#[cfg(test)] +#[path = "flyout_layer_from_background_tests.rs"] +mod flyout_layer_from_background_tests; + #[cfg(test)] mod tests { use super::*;