Skip to content

Commit

Permalink
feat(core): add option to disable tray menu on left click, closes #4584
Browse files Browse the repository at this point in the history
… (#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>
  • Loading branch information
lucasfernog and amrbashir committed Jul 5, 2022
1 parent f7c59ec commit f8a3bec
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changes/menu-on-left-click-config.md
@@ -0,0 +1,5 @@
---
"tauri-utils": patch
---

Added `menu_on_left_click: bool` to the `SystemTrayConfig`.
7 changes: 7 additions & 0 deletions .changes/menu-on-left-click.md
@@ -0,0 +1,7 @@
---
"tauri": patch
"tauri-runtime": patch
"tauri-runtime-wry": patch
---

Added option to disable tray menu on left click on macOS.
4 changes: 3 additions & 1 deletion core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions core/tauri-runtime/src/lib.rs
Expand Up @@ -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")]
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 15 additions & 1 deletion core/tauri-utils/src/config.rs
Expand Up @@ -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
Expand Down Expand Up @@ -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
);
}
}

Expand Down
8 changes: 5 additions & 3 deletions core/tauri/src/app.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion examples/api/src-tauri/tauri.conf.json
Expand Up @@ -129,7 +129,8 @@
},
"systemTray": {
"iconPath": "../../.icons/tray_icon_with_transparency.png",
"iconAsTemplate": true
"iconAsTemplate": true,
"menuOnLeftClick": false
}
}
}
5 changes: 5 additions & 0 deletions tooling/cli/schema.json
Expand Up @@ -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
Expand Down

0 comments on commit f8a3bec

Please sign in to comment.