Add basic systray support

This commit is contained in:
Travis Burtrum 2022-10-01 00:08:42 -04:00
parent faf0463435
commit a1967c66e9
4 changed files with 93 additions and 1 deletions

36
src-tauri/Cargo.lock generated
View File

@ -1464,6 +1464,30 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 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]] [[package]]
name = "libc" name = "libc"
version = "0.2.134" version = "0.2.134"
@ -1479,6 +1503,16 @@ dependencies = [
"pkg-config", "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]] [[package]]
name = "line-wrap" name = "line-wrap"
version = "0.1.1" version = "0.1.1"
@ -3048,6 +3082,7 @@ dependencies = [
"core-foundation", "core-foundation",
"core-graphics", "core-graphics",
"crossbeam-channel", "crossbeam-channel",
"dirs-next",
"dispatch", "dispatch",
"gdk", "gdk",
"gdk-pixbuf", "gdk-pixbuf",
@ -3061,6 +3096,7 @@ dependencies = [
"instant", "instant",
"jni", "jni",
"lazy_static", "lazy_static",
"libappindicator",
"libc", "libc",
"log", "log",
"ndk", "ndk",

View File

@ -17,7 +17,7 @@ tauri-build = { version = "1.1.1", features = [] }
[dependencies] [dependencies]
serde_json = "1.0" serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] } 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"] } 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"] } xmpp-proxy = { git = "https://github.com/moparisthebest/xmpp-proxy", default-features = false, features = ["c2s-outgoing", "tls", "quic", "websocket", "tls-ca-roots-bundled"] }

View File

@ -4,6 +4,9 @@
)] )]
use std::sync::Arc; use std::sync::Arc;
use tauri::{
CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
};
use tokio::net::TcpListener; use tokio::net::TcpListener;
use xmpp_proxy::{ use xmpp_proxy::{
common::{certs_key::CertsKey, outgoing::OutgoingConfig}, common::{certs_key::CertsKey, outgoing::OutgoingConfig},
@ -28,7 +31,56 @@ async fn start_proxy() -> u16 {
} }
fn main() { 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() 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]) .invoke_handler(tauri::generate_handler![start_proxy])
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");

View File

@ -14,6 +14,10 @@
"allowlist": { "allowlist": {
"all": true "all": true
}, },
"systemTray": {
"iconPath": "icons/icon.png",
"iconAsTemplate": true
},
"bundle": { "bundle": {
"active": true, "active": true,
"category": "DeveloperTool", "category": "DeveloperTool",