Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src-tauri/src/appstate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{collections::HashMap, sync::Mutex};

use tauri::async_runtime::{spawn, JoinHandle};
use tauri::{
async_runtime::{spawn, JoinHandle},
PhysicalPosition,
};
use tokio_util::sync::CancellationToken;

use crate::{
Expand All @@ -15,6 +18,7 @@ use crate::{
pub struct AppState {
pub log_watchers: Mutex<HashMap<String, CancellationToken>>,
pub app_config: Mutex<AppConfig>,
pub tray_click_position: Mutex<Option<PhysicalPosition<f64>>>,
stat_threads: Mutex<HashMap<Id, JoinHandle<()>>>, // location ID is the key
pub provisioning_config: Mutex<Option<ProvisioningConfig>>,
}
Expand All @@ -25,6 +29,7 @@ impl AppState {
Self {
log_watchers: Mutex::new(HashMap::new()),
app_config: Mutex::new(config),
tray_click_position: Mutex::new(None),
stat_threads: Mutex::new(HashMap::new()),
provisioning_config: Mutex::new(provisioning_config),
}
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/bin/defguard-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ fn main() {
WebviewWindowBuilder::new(app, "new-ui", new_url)
.title("New UI")
.inner_size(360.0, 675.0)
.visible(false)
.build()?;

// Open old UI window.
Expand Down
3 changes: 1 addition & 2 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,8 +1334,7 @@ fn select_reported_app_version(
) -> String {
build_version_override
.filter(|version| !version.trim().is_empty())
.map(str::to_owned)
.unwrap_or_else(|| package_version.to_owned())
.map_or_else(|| package_version.to_owned(), str::to_owned)
}

fn reported_app_version(handle: &AppHandle) -> String {
Expand Down
62 changes: 51 additions & 11 deletions src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ use tauri::{
AppHandle, Emitter, Manager, Runtime,
};

use tauri::tray::{MouseButton, MouseButtonState, TrayIconEvent};

use crate::{
active_connections::{get_connection_id_by_type, ACTIVE_CONNECTIONS},
appstate::AppState,
commands::{all_instances, all_locations, connect, disconnect},
database::{models::location::Location, DB_POOL},
error::Error,
events::EventKey,
window::show_new_ui_window_near_tray,
ConnectionType,
};

const SUBSCRIBE_UPDATES_LINK: &str = "https://defguard.net/newsletter";
const JOIN_COMMUNITY_LINK: &str = "https://github.com/DefGuard/defguard/discussions/new/choose";
const FOLLOW_US_LINK: &str = "https://floss.social/@defguard";

const MAIN_WINDOW_ID: &str = "main";
const NEW_UI_WINDOW_ID: &str = "new-ui";
const OLD_UI_WINDOW_ID: &str = "old-ui";

const TRAY_ICON_ID: &str = "tray";

Expand All @@ -31,6 +35,22 @@ const TRAY_EVENT_UPDATES: &str = "updates";
const TRAY_EVENT_COMMUNITY: &str = "community";
const TRAY_EVENT_FOLLOW: &str = "follow";

fn store_tray_click_position(app: &AppHandle, event: &TrayIconEvent) {
let position = match event {
TrayIconEvent::Click {
button_state: MouseButtonState::Down,
position,
..
}
| TrayIconEvent::DoubleClick { position, .. } => Some(*position),
_ => None,
};

if let Some(position) = position {
*app.state::<AppState>().tray_click_position.lock().unwrap() = Some(position);
}
}

/// Generate contents of system tray menu.
async fn generate_tray_menu(app: &AppHandle) -> Result<Menu<impl Runtime>, Error> {
debug!("Generating tray menu.");
Expand Down Expand Up @@ -129,6 +149,16 @@ pub async fn setup_tray(app: &AppHandle) -> Result<(), Error> {
TrayIconBuilder::with_id(TRAY_ICON_ID)
.menu(&tray_menu)
.show_menu_on_left_click(true)
.on_tray_icon_event(|icon, event| {
store_tray_click_position(icon.app_handle(), &event);
if let TrayIconEvent::DoubleClick {
button: MouseButton::Left,
..
} = event
{
show_new_ui_window_near_tray(icon.app_handle());
}
})
.on_menu_event(handle_tray_menu_event)
.build(app)?;
// On other systems (especially Windows), system tray menu is on right-click,
Expand All @@ -138,8 +168,13 @@ pub async fn setup_tray(app: &AppHandle) -> Result<(), Error> {
.menu(&tray_menu)
.show_menu_on_left_click(false)
.on_tray_icon_event(|icon, event| {
if let tauri::tray::TrayIconEvent::DoubleClick { .. } = event {
show_main_window(icon.app_handle());
store_tray_click_position(icon.app_handle(), &event);
if let TrayIconEvent::DoubleClick {
button: MouseButton::Left,
..
} = event
{
show_new_ui_window_near_tray(icon.app_handle());
}
})
.on_menu_event(handle_tray_menu_event)
Expand Down Expand Up @@ -169,16 +204,21 @@ fn hide_main_window(app: &AppHandle) {
warn!("Failed to hide application: {err}");
}
#[cfg(not(target_os = "macos"))]
if let Some(main_window) = app.get_webview_window(MAIN_WINDOW_ID) {
if let Err(err) = main_window.hide() {
warn!("Failed to hide main window: {err}");
for window_id in [NEW_UI_WINDOW_ID, OLD_UI_WINDOW_ID] {
if let Some(window) = app.get_webview_window(window_id) {
if let Err(err) = window.hide() {
warn!("Failed to hide window {window_id}: {err}");
}
}
}
}

pub fn show_main_window(app: &AppHandle) {
if let Some(main_window) = app.get_webview_window(MAIN_WINDOW_ID) {
if let Err(err) = main_window.unminimize() {
if let Some(window) = app
.get_webview_window(NEW_UI_WINDOW_ID)
.or_else(|| app.get_webview_window(OLD_UI_WINDOW_ID))
{
if let Err(err) = window.unminimize() {
warn!("Failed to unminimize main window: {err}");
}
#[cfg(target_os = "macos")]
Expand All @@ -187,11 +227,11 @@ pub fn show_main_window(app: &AppHandle) {
}
#[cfg(not(target_os = "macos"))]
{
if let Err(err) = main_window.show() {
if let Err(err) = window.show() {
warn!("Failed to show main window: {err}");
}
}
let _ = main_window.set_focus();
let _ = window.set_focus();
}
}

Expand All @@ -203,7 +243,7 @@ pub fn handle_tray_menu_event(app: &AppHandle, event: MenuEvent) {
info!("Received QUIT request. Initiating shutdown...");
handle.exit(0);
}
TRAY_EVENT_SHOW => show_main_window(app),
TRAY_EVENT_SHOW => show_new_ui_window_near_tray(app),
TRAY_EVENT_HIDE => hide_main_window(app),
TRAY_EVENT_UPDATES => {
let _ = webbrowser::open(SUBSCRIBE_UPDATES_LINK);
Expand Down
Loading