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
This commit is contained in:
jake 2024-02-26 15:17:28 +00:00 committed by GitHub
parent aa0b4abf85
commit 28328034f0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 57 deletions

View file

@ -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`.")
}
}

View file

@ -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