[desktop] Improve tauri traymenu

This commit is contained in:
Vladimir Stoilov 2024-05-22 12:21:55 +03:00
parent a984032621
commit 0b52c5347a
No known key found for this signature in database
GPG key ID: 2F190B67A43A81AF
4 changed files with 297 additions and 56 deletions
desktop/tauri/src-tauri

View file

@ -225,6 +225,7 @@ dependencies = [
"lazy_static",
"log",
"notify-rust",
"reqwest 0.12.4",
"rust-ini",
"serde",
"serde_json",
@ -1257,7 +1258,7 @@ dependencies = [
"cocoa-foundation",
"core-foundation",
"core-graphics",
"foreign-types",
"foreign-types 0.5.0",
"libc",
"objc",
]
@ -1399,7 +1400,7 @@ dependencies = [
"bitflags 1.3.2",
"core-foundation",
"core-graphics-types",
"foreign-types",
"foreign-types 0.5.0",
"libc",
]
@ -2354,6 +2355,15 @@ dependencies = [
"ttf-parser",
]
[[package]]
name = "foreign-types"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
dependencies = [
"foreign-types-shared 0.1.1",
]
[[package]]
name = "foreign-types"
version = "0.5.0"
@ -2361,7 +2371,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965"
dependencies = [
"foreign-types-macros",
"foreign-types-shared",
"foreign-types-shared 0.3.1",
]
[[package]]
@ -2375,6 +2385,12 @@ dependencies = [
"syn 2.0.65",
]
[[package]]
name = "foreign-types-shared"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "foreign-types-shared"
version = "0.3.1"
@ -2935,6 +2951,25 @@ dependencies = [
"tracing",
]
[[package]]
name = "h2"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa82e28a107a8cc405f0839610bdc9b15f1e25ec7d696aa5cf173edbcb1486ab"
dependencies = [
"atomic-waker",
"bytes",
"fnv",
"futures-core",
"futures-sink",
"http 1.1.0",
"indexmap 2.2.6",
"slab",
"tokio",
"tokio-util",
"tracing",
]
[[package]]
name = "half"
version = "2.4.1"
@ -3150,7 +3185,7 @@ dependencies = [
"futures-channel",
"futures-core",
"futures-util",
"h2",
"h2 0.3.26",
"http 0.2.12",
"http-body 0.4.6",
"httparse",
@ -3173,6 +3208,7 @@ dependencies = [
"bytes",
"futures-channel",
"futures-util",
"h2 0.4.5",
"http 1.1.0",
"http-body 1.0.0",
"httparse",
@ -3184,6 +3220,22 @@ dependencies = [
"want",
]
[[package]]
name = "hyper-tls"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0"
dependencies = [
"bytes",
"http-body-util",
"hyper 1.3.1",
"hyper-util",
"native-tls",
"tokio",
"tokio-native-tls",
"tower-service",
]
[[package]]
name = "hyper-util"
version = "0.1.3"
@ -4139,6 +4191,24 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "native-tls"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e"
dependencies = [
"lazy_static",
"libc",
"log",
"openssl",
"openssl-probe",
"openssl-sys",
"schannel",
"security-framework",
"security-framework-sys",
"tempfile",
]
[[package]]
name = "ndk"
version = "0.7.0"
@ -4603,6 +4673,50 @@ dependencies = [
"pathdiff",
]
[[package]]
name = "openssl"
version = "0.10.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f"
dependencies = [
"bitflags 2.5.0",
"cfg-if",
"foreign-types 0.3.2",
"libc",
"once_cell",
"openssl-macros",
"openssl-sys",
]
[[package]]
name = "openssl-macros"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.65",
]
[[package]]
name = "openssl-probe"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-sys"
version = "0.9.102"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c597637d56fbc83893a35eb0dd04b2b8e7a50c91e64e9493e398b5df4fb45fa2"
dependencies = [
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "ordered-float"
version = "2.10.1"
@ -5531,7 +5645,7 @@ dependencies = [
"encoding_rs",
"futures-core",
"futures-util",
"h2",
"h2 0.3.26",
"http 0.2.12",
"http-body 0.4.6",
"hyper 0.14.28",
@ -5564,25 +5678,32 @@ checksum = "566cafdd92868e0939d3fb961bd0dc25fcfaaed179291093b3d43e6b3150ea10"
dependencies = [
"base64 0.22.1",
"bytes",
"encoding_rs",
"futures-core",
"futures-util",
"h2 0.4.5",
"http 1.1.0",
"http-body 1.0.0",
"http-body-util",
"hyper 1.3.1",
"hyper-tls",
"hyper-util",
"ipnet",
"js-sys",
"log",
"mime",
"native-tls",
"once_cell",
"percent-encoding",
"pin-project-lite",
"rustls-pemfile",
"serde",
"serde_json",
"serde_urlencoded",
"sync_wrapper 0.1.2",
"system-configuration",
"tokio",
"tokio-native-tls",
"tokio-util",
"tower-service",
"url",
@ -5889,6 +6010,16 @@ dependencies = [
"zeroize",
]
[[package]]
name = "rustls-pemfile"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29993a25686778eb88d4189742cd713c9bce943bc54251a33509dc63cbacf73d"
dependencies = [
"base64 0.22.1",
"rustls-pki-types",
]
[[package]]
name = "rustls-pki-types"
version = "1.7.0"
@ -5952,6 +6083,15 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "schannel"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "schemars"
version = "0.8.20"
@ -6021,6 +6161,29 @@ dependencies = [
"zeroize",
]
[[package]]
name = "security-framework"
version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c627723fd09706bacdb5cf41499e95098555af3c3c29d014dc3c458ef6be11c0"
dependencies = [
"bitflags 2.5.0",
"core-foundation",
"core-foundation-sys",
"libc",
"security-framework-sys",
]
[[package]]
name = "security-framework-sys"
version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "317936bbbd05227752583946b9e66d7ce3b489f84e11a94a510b4437fef407d7"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "selectors"
version = "0.22.0"
@ -6407,7 +6570,7 @@ dependencies = [
"bytemuck",
"cfg_aliases 0.2.1",
"core-graphics",
"foreign-types",
"foreign-types 0.5.0",
"js-sys",
"log",
"objc2",
@ -7633,6 +7796,16 @@ dependencies = [
"syn 2.0.65",
]
[[package]]
name = "tokio-native-tls"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2"
dependencies = [
"native-tls",
"tokio",
]
[[package]]
name = "tokio-rustls"
version = "0.25.0"
@ -8238,6 +8411,12 @@ dependencies = [
"sval_serde",
]
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "vec_map"
version = "0.8.2"

View file

@ -47,6 +47,7 @@ http = "1.0.0"
url = "2.5.0"
thiserror = "1.0"
log = "0.4.21"
reqwest = { version = "0.12" }
# Linux only
[target.'cfg(target_os = "linux")'.dependencies]

View file

@ -36,6 +36,8 @@ use serde;
use std::sync::Mutex;
use tauri::{AppHandle, Manager, Runtime};
const PORTMASTER_BASE_URL: &'static str = "http://127.0.0.1:817/api/v1/";
pub trait Handler {
fn on_connect(&mut self, cli: PortAPI) -> ();
fn on_disconnect(&mut self);
@ -203,6 +205,25 @@ impl<R: Runtime> PortmasterInterface<R> {
}
}
/// Send Shutdown request to portmaster
pub fn trigger_shutdown(&self) {
tauri::async_runtime::spawn(async move {
let client = reqwest::Client::new();
match client
.post(format!("{}core/shutdown", PORTMASTER_BASE_URL))
.send()
.await
{
Ok(v) => {
debug!("shutdown request sent {:?}", v);
}
Err(err) => {
error!("failed to send shutdown request {}", err);
}
}
});
}
//// Internal functions
fn start_notification_handler(&self) {
if let Some(api) = self.get_api() {

View file

@ -1,13 +1,11 @@
use std::collections::HashMap;
use std::sync::atomic::AtomicBool;
use std::sync::Mutex;
use std::{collections::HashMap, sync::atomic::Ordering};
use log::{debug, error};
use tauri::{
image::Image,
menu::{
CheckMenuItem, CheckMenuItemBuilder, MenuBuilder, MenuItemBuilder, PredefinedMenuItem,
SubmenuBuilder,
},
menu::{MenuBuilder, MenuItem, MenuItemBuilder, PredefinedMenuItem, SubmenuBuilder},
tray::{ClickType, TrayIcon, TrayIconBuilder},
Wry,
};
@ -30,9 +28,12 @@ use tauri_plugin_dialog::DialogExt;
pub type AppIcon = TrayIcon<Wry>;
static SPN_STATE: AtomicBool = AtomicBool::new(false);
lazy_static! {
// Set once setup_tray_menu executed.
static ref SPN_BUTTON: Mutex<Option<CheckMenuItem<Wry>>> = Mutex::new(None);
static ref SPN_STATUS: Mutex<Option<MenuItem<Wry>>> = Mutex::new(None);
static ref SPN_BUTTON: Mutex<Option<MenuItem<Wry>>> = Mutex::new(None);
static ref GLOBAL_STATUS: Mutex<Option<MenuItem<Wry>>> = Mutex::new(None);
}
const PM_TRAY_ICON_ID: &'static str = "pm_icon";
@ -50,16 +51,36 @@ pub fn setup_tray_menu(
app: &mut tauri::App,
) -> core::result::Result<AppIcon, Box<dyn std::error::Error>> {
// Tray menu
let close_btn = MenuItemBuilder::with_id("close", "Exit").build(app)?;
let open_btn = MenuItemBuilder::with_id("open", "Open").build(app)?;
let open_btn = MenuItemBuilder::with_id("open", "Open App").build(app)?;
let exit_ui_btn = MenuItemBuilder::with_id("exit_ui", "Exit UI").build(app)?;
let shutdown_btn = MenuItemBuilder::with_id("shutdown", "Shut Down Portmaster").build(app)?;
let spn = CheckMenuItemBuilder::with_id("spn", "Use SPN")
let global_status = MenuItemBuilder::with_id("global_status", "Status: Secured")
.enabled(false)
.build(app)
.unwrap();
{
let mut button_ref = GLOBAL_STATUS.lock()?;
*button_ref = Some(global_status.clone());
}
// Store the SPN button reference
let mut button_ref = SPN_BUTTON.lock()?;
*button_ref = Some(spn.clone());
// Setup SPN status
let spn_status = MenuItemBuilder::with_id("spn_status", "SPN: Disabled")
.enabled(false)
.build(app)
.unwrap();
{
let mut button_ref = SPN_STATUS.lock()?;
*button_ref = Some(spn_status.clone());
}
// Setup SPN button
let spn = MenuItemBuilder::with_id("spn_toggle", "Enable SPN")
.build(app)
.unwrap();
{
let mut button_ref = SPN_BUTTON.lock()?;
*button_ref = Some(spn.clone());
}
let force_show_window = MenuItemBuilder::with_id("force-show", "Force Show UI").build(app)?;
let reload_btn = MenuItemBuilder::with_id("reload", "Reload User Interface").build(app)?;
@ -67,15 +88,17 @@ pub fn setup_tray_menu(
.items(&[&reload_btn, &force_show_window])
.build()?;
// Drop the reference now so we unlock immediately.
drop(button_ref);
let menu = MenuBuilder::new(app)
.items(&[
&open_btn,
&PredefinedMenuItem::separator(app)?,
&global_status,
&PredefinedMenuItem::separator(app)?,
&spn_status,
&spn,
&PredefinedMenuItem::separator(app)?,
&open_btn,
&close_btn,
&exit_ui_btn,
&shutdown_btn,
&developer_menu,
])
.build()?;
@ -84,7 +107,7 @@ pub fn setup_tray_menu(
.icon(Image::from_bytes(RED_ICON).unwrap())
.menu(&menu)
.on_menu_event(move |app, event| match event.id().as_ref() {
"close" => {
"exit_ui" => {
let handle = app.clone();
app.dialog()
.message("This does not stop the Portmaster system service")
@ -119,15 +142,16 @@ pub fn setup_tray_menu(
}
};
}
"spn" => {
let btn = SPN_BUTTON.lock().unwrap();
if let Some(bt) = &*btn {
if let Ok(is_checked) = bt.is_checked() {
app.portmaster().set_spn_enabled(is_checked);
}
"spn_toggle" => {
if SPN_STATE.load(Ordering::Acquire) {
app.portmaster().set_spn_enabled(false);
} else {
app.portmaster().set_spn_enabled(true);
}
}
"shutdown" => {
app.portmaster().trigger_shutdown();
}
other => {
error!("unknown menu event id: {}", other);
}
@ -147,19 +171,27 @@ pub fn update_icon(icon: AppIcon, subsystems: HashMap<String, Subsystem>, spn_st
let failure = subsystems
.values()
.into_iter()
.map(|s| s.failure_status)
.fold(
subsystem::FAILURE_NONE,
|acc, s| {
if s > acc {
s
} else {
acc
.map(|s| &s.module_status)
.fold((subsystem::FAILURE_NONE, "".to_string()), |mut acc, s| {
for m in s {
if m.failure_status > acc.0 {
acc = (m.failure_status, m.failure_msg.clone())
}
},
);
}
acc
});
let next_icon = match failure {
if failure.0 == subsystem::FAILURE_NONE {
if let Some(global_status) = &mut *(GLOBAL_STATUS.lock().unwrap()) {
_ = global_status.set_text("Status: Secured");
}
} else {
if let Some(global_status) = &mut *(GLOBAL_STATUS.lock().unwrap()) {
_ = global_status.set_text(format!("Status: {}", failure.1));
}
}
let next_icon = match failure.0 {
subsystem::FAILURE_WARNING => YELLOW_ICON,
subsystem::FAILURE_ERROR => RED_ICON,
_ => match spn_status.as_str() {
@ -314,15 +346,7 @@ pub async fn tray_handler(cli: PortAPI, app: tauri::AppHandle) {
if let Some((_, payload)) = res {
match payload.parse::<BooleanValue>() {
Ok(value) => {
let mut btn = SPN_BUTTON.lock().unwrap();
if let Some(btn) = &mut *btn {
if let Some(value) = value.value {
_ = btn.set_checked(value);
} else {
_ = btn.set_checked(false);
}
}
update_spn_ui_state(value.value.unwrap_or(false));
},
Err(err) => match err {
ParseError::JSON(err) => {
@ -338,9 +362,25 @@ pub async fn tray_handler(cli: PortAPI, app: tauri::AppHandle) {
}
}
if let Some(btn) = &mut *(SPN_BUTTON.lock().unwrap()) {
_ = btn.set_checked(false);
}
update_spn_ui_state(false);
_ = icon.set_icon(Some(Image::from_bytes(RED_ICON).unwrap()));
}
fn update_spn_ui_state(enabled: bool) {
let mut spn_status = SPN_STATUS.lock().unwrap();
let Some(spn_status_ref) = &mut *spn_status else {
return;
};
let mut spn_btn = SPN_BUTTON.lock().unwrap();
let Some(spn_btn_ref) = &mut *spn_btn else {
return;
};
if enabled {
_ = spn_status_ref.set_text("SPN: Connected");
_ = spn_btn_ref.set_text("Disable SPN");
} else {
_ = spn_status_ref.set_text("SPN: Disabled");
_ = spn_btn_ref.set_text("Enable SPN");
}
SPN_STATE.store(enabled, Ordering::SeqCst);
}