feat: added reset_window_state method

pull/2727/head
The1111mp 2 months ago
parent 909bc1f9ca
commit 654f23789e
No known key found for this signature in database
GPG Key ID: A94EB6A0F8263C9C

@ -0,0 +1,6 @@
---
window-state: minor
window-state-js: minor
---
Added `reset_window_state` method to reset the window state and start from scratch.

@ -104,6 +104,25 @@ import {
restoreStateCurrent(StateFlags.ALL)
```
If you want to reset the window state, this will start all over again:
```rust
use tauri_plugin_window_state::{AppHandleExt};
// `tauri::AppHandle` now has the following additional method
app.reset_window_state(); // will reset the state of all windows from disk and restart app
```
or through Javascript
```javascript
import {
resetWindowState
} from '@tauri-apps/plugin-window-state'
resetWindowState()
```
## Contributing
PRs accepted. Please make sure to read the Contributing Guide before making a pull request.

File diff suppressed because one or more lines are too long

@ -32,6 +32,13 @@ async function restoreState(
await invoke('plugin:window-state|restore_state', { label, flags })
}
/**
* Reset the state of all windows from disk.
*/
async function resetWindowState(restart: boolean): Promise<void> {
await invoke('plugin:window-state|reset_window_state', {restart})
}
/**
* Restore the state for the current window from disk.
*/
@ -45,4 +52,4 @@ async function filename(): Promise<string> {
return await invoke('plugin:window-state|filename')
}
export { restoreState, restoreStateCurrent, saveWindowState, filename }
export { restoreState, restoreStateCurrent, resetWindowState, saveWindowState, filename }

@ -16,6 +16,15 @@ pub async fn save_window_state<R: Runtime>(
Ok(())
}
#[command]
pub async fn reset_window_state<R: Runtime>(
app: AppHandle<R>,
restart: Option<bool>,
) -> std::result::Result<(), String> {
app.reset_window_state(restart).map_err(|e| e.to_string())?;
Ok(())
}
#[command]
pub async fn restore_state<R: Runtime>(
app: AppHandle<R>,

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

Loading…
Cancel
Save