From a1967c66e933d31d697ebec6f15c2f8e13fcd3d2 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Sat, 1 Oct 2022 00:08:42 -0400 Subject: [PATCH] Add basic systray support --- src-tauri/Cargo.lock | 36 +++++++++++++++++++++++++++ src-tauri/Cargo.toml | 2 +- src-tauri/src/main.rs | 52 +++++++++++++++++++++++++++++++++++++++ src-tauri/tauri.conf.json | 4 +++ 4 files changed, 93 insertions(+), 1 deletion(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 6fd2640..cafce00 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1464,6 +1464,30 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "libappindicator" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db2d3cb96d092b4824cb306c9e544c856a4cb6210c1081945187f7f1924b47e8" +dependencies = [ + "glib", + "gtk", + "gtk-sys", + "libappindicator-sys", + "log", +] + +[[package]] +name = "libappindicator-sys" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1b3b6681973cea8cc3bce7391e6d7d5502720b80a581c9a95c9cbaf592826aa" +dependencies = [ + "gtk-sys", + "libloading", + "once_cell", +] + [[package]] name = "libc" version = "0.2.134" @@ -1479,6 +1503,16 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "libloading" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd" +dependencies = [ + "cfg-if", + "winapi", +] + [[package]] name = "line-wrap" version = "0.1.1" @@ -3048,6 +3082,7 @@ dependencies = [ "core-foundation", "core-graphics", "crossbeam-channel", + "dirs-next", "dispatch", "gdk", "gdk-pixbuf", @@ -3061,6 +3096,7 @@ dependencies = [ "instant", "jni", "lazy_static", + "libappindicator", "libc", "log", "ndk", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index c3455c4..11d85d7 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -17,7 +17,7 @@ tauri-build = { version = "1.1.1", features = [] } [dependencies] serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } -tauri = { version = "1.1.1", features = ["api-all", "devtools"] } +tauri = { version = "1.1.1", features = ["api-all", "devtools", "system-tray"] } tokio = { version = "1.9", features = ["net"] } xmpp-proxy = { git = "https://github.com/moparisthebest/xmpp-proxy", default-features = false, features = ["c2s-outgoing", "tls", "quic", "websocket", "tls-ca-roots-bundled"] } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 481c65a..06dc7cb 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -4,6 +4,9 @@ )] use std::sync::Arc; +use tauri::{ + CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, +}; use tokio::net::TcpListener; use xmpp_proxy::{ common::{certs_key::CertsKey, outgoing::OutgoingConfig}, @@ -28,7 +31,56 @@ async fn start_proxy() -> u16 { } fn main() { + let tray_menu = SystemTrayMenu::new() + .add_item(CustomMenuItem::new("show".to_string(), "Show")) + .add_native_item(SystemTrayMenuItem::Separator) + .add_item(CustomMenuItem::new("hide".to_string(), "Hide")) + .add_native_item(SystemTrayMenuItem::Separator) + .add_item(CustomMenuItem::new("quit".to_string(), "Quit")); + let system_tray = SystemTray::new().with_menu(tray_menu); tauri::Builder::default() + .system_tray(system_tray) + .on_system_tray_event(|app, event| match event { + SystemTrayEvent::LeftClick { + position: _, + size: _, + .. + } => { + println!("system tray received a left click"); + // doesn't seem to work + } + SystemTrayEvent::RightClick { + position: _, + size: _, + .. + } => { + println!("system tray received a right click"); + // doesn't seem to work + } + SystemTrayEvent::DoubleClick { + position: _, + size: _, + .. + } => { + println!("system tray received a double click"); + // doesn't seem to work + } + SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() { + "quit" => { + std::process::exit(0); + } + "hide" => { + let window = app.get_window("main").unwrap(); + window.hide().unwrap(); + } + "show" => { + let window = app.get_window("main").unwrap(); + window.show().unwrap(); + } + _ => {} + }, + _ => {} + }) .invoke_handler(tauri::generate_handler![start_proxy]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index c44b45e..4e00da8 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -14,6 +14,10 @@ "allowlist": { "all": true }, + "systemTray": { + "iconPath": "icons/icon.png", + "iconAsTemplate": true + }, "bundle": { "active": true, "category": "DeveloperTool",