Skip to content

Commit

Permalink
feat: add id option for tray icon in config file (#7871)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Fernandes Nogueira <[email protected]>
  • Loading branch information
amrbashir and lucasfernog authored Oct 3, 2023
1 parent 68e7319 commit b597aa5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changes/tauri-tray-icon-id.md
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions .changes/tauri-utils-tray-icon-id copy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-utils': 'patch:enhance'
---

Add an option to specify `id` for the tray icon in the tauri configuration file.
7 changes: 7 additions & 0 deletions core/tauri-config-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -2578,6 +2581,7 @@ mod build {
literal_struct!(
tokens,
TrayIconConfig,
id,
icon_path,
icon_as_template,
menu_on_left_click,
Expand Down
11 changes: 6 additions & 5 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand Down Expand Up @@ -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());
}
Expand Down
7 changes: 7 additions & 0 deletions tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit b597aa5

Please sign in to comment.