feat(core): add accelerator method to CustomMenuItem (#2043)

This commit is contained in:
Lucas Fernandes Nogueira 2021-06-22 12:53:13 -03:00 committed by GitHub
parent 842652abe5
commit 034c26013b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 2 deletions

View File

@ -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.

View File

@ -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();
}

View File

@ -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

View File

@ -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);