From b597aa5f3974f5ca5ca5159d441abc9ed3e80721 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Tue, 3 Oct 2023 16:18:19 +0300 Subject: [PATCH] feat: add `id` option for tray icon in config file (#7871) Co-authored-by: Lucas Fernandes Nogueira --- .changes/tauri-tray-icon-id.md | 5 +++++ .changes/tauri-utils-tray-icon-id copy.md | 5 +++++ core/tauri-config-schema/schema.json | 7 +++++++ core/tauri-utils/src/config.rs | 4 ++++ core/tauri/src/app.rs | 11 ++++++----- tooling/cli/schema.json | 7 +++++++ 6 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 .changes/tauri-tray-icon-id.md create mode 100644 .changes/tauri-utils-tray-icon-id copy.md diff --git a/.changes/tauri-tray-icon-id.md b/.changes/tauri-tray-icon-id.md new file mode 100644 index 000000000..5e42a4fda --- /dev/null +++ b/.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")`. diff --git a/.changes/tauri-utils-tray-icon-id copy.md b/.changes/tauri-utils-tray-icon-id copy.md new file mode 100644 index 000000000..6f1eea9be --- /dev/null +++ b/.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. diff --git a/core/tauri-config-schema/schema.json b/core/tauri-config-schema/schema.json index 6d72cf57a..715a118c7 100644 --- a/core/tauri-config-schema/schema.json +++ b/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" diff --git a/core/tauri-utils/src/config.rs b/core/tauri-utils/src/config.rs index ba0f31008..58dc4118a 100644 --- a/core/tauri-utils/src/config.rs +++ b/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, /// 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, diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index 3cc77496f..eaa3b6360 100644 --- a/core/tauri/src/app.rs +++ b/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> { @@ -1615,9 +1615,10 @@ impl Builder { { 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()); } diff --git a/tooling/cli/schema.json b/tooling/cli/schema.json index 6d72cf57a..715a118c7 100644 --- a/tooling/cli/schema.json +++ b/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"