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

Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.studio>
This commit is contained in:
Amr Bashir 2023-10-03 16:18:19 +03:00 committed by GitHub
parent 68e7319305
commit b597aa5f39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 5 deletions

View File

@ -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")`.

View File

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

View File

@ -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"

View File

@ -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,

View File

@ -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());
}

View File

@ -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"