Skip to content

Commit 5e71d19

Browse files
authored
feat(tray): new icons for non macos (#286)
1 parent ce0c6da commit 5e71d19

File tree

7 files changed

+42
-19
lines changed

7 files changed

+42
-19
lines changed

src-tauri/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tauri-build = { version = "1.5.1", features = [] }
1818
devtools = "0.3.1"
1919
serde_json = "1.0.116"
2020
serde = { version = "1.0.200", features = ["derive"] }
21-
tauri = { version = "1.6.2", features = [ "os-all", "system-tray", "notification-all", "shell-open", "updater", "window-start-dragging", "icon-png"] }
21+
tauri = { version = "1.6.2", features = [ "system-tray", "os-all", "notification-all", "shell-open", "updater", "window-start-dragging", "icon-png"] }
2222
tauri-plugin-autostart = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
2323
tauri-plugin-deep-link = "0.1.2"
2424
tauri-plugin-positioner = { version = "1.0.5", features = ["system-tray"] }

src-tauri/icons/tray-base-macos.png

1.38 KB
Loading

src-tauri/icons/tray-base.png

5.12 KB
Loading

src-tauri/icons/tray-new-macos.png

1.5 KB
Loading

src-tauri/icons/tray-new.png

4.13 KB
Loading

src-tauri/src/commands.rs

+38-16
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,57 @@ pub fn update_tray(app_handle: tauri::AppHandle, title: Option<String>, new_icon
99
}
1010
if let Some(new_icon) = new_icon {
1111
if new_icon {
12-
tray_handle
13-
.set_icon(tauri::Icon::Raw(
14-
include_bytes!("../icons/tray-new.png").to_vec(),
15-
))
16-
.unwrap();
12+
tray_handle.set_icon(get_raw_tray_icon("new")).unwrap();
1713
} else {
18-
tray_handle
19-
.set_icon(tauri::Icon::Raw(
20-
include_bytes!("../icons/tray-base.png").to_vec(),
21-
))
22-
.unwrap();
14+
tray_handle.set_icon(get_raw_tray_icon("base")).unwrap();
2315
}
2416
}
2517
}
2618

2719
#[tauri::command]
2820
pub fn toggle_tray(app_handle: tauri::AppHandle, show: bool) {
29-
let tray_handle = app_handle.tray_handle_by_id("tray").unwrap();
30-
tray_handle.destroy().unwrap();
21+
let tray_handle = app_handle.tray_handle_by_id("tray");
22+
if let Some(tray_handle) = tray_handle {
23+
tray_handle.destroy().unwrap();
24+
}
3125
if show {
32-
SystemTray::new()
26+
let mut system_tray = SystemTray::new()
3327
.with_id("tray")
28+
.with_icon(get_raw_tray_icon("base"))
3429
.with_menu(
3530
SystemTrayMenu::new()
3631
.add_item(CustomMenuItem::new("dashboard".to_string(), "Dashboard..."))
3732
.add_native_item(SystemTrayMenuItem::Separator)
3833
.add_item(CustomMenuItem::new("quit".to_string(), "Quit")),
39-
)
40-
.build(&app_handle)
41-
.unwrap();
34+
);
35+
#[cfg(target_os = "macos")]
36+
{
37+
system_tray = system_tray
38+
.with_icon_as_template(true)
39+
.with_menu_on_left_click(false)
40+
}
41+
system_tray.build(&app_handle).unwrap();
4242
}
4343
}
44+
45+
fn get_raw_tray_icon(image: &str) -> tauri::Icon {
46+
let is_macos = cfg!(target_os = "macos");
47+
let bytes = match image {
48+
"base" => {
49+
if is_macos {
50+
include_bytes!("../icons/tray-base-macos.png").to_vec()
51+
} else {
52+
include_bytes!("../icons/tray-base.png").to_vec()
53+
}
54+
}
55+
"new" => {
56+
if is_macos {
57+
include_bytes!("../icons/tray-new-macos.png").to_vec()
58+
} else {
59+
include_bytes!("../icons/tray-new.png").to_vec()
60+
}
61+
}
62+
_ => panic!("Unknown tray icon"),
63+
};
64+
tauri::Icon::Raw(bytes)
65+
}

src-tauri/src/main.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#[macro_use]
88
extern crate objc;
99

10-
use tauri::{Manager, SystemTray, SystemTrayEvent};
10+
use tauri::{Manager, SystemTrayEvent};
1111
use tauri_plugin_autostart::MacosLauncher;
1212
use tauri_plugin_positioner::{Position, WindowExt};
1313

@@ -46,6 +46,8 @@ fn main() {
4646
#[cfg(not(target_os = "macos"))]
4747
tray_window.set_decorations(false).unwrap();
4848

49+
commands::toggle_tray(app.handle(), true);
50+
4951
Ok(())
5052
})
5153
.plugin(tauri_plugin_autostart::init(
@@ -54,7 +56,6 @@ fn main() {
5456
))
5557
.plugin(tauri_plugin_positioner::init())
5658
.plugin(tauri_plugin_window_state::Builder::default().build())
57-
.system_tray(SystemTray::new().with_id("tray"))
5859
.on_system_tray_event(|app, event| {
5960
tauri_plugin_positioner::on_tray_event(app, &event);
6061
match event {

0 commit comments

Comments
 (0)