Skip to content

Commit

Permalink
feat: add tray icon
Browse files Browse the repository at this point in the history
  • Loading branch information
dubisdev committed Mar 30, 2023
1 parent e41c16a commit bb51b4d
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 25 deletions.
74 changes: 55 additions & 19 deletions src-tauri/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 src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2021"
tauri-build = { version = "1.2", features = [] }

[dependencies]
tauri = { version = "1.2", features = ["fs-create-dir", "fs-read-dir", "fs-read-file", "fs-write-file", "global-shortcut-all", "path-all", "shell-open", "window-hide", "window-set-focus", "window-set-size", "window-show", "window-start-dragging"] }
tauri = { version = "1.2", features = ["fs-create-dir", "fs-read-dir", "fs-read-file", "fs-write-file", "global-shortcut-all", "path-all", "shell-open", "system-tray", "window-hide", "window-set-focus", "window-set-size", "window-show", "window-start-dragging"] }
tauri-plugin-autostart = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
serde = { version = "1.0", features = ["derive"] }
Expand Down
50 changes: 45 additions & 5 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,61 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri::{AppHandle, Manager};
use tauri::{CustomMenuItem, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem};
use tauri_plugin_autostart::MacosLauncher;
use tauri::Manager;

#[derive(Clone, serde::Serialize)]
struct Payload {
args: Vec<String>,
cwd: String,
args: Vec<String>,
cwd: String,
}

fn main() {
let tray = create_tray_menu();

tauri::Builder::default()
.plugin(tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, Some(vec![""])))
.plugin(tauri_plugin_autostart::init(
MacosLauncher::LaunchAgent,
Some(vec![""]),
))
.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
app.emit_all("single-instance", Payload { args: argv, cwd }).unwrap();
app.emit_all("single-instance", Payload { args: argv, cwd })
.unwrap();
}))
.system_tray(tray)
.on_system_tray_event(|app, event| match event {
SystemTrayEvent::LeftClick { .. } => toggle_window(app),

SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
"quit" => std::process::exit(0),
"toggle" => toggle_window(app),
_ => {}
},
_ => {}
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

fn create_tray_menu() -> SystemTray {
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let hide = CustomMenuItem::new("toggle".to_string(), "Toggle App");
let tray_menu = SystemTrayMenu::new()
.add_item(hide)
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(quit);

SystemTray::new().with_menu(tray_menu)
}

fn toggle_window(app: &AppHandle) {
let window = app.get_window("main").unwrap();
if window.is_visible().unwrap() {
window.hide().unwrap();
return;
} else {
window.show().unwrap();
return;
}
}
3 changes: 3 additions & 0 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
"updater": {
"active": false
},
"systemTray": {
"iconPath": "icons/icon.ico"
},
"windows": [
{
"resizable": false,
Expand Down

0 comments on commit bb51b4d

Please sign in to comment.