diff --git a/crates/ui-egui/src/menus/bar.rs b/crates/ui-egui/src/menus/bar.rs index 2dbebb873b..4512d1a3c1 100644 --- a/crates/ui-egui/src/menus/bar.rs +++ b/crates/ui-egui/src/menus/bar.rs @@ -29,14 +29,14 @@ pub fn menu_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { MenuBarUi::new(&model.bar).mnemonics(&TOP_PREFERRED).help_search(true).keyboard(!(gate.modal || gate.recording_shortcut)).show(ui); } -/// The commands of the menu model's bar as the command palette lists them, with each path and the English names the model carries. The bar -/// is laid out with the options the model was built with, so the palette and the bars agree. +/// The commands of the menu model's bar as the command palette lists them, with each path and the English names the model carries. The model's +/// source is laid out with the options the model was built with, so the palette and the bars agree. pub(crate) fn help_entries(app: &mut PhotocraftApp, ctx: &egui::Context) -> Vec { - use craft::menu::{LayoutOptions, Target}; + use craft::menu::LayoutOptions; let lang = crate::i18n::current(); let model = items::model(app, ctx); let label = |s: &'static str| crate::i18n::tr(lang, s).to_string(); let keys = |chord: &craft::keymap::Chord| craft::ui::runtime::commands::holder(ctx, chord); let options = LayoutOptions::new(crate::native_menu::APP_NAME).label(&label).keys(&keys); - model.bar.help_entries(Target::current(), &options) + model.source().help_entries(items::target(ctx), &options) } diff --git a/crates/ui-egui/src/menus/items.rs b/crates/ui-egui/src/menus/items.rs index 0462787361..35685a392c 100644 --- a/crates/ui-egui/src/menus/items.rs +++ b/crates/ui-egui/src/menus/items.rs @@ -58,6 +58,14 @@ pub struct MenuModel { pub bar: Tracked, } +impl MenuModel { + /// The tagged, translated menus before any platform layout: what the native bar and the Help search lay out + /// themselves, so a bar is never laid out twice. + pub fn source(&self) -> &MenuBar { + &self.built.source + } +} + impl MenuModel { /// The rows with labels as shown, after the user's Edit › Menus customization; each translated label carries its /// English name. @@ -69,6 +77,7 @@ impl MenuModel { /// One build of the model: the rows and the bar laid out from them. struct Built { rows: Vec, + source: MenuBar, bar: MenuBar, } @@ -399,6 +408,11 @@ pub(crate) fn fresh_bar(app: &PhotocraftApp) -> MenuBar { build(app).bar } +/// The platform the menus are laid out for: the shell's, so a test can run the macOS layout on any build. +pub(crate) fn target(ctx: &egui::Context) -> Target { + Target::of_shell(&craft::ui::runtime::windows::shell_of(ctx, egui::ViewportId::ROOT)) +} + fn build(app: &PhotocraftApp) -> Built { let lang = crate::i18n::current(); let mut rows = menu_rows(app); @@ -435,7 +449,7 @@ fn build(app: &PhotocraftApp) -> Built { let label = |s: &'static str| crate::i18n::tr(lang, s).to_string(); let keys = |chord: &Chord| craft::ui::runtime::commands::holder(app.ctx(), chord); let options = LayoutOptions::new(crate::native_menu::APP_NAME).label(&label).keys(&keys); - let mut bar = layout(&bar, Target::current(), &options).bar; translate_titles(&mut bar, lang); - Built { rows, bar } + let laid = layout(&bar, target(app.ctx()), &options).bar; + Built { rows, source: bar, bar: laid } } diff --git a/crates/ui-egui/src/menus/native_menu.rs b/crates/ui-egui/src/menus/native_menu.rs index 2ba2a23092..fa2cadca3f 100644 --- a/crates/ui-egui/src/menus/native_menu.rs +++ b/crates/ui-egui/src/menus/native_menu.rs @@ -30,7 +30,7 @@ pub(crate) fn laid_out(app: &mut PhotocraftApp, ctx: &egui::Context) -> Layout { // The system items give way to the command the keymap gives a key to, in a menu or not (tool keys, canvas keys). let keys = |chord: &craft::keymap::Chord| holder(ctx, chord); let options = LayoutOptions::new(APP_NAME).label(&label).keys(&keys); - mac_layout(&items::model(app, ctx).bar, &options) + mac_layout(items::model(app, ctx).source(), &options) } /// Installs the macOS menu bar; `None` where there is none (every other platform). diff --git a/crates/ui-egui/src/menus/native_menu/tests.rs b/crates/ui-egui/src/menus/native_menu/tests.rs index fbecc98f8c..3efd60f799 100644 --- a/crates/ui-egui/src/menus/native_menu/tests.rs +++ b/crates/ui-egui/src/menus/native_menu/tests.rs @@ -256,3 +256,47 @@ fn the_menu_state_follows_the_preferences_the_rows_show() { app.session.edit_prefs(|p| p.export.quick_export_format = photocraft_engine::prefs::QuickExportFormat::Jpg); assert_ne!(crate::menus::facts::key(&app), hidden, "the Quick Export label names the format"); } + +/// The standard Mac items (the app menu's Services, Hide, Hide Others, Show All; the Window menu's Minimize, Zoom, Bring +/// All to Front) appear once in the Mac model, which the layout builds from the source bar in one pass. +#[test] +fn the_mac_model_lists_each_standard_item_once() { + fn count(nodes: &[Node], found: &mut Vec) { + for node in nodes { + match node { + Node::Standard { kind, .. } => found.push(format!("{kind:?}")), + Node::Item(item) if item.command.id.starts_with("craft-menu.") => found.push(item.command.id.clone()), + Node::Submenu { children, .. } => count(children, found), + _ => {} + } + } + } + let mut a = app(false); + craft::ui::testing::windows::set_os(a.ctx(), craft::host::target::Os::Mac); + let ctx = a.ctx().clone(); + let held = (*crate::menus::items::model(&mut a, &ctx).bar).clone(); + for layout in [laid(&mut a).bar, held] { + let mut found = Vec::new(); + layout.menus.iter().for_each(|menu| count(&menu.children, &mut found)); + assert!(found.len() >= 6, "the Mac layout adds the system items: {found:?}"); + let mut unique = found.clone(); + unique.sort(); + unique.dedup(); + assert_eq!(unique.len(), found.len(), "a system item listed twice: {found:?}"); + } +} + +/// Help search and the command palette list each open document once. +#[test] +fn help_search_lists_each_open_document_once() { + let mut a = app(true); + a.run("file.new", json!({"width": 32, "height": 32})).unwrap(); + a.sync_views(); + let ctx = a.ctx().clone(); + let entries = crate::menus::help_entries(&mut a, &ctx); + let mut ids: Vec = entries.iter().map(|e| e.command().id.clone()).filter(|id| id.starts_with(crate::menus::DOCUMENT_ITEM)).collect(); + assert_eq!(ids.len(), 2, "one entry per open document: {ids:?}"); + ids.sort(); + ids.dedup(); + assert_eq!(ids.len(), 2); +} diff --git a/crates/ui-egui/src/menus/view_cmds/arrange.rs b/crates/ui-egui/src/menus/view_cmds/arrange.rs index e74fc14e6f..7c1610fbed 100644 --- a/crates/ui-egui/src/menus/view_cmds/arrange.rs +++ b/crates/ui-egui/src/menus/view_cmds/arrange.rs @@ -50,10 +50,10 @@ fn screen_position(v: &Value) -> Option<[f32; 2]> { Some([n(0)?, n(1)?]) } -/// The area and sizes the framework's arrangements work in. New windows take half the document area; a window that -/// follows its document's size is resized by `DocumentWindow` afterwards. -fn space(app: &PhotocraftApp) -> ArrangeSpace { - let area = app.ctx().content_rect(); +/// The area and sizes the framework's arrangements work in: the document area, not the app's whole content. New +/// windows take half of it; a window that follows its document's size is resized by `DocumentWindow` afterwards. +pub(super) fn space(app: &PhotocraftApp) -> ArrangeSpace { + let area = app.last_canvas_rect; let step = craft::ui::style::StyleCx::with_root(app.ctx(), |cx| cx.tokens.size.window.cascade); ArrangeSpace::new(area, area.size() * 0.5, step, app.session.active().map(|st| st.doc.id)) } diff --git a/crates/ui-egui/src/menus/view_cmds/tests.rs b/crates/ui-egui/src/menus/view_cmds/tests.rs index 0e394c0fb8..102d6de8ac 100644 --- a/crates/ui-egui/src/menus/view_cmds/tests.rs +++ b/crates/ui-egui/src/menus/view_cmds/tests.rs @@ -314,6 +314,18 @@ fn print_size_uses_the_screen_resolution_preference() { assert_eq!(app.ui.views[0].zoom, 96.0, "96 ppi screen / 1 ppi document = 9600 %"); } +/// Window › Arrange works in the document area (the canvas panel's), not the app's whole content. +#[test] +fn the_arrange_space_is_the_document_area() { + let (mut app, ctx) = app_with(2); + let area = egui::Rect::from_min_size(egui::pos2(210.0, 64.0), egui::vec2(500.0, 400.0)); + assert_ne!(area, ctx.content_rect()); + app.last_canvas_rect = area; + let space = super::arrange::space(&app); + assert_eq!(space.area, area); + assert_eq!(space.size, area.size() * 0.5); +} + #[test] fn arrange_layouts_floating_windows_and_matching() { let (mut app, ctx) = app_with(3);