Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -535,8 +535,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 @@ -1616,9 +1616,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
Loading