Skip to content

Commit 034c260

Browse files
authored
feat(core): add accelerator method to CustomMenuItem (#2043)
1 parent 842652a commit 034c260

4 files changed

Lines changed: 20 additions & 2 deletions

File tree

.changes/menu-item-accelerator.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": patch
3+
"tauri-runtime": patch
4+
---
5+
6+
Adds `accelerator` method to the `CustomMenuItem` struct to define a keyboard shortcut for the menu item.

core/tauri-runtime-wry/src/menu.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ pub fn convert_menu_id<I: MenuId>(mut new_menu: Menu<u16>, menu: Menu<I>) -> Men
199199
if let Some(native_image) = c.native_image {
200200
item = item.native_image(native_image);
201201
}
202+
if let Some(accelerator) = c.keyboard_accelerator {
203+
item = item.accelerator(accelerator);
204+
}
202205
if !c.enabled {
203206
item = item.disabled();
204207
}

core/tauri-runtime/src/menu.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,15 @@ impl<I: MenuId> CustomMenuItem<I> {
229229
}
230230
}
231231

232+
/// Assign a keyboard shortcut to the menu action.
233+
pub fn accelerator<T: Into<String>>(mut self, accelerator: T) -> Self {
234+
self.keyboard_accelerator.replace(accelerator.into());
235+
self
236+
}
237+
232238
#[cfg(target_os = "macos")]
239+
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
240+
/// A native image do render on the menu item.
233241
pub fn native_image(mut self, image: NativeImage) -> Self {
234242
self.native_image.replace(image);
235243
self

examples/api/src-tauri/src/menu.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use tauri::{CustomMenuItem, Menu, MenuItem, Submenu};
66

77
pub fn get_menu() -> Menu<String> {
88
#[allow(unused_mut)]
9-
let mut disable_item = CustomMenuItem::new("disable-menu".into(), "Disable menu");
9+
let mut disable_item =
10+
CustomMenuItem::new("disable-menu".into(), "Disable menu").accelerator("CmdOrControl+D");
1011
#[allow(unused_mut)]
11-
let mut test_item = CustomMenuItem::new("test".into(), "Test");
12+
let mut test_item = CustomMenuItem::new("test".into(), "Test").accelerator("CmdOrControl+T");
1213
#[cfg(target_os = "macos")]
1314
{
1415
disable_item = disable_item.native_image(tauri::NativeImage::MenuOnState);

0 commit comments

Comments
 (0)