Skip to content

Commit f8a3bec

Browse files
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>
1 parent f7c59ec commit f8a3bec

File tree

8 files changed

+52
-6
lines changed

8 files changed

+52
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-utils": patch
3+
---
4+
5+
Added `menu_on_left_click: bool` to the `SystemTrayConfig`.

.changes/menu-on-left-click.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": patch
3+
"tauri-runtime": patch
4+
"tauri-runtime-wry": patch
5+
---
6+
7+
Added option to disable tray menu on left click on macOS.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,9 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
19111911

19121912
#[cfg(target_os = "macos")]
19131913
{
1914-
tray_builder = tray_builder.with_icon_as_template(system_tray.icon_as_template);
1914+
tray_builder = tray_builder
1915+
.with_icon_as_template(system_tray.icon_as_template)
1916+
.with_menu_on_left_click(system_tray.menu_on_left_click);
19151917
}
19161918

19171919
let tray = tray_builder

core/tauri-runtime/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ pub struct SystemTray {
4141
pub menu: Option<menu::SystemTrayMenu>,
4242
#[cfg(target_os = "macos")]
4343
pub icon_as_template: bool,
44+
#[cfg(target_os = "macos")]
45+
pub menu_on_left_click: bool,
4446
}
4547

4648
#[cfg(feature = "system-tray")]
@@ -69,6 +71,14 @@ impl SystemTray {
6971
self
7072
}
7173

74+
/// Sets whether the menu should appear when the tray receives a left click. Defaults to `true`.
75+
#[cfg(target_os = "macos")]
76+
#[must_use]
77+
pub fn with_menu_on_left_click(mut self, menu_on_left_click: bool) -> Self {
78+
self.menu_on_left_click = menu_on_left_click;
79+
self
80+
}
81+
7282
/// Sets the menu to show when the system tray is right clicked.
7383
#[must_use]
7484
pub fn with_menu(mut self, menu: menu::SystemTrayMenu) -> Self {

core/tauri-utils/src/config.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2295,6 +2295,13 @@ pub struct SystemTrayConfig {
22952295
/// 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.
22962296
#[serde(default)]
22972297
pub icon_as_template: bool,
2298+
/// A Boolean value that determines whether the menu should appear when the tray icon receives a left click on macOS.
2299+
#[serde(default = "default_tray_menu_on_left_click")]
2300+
pub menu_on_left_click: bool,
2301+
}
2302+
2303+
fn default_tray_menu_on_left_click() -> bool {
2304+
true
22982305
}
22992306

23002307
// We enable the unnecessary_wraps because we need
@@ -3184,8 +3191,15 @@ mod build {
31843191
impl ToTokens for SystemTrayConfig {
31853192
fn to_tokens(&self, tokens: &mut TokenStream) {
31863193
let icon_as_template = self.icon_as_template;
3194+
let menu_on_left_click = self.menu_on_left_click;
31873195
let icon_path = path_buf_lit(&self.icon_path);
3188-
literal_struct!(tokens, SystemTrayConfig, icon_path, icon_as_template);
3196+
literal_struct!(
3197+
tokens,
3198+
SystemTrayConfig,
3199+
icon_path,
3200+
icon_as_template,
3201+
menu_on_left_click
3202+
);
31893203
}
31903204
}
31913205

core/tauri/src/app.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,12 +1346,12 @@ impl<R: Runtime> Builder<R> {
13461346
let system_tray_icon = context.system_tray_icon.clone();
13471347

13481348
#[cfg(all(feature = "system-tray", target_os = "macos"))]
1349-
let system_tray_icon_as_template = context
1349+
let (system_tray_icon_as_template, system_tray_menu_on_left_click) = context
13501350
.config
13511351
.tauri
13521352
.system_tray
13531353
.as_ref()
1354-
.map(|t| t.icon_as_template)
1354+
.map(|t| (t.icon_as_template, t.menu_on_left_click))
13551355
.unwrap_or_default();
13561356

13571357
#[cfg(shell_scope)]
@@ -1492,7 +1492,9 @@ impl<R: Runtime> Builder<R> {
14921492
tray = tray.with_menu(menu);
14931493
}
14941494
#[cfg(target_os = "macos")]
1495-
let tray = tray.with_icon_as_template(system_tray_icon_as_template);
1495+
let tray = tray
1496+
.with_icon_as_template(system_tray_icon_as_template)
1497+
.with_menu_on_left_click(system_tray_menu_on_left_click);
14961498

14971499
let tray_handler = app
14981500
.runtime

examples/api/src-tauri/tauri.conf.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@
129129
},
130130
"systemTray": {
131131
"iconPath": "../../.icons/tray_icon_with_transparency.png",
132-
"iconAsTemplate": true
132+
"iconAsTemplate": true,
133+
"menuOnLeftClick": false
133134
}
134135
}
135136
}

tooling/cli/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,6 +2409,11 @@
24092409
"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.",
24102410
"default": false,
24112411
"type": "boolean"
2412+
},
2413+
"menuOnLeftClick": {
2414+
"description": "A Boolean value that determines whether the menu should appear when the tray icon receives a left click on macOS.",
2415+
"default": true,
2416+
"type": "boolean"
24122417
}
24132418
},
24142419
"additionalProperties": false

0 commit comments

Comments
 (0)