Skip to content

Commit

Permalink
feat: Add tray icon with toggle and quit utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
dubisdev committed Apr 1, 2023
1 parent 1aedf76 commit 4fb2f68
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 19 deletions.
36 changes: 36 additions & 0 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 @@ -17,7 +17,7 @@ tauri-build = { version = "1.2.1", features = [] }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.2", features = ["clipboard-write-text", "fs-create-dir", "fs-read-dir", "fs-read-file", "fs-write-file", "global-shortcut-all", "notification-all", "path-all", "process-exit", "window-close", "window-create", "window-hide", "window-set-focus", "window-show", "window-start-dragging", "window-unminimize"] }
tauri = { version = "1.2", features = ["clipboard-write-text", "fs-create-dir", "fs-read-dir", "fs-read-file", "fs-write-file", "global-shortcut-all", "notification-all", "path-all", "process-exit", "system-tray", "window-close", "window-create", "window-hide", "window-set-focus", "window-show", "window-start-dragging", "window-unminimize"] }
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" }

Expand Down
72 changes: 55 additions & 17 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]

use tauri::{
AppHandle, CustomMenuItem, Manager, 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 context = tauri::generate_context!();
tauri::Builder::default()
.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();
}))
.menu(if cfg!(target_os = "macos") {
tauri::Menu::os_default(&context.package_info().name)
let tray = create_tray_menu();
let context = tauri::generate_context!();

tauri::Builder::default()
.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();
}))
.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(context)
.expect("error while running tauri application");
}

fn create_tray_menu() -> SystemTray {
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let toggle = CustomMenuItem::new("toggle".to_string(), "Toggle App");

let tray_menu = SystemTrayMenu::new()
.add_item(toggle)
.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 {
tauri::Menu::default()
})
.run(context)
.expect("error while running tauri application");
window.show().unwrap();
return;
}
}
5 changes: 4 additions & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"version": "../package.json"
},
"tauri": {
"systemTray": {
"iconPath": "icons/app-icon.png"
},
"allowlist": {
"globalShortcut": {
"all": true
Expand Down Expand Up @@ -81,8 +84,8 @@
},
"windows": [
{
"title": "RunMath",
"alwaysOnTop": true,
"label": "main",
"fullscreen": false,
"height": 100,
"resizable": false,
Expand Down

0 comments on commit 4fb2f68

Please sign in to comment.