|
|
|
@ -108,11 +108,16 @@ struct WindowStateCache(Arc<Mutex<HashMap<String, WindowState>>>);
|
|
|
|
|
/// Used to prevent deadlocks from resize and position event listeners setting the cached state on restoring states
|
|
|
|
|
struct RestoringWindowState(Mutex<()>);
|
|
|
|
|
|
|
|
|
|
/// Whether to update the window status when the application exits
|
|
|
|
|
static SHOULD_UPDATE_STATE: Mutex<bool> = Mutex::new(true);
|
|
|
|
|
|
|
|
|
|
pub trait AppHandleExt {
|
|
|
|
|
/// Saves all open windows state to disk
|
|
|
|
|
fn save_window_state(&self, flags: StateFlags) -> Result<()>;
|
|
|
|
|
/// Get the name of the file used to store window state.
|
|
|
|
|
fn filename(&self) -> String;
|
|
|
|
|
/// Reset all windows state
|
|
|
|
|
fn reset_window_state(&self, restart: Option<bool>) -> Result<()>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<R: Runtime> AppHandleExt for tauri::AppHandle<R> {
|
|
|
|
@ -147,6 +152,28 @@ impl<R: Runtime> AppHandleExt for tauri::AppHandle<R> {
|
|
|
|
|
fn filename(&self) -> String {
|
|
|
|
|
self.state::<PluginState>().filename.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn reset_window_state(&self, restart: Option<bool>) -> Result<()> {
|
|
|
|
|
let restart = restart.unwrap_or(true);
|
|
|
|
|
if let Ok(mut should_update) = SHOULD_UPDATE_STATE.lock() {
|
|
|
|
|
*should_update = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let app_dir = self.path().app_config_dir()?;
|
|
|
|
|
let plugin_state = self.state::<PluginState>();
|
|
|
|
|
let state_path = app_dir.join(&plugin_state.filename);
|
|
|
|
|
// Directly remove the saved state file
|
|
|
|
|
if state_path.exists() {
|
|
|
|
|
std::fs::remove_file(state_path)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Need to restart the application
|
|
|
|
|
if restart {
|
|
|
|
|
self.app_handle().restart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait WindowExt {
|
|
|
|
@ -388,6 +415,7 @@ impl Builder {
|
|
|
|
|
PluginBuilder::new("window-state")
|
|
|
|
|
.invoke_handler(tauri::generate_handler![
|
|
|
|
|
cmd::save_window_state,
|
|
|
|
|
cmd::reset_window_state,
|
|
|
|
|
cmd::restore_state,
|
|
|
|
|
cmd::filename
|
|
|
|
|
])
|
|
|
|
@ -499,7 +527,10 @@ impl Builder {
|
|
|
|
|
})
|
|
|
|
|
.on_event(move |app, event| {
|
|
|
|
|
if let RunEvent::Exit = event {
|
|
|
|
|
let _ = app.save_window_state(flags);
|
|
|
|
|
let should_update = *SHOULD_UPDATE_STATE.lock().unwrap();
|
|
|
|
|
if should_update {
|
|
|
|
|
let _ = app.save_window_state(flags);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.build()
|
|
|
|
|