Skip to main content

gpui_platform/
menu.rs

1//! Pre-resolved application menu vocabulary shared by `gpui` and its platform backends.
2//!
3//! `gpui` resolves its higher-level menu tree (which carries typed actions and a
4//! keymap) into these plain-data structures before handing them to a platform.
5//! Actionable items carry an opaque [`MenuCommandId`] that the platform reports
6//! back through its menu callbacks.
7
8use gpui_shared_string::SharedString;
9use gpui_types::Keystroke;
10
11/// Identifies a menu command whose action `gpui` will dispatch.
12pub type MenuCommandId = usize;
13
14/// The type of a system-managed menu.
15#[derive(Copy, Clone, Eq, PartialEq, Debug)]
16pub enum SystemMenuType {
17    /// The 'Services' menu in the Application menu on macOS.
18    Services,
19}
20
21/// An action that the operating system recognizes and can give specialized
22/// behavior to.
23#[derive(Copy, Clone, Eq, PartialEq, Debug)]
24pub enum OsAction {
25    /// The 'cut' action.
26    Cut,
27    /// The 'copy' action.
28    Copy,
29    /// The 'paste' action.
30    Paste,
31    /// The 'select all' action.
32    SelectAll,
33    /// The 'undo' action.
34    Undo,
35    /// The 'redo' action.
36    Redo,
37}
38
39/// A menu managed by the operating system.
40#[derive(Clone, Debug)]
41pub struct PlatformOsMenu {
42    /// The name of the menu.
43    pub name: SharedString,
44    /// The type of menu.
45    pub menu_type: SystemMenuType,
46}
47
48/// A menu of the application, either a main menu or a submenu.
49#[derive(Clone, Debug)]
50pub struct PlatformMenu {
51    /// The name of the menu.
52    pub name: SharedString,
53    /// The items in the menu.
54    pub items: Vec<PlatformMenuItem>,
55    /// Whether this menu is disabled.
56    pub disabled: bool,
57}
58
59/// The different kinds of items that can be in a menu.
60#[derive(Clone, Debug)]
61pub enum PlatformMenuItem {
62    /// A separator between items.
63    Separator,
64
65    /// A submenu.
66    Submenu(PlatformMenu),
67
68    /// A menu managed by the system (for example, the Services menu on macOS).
69    SystemMenu(PlatformOsMenu),
70
71    /// An action that can be performed.
72    Action {
73        /// The name of this menu item.
74        name: String,
75        /// The opaque command id that identifies the action to dispatch.
76        command_id: MenuCommandId,
77        /// The resolved keyboard accelerator to display, if the action has
78        /// exactly one applicable keystroke.
79        keystroke: Option<Keystroke>,
80        /// The OS action that corresponds to this action, if any.
81        os_action: Option<OsAction>,
82        /// Whether this action is checked.
83        checked: bool,
84        /// Whether this action is disabled.
85        disabled: bool,
86    },
87}