From 28328034f02ec23bab73a25560928b2fd3f0261a Mon Sep 17 00:00:00 2001 From: jake <77554505+brxken128@users.noreply.github.com> Date: Mon, 26 Feb 2024 15:17:28 +0000 Subject: [PATCH] Clean-up MacOS window closing behaviour code (#2124) * fix: delete dead/unused file * refactor: add the window event handler with the rest of them * refactor: formatting --- .../src-tauri/src/clear_localstorage.rs | 44 ------------------- apps/desktop/src-tauri/src/main.rs | 25 +++++------ 2 files changed, 12 insertions(+), 57 deletions(-) delete mode 100644 apps/desktop/src-tauri/src/clear_localstorage.rs diff --git a/apps/desktop/src-tauri/src/clear_localstorage.rs b/apps/desktop/src-tauri/src/clear_localstorage.rs deleted file mode 100644 index c70afe362..000000000 --- a/apps/desktop/src-tauri/src/clear_localstorage.rs +++ /dev/null @@ -1,44 +0,0 @@ -use directories::BaseDirs; -use tokio::fs; -use tracing::{info, warn}; - -#[cfg(target_os = "linux")] -const EXTRA_DIRS: [&str; 1] = [".cache/spacedrive"]; -#[cfg(target_os = "macos")] -const EXTRA_DIRS: [&str; 2] = ["Library/WebKit/Spacedrive", "Library/Caches/Spacedrive"]; - -pub async fn clear_localstorage() { - if let Some(base_dir) = BaseDirs::new() { - let data_dir = base_dir.data_dir().join("com.spacedrive.desktop"); // maybe tie this into something static? - - fs::remove_dir_all(&data_dir) - .await - .map_err(|_| warn!("Unable to delete the `localStorage` primary directory.")) - .ok(); - - // Windows needs both AppData/Local and AppData/Roaming clearing as it stores data in both - #[cfg(target_os = "windows")] - fs::remove_dir_all(&base_dir.data_local_dir().join("com.spacedrive.desktop")) - .await - .map_err(|_| warn!("Unable to delete the `localStorage` directory in Local AppData.")) - .ok(); - - info!("Deleted {}", data_dir.display()); - - let home_dir = base_dir.home_dir(); - - #[cfg(any(target_os = "linux", target_os = "macos"))] - for path in EXTRA_DIRS { - fs::remove_dir_all(home_dir.join(path)) - .await - .map_err(|_| warn!("Unable to delete a `localStorage` cache: {path}")) - .ok(); - - info!("Deleted {path}"); - } - - info!("Successfully wiped `localStorage` and related caches.") - } else { - warn!("Unable to source `BaseDirs` in order to clear `localStorage`.") - } -} diff --git a/apps/desktop/src-tauri/src/main.rs b/apps/desktop/src-tauri/src/main.rs index ae5366583..1f4e236f3 100644 --- a/apps/desktop/src-tauri/src/main.rs +++ b/apps/desktop/src-tauri/src/main.rs @@ -24,7 +24,6 @@ use tauri_specta::{collect_events, ts, Event}; use tokio::time::sleep; use tracing::error; -mod clear_localstorage; mod file; mod menu; mod tauri_plugins; @@ -208,18 +207,6 @@ async fn main() -> tauri::Result<()> { .plugin(sd_server_plugin(node.clone()).await.unwrap()) // TODO: Handle `unwrap` .manage(node.clone()); - // macOS expected behavior is for the app to not exit when the main window is closed. - // Instead, the window is hidden and the dock icon remains so that on user click it should show the window again. - #[cfg(target_os = "macos")] - let app = app.on_window_event(|event| { - if let tauri::WindowEvent::CloseRequested { api, .. } = event.event() { - if event.window().label() == "main" { - AppHandle::hide(&event.window().app_handle()).expect("Window should hide on macOS"); - api.prevent_close(); - } - } - }); - let specta_builder = { let specta_builder = ts::builder() .events(collect_events![DragAndDropEvent]) @@ -316,6 +303,18 @@ async fn main() -> tauri::Result<()> { }) .on_menu_event(menu::handle_menu_event) .on_window_event(move |event| match event.event() { + // macOS expected behavior is for the app to not exit when the main window is closed. + // Instead, the window is hidden and the dock icon remains so that on user click it should show the window again. + #[cfg(target_os = "macos")] + WindowEvent::CloseRequested { api, .. } => { + // TODO: make this multi-window compatible in the future + event + .window() + .app_handle() + .hide() + .expect("Window should hide on macOS"); + api.prevent_close(); + } WindowEvent::FileDrop(drop) => { let window = event.window(); let mut file_drop_status = file_drop_status