diff --git a/crates/ui-egui/src/menus/background_entry_swap_tests.rs b/crates/ui-egui/src/menus/background_entry_swap_tests.rs new file mode 100644 index 0000000000..826aa29a4a --- /dev/null +++ b/crates/ui-egui/src/menus/background_entry_swap_tests.rs @@ -0,0 +1,82 @@ +//! Layer › New lists Layer from Background… or Background from Layer in one slot, never both (#16761). + +use serde_json::json; + +use super::*; +use crate::control::{ControlRequest, Reply}; + +const LAYER_FROM: &str = "layer.new.layerFromBackground"; +const BACKGROUND_FROM: &str = "layer.new.backgroundFromLayer"; + +fn app(doc: bool) -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + if doc { + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.sync_views(); + } + app +} + +/// The Layer › New rows the menu bar shows, as (id, label, enabled). +fn layer_new(app: &PhotocraftApp) -> Vec<(String, String, bool)> { + menu_rows(app).into_iter().filter(|r| r.path == ["Layer", "New"]).map(|r| (r.command.id, r.label, r.enabled)).collect() +} + +/// Which of the two entries `ui.menu.list` and Help search report. +fn listed_and_searched(app: &mut PhotocraftApp) -> (Vec, Vec) { + let ctx = app.ctx().clone(); + let (req, _rx) = ControlRequest::new("ui.menu.list", json!({})); + let Reply::Done(listed) = crate::control::handle(app, &ctx, &req) else { panic!("ui.menu.list: expected an immediate reply") }; + let pair = |id: &str| id == LAYER_FROM || id == BACKGROUND_FROM; + let listed = listed["result"].as_array().unwrap().iter().filter_map(|it| it["id"].as_str()).filter(|id| pair(id)).map(String::from).collect(); + let searched = crate::menus::help_entries(app, &ctx).iter().map(|e| e.command().id.clone()).filter(|id| pair(id)).collect(); + (listed, searched) +} + +#[test] +fn with_a_background_only_layer_from_background_is_listed() { + let mut a = app(true); + assert!(a.session.active().unwrap().doc.background().is_some(), "a new white document has a Background"); + let rows = layer_new(&a); + assert_eq!(rows[1].0, LAYER_FROM, "second entry of Layer › New: {rows:?}"); + assert!(rows[1].2, "Layer from Background… runs with the Background active"); + assert!(rows.iter().all(|r| r.0 != BACKGROUND_FROM), "{rows:?}"); + let (listed, searched) = listed_and_searched(&mut a); + assert_eq!(listed, [LAYER_FROM]); + assert_eq!(searched, [LAYER_FROM]); +} + +#[test] +fn without_a_background_only_background_from_layer_is_listed_in_that_slot() { + let mut a = app(true); + a.run(LAYER_FROM, json!({})).unwrap(); + assert!(a.session.active().unwrap().doc.background().is_none()); + let rows = layer_new(&a); + assert_eq!(rows[1].0, BACKGROUND_FROM, "Background from Layer takes the same slot: {rows:?}"); + assert_eq!(rows[1].1, "Background from Layer"); + assert!(rows[1].2, "it runs on the former Background"); + assert!(rows.iter().all(|r| r.0 != LAYER_FROM), "{rows:?}"); + let (listed, searched) = listed_and_searched(&mut a); + assert_eq!(listed, [BACKGROUND_FROM]); + assert_eq!(searched, [BACKGROUND_FROM]); +} + +#[test] +fn with_no_document_the_slot_reads_layer_from_background_greyed() { + let a = app(false); + let rows = layer_new(&a); + assert_eq!(rows[1].0, LAYER_FROM, "{rows:?}"); + assert!(!rows[1].2, "greyed with no document"); + assert!(rows.iter().all(|r| r.0 != BACKGROUND_FROM), "{rows:?}"); +} + +/// The Keyboard Shortcuts dialog reads the rows before the swap, so either command can take a shortcut at any time. +#[test] +fn the_keyboard_shortcuts_rows_keep_both_entries() { + for doc in [false, true] { + let rows = base_rows(&app(doc)); + for id in [LAYER_FROM, BACKGROUND_FROM] { + assert!(rows.iter().any(|r| r.command.id == id), "doc {doc}: {id}"); + } + } +} diff --git a/crates/ui-egui/src/menus/items.rs b/crates/ui-egui/src/menus/items.rs index 35685a392c..0d9bd0e3d3 100644 --- a/crates/ui-egui/src/menus/items.rs +++ b/crates/ui-egui/src/menus/items.rs @@ -341,9 +341,15 @@ pub fn base_rows(app: &PhotocraftApp) -> Vec { rows } -/// [`base_rows`] with Edit › Menus applied: hidden rows dropped and the chosen colours set. +/// [`base_rows`] as the menus show them: Layer › New's two Background entries narrowed to the one that applies, then +/// Edit › Menus applied (hidden rows dropped and the chosen colours set). pub fn menu_rows(app: &PhotocraftApp) -> Vec { let mut rows = base_rows(app); + // Layer › New lists Layer from Background… while the document has a Background (or none is open) and Background + // from Layer while it has none, in the same slot (#16761). The Keyboard Shortcuts dialog keeps both. + let background = app.session.active().is_none_or(|st| st.doc.background().is_some()); + let other = if background { "layer.new.backgroundFromLayer" } else { "layer.new.layerFromBackground" }; + rows.retain(|r| r.command.id != other); let prefs = app.session.prefs(); let hidden = prefs.menus.hidden.iter().filter(|id| photocraft_engine::prefs::MenuCustomization::hideable(id)).cloned(); let colors = if prefs.interface.show_menu_colors { @@ -453,3 +459,7 @@ fn build(app: &PhotocraftApp) -> Built { let laid = layout(&bar, target(app.ctx()), &options).bar; Built { rows, source: bar, bar: laid } } + +#[cfg(test)] +#[path = "background_entry_swap_tests.rs"] +mod background_entry_swap_tests;