feat(core): add option to disable tray menu on left click, closes #4584 (#4587)

* feat(core): add option to disable tray menu on left click, closes #4584

* Update .changes/menu-on-left-click.md [skip ci]

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
This commit is contained in:
Lucas Fernandes Nogueira 2022-07-05 09:05:01 -03:00 committed by GitHub
parent f7c59ecfc8
commit f8a3becb28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 6 deletions

View File

@ -0,0 +1,5 @@
---
"tauri-utils": patch
---
Added `menu_on_left_click: bool` to the `SystemTrayConfig`.

View File

@ -0,0 +1,7 @@
---
"tauri": patch
"tauri-runtime": patch
"tauri-runtime-wry": patch
---
Added option to disable tray menu on left click on macOS.

View File

@ -1911,7 +1911,9 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
#[cfg(target_os = "macos")]
{
tray_builder = tray_builder.with_icon_as_template(system_tray.icon_as_template);
tray_builder = tray_builder
.with_icon_as_template(system_tray.icon_as_template)
.with_menu_on_left_click(system_tray.menu_on_left_click);
}
let tray = tray_builder

View File

@ -41,6 +41,8 @@ pub struct SystemTray {
pub menu: Option<menu::SystemTrayMenu>,
#[cfg(target_os = "macos")]
pub icon_as_template: bool,
#[cfg(target_os = "macos")]
pub menu_on_left_click: bool,
}
#[cfg(feature = "system-tray")]
@ -69,6 +71,14 @@ impl SystemTray {
self
}
/// Sets whether the menu should appear when the tray receives a left click. Defaults to `true`.
#[cfg(target_os = "macos")]
#[must_use]
pub fn with_menu_on_left_click(mut self, menu_on_left_click: bool) -> Self {
self.menu_on_left_click = menu_on_left_click;
self
}
/// Sets the menu to show when the system tray is right clicked.
#[must_use]
pub fn with_menu(mut self, menu: menu::SystemTrayMenu) -> Self {

View File

@ -2295,6 +2295,13 @@ pub struct SystemTrayConfig {
/// A Boolean value that determines whether the image represents a [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc) image on macOS.
#[serde(default)]
pub icon_as_template: bool,
/// A Boolean value that determines whether the menu should appear when the tray icon receives a left click on macOS.
#[serde(default = "default_tray_menu_on_left_click")]
pub menu_on_left_click: bool,
}
fn default_tray_menu_on_left_click() -> bool {
true
}
// We enable the unnecessary_wraps because we need
@ -3184,8 +3191,15 @@ mod build {
impl ToTokens for SystemTrayConfig {
fn to_tokens(&self, tokens: &mut TokenStream) {
let icon_as_template = self.icon_as_template;
let menu_on_left_click = self.menu_on_left_click;
let icon_path = path_buf_lit(&self.icon_path);
literal_struct!(tokens, SystemTrayConfig, icon_path, icon_as_template);
literal_struct!(
tokens,
SystemTrayConfig,
icon_path,
icon_as_template,
menu_on_left_click
);
}
}

View File

@ -1346,12 +1346,12 @@ impl<R: Runtime> Builder<R> {
let system_tray_icon = context.system_tray_icon.clone();
#[cfg(all(feature = "system-tray", target_os = "macos"))]
let system_tray_icon_as_template = context
let (system_tray_icon_as_template, system_tray_menu_on_left_click) = context
.config
.tauri
.system_tray
.as_ref()
.map(|t| t.icon_as_template)
.map(|t| (t.icon_as_template, t.menu_on_left_click))
.unwrap_or_default();
#[cfg(shell_scope)]
@ -1492,7 +1492,9 @@ impl<R: Runtime> Builder<R> {
tray = tray.with_menu(menu);
}
#[cfg(target_os = "macos")]
let tray = tray.with_icon_as_template(system_tray_icon_as_template);
let tray = tray
.with_icon_as_template(system_tray_icon_as_template)
.with_menu_on_left_click(system_tray_menu_on_left_click);
let tray_handler = app
.runtime

View File

@ -129,7 +129,8 @@
},
"systemTray": {
"iconPath": "../../.icons/tray_icon_with_transparency.png",
"iconAsTemplate": true
"iconAsTemplate": true,
"menuOnLeftClick": false
}
}
}

View File

@ -2409,6 +2409,11 @@
"description": "A Boolean value that determines whether the image represents a [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc) image on macOS.",
"default": false,
"type": "boolean"
},
"menuOnLeftClick": {
"description": "A Boolean value that determines whether the menu should appear when the tray icon receives a left click on macOS.",
"default": true,
"type": "boolean"
}
},
"additionalProperties": false