Skip to content

Commit

Permalink
gpui: Update Menu's name type to use Cow<'a, str> to support more t…
Browse files Browse the repository at this point in the history
…ypes.
  • Loading branch information
huacnlee committed Jul 19, 2024
1 parent 48211e8 commit b9c909a
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion crates/gpui/examples/image/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn main() {
cx.on_action(|_: &Quit, cx| cx.quit());
cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]);
cx.set_menus(vec![Menu {
name: "Image",
name: "Image".into(),
items: vec![MenuItem::action("Quit", Quit)],
}]);

Expand Down
2 changes: 1 addition & 1 deletion crates/gpui/examples/set_menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
cx.on_action(quit);
// Add menu items
cx.set_menus(vec![Menu {
name: "set_menus",
name: "set_menus".into(),
items: vec![MenuItem::action("Quit", Quit)],
}]);
cx.open_window(WindowOptions::default(), |cx| {
Expand Down
16 changes: 10 additions & 6 deletions crates/gpui/src/platform/app_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use util::ResultExt;
/// A menu of the application, either a main menu or a submenu
pub struct Menu<'a> {
/// The name of the menu
pub name: &'a str,
pub name: Cow<'a, str>,

/// The items in the menu
pub items: Vec<MenuItem<'a>>,
Expand All @@ -33,7 +33,7 @@ pub enum MenuItem<'a> {
/// An action that can be performed
Action {
/// The name of this menu item
name: &'a str,
name: Cow<'a, str>,

/// the action to perform when this menu item is selected
action: Box<dyn Action>,
Expand All @@ -56,18 +56,22 @@ impl<'a> MenuItem<'a> {
}

/// 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<Cow<'a, str>>, action: impl Action) -> Self {
Self::Action {
name,
name: name.into(),
action: Box::new(action),
os_action: None,
}
}

/// 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<Cow<'a, str>>,
action: impl Action,
os_action: OsAction,
) -> Self {
Self::Action {
name,
name: name.into(),
action: Box::new(action),
os_action: Some(os_action),
}
Expand Down
8 changes: 4 additions & 4 deletions crates/gpui/src/platform/mac/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl MacPlatform {

for menu_config in menus {
let menu = NSMenu::new(nil).autorelease();
menu.setTitle_(ns_string(menu_config.name));
menu.setTitle_(ns_string(&menu_config.name));
menu.setDelegate_(delegate);

for item_config in menu_config.items {
Expand Down Expand Up @@ -310,7 +310,7 @@ impl MacPlatform {

item = NSMenuItem::alloc(nil)
.initWithTitle_action_keyEquivalent_(
ns_string(name),
ns_string(&name),
selector,
ns_string(key_to_native(&keystroke.key).as_ref()),
)
Expand Down Expand Up @@ -341,7 +341,7 @@ impl MacPlatform {
} else {
item = NSMenuItem::alloc(nil)
.initWithTitle_action_keyEquivalent_(
ns_string(name),
ns_string(&name),
selector,
ns_string(""),
)
Expand All @@ -361,7 +361,7 @@ impl MacPlatform {
submenu.addItem_(Self::create_menu_item(item, delegate, actions, keymap));
}
item.setSubmenu_(submenu);
item.setTitle_(ns_string(name));
item.setTitle_(ns_string(&name));
item
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/live_kit_client/examples/test_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ fn main() {
cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]);

cx.set_menus(vec![Menu {
name: "Zed",
name: "Zed".into(),
items: vec![MenuItem::Action {
name: "Quit",
name: "Quit".into(),
action: Box::new(Quit),
os_action: None,
}],
Expand Down
2 changes: 1 addition & 1 deletion crates/storybook/src/app_menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn app_menus() -> Vec<Menu<'static>> {
use crate::actions::Quit;

vec![Menu {
name: "Storybook",
name: "Storybook".into(),
items: vec![MenuItem::action("Quit", Quit)],
}]
}
20 changes: 10 additions & 10 deletions crates/zed/src/zed/app_menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ pub fn app_menus() -> Vec<Menu<'static>> {

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

0 comments on commit b9c909a

Please sign in to comment.