From 2557afd37f6dfada04e0551b148ce603230b2102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Ciarcin=CC=81ski?= Date: Thu, 28 Aug 2025 17:44:48 +0200 Subject: [PATCH] Tray: omit submenus for one instance --- package.json | 2 +- pnpm-lock.yaml | 10 +++---- src-tauri/Cargo.lock | 55 +++++++++++++++------------------------ src-tauri/src/commands.rs | 11 ++++++++ src-tauri/src/tray.rs | 50 +++++++++++++++++++++-------------- 5 files changed, 69 insertions(+), 59 deletions(-) diff --git a/package.json b/package.json index 09d80de6..2ad10286 100644 --- a/package.json +++ b/package.json @@ -115,7 +115,7 @@ "@types/lodash-es": "^4.17.12", "@types/node": "^24.3.0", "@types/react": "^19.1.12", - "@types/react-dom": "^19.1.8", + "@types/react-dom": "^19.1.9", "@vitejs/plugin-react": "^5.0.2", "@vitejs/plugin-react-swc": "^4.0.1", "autoprefixer": "^10.4.21", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d295a28f..de0972a2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -208,8 +208,8 @@ importers: specifier: ^19.1.12 version: 19.1.12 '@types/react-dom': - specifier: ^19.1.8 - version: 19.1.8(@types/react@19.1.12) + specifier: ^19.1.9 + version: 19.1.9(@types/react@19.1.12) '@vitejs/plugin-react': specifier: ^5.0.2 version: 5.0.2(vite@7.1.3(@types/node@24.3.0)(sass@1.70.0)(yaml@2.8.1)) @@ -1195,8 +1195,8 @@ packages: '@types/parse-json@4.0.2': resolution: {integrity: sha512-dISoDXWWQwUquiKsyZ4Ng+HX2KsPL7LyHKHQwgGFEA3IaKac4Obd+h2a/a6waisAoepJlBcx9paWqjA8/HVjCw==} - '@types/react-dom@19.1.8': - resolution: {integrity: sha512-xG7xaBMJCpcK0RpN8jDbAACQo54ycO6h4dSSmgv8+fu6ZIAdANkx/WsawASUjVXYfy+J9AbUpRMNNEsXCDfDBQ==} + '@types/react-dom@19.1.9': + resolution: {integrity: sha512-qXRuZaOsAdXKFyOhRBg6Lqqc0yay13vN7KrIg4L7N4aaHN68ma9OK3NE1BoDFgFOTfM7zg+3/8+2n8rLUH3OKQ==} peerDependencies: '@types/react': ^19.0.0 @@ -3773,7 +3773,7 @@ snapshots: '@types/parse-json@4.0.2': {} - '@types/react-dom@19.1.8(@types/react@19.1.12)': + '@types/react-dom@19.1.9(@types/react@19.1.12)': dependencies: '@types/react': 19.1.12 diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index ae66fa66..5af5337e 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2129,7 +2129,7 @@ dependencies = [ "js-sys", "libc", "r-efi", - "wasi 0.14.2+wasi-0.2.4", + "wasi 0.14.3+wasi-0.2.4", "wasm-bindgen", ] @@ -2562,7 +2562,7 @@ dependencies = [ "libc", "percent-encoding", "pin-project-lite", - "socket2 0.6.0", + "socket2", "system-configuration", "tokio", "tower-service", @@ -4476,9 +4476,9 @@ dependencies = [ [[package]] name = "quinn" -version = "0.11.8" +version = "0.11.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "626214629cda6781b6dc1d316ba307189c85ba657213ce642d9c77670f8202c8" +checksum = "b9e20a958963c291dc322d98411f541009df2ced7b5a4f2bd52337638cfccf20" dependencies = [ "bytes", "cfg_aliases", @@ -4487,7 +4487,7 @@ dependencies = [ "quinn-udp", "rustc-hash", "rustls", - "socket2 0.5.10", + "socket2", "thiserror 2.0.16", "tokio", "tracing", @@ -4496,9 +4496,9 @@ dependencies = [ [[package]] name = "quinn-proto" -version = "0.11.12" +version = "0.11.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49df843a9161c85bb8aae55f101bc0bac8bcafd637a620d9122fd7e0b2f7422e" +checksum = "f1906b49b0c3bc04b5fe5d86a77925ae6524a19b816ae38ce1e426255f1d8a31" dependencies = [ "bytes", "getrandom 0.3.3", @@ -4517,16 +4517,16 @@ dependencies = [ [[package]] name = "quinn-udp" -version = "0.5.13" +version = "0.5.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcebb1209ee276352ef14ff8732e24cc2b02bbac986cd74a4c81bcb2f9881970" +checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd" dependencies = [ "cfg_aliases", "libc", "once_cell", - "socket2 0.5.10", + "socket2", "tracing", - "windows-sys 0.59.0", + "windows-sys 0.60.2", ] [[package]] @@ -5445,16 +5445,6 @@ dependencies = [ "serde", ] -[[package]] -name = "socket2" -version = "0.5.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "socket2" version = "0.6.0" @@ -6584,7 +6574,7 @@ dependencies = [ "pin-project-lite", "signal-hook-registry", "slab", - "socket2 0.6.0", + "socket2", "tokio-macros", "tracing", "windows-sys 0.59.0", @@ -6761,7 +6751,7 @@ dependencies = [ "percent-encoding", "pin-project", "rustls-native-certs", - "socket2 0.6.0", + "socket2", "sync_wrapper", "tokio", "tokio-rustls", @@ -7284,11 +7274,11 @@ checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" [[package]] name = "wasi" -version = "0.14.2+wasi-0.2.4" +version = "0.14.3+wasi-0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" +checksum = "6a51ae83037bdd272a9e28ce236db8c07016dd0d50c27038b3f407533c030c95" dependencies = [ - "wit-bindgen-rt", + "wit-bindgen", ] [[package]] @@ -8116,13 +8106,10 @@ dependencies = [ ] [[package]] -name = "wit-bindgen-rt" -version = "0.39.0" +name = "wit-bindgen" +version = "0.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" -dependencies = [ - "bitflags 2.9.3", -] +checksum = "052283831dbae3d879dc7f51f3d92703a316ca49f91540417d38591826127814" [[package]] name = "wl-clipboard-rs" @@ -8151,9 +8138,9 @@ checksum = "ea2f10b9bb0928dfb1b42b65e1f9e36f7f54dbdf08457afefb38afcdec4fa2bb" [[package]] name = "wry" -version = "0.53.2" +version = "0.53.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3b6763512fe4b51c80b3ce9b50939d682acb4de335dfabbdb20d7a2642199b7" +checksum = "31f0e9642a0d061f6236c54ccae64c2722a7879ad4ec7dff59bd376d446d8e90" dependencies = [ "base64 0.22.1", "block2 0.6.1", diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 19a49cc2..1c90939b 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -351,6 +351,17 @@ pub struct LocationInfo { pub location_mfa_mode: LocationMfaMode, } +impl LocationInfo { + /// Label used in system tray menu. + pub(crate) fn menu_label(&self) -> String { + format!( + "{}: {}", + if self.active { "Disconnect" } else { "Connect" }, + self.name + ) + } +} + impl fmt::Display for LocationInfo { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.name) diff --git a/src-tauri/src/tray.rs b/src-tauri/src/tray.rs index 2d174339..a2882ea6 100644 --- a/src-tauri/src/tray.rs +++ b/src-tauri/src/tray.rs @@ -59,37 +59,49 @@ async fn generate_tray_menu(app: &AppHandle) -> Result, Error Ok(instances) => { let instance_count = instances.len(); debug!("Got {instance_count} instances to display in the tray menu"); - for instance in instances { - let mut instance_menu = SubmenuBuilder::new(app, &instance.name); - let all_locations = all_locations(instance.id).await.unwrap(); + + // One instance omits sub-menu. + if instance_count == 1 { + let instance = &instances[0]; + let all_locations = all_locations(instance.id).await?; debug!( - "Found {} locations for the {} instance to display in the tray menu", + "Found {} locations for the {instance} instance to display in the tray menu", all_locations.len(), - instance ); - // TODO: Use icons instead of Connect/Disconnect when Defguard utilizes tauri v2. for location in all_locations { - let item_name = format!( - "{}: {}", - if location.active { - "Disconnect" - } else { - "Connect" - }, - location.name - ); let menu_item = MenuItem::with_id( app, location.id.to_string(), - item_name, + location.menu_label(), true, None::<&str>, )?; - instance_menu = instance_menu.item(&menu_item); + menu = menu.item(&menu_item); + } + } else { + for instance in instances { + let mut instance_menu = SubmenuBuilder::new(app, &instance.name); + let all_locations = all_locations(instance.id).await?; + debug!( + "Found {} locations for the {instance} instance to display in the tray menu", + all_locations.len(), + ); + + // TODO: Use icons instead of Connect/Disconnect when Defguard utilizes tauri v2. + for location in all_locations { + let menu_item = MenuItem::with_id( + app, + location.id.to_string(), + location.menu_label(), + true, + None::<&str>, + )?; + instance_menu = instance_menu.item(&menu_item); + } + let submenu = instance_menu.build()?; + menu = menu.item(&submenu); } - let submenu = instance_menu.build()?; - menu = menu.item(&submenu); } } Err(err) => {