mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-22 18:41:27 +01:00
# Conflicts: # crates/automation/Cargo.toml # crates/automation/src/client.rs # crates/automation/src/control.rs # crates/automation/src/screenshot.rs # crates/automation/src/synthetic.rs # crates/automation/tests/it/main.rs # crates/automation/tests/it/synthetic.rs # crates/color-ui/src/palette.rs # crates/color-ui/src/ui/area.rs # crates/color-ui/src/ui/disc/stories.rs # crates/color-ui/src/ui/mod.rs # crates/color-ui/src/ui/paint_chip.rs # crates/color-ui/src/ui/pair/stories.rs # crates/color-ui/src/ui/palette/stories.rs # crates/color-ui/src/ui/panel.rs # crates/color-ui/src/ui/picker/stories.rs # crates/color-ui/src/ui/picker_dialog.rs # crates/color-ui/src/ui/swatches/library.rs # crates/color-ui/src/ui/swatches/panel.rs # crates/color-ui/src/ui/swatches/stories.rs # crates/color-ui/src/ui/wheel/stories.rs # crates/color-ui/tests/it/main.rs # crates/color-ui/tests/it/panels.rs # crates/color-ui/tests/it/picker.rs # crates/dialogs/src/lib.rs # crates/dialogs/src/native.rs # crates/dialogs/src/portal.rs # crates/dialogs/src/service.rs # crates/draw/src/ui/brush_presets/panel.rs # crates/draw/src/ui/brush_presets/picker.rs # crates/draw/src/ui/brush_presets/stories.rs # crates/draw/src/ui/gradient/palette/mod.rs # crates/draw/src/ui/pattern/editor.rs # crates/draw/src/ui/pen/stories.rs # crates/draw/src/ui/transform/mod.rs # crates/draw/src/ui/transform/transform_box.rs # crates/draw/src/ui/transform/transform_box/stories.rs # crates/draw/src/ui/transform/transform_fields.rs # crates/draw/tests/it/brush_presets.rs # crates/draw/tests/it/main.rs # crates/draw/tests/it/transform_box.rs # crates/font-manager/src/axes.rs # crates/font-manager/src/picker.rs # crates/font-manager/src/picker/rows.rs # crates/font-manager/src/picker/stories.rs # crates/font-manager/tests/it/main.rs # crates/font-manager/tests/it/picker.rs # crates/host/src/files.rs # crates/i18n/src/lib.rs # crates/imaging/src/ui/layer_style/styles.rs # crates/keymap/src/keymap.rs # crates/keymap/src/matcher.rs # crates/layers/src/ui/filter/mod.rs # crates/layers/src/ui/filter/stories.rs # crates/layers/src/ui/mock.rs # crates/layers/src/ui/panel/mod.rs # crates/layers/src/ui/panel/stories.rs # crates/layers/src/ui/tree.rs # crates/layers/tests/it/panel_drag_menu.rs # crates/layers/tests/it/panel_filter.rs # crates/menu/src/bar.rs # crates/menu/src/driver.rs # crates/menu/src/model.rs # crates/menu/src/shortcuts/pages.rs # crates/menu/src/shortcuts/state.rs # crates/menu/src/shortcuts/stories.rs # crates/menu/src/shortcuts/view.rs # crates/menu/tests/it/main.rs # crates/menu/tests/it/native.rs # crates/menu/tests/it/shortcuts/mod.rs # crates/pages/src/ui/mod.rs # crates/pages/src/ui/view.rs # crates/platform/README.md # crates/platform/src/clipboard_tests.rs # crates/platform/src/open.rs # crates/print/src/ui/stories.rs # crates/text/src/ui/character.rs # crates/text/src/ui/character/stories.rs # crates/text/src/ui/find.rs # crates/text/src/ui/find/dialog.rs # crates/text/src/ui/find/stories.rs # crates/text/src/ui/ruler.rs # crates/text/src/ui/ruler/paint.rs # crates/text/src/ui/ruler/run.rs # crates/text/tests/it/main.rs # crates/text/tests/it/ruler.rs # crates/ui-canvas/src/canvas/mod.rs # crates/ui-canvas/src/canvas/tool.rs # crates/ui-canvas/src/canvas/tool_host.rs # crates/ui-canvas/src/canvas_controls/grid/mod.rs # crates/ui-canvas/src/canvas_controls/grid/stories.rs # crates/ui-canvas/src/canvas_controls/guides/stories.rs # crates/ui-canvas/src/canvas_controls/task_bar.rs # crates/ui-canvas/src/canvas_controls/task_bar/stories.rs # crates/ui-canvas/tests/it/canvas/navigate.rs # crates/ui-canvas/tests/it/canvas/rulers.rs # crates/ui-canvas/tests/it/canvas/tool.rs # crates/ui-canvas/tests/it/canvas_controls/grid.rs # crates/ui-contract/src/contract/body/presses.rs # crates/ui-contract/src/contract/items/drag.rs # crates/ui-contract/src/contract/items/hooks.rs # crates/ui-contract/src/contract/items/lead.rs # crates/ui-contract/src/contract/overlay/placement.rs # crates/ui-contract/src/contract/overlay/props.rs # crates/ui-contract/src/contract/overlay/ui.rs # crates/ui-contract/src/contract/steps/label.rs # crates/ui-contract/src/contract/text.rs # crates/ui-contract/src/contract/tooltip.rs # crates/ui-contract/src/layout/body.rs # crates/ui-contract/src/primitives/draw.rs # crates/ui-contract/src/primitives/icon.rs # crates/ui-contract/src/primitives/mod.rs # crates/ui-contract/src/primitives/textures/thumbnails.rs # crates/ui-contract/src/runtime/commands/board.rs # crates/ui-contract/src/runtime/commands/mod.rs # crates/ui-contract/src/runtime/drag.rs # crates/ui-contract/src/runtime/egui/input.rs # crates/ui-contract/src/runtime/egui/plugin.rs # crates/ui-contract/src/runtime/frame.rs # crates/ui-contract/src/runtime/keys.rs # crates/ui-contract/src/runtime/keys/input.rs # crates/ui-contract/src/runtime/keys/route.rs # crates/ui-contract/src/runtime/pointer.rs # crates/ui-contract/src/runtime/wheel.rs # crates/ui-contract/src/runtime/windows.rs # crates/ui-contract/src/runtime/windows/viewport.rs # crates/ui-contract/src/style/cx.rs # crates/ui-contract/src/style/span.rs # crates/ui-contract/src/testing/access.rs # crates/ui-contract/src/testing/icon_trials.rs # crates/ui-contract/tests/it/body/mod.rs # crates/ui-contract/tests/it/runtime/mod.rs # crates/ui-contract/tests/it/runtime/wheel.rs # crates/ui-contract/tests/it/runtime/windows.rs # crates/ui-contract/tests/it/text/editor.rs # crates/ui-gallery/src/mock/panels.rs # crates/ui-panels/src/dialog/mod.rs # crates/ui-panels/src/docking/group.rs # crates/ui-panels/src/rails/drawer/stories.rs # crates/ui-panels/src/rails/tool.rs # crates/ui-panels/src/rails/tool/stories.rs # crates/ui-panels/src/rails/tool/trials.rs # crates/ui-panels/src/sections/footer.rs # crates/ui-panels/tests/it/dialogs.rs # crates/ui-patterns/src/patterns/dock/hide.rs # crates/ui-patterns/src/patterns/dock/panes.rs # crates/ui-patterns/src/patterns/form_dialogs/dialog.rs # crates/ui-patterns/src/patterns/form_dialogs/dialog/stories.rs # crates/ui-patterns/src/patterns/form_dialogs/list_manager.rs # crates/ui-patterns/src/patterns/form_dialogs/list_manager/stories.rs # crates/ui-patterns/src/patterns/form_dialogs/mod.rs # crates/ui-patterns/src/patterns/form_dialogs/new_document.rs # crates/ui-patterns/src/patterns/form_dialogs/new_document/cards.rs # crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs # crates/ui-patterns/src/patterns/form_dialogs/param.rs # crates/ui-patterns/src/patterns/form_dialogs/pdf_export.rs # crates/ui-patterns/src/patterns/form_dialogs/pdf_export/stories.rs # crates/ui-patterns/src/patterns/form_dialogs/report.rs # crates/ui-patterns/src/patterns/form_dialogs/report/stories.rs # crates/ui-patterns/src/patterns/form_dialogs/rows.rs # crates/ui-patterns/src/patterns/form_dialogs/save_for_web.rs # crates/ui-patterns/src/patterns/form_dialogs/save_for_web/hosted.rs # crates/ui-patterns/src/patterns/form_dialogs/save_for_web/model.rs # crates/ui-patterns/src/patterns/form_dialogs/save_for_web/options.rs # crates/ui-patterns/src/patterns/form_dialogs/save_for_web/stories.rs # crates/ui-patterns/src/patterns/panels/palette.rs # crates/ui-patterns/src/patterns/panels/palette/stories.rs # crates/ui-patterns/src/patterns/preferences/document/model.rs # crates/ui-patterns/src/patterns/preferences/page.rs # crates/ui-patterns/src/patterns/preferences/window.rs # crates/ui-patterns/src/patterns/preferences/window/stories.rs # crates/ui-patterns/src/patterns/presets/browser.rs # crates/ui-patterns/src/patterns/presets/browser/stories.rs # crates/ui-patterns/src/patterns/presets/manager.rs # crates/ui-patterns/src/patterns/presets/mod.rs # crates/ui-patterns/src/patterns/presets/named.rs # crates/ui-patterns/src/patterns/presets/picker.rs # crates/ui-patterns/tests/it/docking/drop_session.rs # crates/ui-patterns/tests/it/docking/native.rs # crates/ui-patterns/tests/it/form_dialogs/hosted.rs # crates/ui-patterns/tests/it/form_dialogs/list_manager.rs # crates/ui-patterns/tests/it/form_dialogs/mod.rs # crates/ui-patterns/tests/it/form_dialogs/params.rs # crates/ui-patterns/tests/it/form_dialogs/save_for_web.rs # crates/ui-patterns/tests/it/panels/palette.rs # crates/ui-patterns/tests/it/presets/browser.rs # crates/ui-shell/src/shell/app_shell.rs # crates/ui-shell/src/shell/app_shell/stories.rs # crates/ui-shell/src/shell/mod.rs # crates/ui-shell/src/shell/start.rs # crates/ui-shell/src/shell/start/stories.rs # crates/ui-shell/tests/it/commands/mod.rs # crates/ui-shell/tests/it/shell/app_shell.rs # crates/ui-shell/tests/it/shell/mod.rs # crates/ui-widgets/src/bars/bar.rs # crates/ui-widgets/src/buttons/button.rs # crates/ui-widgets/src/buttons/face.rs # crates/ui-widgets/src/buttons/mod.rs # crates/ui-widgets/src/buttons/stories.rs # crates/ui-widgets/src/choice/mod.rs # crates/ui-widgets/src/choice/swatch.rs # crates/ui-widgets/src/choice/swatch/stories.rs # crates/ui-widgets/src/choice/swatch_table.rs # crates/ui-widgets/src/choice/swatch_table/stories.rs # crates/ui-widgets/src/display/text.rs # crates/ui-widgets/src/list/row.rs # crates/ui-widgets/src/list/row/paint.rs # crates/ui-widgets/src/list/stories.rs # crates/ui-widgets/src/list/view_stories.rs # crates/ui-widgets/src/menu/bar.rs # crates/ui-widgets/src/menu/item.rs # crates/ui-widgets/src/menu/search.rs # crates/ui-widgets/src/menu/stories.rs # crates/ui-widgets/src/pickers/mod.rs # crates/ui-widgets/src/pickers/reference_point.rs # crates/ui-widgets/src/pickers/reference_point/stories.rs # crates/ui-widgets/src/tabs/strip.rs # crates/ui-widgets/src/text/number.rs # crates/ui-widgets/src/text/number/stories.rs # crates/ui-widgets/src/tile_grid/grid.rs # crates/ui-widgets/tests/it/buttons.rs # crates/ui-widgets/tests/it/label/mod.rs # crates/ui-widgets/tests/it/label/tooltip.rs # crates/ui-widgets/tests/it/main.rs # crates/ui-widgets/tests/it/menu/bar.rs # crates/ui-widgets/tests/it/menu/mod.rs # crates/ui-widgets/tests/it/menu/rows.rs # crates/ui-widgets/tests/it/menu/search.rs # crates/ui-widgets/tests/it/pickers/select_list.rs # crates/ui-widgets/tests/it/text/mod.rs
526 lines
22 KiB
Rust
526 lines
22 KiB
Rust
//! The platform layouts of an app's menus. See the crate docs for what they do.
|
|
|
|
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};
|
|
|
|
const CTRL_CMD: Mods = Mods { ctrl: true, ..Mods::CMD };
|
|
const ALT_CMD: Mods = Mods { alt: true, ..Mods::CMD };
|
|
const CMD_H: Chord = Chord::new(Mods::CMD, Key::H);
|
|
const CTRL_CMD_H: Chord = Chord::new(CTRL_CMD, Key::H);
|
|
const ALT_CMD_H: Chord = Chord::new(ALT_CMD, Key::H);
|
|
const CMD_Q: Chord = Chord::new(Mods::CMD, Key::Q);
|
|
const CMD_W: Chord = Chord::new(Mods::CMD, Key::W);
|
|
const CMD_M: Chord = Chord::new(Mods::CMD, Key::M);
|
|
const CTRL_CMD_M: Chord = Chord::new(CTRL_CMD, Key::M);
|
|
const CMD_COMMA: Chord = Chord::new(Mods::CMD, Key::Comma);
|
|
|
|
/// The platform a layout is for.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, craft_ui::contract::MemoProps)]
|
|
#[non_exhaustive]
|
|
pub enum Target {
|
|
#[allow(missing_docs)]
|
|
Mac,
|
|
#[allow(missing_docs)]
|
|
Windows,
|
|
#[allow(missing_docs)]
|
|
Linux,
|
|
/// A browser, on any operating system.
|
|
Web,
|
|
}
|
|
|
|
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 {
|
|
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,
|
|
_ => Target::Linux,
|
|
}
|
|
}
|
|
|
|
/// The [`Platforms`] flag an item needs to be shown on this target.
|
|
pub const fn platforms(self) -> Platforms {
|
|
match self {
|
|
Target::Mac => Platforms::MAC,
|
|
Target::Windows => Platforms::WINDOWS,
|
|
Target::Linux => Platforms::LINUX,
|
|
Target::Web => Platforms::WEB,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The app's name, its translations of the labels the layout writes itself, and its keymap's holder
|
|
/// of a chord.
|
|
#[non_exhaustive]
|
|
pub struct LayoutOptions<'a> {
|
|
/// Shown in the About, Hide and Quit items.
|
|
pub app_name: &'a str,
|
|
/// The app's translation of an English label a layout uses: `About {app}`, `Settings…`,
|
|
/// `Settings`, `Language`, `Appearance`, `Hide {app}`, `Hide Others`, `Quit {app}`, `Minimize`,
|
|
/// `Preferences…`, `Preferences`, `Exit`, `Quit`, the menu titles `File`, `Edit`, `Window` and
|
|
/// `Help`, and each [`Standard::english`]. `{app}` is replaced by [`LayoutOptions::app_name`]
|
|
/// afterwards.
|
|
pub label: &'a dyn Fn(&'static str) -> String,
|
|
/// The command the app's keymap gives a chord to, whatever has focus
|
|
/// (`craft_ui::runtime::commands::holder`): a system item gives way to it, and to an item that
|
|
/// shows the chord.
|
|
pub keys: &'a dyn Fn(&Chord) -> Option<CommandRef>,
|
|
}
|
|
|
|
impl<'a> LayoutOptions<'a> {
|
|
/// English labels.
|
|
pub fn new(app_name: &'a str) -> LayoutOptions<'a> {
|
|
LayoutOptions { app_name, label: &english, keys: &no_keys }
|
|
}
|
|
|
|
/// Set the app's translation of the labels a layout writes; see [`LayoutOptions::label`].
|
|
pub fn label(mut self, label: &'a dyn Fn(&'static str) -> String) -> LayoutOptions<'a> {
|
|
self.label = label;
|
|
self
|
|
}
|
|
|
|
/// Set the app's keymap holder of a chord; see [`LayoutOptions::keys`].
|
|
pub fn keys(mut self, keys: &'a dyn Fn(&Chord) -> Option<CommandRef>) -> LayoutOptions<'a> {
|
|
self.keys = keys;
|
|
self
|
|
}
|
|
|
|
fn text(&self, english: &'static str) -> String {
|
|
(self.label)(english).replace("{app}", self.app_name)
|
|
}
|
|
|
|
/// The English text of a label `text` writes, `{app}` filled in.
|
|
fn english_text(&self, english: &'static str) -> String {
|
|
english.replace("{app}", self.app_name)
|
|
}
|
|
|
|
fn standard(&self, kind: Standard) -> Node {
|
|
Node::Standard { kind, label: self.text(kind.english()) }
|
|
}
|
|
}
|
|
|
|
fn english(s: &'static str) -> String {
|
|
s.into()
|
|
}
|
|
|
|
fn no_keys(_: &Chord) -> Option<CommandRef> {
|
|
None
|
|
}
|
|
|
|
/// A system or browser shortcut the app also uses for one of its commands.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
#[non_exhaustive]
|
|
pub struct Clash {
|
|
/// The shortcut the app binds.
|
|
pub shortcut: Chord,
|
|
/// What macOS or the browser uses it for.
|
|
pub system: &'static str,
|
|
/// The app's command bound to it.
|
|
pub command: CommandRef,
|
|
/// What the layout did about it.
|
|
pub resolution: &'static str,
|
|
}
|
|
|
|
/// A laid-out menu bar and the shortcut clashes found.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
#[non_exhaustive]
|
|
pub struct Layout {
|
|
/// The menus to show.
|
|
pub bar: MenuBar,
|
|
/// App shortcuts that collide with system ones.
|
|
pub clashes: Vec<Clash>,
|
|
}
|
|
|
|
/// Lay an app's menus out for `target`: [`mac_layout`], [`desktop_layout`] or [`web_layout`].
|
|
pub fn layout(bar: &MenuBar, target: Target, options: &LayoutOptions) -> Layout {
|
|
match target {
|
|
Target::Mac => mac_layout(bar, options),
|
|
Target::Windows | Target::Linux => desktop_layout(bar, target, options),
|
|
Target::Web => web_layout(bar, options),
|
|
}
|
|
}
|
|
|
|
/// Turn an app's menus, tagged with roles, into a Mac menu bar. See the crate docs.
|
|
///
|
|
/// A menu the app tagged [`MenuRole::App`] is merged into the app menu: its tagged items go where
|
|
/// their roles put them, the rest after Settings, Language and Appearance.
|
|
pub fn mac_layout(bar: &MenuBar, options: &LayoutOptions) -> Layout {
|
|
let mut bar = shown_on(bar, Target::Mac);
|
|
let mut clashes = Vec::new();
|
|
// A chord's owner: the keymap's holder, else an item that shows it (a key no keymap holds).
|
|
let shown: Vec<(Chord, CommandRef)> = bar.items().into_iter().filter_map(|it| Some((it.shortcut?, it.command.clone()))).collect();
|
|
let owner = |sc: Chord| (options.keys)(&sc).or_else(|| shown.iter().find(|(c, _)| *c == sc).map(|(_, command)| command.clone()));
|
|
let taken = |sc: Chord, own: &[CommandRef]| owner(sc).filter(|cmd| !own.contains(cmd));
|
|
let tagged = |role: ItemRole| {
|
|
move |n: &Node| match n {
|
|
Node::Item(it) => it.role == Some(role),
|
|
Node::Submenu { role: r, .. } => *r == Some(role),
|
|
_ => false,
|
|
}
|
|
};
|
|
|
|
let about = take(&mut bar, |n| matches!(n, Node::Item(it) if it.role == Some(ItemRole::About)));
|
|
let settings = take(&mut bar, tagged(ItemRole::Settings));
|
|
let language = take(&mut bar, tagged(ItemRole::Language));
|
|
let quit = take(&mut bar, |n| matches!(n, Node::Item(it) if it.role == Some(ItemRole::Quit)));
|
|
let appearance = appearance_node(&mut bar, options);
|
|
let own_app: Vec<Node> = match bar.menus.iter().position(|m| m.role == MenuRole::App) {
|
|
Some(i) => bar.menus.remove(i).children,
|
|
None => Vec::new(),
|
|
};
|
|
|
|
let mut menu = Vec::new();
|
|
if let Some(Node::Item(mut it)) = about {
|
|
it.label = options.text("About {app}");
|
|
it.english = Some(options.english_text("About {app}"));
|
|
menu.extend([Node::Item(it), Node::Separator]);
|
|
}
|
|
if let Some(s) = settings {
|
|
let comma = taken(CMD_COMMA, &node_commands(&s));
|
|
menu.push(settings_node(s, comma, options, &mut clashes));
|
|
}
|
|
if let Some(mut l) = language {
|
|
if let Node::Submenu { label, english, .. } = &mut l {
|
|
*label = options.text("Language");
|
|
*english = Some("Language".to_owned());
|
|
}
|
|
menu.push(l);
|
|
}
|
|
menu.extend(appearance);
|
|
if !own_app.is_empty() {
|
|
menu.push(Node::Separator);
|
|
menu.extend(own_app);
|
|
}
|
|
if !menu.is_empty() && menu.last() != Some(&Node::Separator) {
|
|
menu.push(Node::Separator);
|
|
}
|
|
menu.extend([options.standard(Standard::Services), Node::Separator]);
|
|
let mut hide = Item::new(Builtin::Hide.command(), options.text("Hide {app}")).role(ItemRole::Hide);
|
|
hide.layout_shortcut = true;
|
|
match taken(CMD_H, &[]) {
|
|
Some(command) => {
|
|
let free = taken(CTRL_CMD_H, &[]).is_none();
|
|
if free {
|
|
hide.shortcut = Some(CTRL_CMD_H);
|
|
}
|
|
let resolution = if free { "Hide moves to ⌃⌘H" } else { "Hide has no shortcut" };
|
|
clashes.push(Clash { shortcut: CMD_H, system: "Hide", command, resolution });
|
|
}
|
|
None => hide.shortcut = Some(CMD_H),
|
|
}
|
|
menu.push(Node::Item(hide));
|
|
let mut hide_others = Item::new(Builtin::HideOthers.command(), options.text("Hide Others"));
|
|
hide_others.layout_shortcut = true;
|
|
match taken(ALT_CMD_H, &[]) {
|
|
Some(command) => clashes.push(Clash { shortcut: ALT_CMD_H, system: "Hide Others", command, resolution: "Hide Others has no shortcut" }),
|
|
None => hide_others.shortcut = Some(ALT_CMD_H),
|
|
}
|
|
menu.extend([Node::Item(hide_others), options.standard(Standard::ShowAll), Node::Separator]);
|
|
let mut quit = match quit {
|
|
Some(Node::Item(it)) => it,
|
|
_ => Item::new(Builtin::Quit.command(), "").role(ItemRole::Quit),
|
|
};
|
|
quit.label = options.text("Quit {app}");
|
|
quit.english = Some(options.english_text("Quit {app}"));
|
|
match taken(CMD_Q, std::slice::from_ref(&quit.command)) {
|
|
Some(command) => {
|
|
clashes.push(Clash { shortcut: CMD_Q, system: "Quit", command, resolution: "Quit has no shortcut" });
|
|
quit.shortcut = None;
|
|
}
|
|
None => {
|
|
quit.layout_shortcut = quit.shortcut != Some(CMD_Q);
|
|
quit.shortcut = Some(CMD_Q);
|
|
}
|
|
}
|
|
menu.push(Node::Item(quit));
|
|
bar.menus.insert(0, Menu::new(options.app_name, menu).role(MenuRole::App));
|
|
|
|
match taken(CMD_W, &[]) {
|
|
Some(command) => clashes.push(Clash { shortcut: CMD_W, system: "Close Window", command, resolution: "no system Close Window item" }),
|
|
None => {
|
|
if let Some(file) = bar.menus.iter_mut().find(|m| m.role == MenuRole::File) {
|
|
file.children.extend([Node::Separator, options.standard(Standard::CloseWindow)]);
|
|
}
|
|
}
|
|
}
|
|
|
|
let minimize = match taken(CMD_M, &[]) {
|
|
Some(command) => {
|
|
let mut it = Item::new(Builtin::Minimize.command(), options.text("Minimize")).role(ItemRole::Minimize);
|
|
it.layout_shortcut = true;
|
|
let free = taken(CTRL_CMD_M, &[]).is_none();
|
|
if free {
|
|
it.shortcut = Some(CTRL_CMD_M);
|
|
}
|
|
let resolution = if free { "Minimize moves to ⌃⌘M" } else { "Minimize has no shortcut" };
|
|
clashes.push(Clash { shortcut: CMD_M, system: "Minimize", command, resolution });
|
|
Node::Item(it)
|
|
}
|
|
None => options.standard(Standard::Minimize),
|
|
};
|
|
let documents = document_items(&bar);
|
|
let window = ensure_menu(&mut bar, MenuRole::Window, "Window", options);
|
|
let mut children = vec![minimize, options.standard(Standard::Zoom), Node::Separator];
|
|
children.append(&mut window.children);
|
|
children.extend([Node::Separator, options.standard(Standard::BringAllToFront), Node::Separator]);
|
|
children.extend(documents);
|
|
window.children = children;
|
|
|
|
// The Help menu stays even when empty: it has the search field.
|
|
finish(&mut bar, |m| m.role == MenuRole::Help);
|
|
Layout { bar, clashes }
|
|
}
|
|
|
|
/// Lay an app's menus out the Windows or Linux way:
|
|
/// - About goes last in Help, as `About {app}`;
|
|
/// - Settings goes last in Edit, as `Preferences…` (an item) or `Preferences` (a submenu);
|
|
/// - Quit goes last in File, as `Exit` on Windows and `Quit` elsewhere: the app's item tagged
|
|
/// [`ItemRole::Quit`], or [`Builtin::Quit`];
|
|
/// - the open documents go last in Window, as check items.
|
|
///
|
|
/// The layout creates the File, Edit, Help and Window menus these need, and adds no Services,
|
|
/// Hide or Minimize items. Language and Appearance stay where the app put them, and items keep
|
|
/// the app's shortcuts. AppKit's items ([`Node::Standard`]) are dropped.
|
|
pub fn desktop_layout(bar: &MenuBar, target: Target, options: &LayoutOptions) -> Layout {
|
|
let quit = if target == Target::Windows { "Exit" } else { "Quit" };
|
|
Layout { bar: desktop_bar(bar, target, Some(quit), options), clashes: Vec::new() }
|
|
}
|
|
|
|
/// Lay an app's menus out for a browser: [`desktop_layout`] without a Quit item. Shortcuts the
|
|
/// browser keeps for itself (Ctrl or ⌘ with W, N, T or Q, and with Shift+N, Shift+T or Shift+W)
|
|
/// are taken off the items and reported as clashes, since the page never sees them.
|
|
pub fn web_layout(bar: &MenuBar, options: &LayoutOptions) -> Layout {
|
|
let mut bar = desktop_bar(bar, Target::Web, None, options);
|
|
let mut clashes = Vec::new();
|
|
bar.for_each_item_mut(|it| {
|
|
if let Some(shortcut) = it.shortcut
|
|
&& let Some(system) = browser_reserved(shortcut)
|
|
{
|
|
clashes.push(Clash { shortcut, system, command: it.command.clone(), resolution: "not shown: the browser keeps it" });
|
|
it.shortcut = None;
|
|
}
|
|
});
|
|
Layout { bar, clashes }
|
|
}
|
|
|
|
// Browsers run these themselves and never pass them to the page. craft_keymap::reserved lists
|
|
// only the operating systems' chords.
|
|
const BROWSER: &[(Key, bool, &str)] = &[
|
|
(Key::W, false, "Close Tab"),
|
|
(Key::N, false, "New Window"),
|
|
(Key::T, false, "New Tab"),
|
|
(Key::Q, false, "Quit"),
|
|
(Key::N, true, "New Private Window"),
|
|
(Key::T, true, "Reopen Closed Tab"),
|
|
(Key::W, true, "Close Window"),
|
|
];
|
|
|
|
/// What the browser uses `chord` for, if it keeps it. Either command modifier counts (Ctrl off
|
|
/// macOS, ⌘ on it), since a web build serves both.
|
|
fn browser_reserved(chord: Chord) -> Option<&'static str> {
|
|
let m = chord.mods;
|
|
if m.cmd == m.ctrl || m.alt {
|
|
return None;
|
|
}
|
|
BROWSER.iter().find(|(key, shift, _)| *key == chord.key && *shift == m.shift).map(|(_, _, what)| *what)
|
|
}
|
|
|
|
/// [`desktop_layout`], with Quit labelled `quit` or left out.
|
|
fn desktop_bar(bar: &MenuBar, target: Target, quit: Option<&'static str>, options: &LayoutOptions) -> MenuBar {
|
|
let mut bar = shown_on(bar, target);
|
|
let item = |role: ItemRole| move |n: &Node| matches!(n, Node::Item(it) if it.role == Some(role));
|
|
let about = take(&mut bar, item(ItemRole::About));
|
|
let settings = take(&mut bar, |n| match n {
|
|
Node::Item(it) => it.role == Some(ItemRole::Settings),
|
|
Node::Submenu { role, .. } => *role == Some(ItemRole::Settings),
|
|
_ => false,
|
|
});
|
|
let own_quit = take(&mut bar, item(ItemRole::Quit));
|
|
|
|
if let Some(label) = quit {
|
|
let mut it = match own_quit {
|
|
Some(Node::Item(it)) => it,
|
|
_ => Item::new(Builtin::Quit.command(), "").role(ItemRole::Quit),
|
|
};
|
|
it.label = options.text(label);
|
|
it.english = Some(options.english_text(label));
|
|
ensure_menu(&mut bar, MenuRole::File, "File", options).children.extend([Node::Separator, Node::Item(it)]);
|
|
}
|
|
if let Some(s) = settings {
|
|
let s = match s {
|
|
Node::Item(mut it) => {
|
|
it.label = options.text("Preferences…");
|
|
it.english = Some("Preferences…".to_owned());
|
|
Node::Item(it)
|
|
}
|
|
Node::Submenu { children, role, platforms, .. } => {
|
|
Node::Submenu { label: options.text("Preferences"), english: Some("Preferences".to_owned()), children, role, platforms }
|
|
}
|
|
other => other,
|
|
};
|
|
ensure_menu(&mut bar, MenuRole::Edit, "Edit", options).children.extend([Node::Separator, s]);
|
|
}
|
|
let documents = document_items(&bar);
|
|
if !documents.is_empty() {
|
|
let window = ensure_menu(&mut bar, MenuRole::Window, "Window", options);
|
|
window.children.push(Node::Separator);
|
|
window.children.extend(documents);
|
|
}
|
|
if let Some(Node::Item(mut it)) = about {
|
|
it.label = options.text("About {app}");
|
|
it.english = Some(options.english_text("About {app}"));
|
|
ensure_menu(&mut bar, MenuRole::Help, "Help", options).children.extend([Node::Separator, Node::Item(it)]);
|
|
}
|
|
finish(&mut bar, |_| false);
|
|
bar
|
|
}
|
|
|
|
/// The app's menus with only what `target` shows: no items or submenus whose [`Platforms`]
|
|
/// leave it out, and no AppKit items off macOS. Tidied, so a submenu left empty goes.
|
|
fn shown_on(bar: &MenuBar, target: Target) -> MenuBar {
|
|
fn keep(nodes: &[Node], on: Platforms, appkit: bool) -> Vec<Node> {
|
|
let shown = nodes.iter().filter_map(|n| match n {
|
|
Node::Item(it) if !it.platforms.contains(on) => None,
|
|
Node::Submenu { platforms, .. } if !platforms.contains(on) => None,
|
|
Node::Submenu { label, english, children, role, platforms } => Some(Node::Submenu {
|
|
label: label.clone(),
|
|
english: english.clone(),
|
|
children: keep(children, on, appkit),
|
|
role: *role,
|
|
platforms: *platforms,
|
|
}),
|
|
Node::Standard { .. } if !appkit => None,
|
|
other => Some(other.clone()),
|
|
});
|
|
let mut kept: Vec<Node> = shown.collect();
|
|
tidy(&mut kept);
|
|
kept
|
|
}
|
|
let on = target.platforms();
|
|
let menus = bar.menus.iter().map(|m| Menu { children: keep(&m.children, on, target == Target::Mac), ..m.clone() }).collect();
|
|
MenuBar { menus, documents: bar.documents.clone() }
|
|
}
|
|
|
|
/// The listed documents as check items, the one in front checked, after a separator.
|
|
fn document_items(bar: &MenuBar) -> Vec<Node> {
|
|
let items: Vec<Node> =
|
|
bar.documents.iter().filter(|d| !d.hidden).map(|d| Item::new(d.command.clone(), d.label.clone()).check(d.active).into()).collect();
|
|
if items.is_empty() {
|
|
return items;
|
|
}
|
|
std::iter::once(Node::Separator).chain(items).collect()
|
|
}
|
|
|
|
/// The menu with `role`, added with the title `english` when the app has none: File first, Edit
|
|
/// after File, Help last, any other before Help.
|
|
fn ensure_menu<'b>(bar: &'b mut MenuBar, role: MenuRole, english: &'static str, options: &LayoutOptions) -> &'b mut Menu {
|
|
let at = match bar.menus.iter().position(|m| m.role == role) {
|
|
Some(i) => i,
|
|
None => {
|
|
let at = match role {
|
|
MenuRole::File => 0,
|
|
MenuRole::Edit => bar.menus.iter().position(|m| m.role == MenuRole::File).map_or(0, |i| i + 1),
|
|
MenuRole::Help => bar.menus.len(),
|
|
_ => bar.menus.iter().position(|m| m.role == MenuRole::Help).unwrap_or(bar.menus.len()),
|
|
};
|
|
bar.menus.insert(at, Menu::new(options.text(english), Vec::new()).english(english).role(role));
|
|
at
|
|
}
|
|
};
|
|
&mut bar.menus[at]
|
|
}
|
|
|
|
/// Tidy every menu and drop the ones the moves emptied, unless `keep_empty`.
|
|
fn finish(bar: &mut MenuBar, keep_empty: impl Fn(&Menu) -> bool) {
|
|
for m in &mut bar.menus {
|
|
tidy(&mut m.children);
|
|
}
|
|
bar.menus.retain(|m| !m.children.is_empty() || keep_empty(m));
|
|
}
|
|
|
|
/// Settings in the app menu: an item becomes "Settings…", a submenu of settings pages
|
|
/// "Settings". ⌘, goes to the item or the first enabled page, unless the app binds ⌘, elsewhere.
|
|
fn settings_node(node: Node, comma_taken: Option<CommandRef>, options: &LayoutOptions, clashes: &mut Vec<Clash>) -> Node {
|
|
if let Some(command) = &comma_taken {
|
|
clashes.push(Clash { shortcut: CMD_COMMA, system: "Settings", command: command.clone(), resolution: "Settings has no shortcut" });
|
|
}
|
|
let give = |it: &mut Item| {
|
|
if comma_taken.is_none() && it.shortcut.is_none() {
|
|
it.shortcut = Some(CMD_COMMA);
|
|
it.layout_shortcut = true;
|
|
}
|
|
};
|
|
match node {
|
|
Node::Item(mut it) => {
|
|
it.label = options.text("Settings…");
|
|
it.english = Some("Settings…".to_owned());
|
|
give(&mut it);
|
|
Node::Item(it)
|
|
}
|
|
Node::Submenu { mut children, role, platforms, .. } => {
|
|
if let Some(Node::Item(first)) = children.iter_mut().find(|n| matches!(n, Node::Item(i) if i.enabled)) {
|
|
give(first);
|
|
}
|
|
Node::Submenu { label: options.text("Settings"), english: Some("Settings".to_owned()), children, role, platforms }
|
|
}
|
|
other => other,
|
|
}
|
|
}
|
|
|
|
/// Appearance in the app menu: the submenu tagged [`ItemRole::Appearance`], renamed, with the
|
|
/// items tagged the same (Next Theme) after it. Without such a submenu the tagged items go into
|
|
/// the app menu as they are.
|
|
fn appearance_node(bar: &mut MenuBar, options: &LayoutOptions) -> Vec<Node> {
|
|
let submenu = take(bar, |n| matches!(n, Node::Submenu { role, .. } if *role == Some(ItemRole::Appearance)));
|
|
let mut items = Vec::new();
|
|
while let Some(it) = take(bar, |n| matches!(n, Node::Item(it) if it.role == Some(ItemRole::Appearance))) {
|
|
items.push(it);
|
|
}
|
|
match submenu {
|
|
Some(Node::Submenu { mut children, role, platforms, .. }) => {
|
|
if !items.is_empty() {
|
|
children.push(Node::Separator);
|
|
children.extend(items);
|
|
}
|
|
vec![Node::Submenu { label: options.text("Appearance"), english: Some("Appearance".to_owned()), children, role, platforms }]
|
|
}
|
|
_ => items,
|
|
}
|
|
}
|
|
|
|
pub(crate) fn node_commands(node: &Node) -> Vec<CommandRef> {
|
|
match node {
|
|
Node::Item(it) => vec![it.command.clone()],
|
|
Node::Submenu { children, .. } => children.iter().flat_map(node_commands).collect(),
|
|
_ => Vec::new(),
|
|
}
|
|
}
|
|
|
|
/// Remove and return the first node matching `pred`, at any depth.
|
|
fn take(bar: &mut MenuBar, pred: impl Fn(&Node) -> bool + Copy) -> Option<Node> {
|
|
fn walk(nodes: &mut Vec<Node>, pred: impl Fn(&Node) -> bool + Copy) -> Option<Node> {
|
|
if let Some(i) = nodes.iter().position(pred) {
|
|
return Some(nodes.remove(i));
|
|
}
|
|
nodes.iter_mut().find_map(|n| match n {
|
|
Node::Submenu { children, .. } => walk(children, pred),
|
|
_ => None,
|
|
})
|
|
}
|
|
bar.menus.iter_mut().find_map(|m| walk(&mut m.children, pred))
|
|
}
|