mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-12-01 03:02:28 +03:00
feat(core): add accelerator
method to CustomMenuItem
(#2043)
This commit is contained in:
parent
842652abe5
commit
034c26013b
6
.changes/menu-item-accelerator.md
Normal file
6
.changes/menu-item-accelerator.md
Normal 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.
|
@ -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();
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user