gpui: Update Menu name to use SharedString type to support more types (#14791)

Release Notes:

- N/A
This commit is contained in:
Jason Lee 2024-07-19 20:51:31 +08:00 committed by GitHub
parent 836f623800
commit fb541accb2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 40 additions and 38 deletions

View file

@ -68,7 +68,7 @@ fn main() {
cx.on_action(|_: &Quit, cx| cx.quit()); cx.on_action(|_: &Quit, cx| cx.quit());
cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]); cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]);
cx.set_menus(vec![Menu { cx.set_menus(vec![Menu {
name: "Image", name: "Image".into(),
items: vec![MenuItem::action("Quit", Quit)], items: vec![MenuItem::action("Quit", Quit)],
}]); }]);

View file

@ -24,7 +24,7 @@ fn main() {
cx.on_action(quit); cx.on_action(quit);
// Add menu items // Add menu items
cx.set_menus(vec![Menu { cx.set_menus(vec![Menu {
name: "set_menus", name: "set_menus".into(),
items: vec![MenuItem::action("Quit", Quit)], items: vec![MenuItem::action("Quit", Quit)],
}]); }]);
cx.open_window(WindowOptions::default(), |cx| { cx.open_window(WindowOptions::default(), |cx| {

View file

@ -1,18 +1,16 @@
use std::borrow::Cow; use crate::{Action, AppContext, Platform, SharedString};
use crate::{Action, AppContext, Platform};
use util::ResultExt; use util::ResultExt;
/// A menu of the application, either a main menu or a submenu /// A menu of the application, either a main menu or a submenu
pub struct Menu<'a> { pub struct Menu {
/// The name of the menu /// The name of the menu
pub name: &'a str, pub name: SharedString,
/// The items in the menu /// The items in the menu
pub items: Vec<MenuItem<'a>>, pub items: Vec<MenuItem>,
} }
impl<'a> Menu<'a> { impl Menu {
/// Create an OwnedMenu from this Menu /// Create an OwnedMenu from this Menu
pub fn owned(self) -> OwnedMenu { pub fn owned(self) -> OwnedMenu {
OwnedMenu { OwnedMenu {
@ -23,17 +21,17 @@ impl<'a> Menu<'a> {
} }
/// The different kinds of items that can be in a menu /// The different kinds of items that can be in a menu
pub enum MenuItem<'a> { pub enum MenuItem {
/// A separator between items /// A separator between items
Separator, Separator,
/// A submenu /// A submenu
Submenu(Menu<'a>), Submenu(Menu),
/// An action that can be performed /// An action that can be performed
Action { Action {
/// The name of this menu item /// The name of this menu item
name: &'a str, name: SharedString,
/// the action to perform when this menu item is selected /// the action to perform when this menu item is selected
action: Box<dyn Action>, action: Box<dyn Action>,
@ -44,30 +42,34 @@ pub enum MenuItem<'a> {
}, },
} }
impl<'a> MenuItem<'a> { impl MenuItem {
/// Creates a new menu item that is a separator /// Creates a new menu item that is a separator
pub fn separator() -> Self { pub fn separator() -> Self {
Self::Separator Self::Separator
} }
/// Creates a new menu item that is a submenu /// Creates a new menu item that is a submenu
pub fn submenu(menu: Menu<'a>) -> Self { pub fn submenu(menu: Menu) -> Self {
Self::Submenu(menu) Self::Submenu(menu)
} }
/// Creates a new menu item that invokes an action /// Creates a new menu item that invokes an action
pub fn action(name: &'a str, action: impl Action) -> Self { pub fn action(name: impl Into<SharedString>, action: impl Action) -> Self {
Self::Action { Self::Action {
name, name: name.into(),
action: Box::new(action), action: Box::new(action),
os_action: None, os_action: None,
} }
} }
/// Creates a new menu item that invokes an action and has an OS action /// Creates a new menu item that invokes an action and has an OS action
pub fn os_action(name: &'a str, action: impl Action, os_action: OsAction) -> Self { pub fn os_action(
name: impl Into<SharedString>,
action: impl Action,
os_action: OsAction,
) -> Self {
Self::Action { Self::Action {
name, name: name.into(),
action: Box::new(action), action: Box::new(action),
os_action: Some(os_action), os_action: Some(os_action),
} }
@ -95,7 +97,7 @@ impl<'a> MenuItem<'a> {
#[derive(Clone)] #[derive(Clone)]
pub struct OwnedMenu { pub struct OwnedMenu {
/// The name of the menu /// The name of the menu
pub name: Cow<'static, str>, pub name: SharedString,
/// The items in the menu /// The items in the menu
pub items: Vec<OwnedMenuItem>, pub items: Vec<OwnedMenuItem>,

View file

@ -206,7 +206,7 @@ impl MacPlatform {
for menu_config in menus { for menu_config in menus {
let menu = NSMenu::new(nil).autorelease(); let menu = NSMenu::new(nil).autorelease();
menu.setTitle_(ns_string(menu_config.name)); menu.setTitle_(ns_string(&menu_config.name));
menu.setDelegate_(delegate); menu.setDelegate_(delegate);
for item_config in menu_config.items { for item_config in menu_config.items {
@ -310,7 +310,7 @@ impl MacPlatform {
item = NSMenuItem::alloc(nil) item = NSMenuItem::alloc(nil)
.initWithTitle_action_keyEquivalent_( .initWithTitle_action_keyEquivalent_(
ns_string(name), ns_string(&name),
selector, selector,
ns_string(key_to_native(&keystroke.key).as_ref()), ns_string(key_to_native(&keystroke.key).as_ref()),
) )
@ -341,7 +341,7 @@ impl MacPlatform {
} else { } else {
item = NSMenuItem::alloc(nil) item = NSMenuItem::alloc(nil)
.initWithTitle_action_keyEquivalent_( .initWithTitle_action_keyEquivalent_(
ns_string(name), ns_string(&name),
selector, selector,
ns_string(""), ns_string(""),
) )
@ -361,7 +361,7 @@ impl MacPlatform {
submenu.addItem_(Self::create_menu_item(item, delegate, actions, keymap)); submenu.addItem_(Self::create_menu_item(item, delegate, actions, keymap));
} }
item.setSubmenu_(submenu); item.setSubmenu_(submenu);
item.setTitle_(ns_string(name)); item.setTitle_(ns_string(&name));
item item
} }
} }

View file

@ -25,9 +25,9 @@ fn main() {
cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]); cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]);
cx.set_menus(vec![Menu { cx.set_menus(vec![Menu {
name: "Zed", name: "Zed".into(),
items: vec![MenuItem::Action { items: vec![MenuItem::Action {
name: "Quit", name: "Quit".into(),
action: Box::new(Quit), action: Box::new(Quit),
os_action: None, os_action: None,
}], }],

View file

@ -1,10 +1,10 @@
use gpui::{Menu, MenuItem}; use gpui::{Menu, MenuItem};
pub fn app_menus() -> Vec<Menu<'static>> { pub fn app_menus() -> Vec<Menu> {
use crate::actions::Quit; use crate::actions::Quit;
vec![Menu { vec![Menu {
name: "Storybook", name: "Storybook".into(),
items: vec![MenuItem::action("Quit", Quit)], items: vec![MenuItem::action("Quit", Quit)],
}] }]
} }

View file

@ -2,18 +2,18 @@ use collab_ui::collab_panel;
use gpui::{Menu, MenuItem, OsAction}; use gpui::{Menu, MenuItem, OsAction};
use terminal_view::terminal_panel; use terminal_view::terminal_panel;
pub fn app_menus() -> Vec<Menu<'static>> { pub fn app_menus() -> Vec<Menu> {
use zed_actions::Quit; use zed_actions::Quit;
vec![ vec![
Menu { Menu {
name: "Zed", name: "Zed".into(),
items: vec![ items: vec![
MenuItem::action("About Zed…", zed_actions::About), MenuItem::action("About Zed…", zed_actions::About),
MenuItem::action("Check for Updates", auto_update::Check), MenuItem::action("Check for Updates", auto_update::Check),
MenuItem::separator(), MenuItem::separator(),
MenuItem::submenu(Menu { MenuItem::submenu(Menu {
name: "Preferences", name: "Preferences".into(),
items: vec![ items: vec![
MenuItem::action("Open Settings", super::OpenSettings), MenuItem::action("Open Settings", super::OpenSettings),
MenuItem::action("Open Key Bindings", zed_actions::OpenKeymap), MenuItem::action("Open Key Bindings", zed_actions::OpenKeymap),
@ -33,7 +33,7 @@ pub fn app_menus() -> Vec<Menu<'static>> {
], ],
}, },
Menu { Menu {
name: "File", name: "File".into(),
items: vec![ items: vec![
MenuItem::action("New", workspace::NewFile), MenuItem::action("New", workspace::NewFile),
MenuItem::action("New Window", workspace::NewWindow), MenuItem::action("New Window", workspace::NewWindow),
@ -58,7 +58,7 @@ pub fn app_menus() -> Vec<Menu<'static>> {
], ],
}, },
Menu { Menu {
name: "Edit", name: "Edit".into(),
items: vec![ items: vec![
MenuItem::os_action("Undo", editor::actions::Undo, OsAction::Undo), MenuItem::os_action("Undo", editor::actions::Undo, OsAction::Undo),
MenuItem::os_action("Redo", editor::actions::Redo, OsAction::Redo), MenuItem::os_action("Redo", editor::actions::Redo, OsAction::Redo),
@ -77,7 +77,7 @@ pub fn app_menus() -> Vec<Menu<'static>> {
], ],
}, },
Menu { Menu {
name: "Selection", name: "Selection".into(),
items: vec![ items: vec![
MenuItem::os_action( MenuItem::os_action(
"Select All", "Select All",
@ -102,7 +102,7 @@ pub fn app_menus() -> Vec<Menu<'static>> {
], ],
}, },
Menu { Menu {
name: "View", name: "View".into(),
items: vec![ items: vec![
MenuItem::action("Zoom In", zed_actions::IncreaseBufferFontSize), MenuItem::action("Zoom In", zed_actions::IncreaseBufferFontSize),
MenuItem::action("Zoom Out", zed_actions::DecreaseBufferFontSize), MenuItem::action("Zoom Out", zed_actions::DecreaseBufferFontSize),
@ -113,7 +113,7 @@ pub fn app_menus() -> Vec<Menu<'static>> {
MenuItem::action("Toggle Bottom Dock", workspace::ToggleBottomDock), MenuItem::action("Toggle Bottom Dock", workspace::ToggleBottomDock),
MenuItem::action("Close All Docks", workspace::CloseAllDocks), MenuItem::action("Close All Docks", workspace::CloseAllDocks),
MenuItem::submenu(Menu { MenuItem::submenu(Menu {
name: "Editor Layout", name: "Editor Layout".into(),
items: vec![ items: vec![
MenuItem::action("Split Up", workspace::SplitUp), MenuItem::action("Split Up", workspace::SplitUp),
MenuItem::action("Split Down", workspace::SplitDown), MenuItem::action("Split Down", workspace::SplitDown),
@ -132,7 +132,7 @@ pub fn app_menus() -> Vec<Menu<'static>> {
], ],
}, },
Menu { Menu {
name: "Go", name: "Go".into(),
items: vec![ items: vec![
MenuItem::action("Back", workspace::GoBack), MenuItem::action("Back", workspace::GoBack),
MenuItem::action("Forward", workspace::GoForward), MenuItem::action("Forward", workspace::GoForward),
@ -153,7 +153,7 @@ pub fn app_menus() -> Vec<Menu<'static>> {
], ],
}, },
Menu { Menu {
name: "Window", name: "Window".into(),
items: vec![ items: vec![
MenuItem::action("Minimize", super::Minimize), MenuItem::action("Minimize", super::Minimize),
MenuItem::action("Zoom", super::Zoom), MenuItem::action("Zoom", super::Zoom),
@ -161,7 +161,7 @@ pub fn app_menus() -> Vec<Menu<'static>> {
], ],
}, },
Menu { Menu {
name: "Help", name: "Help".into(),
items: vec![ items: vec![
MenuItem::action("View Telemetry", zed_actions::OpenTelemetryLog), MenuItem::action("View Telemetry", zed_actions::OpenTelemetryLog),
MenuItem::action("View Dependency Licenses", zed_actions::OpenLicenses), MenuItem::action("View Dependency Licenses", zed_actions::OpenLicenses),