Skip to content

Commit

Permalink
feat(core): add accelerator method to CustomMenuItem (#2043)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jun 22, 2021
1 parent 842652a commit 034c260
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changes/menu-item-accelerator.md
@@ -0,0 +1,6 @@
---
"tauri": patch
"tauri-runtime": patch
---

Adds `accelerator` method to the `CustomMenuItem` struct to define a keyboard shortcut for the menu item.
3 changes: 3 additions & 0 deletions core/tauri-runtime-wry/src/menu.rs
Expand Up @@ -199,6 +199,9 @@ pub fn convert_menu_id<I: MenuId>(mut new_menu: Menu<u16>, menu: Menu<I>) -> Men
if let Some(native_image) = c.native_image {
item = item.native_image(native_image);
}
if let Some(accelerator) = c.keyboard_accelerator {
item = item.accelerator(accelerator);
}
if !c.enabled {
item = item.disabled();
}
Expand Down
8 changes: 8 additions & 0 deletions core/tauri-runtime/src/menu.rs
Expand Up @@ -229,7 +229,15 @@ impl<I: MenuId> CustomMenuItem<I> {
}
}

/// Assign a keyboard shortcut to the menu action.
pub fn accelerator<T: Into<String>>(mut self, accelerator: T) -> Self {
self.keyboard_accelerator.replace(accelerator.into());
self
}

#[cfg(target_os = "macos")]
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
/// A native image do render on the menu item.
pub fn native_image(mut self, image: NativeImage) -> Self {
self.native_image.replace(image);
self
Expand Down
5 changes: 3 additions & 2 deletions examples/api/src-tauri/src/menu.rs
Expand Up @@ -6,9 +6,10 @@ use tauri::{CustomMenuItem, Menu, MenuItem, Submenu};

pub fn get_menu() -> Menu<String> {
#[allow(unused_mut)]
let mut disable_item = CustomMenuItem::new("disable-menu".into(), "Disable menu");
let mut disable_item =
CustomMenuItem::new("disable-menu".into(), "Disable menu").accelerator("CmdOrControl+D");
#[allow(unused_mut)]
let mut test_item = CustomMenuItem::new("test".into(), "Test");
let mut test_item = CustomMenuItem::new("test".into(), "Test").accelerator("CmdOrControl+T");
#[cfg(target_os = "macos")]
{
disable_item = disable_item.native_image(tauri::NativeImage::MenuOnState);
Expand Down

0 comments on commit 034c260

Please sign in to comment.