Skip to content

Commit 88528d9

Browse files
committed
Merge common logic
1 parent 4c5cc17 commit 88528d9

1 file changed

Lines changed: 26 additions & 30 deletions

File tree

src-tauri/src/bin/defguard-client.rs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use defguard_client::{
3434
LOG_FILENAME, VERSION,
3535
};
3636
use log::{Level, LevelFilter};
37-
use tauri::{AppHandle, Builder, Manager, RunEvent, WindowEvent};
37+
use tauri::{async_runtime, AppHandle, Builder, Manager, RunEvent, WindowEvent};
3838
use tauri_plugin_deep_link::DeepLinkExt;
3939
use tauri_plugin_log::{Target, TargetKind};
4040

@@ -100,7 +100,7 @@ async fn startup(app_handle: &AppHandle) {
100100
.lock()
101101
.expect("failed to lock app state")
102102
.mtu();
103-
let handle = tauri::async_runtime::spawn(async move {
103+
let handle = async_runtime::spawn(async move {
104104
if let Err(err) = defguard_client::apple::sync_locations_and_tunnels(mtu).await {
105105
error!("Failed to sync locations and tunnels: {err}");
106106
}
@@ -122,7 +122,7 @@ async fn startup(app_handle: &AppHandle) {
122122
});
123123

124124
let handle = app_handle.clone();
125-
tauri::async_runtime::spawn(async move {
125+
async_runtime::spawn(async move {
126126
defguard_client::apple::connection_state_update_thread(&handle).await;
127127
error!("Connection state update thread has exited unexpectedly, quitting the app.");
128128
handle.exit(0);
@@ -131,7 +131,7 @@ async fn startup(app_handle: &AppHandle) {
131131

132132
// Run periodic tasks.
133133
let periodic_tasks_handle = app_handle.clone();
134-
tauri::async_runtime::spawn(async move {
134+
async_runtime::spawn(async move {
135135
run_periodic_tasks(&periodic_tasks_handle).await;
136136
// One of the tasks exited, so something went wrong, quit the app
137137
error!("One of the periodic tasks has stopped unexpectedly. Exiting the application.");
@@ -154,6 +154,16 @@ async fn startup(app_handle: &AppHandle) {
154154
debug!("Tray menu has been re-generated successfully.");
155155
}
156156

157+
/// Open the appropriate window, either the old or the new UI, depending if there are locations.
158+
fn open_appropriate_window(app_handle: &AppHandle) {
159+
let has_locations = async_runtime::block_on(has_non_service_locations());
160+
if has_locations {
161+
let _ = WindowManager::open_tray(app_handle);
162+
} else {
163+
let _ = WindowManager::open_full_view(app_handle);
164+
}
165+
}
166+
157167
fn main() {
158168
let app = Builder::default()
159169
.invoke_handler(tauri::generate_handler![
@@ -220,14 +230,7 @@ fn main() {
220230

221231
#[cfg(not(target_os = "linux"))]
222232
{
223-
let has_locations = tauri::async_runtime::block_on(
224-
defguard_client::window_manager::has_non_service_locations(),
225-
);
226-
if has_locations {
227-
let _ = WindowManager::open_tray(app);
228-
} else {
229-
let _ = WindowManager::open_full_view(app);
230-
}
233+
open_appropriate_window(app);
231234
}
232235
}
233236
}))
@@ -370,12 +373,12 @@ fn main() {
370373
)?;
371374

372375
// run DB migrations
373-
tauri::async_runtime::block_on(handle_db_migrations());
376+
async_runtime::block_on(handle_db_migrations());
374377

375378
// Check if client needs to be initialized
376379
// and try to load provisioning config if necessary
377380
let provisioning_config =
378-
tauri::async_runtime::block_on(handle_client_initialization(app_handle));
381+
async_runtime::block_on(handle_client_initialization(app_handle));
379382

380383
let state = AppState::new(config, provisioning_config);
381384
app.manage(state);
@@ -395,7 +398,8 @@ fn main() {
395398
}
396399
#[cfg(not(target_os = "linux"))]
397400
{
398-
// If the app was cold-launched by a deep link the full view must open, not the tray.
401+
// If the app was cold-launched by a deep-link, the full view must open, not the
402+
// tray.
399403
let launched_by_deep_link = app_handle
400404
.deep_link()
401405
.get_current()
@@ -406,15 +410,7 @@ fn main() {
406410
info!("App launched via deep link, opening full view directly.");
407411
let _ = WindowManager::open_full_view(app_handle);
408412
} else {
409-
let has_locations = tauri::async_runtime::block_on(
410-
defguard_client::window_manager::has_non_service_locations()
411-
);
412-
if has_locations {
413-
WindowManager::open_tray(app_handle)?;
414-
} else {
415-
info!("No locations found, showing full view on startup.");
416-
let _ = WindowManager::open_full_view(app_handle);
417-
}
413+
open_appropriate_window(app_handle);
418414
}
419415
}
420416

@@ -455,7 +451,7 @@ fn main() {
455451
log_dir.display(),
456452
service::config::DEFAULT_LOG_DIR
457453
);
458-
tauri::async_runtime::block_on(startup(app_handle));
454+
async_runtime::block_on(startup(app_handle));
459455

460456
// Handle a deep link that launched the app (startup case).
461457
if let Ok(Some(urls)) = app_handle.deep_link().get_current() {
@@ -465,7 +461,7 @@ fn main() {
465461
// Handle Ctrl-C.
466462
debug!("Setting up Ctrl-C handler.");
467463
let app_handle_clone = app_handle.clone();
468-
tauri::async_runtime::spawn(async move {
464+
async_runtime::spawn(async move {
469465
tokio::signal::ctrl_c()
470466
.await
471467
.expect("Signal handler failure");
@@ -490,7 +486,7 @@ fn main() {
490486
let semaphore = Arc::new(AtomicBool::new(false));
491487
let semaphore_clone = Arc::clone(&semaphore);
492488

493-
let handle = tauri::async_runtime::spawn(async move {
489+
let handle = async_runtime::spawn(async move {
494490
let _ = close_all_connections().await;
495491
// This will clean the database file, pruning write-ahead log.
496492
DB_POOL.close().await;
@@ -499,13 +495,13 @@ fn main() {
499495
// Obj-C API needs a runtime, but at this point Tauri has closed its runtime, so
500496
// create a temporary one.
501497
defguard_client::apple::spawn_runloop_and_wait_for(&semaphore);
502-
tauri::async_runtime::block_on(async move {
498+
async_runtime::block_on(async move {
503499
let _ = handle.await;
504500
});
505501
}
506502
#[cfg(not(target_os = "macos"))]
507503
{
508-
tauri::async_runtime::block_on(async move {
504+
async_runtime::block_on(async move {
509505
let _ = close_all_connections().await;
510506
// This will clean the database file, pruning write-ahead log.
511507
DB_POOL.close().await;
@@ -518,7 +514,7 @@ fn main() {
518514
..
519515
} => {
520516
if !has_visible_windows {
521-
let _ = WindowManager::open_tray(app_handle);
517+
open_appropriate_window(app_handle);
522518
}
523519
}
524520
_ => {

0 commit comments

Comments
 (0)