From 27e28cd24e7c53264fd7011bf19e3f50c6ce0fca Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 17 Nov 2026 22:58:38 +0000 Subject: [PATCH] menu: the bar and context rows take the shell's platform unless one is set MenuBarUi and context::rows used the build's target, so a test that runs the shell as Linux on a Mac runner still got a Mac bar without Alt letters. They read Target::of_shell from the frame's Env, as the shortcut column already does; the CJK mnemonic test runs as Linux. --- crates/menu/src/bar.rs | 14 ++++++++------ crates/menu/src/context.rs | 2 +- crates/menu/src/layout.rs | 14 ++++++++++++-- crates/menu/tests/it/mnemonic.rs | 2 ++ 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/crates/menu/src/bar.rs b/crates/menu/src/bar.rs index ed436e6e1..b9092e82a 100644 --- a/crates/menu/src/bar.rs +++ b/crates/menu/src/bar.rs @@ -70,7 +70,8 @@ pub struct MenuBarOutput { #[derive(craft_ui::contract::Pattern)] pub struct MenuBarUi<'a> { bar: &'a MenuBar, - target: Target, + /// The platform set with [`target`](Self::target); the shell's when none is. + target: Option, english: Option<&'a EnglishHook<'a>>, preferred: &'a [(&'a str, char)], keyboard: Option, @@ -78,16 +79,16 @@ pub struct MenuBarUi<'a> { } impl<'a> MenuBarUi<'a> { - /// The bar for `bar`, already laid out ([`crate::layout`]), on [`Target::current`]. + /// The bar for `bar`, already laid out ([`crate::layout`]), on the shell's platform ([`Target::of_shell`]). pub fn new(bar: &'a MenuBar) -> MenuBarUi<'a> { - MenuBarUi { bar, target: Target::current(), english: None, preferred: &[], keyboard: None, help_search: false } + MenuBarUi { bar, target: None, english: None, preferred: &[], keyboard: None, help_search: false } } /// The platform the bar is on, for the shortcut column ([`craft_keymap::Chord::display`]): ⌘ glyphs on /// [`Target::Mac`] and in a browser on a Mac, Ctrl names everywhere else. On [`Target::Mac`] the native bar /// has the keys, and this one takes none unless [`keyboard`](Self::keyboard) says so. pub fn target(mut self, target: Target) -> MenuBarUi<'a> { - self.target = target; + self.target = Some(target); self } @@ -127,7 +128,8 @@ impl<'a> MenuBarUi<'a> { /// the root window instead. pub fn show(self, ui: &mut Ui) -> MenuBarOutput { let env = Env::of(ui); - let rows = Rows::new(self.target).english(self.english).preferred(self.preferred); + let target = self.target.unwrap_or_else(|| Target::of_shell(env.view().shell())); + let rows = Rows::new(target).english(self.english).preferred(self.preferred); let labels = rows.labels(&self.bar.menus, |menu| menu.title.as_str(), |menu| menu.title.as_str(), |menu| rows.english_of(Labeled::Title(&menu.title))); let shared = &rows; @@ -135,7 +137,7 @@ impl<'a> MenuBarUi<'a> { self.bar.menus.iter().zip(labels).map(|(menu, label)| MenuTitle::new(label, move |ui| shared.show(ui, &menu.children))).collect(); let help = self.help_search.then(|| self.bar.menus.iter().position(|menu| menu.role == MenuRole::Help)).flatten(); let entries = if help.is_some() { entries_of(self.bar, self.english) } else { Vec::new() }; - let mut bar = Bar::new(env.words::().label.clone(), titles).keyboard(self.keyboard.unwrap_or(self.target != Target::Mac)); + let mut bar = Bar::new(env.words::().label.clone(), titles).keyboard(self.keyboard.unwrap_or(target != Target::Mac)); if let Some(menu) = help { bar = bar.help(MenuHelp::new(menu, &entries)); } diff --git a/crates/menu/src/context.rs b/crates/menu/src/context.rs index cc30e4d0b..7e1dfd667 100644 --- a/crates/menu/src/context.rs +++ b/crates/menu/src/context.rs @@ -26,7 +26,7 @@ use crate::model::{Builtin, Item, ItemKind, Node}; /// AppKit's own items ([`Node::Standard`]) are left out. The chord of a row without a binding is the item's own /// shortcut, written for the shell's operating system. pub fn rows(ui: &mut Ui, nodes: &[Node]) { - Rows::new(Target::current()).show(ui, nodes); + Rows::new(Target::of_shell(Env::of(ui).view().shell())).show(ui, nodes); } /// How the rows of a menu are written: the target that names the shortcuts, the app's English labels and its diff --git a/crates/menu/src/layout.rs b/crates/menu/src/layout.rs index b4e3fe3d6..7e1f20b13 100644 --- a/crates/menu/src/layout.rs +++ b/crates/menu/src/layout.rs @@ -1,6 +1,6 @@ //! The platform layouts of an app's menus. See the crate docs for what they do. -use craft_host::target::{Os, Runtime}; +use craft_host::target::{Os, Runtime, ShellInfo}; use craft_keymap::{Chord, CommandRef, Key, Mods}; use crate::model::{Builtin, Item, ItemRole, Menu, MenuBar, MenuRole, Node, Platforms, Standard, tidy}; @@ -34,7 +34,17 @@ impl Target { /// The platform this build runs on: `wasm32` builds are [`Target::Web`], and operating /// systems other than macOS and Windows are [`Target::Linux`]. pub const fn current() -> Target { - match (Runtime::current(), Os::current()) { + Target::on(Runtime::current(), Os::current()) + } + + /// The platform of the shell a frame runs in, as [`Env`](craft_ui::runtime::env::Env) reports it: a test + /// or a browser can run on another operating system than the build's. + pub const fn of_shell(shell: &ShellInfo) -> Target { + Target::on(shell.runtime, shell.os) + } + + const fn on(runtime: Runtime, os: Os) -> Target { + match (runtime, os) { (Runtime::Web, _) => Target::Web, (_, Os::Mac) => Target::Mac, (_, Os::Windows) => Target::Windows, diff --git a/crates/menu/tests/it/mnemonic.rs b/crates/menu/tests/it/mnemonic.rs index fcb0c99fc..83b065159 100644 --- a/crates/menu/tests/it/mnemonic.rs +++ b/crates/menu/tests/it/mnemonic.rs @@ -20,6 +20,8 @@ fn a_cjk_title_shows_its_english_letter_and_alt_with_the_letter_opens_it() { let mut fixture = Fixture::new(move |ui| { MenuBarUi::new(&bar).english(&english).show(ui); }); + // A Mac bar has no letters. + fixture.os(Os::Linux); fixture.settle(); assert!(fixture.by_label("文件(F)").is_some(), "the title shows the English letter after it"); fixture.key(Key::F, Modifiers::ALT);