diff --git a/plugins/window-state/README.md b/plugins/window-state/README.md index de309e73..aa9aa865 100644 --- a/plugins/window-state/README.md +++ b/plugins/window-state/README.md @@ -39,19 +39,19 @@ Afterwards all windows will remember their state when the app is being closed an Optionally you can also tell the plugin to save the state of all open window to disk my using the `save_window_state()` method exposed by the `AppHandleExt` trait: ```rust -use tauri_plugin_window_state::AppHandleExt; +use tauri_plugin_window_state::{AppHandleExt, StateFlags}; // `tauri::AppHandle` now has the following additional method -app.save_window_state(); // will save the state of all open windows to disk +app.save_window_state(StateFlags::all()); // will save the state of all open windows to disk ``` To manually restore a windows state from disk you can call the `restore_state()` method exposed by the `WindowExt` trait: ```rust -use tauri_plugin_window_state::{WindowExt, ShowMode}; +use tauri_plugin_window_state::{WindowExt, StateFlags}; // all `Window` types now have the following additional method -window.restore_state(ShowMode::LastSaved); // will restore the windows state from disk +window.restore_state(StateFlags::all()); // will restore the windows state from disk ``` ## Contributing diff --git a/plugins/window-state/src/lib.rs b/plugins/window-state/src/lib.rs index 200cf9d7..2c82c1a3 100644 --- a/plugins/window-state/src/lib.rs +++ b/plugins/window-state/src/lib.rs @@ -64,15 +64,21 @@ struct WindowState { struct WindowStateCache(Arc>>); pub trait AppHandleExt { - fn save_window_state(&self) -> Result<()>; + fn save_window_state(&self, flags: StateFlags) -> Result<()>; } impl AppHandleExt for tauri::AppHandle { - fn save_window_state(&self) -> Result<()> { + fn save_window_state(&self, flags: StateFlags) -> Result<()> { if let Some(app_dir) = self.path_resolver().app_config_dir() { let state_path = app_dir.join(STATE_FILENAME); let cache = self.state::(); - let state = cache.0.lock().unwrap(); + let mut state = cache.0.lock().unwrap(); + for (label, s) in state.iter_mut() { + if let Some(window) = self.get_window(label) { + window.update_state(s, flags)?; + } + } + create_dir_all(&app_dir) .map_err(Error::Io) .and_then(|_| File::create(state_path).map_err(Into::into)) @@ -261,6 +267,7 @@ impl Builder { } pub fn build(self) -> TauriPlugin { + let flags = self.state_flags; PluginBuilder::new("window-state") .setup(|app| { let cache: Arc>> = if let Some(app_dir) = @@ -306,9 +313,9 @@ impl Builder { } }); }) - .on_event(|app, event| { + .on_event(move |app, event| { if let RunEvent::Exit = event { - let _ = app.save_window_state(); + let _ = app.save_window_state(flags); } }) .build()