Skip to content

Commit b597aa5

Browse files
feat: add id option for tray icon in config file (#7871)
Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.studio>
1 parent 68e7319 commit b597aa5

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

.changes/tauri-tray-icon-id.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri': 'patch:enhance'
3+
---
4+
5+
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")`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri-utils': 'patch:enhance'
3+
---
4+
5+
Add an option to specify `id` for the tray icon in the tauri configuration file.

core/tauri-config-schema/schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,13 @@
20722072
"iconPath"
20732073
],
20742074
"properties": {
2075+
"id": {
2076+
"description": "Set an id for this tray icon so you can reference it later, defaults to `main`.",
2077+
"type": [
2078+
"string",
2079+
"null"
2080+
]
2081+
},
20752082
"iconPath": {
20762083
"description": "Path to the default icon to use for the tray icon.",
20772084
"type": "string"

core/tauri-utils/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,8 @@ pub struct UpdaterWindowsConfig {
15581558
#[cfg_attr(feature = "schema", derive(JsonSchema))]
15591559
#[serde(rename_all = "camelCase", deny_unknown_fields)]
15601560
pub struct TrayIconConfig {
1561+
/// Set an id for this tray icon so you can reference it later, defaults to `main`.
1562+
pub id: Option<String>,
15611563
/// Path to the default icon to use for the tray icon.
15621564
#[serde(alias = "icon-path")]
15631565
pub icon_path: PathBuf,
@@ -2570,6 +2572,7 @@ mod build {
25702572

25712573
impl ToTokens for TrayIconConfig {
25722574
fn to_tokens(&self, tokens: &mut TokenStream) {
2575+
let id = opt_str_lit(self.id.as_ref());
25732576
let icon_as_template = self.icon_as_template;
25742577
let menu_on_left_click = self.menu_on_left_click;
25752578
let icon_path = path_buf_lit(&self.icon_path);
@@ -2578,6 +2581,7 @@ mod build {
25782581
literal_struct!(
25792582
tokens,
25802583
TrayIconConfig,
2584+
id,
25812585
icon_path,
25822586
icon_as_template,
25832587
menu_on_left_click,

core/tauri/src/app.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,8 @@ macro_rules! shared_app_impl {
531531
.push(Box::new(handler));
532532
}
533533

534-
/// Gets the first tray icon registerd, usually the one configured in
535-
/// tauri config file.
534+
/// Gets the first tray icon registered,
535+
/// usually the one configured in the Tauri configuration file.
536536
#[cfg(all(desktop, feature = "tray-icon"))]
537537
#[cfg_attr(doc_cfg, doc(cfg(all(desktop, feature = "tray-icon"))))]
538538
pub fn tray(&self) -> Option<TrayIcon<R>> {
@@ -1615,9 +1615,10 @@ impl<R: Runtime> Builder<R> {
16151615
{
16161616
let config = app.config();
16171617
if let Some(tray_config) = &config.tauri.tray_icon {
1618-
let mut tray = TrayIconBuilder::new()
1619-
.icon_as_template(tray_config.icon_as_template)
1620-
.menu_on_left_click(tray_config.menu_on_left_click);
1618+
let mut tray =
1619+
TrayIconBuilder::with_id(tray_config.id.clone().unwrap_or_else(|| "main".into()))
1620+
.icon_as_template(tray_config.icon_as_template)
1621+
.menu_on_left_click(tray_config.menu_on_left_click);
16211622
if let Some(icon) = &app.manager.inner.tray_icon {
16221623
tray = tray.icon(icon.clone());
16231624
}

tooling/cli/schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,13 @@
20722072
"iconPath"
20732073
],
20742074
"properties": {
2075+
"id": {
2076+
"description": "Set an id for this tray icon so you can reference it later, defaults to `main`.",
2077+
"type": [
2078+
"string",
2079+
"null"
2080+
]
2081+
},
20752082
"iconPath": {
20762083
"description": "Path to the default icon to use for the tray icon.",
20772084
"type": "string"

0 commit comments

Comments
 (0)