Browse Source

feat: add `id` option for tray icon in config file (#7871)

Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.studio>
Amr Bashir 1 năm trước cách đây
mục cha
commit
b597aa5f39

+ 5 - 0
.changes/tauri-tray-icon-id.md

@@ -0,0 +1,5 @@
+---
+'tauri': 'patch:enhance'
+---
+
+Set `main` as the default `id` for the tray icon registered from the configuration file, so if the `id` is not specified, it can be retrieved using `app.tray_by_id("main")`.

+ 5 - 0
.changes/tauri-utils-tray-icon-id copy.md

@@ -0,0 +1,5 @@
+---
+'tauri-utils': 'patch:enhance'
+---
+
+Add an option to specify `id` for the tray icon in the tauri configuration file.

+ 7 - 0
core/tauri-config-schema/schema.json

@@ -2072,6 +2072,13 @@
         "iconPath"
       ],
       "properties": {
+        "id": {
+          "description": "Set an id for this tray icon so you can reference it later, defaults to `main`.",
+          "type": [
+            "string",
+            "null"
+          ]
+        },
         "iconPath": {
           "description": "Path to the default icon to use for the tray icon.",
           "type": "string"

+ 4 - 0
core/tauri-utils/src/config.rs

@@ -1558,6 +1558,8 @@ pub struct UpdaterWindowsConfig {
 #[cfg_attr(feature = "schema", derive(JsonSchema))]
 #[serde(rename_all = "camelCase", deny_unknown_fields)]
 pub struct TrayIconConfig {
+  /// Set an id for this tray icon so you can reference it later, defaults to `main`.
+  pub id: Option<String>,
   /// Path to the default icon to use for the tray icon.
   #[serde(alias = "icon-path")]
   pub icon_path: PathBuf,
@@ -2570,6 +2572,7 @@ mod build {
 
   impl ToTokens for TrayIconConfig {
     fn to_tokens(&self, tokens: &mut TokenStream) {
+      let id = opt_str_lit(self.id.as_ref());
       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);
@@ -2578,6 +2581,7 @@ mod build {
       literal_struct!(
         tokens,
         TrayIconConfig,
+        id,
         icon_path,
         icon_as_template,
         menu_on_left_click,

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

@@ -531,8 +531,8 @@ macro_rules! shared_app_impl {
           .push(Box::new(handler));
       }
 
-      /// Gets the first tray icon registerd, usually the one configured in
-      /// tauri config file.
+      /// Gets the first tray icon registered,
+      /// usually the one configured in the Tauri configuration file.
       #[cfg(all(desktop, feature = "tray-icon"))]
       #[cfg_attr(doc_cfg, doc(cfg(all(desktop, feature = "tray-icon"))))]
       pub fn tray(&self) -> Option<TrayIcon<R>> {
@@ -1615,9 +1615,10 @@ impl<R: Runtime> Builder<R> {
     {
       let config = app.config();
       if let Some(tray_config) = &config.tauri.tray_icon {
-        let mut tray = TrayIconBuilder::new()
-          .icon_as_template(tray_config.icon_as_template)
-          .menu_on_left_click(tray_config.menu_on_left_click);
+        let mut tray =
+          TrayIconBuilder::with_id(tray_config.id.clone().unwrap_or_else(|| "main".into()))
+            .icon_as_template(tray_config.icon_as_template)
+            .menu_on_left_click(tray_config.menu_on_left_click);
         if let Some(icon) = &app.manager.inner.tray_icon {
           tray = tray.icon(icon.clone());
         }

+ 7 - 0
tooling/cli/schema.json

@@ -2072,6 +2072,13 @@
         "iconPath"
       ],
       "properties": {
+        "id": {
+          "description": "Set an id for this tray icon so you can reference it later, defaults to `main`.",
+          "type": [
+            "string",
+            "null"
+          ]
+        },
         "iconPath": {
           "description": "Path to the default icon to use for the tray icon.",
           "type": "string"