Explorar el Código

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>
Lucas Fernandes Nogueira hace 3 años
padre
commit
f8a3becb28

+ 5 - 0
.changes/menu-on-left-click-config.md

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

+ 7 - 0
.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.

+ 3 - 1
core/tauri-runtime-wry/src/lib.rs

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

+ 10 - 0
core/tauri-runtime/src/lib.rs

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

+ 15 - 1
core/tauri-utils/src/config.rs

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

+ 5 - 3
core/tauri/src/app.rs

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

+ 2 - 1
examples/api/src-tauri/tauri.conf.json

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

+ 5 - 0
tooling/cli/schema.json

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