Skip to content

Commit 167a48b

Browse files
Rust format + PR feedback
1 parent 63e5105 commit 167a48b

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

crates/tauri/src/tray/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,9 @@ pub fn set_icon_with_template(
596596
None => None,
597597
};
598598
run_item_main_thread!(self, |self_: Self| {
599-
self_.inner.set_icon_with_as_template(tray_icon, is_template)
599+
self_
600+
.inner
601+
.set_icon_with_as_template(tray_icon, is_template)
600602
})??;
601603
}
602604
#[cfg(not(target_os = "macos"))]

crates/tauri/src/tray/plugin.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,24 @@ fn set_icon_as_template<R: Runtime>(
203203
tray.set_icon_as_template(as_template)
204204
}
205205

206+
#[command(root = "crate")]
207+
fn set_icon_with_template<R: Runtime>(
208+
app: AppHandle<R>,
209+
webview: Webview<R>,
210+
rid: ResourceId,
211+
icon: Option<JsImage>,
212+
as_template: bool,
213+
) -> crate::Result<()> {
214+
let resources_table = app.resources_table();
215+
let tray = resources_table.get::<TrayIcon<R>>(rid)?;
216+
let webview_resources_table = webview.resources_table();
217+
let icon = match icon {
218+
Some(i) => Some(i.into_img(&webview_resources_table)?.as_ref().clone()),
219+
None => None,
220+
};
221+
tray.set_icon_with_template(icon, as_template)
222+
}
223+
206224
#[command(root = "crate")]
207225
fn set_show_menu_on_left_click<R: Runtime>(
208226
app: AppHandle<R>,
@@ -228,6 +246,7 @@ pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
228246
set_visible,
229247
set_temp_dir_path,
230248
set_icon_as_template,
249+
set_icon_with_template,
231250
set_show_menu_on_left_click,
232251
])
233252
.build()

packages/api/src/tray.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,27 @@ export class TrayIcon extends Resource {
296296
})
297297
}
298298

299+
/**
300+
* Sets a new tray icon and template status atomically. **macOS only**.
301+
*
302+
* Note that you may need the `image-ico` or `image-png` Cargo features to use this API.
303+
* To enable it, change your Cargo.toml file:
304+
* ```toml
305+
* [dependencies]
306+
* tauri = { version = "...", features = ["...", "image-png"] }
307+
* ```
308+
*/
309+
async setIconWithTemplate(
310+
icon: string | Image | Uint8Array | ArrayBuffer | number[] | null,
311+
asTemplate: boolean
312+
): Promise<void> {
313+
let trayIcon = null
314+
if (icon) {
315+
trayIcon = transformImage(icon)
316+
}
317+
return invoke('plugin:tray|set_icon_with_template', { rid: this.rid, icon: trayIcon, asTemplate })
318+
}
319+
299320
/**
300321
* Disable or enable showing the tray menu on left click.
301322
*

0 commit comments

Comments
 (0)