Skip to content

Commit

Permalink
feat(app): use tray_icon only on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
meowtec committed Apr 27, 2023
1 parent 5824d67 commit 8bc9bfe
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
26 changes: 24 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/launcher/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tauri-build = { version = "1.2", features = [] }

[dependencies]
# "dialog-all", "shell-open"
tauri = { version = "1.2", features = ["dialog-all", "shell-open", "system-tray"] }
tauri = { version = "1.2", features = ["dialog-all", "icon-png", "shell-open", "system-tray"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.26", features = ["rt", "rt-multi-thread", "macros", "fs"] }
Expand Down
40 changes: 22 additions & 18 deletions packages/launcher/src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use tauri::{SystemTray, SystemTrayEvent};

use tauri::{CustomMenuItem, SystemTrayMenu};
use tauri::{CustomMenuItem, SystemTray, SystemTrayEvent, SystemTrayMenu};

use crate::window::reopen_window;

Expand All @@ -9,21 +7,27 @@ pub fn init_tray(app: &tauri::App) {
let tray_menu =
SystemTrayMenu::new().add_item(CustomMenuItem::new("quit".to_string(), "Quit".to_string()));

SystemTray::new()
.with_menu(tray_menu)
.on_event(move |event| match event {
#[allow(clippy::single_match)]
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
"quit" => {
handle.exit(0);
}
_ => {}
},
SystemTrayEvent::LeftClick { .. } => {
reopen_window(&handle);
#[allow(unused_mut)]
let mut tray = SystemTray::new().with_menu(tray_menu);

#[cfg(target_os = "macos")]
{
tray = tray.with_icon(Icon::Raw(include_bytes!("../icons/icon_tray.png").to_vec()));
}

tray.on_event(move |event| match event {
#[allow(clippy::single_match)]
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
"quit" => {
handle.exit(0);
}
_ => {}
})
.build(app)
.unwrap();
},
SystemTrayEvent::LeftClick { .. } => {
reopen_window(&handle);
}
_ => {}
})
.build(app)
.unwrap();
}
2 changes: 1 addition & 1 deletion packages/launcher/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
},
"systemTray": {
"iconPath": "icons/icon_tray.png",
"iconPath": "icons/icon.png",
"iconAsTemplate": true
},
"bundle": {
Expand Down

0 comments on commit 8bc9bfe

Please sign in to comment.