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.
This commit is contained in:
aiongg
2026-11-17 22:58:38 +00:00
parent 5f74a21acb
commit 27e28cd24e
4 changed files with 23 additions and 9 deletions
+8 -6
View File
@@ -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<Target>,
english: Option<&'a EnglishHook<'a>>,
preferred: &'a [(&'a str, char)],
keyboard: Option<bool>,
@@ -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::<BarWords>().label.clone(), titles).keyboard(self.keyboard.unwrap_or(self.target != Target::Mac));
let mut bar = Bar::new(env.words::<BarWords>().label.clone(), titles).keyboard(self.keyboard.unwrap_or(target != Target::Mac));
if let Some(menu) = help {
bar = bar.help(MenuHelp::new(menu, &entries));
}
+1 -1
View File
@@ -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
+12 -2
View File
@@ -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,
+2
View File
@@ -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);