feat(window-state): make flags optional in js side

pull/2619/head
Tony 3 months ago
parent c698e72594
commit 8da67733ec
No known key found for this signature in database
GPG Key ID: 34BDD3EA27824956

@ -0,0 +1,6 @@
---
'window-state': 'minor'
'window-state-js': 'minor'
---
Making `flags` optional in the `saveWindowState`, `restoreState`, `restoreStateCurrent` JavaScripts APIs, leaving it empty will make it use plugin's default flags

@ -18,7 +18,7 @@ export enum StateFlags {
/** /**
* Save the state of all open windows to disk. * Save the state of all open windows to disk.
*/ */
async function saveWindowState(flags: StateFlags): Promise<void> { async function saveWindowState(flags?: StateFlags): Promise<void> {
await invoke('plugin:window-state|save_window_state', { flags }) await invoke('plugin:window-state|save_window_state', { flags })
} }
@ -27,7 +27,7 @@ async function saveWindowState(flags: StateFlags): Promise<void> {
*/ */
async function restoreState( async function restoreState(
label: WindowLabel, label: WindowLabel,
flags: StateFlags flags?: StateFlags
): Promise<void> { ): Promise<void> {
await invoke('plugin:window-state|restore_state', { label, flags }) await invoke('plugin:window-state|restore_state', { label, flags })
} }
@ -35,7 +35,7 @@ async function restoreState(
/** /**
* Restore the state for the current window from disk. * Restore the state for the current window from disk.
*/ */
async function restoreStateCurrent(flags: StateFlags): Promise<void> { async function restoreStateCurrent(flags?: StateFlags): Promise<void> {
await restoreState(getCurrentWindow().label, flags) await restoreState(getCurrentWindow().label, flags)
} }
/** /**

@ -5,13 +5,25 @@
use crate::{AppHandleExt, StateFlags, WindowExt}; use crate::{AppHandleExt, StateFlags, WindowExt};
use tauri::{command, AppHandle, Manager, Runtime}; use tauri::{command, AppHandle, Manager, Runtime};
fn get_state_flags<R: Runtime>(
app: &AppHandle<R>,
flags: Option<u32>,
) -> std::result::Result<StateFlags, String> {
let flags = if let Some(flags) = flags {
StateFlags::from_bits(flags).ok_or_else(|| format!("Invalid state flags bits: {flags}"))?
} else {
let plugin_state = app.state::<crate::PluginState>();
plugin_state.state_flags
};
Ok(flags)
}
#[command] #[command]
pub async fn save_window_state<R: Runtime>( pub async fn save_window_state<R: Runtime>(
app: AppHandle<R>, app: AppHandle<R>,
flags: u32, flags: Option<u32>,
) -> std::result::Result<(), String> { ) -> std::result::Result<(), String> {
let flags = StateFlags::from_bits(flags) let flags = get_state_flags(&app, flags)?;
.ok_or_else(|| format!("Invalid state flags bits: {}", flags))?;
app.save_window_state(flags).map_err(|e| e.to_string())?; app.save_window_state(flags).map_err(|e| e.to_string())?;
Ok(()) Ok(())
} }
@ -20,12 +32,11 @@ pub async fn save_window_state<R: Runtime>(
pub async fn restore_state<R: Runtime>( pub async fn restore_state<R: Runtime>(
app: AppHandle<R>, app: AppHandle<R>,
label: String, label: String,
flags: u32, flags: Option<u32>,
) -> std::result::Result<(), String> { ) -> std::result::Result<(), String> {
let flags = StateFlags::from_bits(flags) let flags = get_state_flags(&app, flags)?;
.ok_or_else(|| format!("Invalid state flags bits: {}", flags))?;
app.get_webview_window(&label) app.get_webview_window(&label)
.ok_or_else(|| format!("Couldn't find window with label: {}", label))? .ok_or_else(|| format!("Couldn't find window with label: {label}"))?
.restore_state(flags) .restore_state(flags)
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
Ok(()) Ok(())

@ -60,12 +60,14 @@ bitflags! {
} }
impl Default for StateFlags { impl Default for StateFlags {
/// Default to [`all`](Self::all)
fn default() -> Self { fn default() -> Self {
Self::all() Self::all()
} }
} }
struct PluginState { struct PluginState {
pub(crate) state_flags: StateFlags,
filename: String, filename: String,
map_label: Option<Box<LabelMapperFn>>, map_label: Option<Box<LabelMapperFn>>,
} }
@ -381,7 +383,7 @@ impl Builder {
} }
pub fn build<R: Runtime>(self) -> TauriPlugin<R> { pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
let flags = self.state_flags; let state_flags = self.state_flags;
let filename = self.filename.unwrap_or_else(|| DEFAULT_FILENAME.into()); let filename = self.filename.unwrap_or_else(|| DEFAULT_FILENAME.into());
let map_label = self.map_label; let map_label = self.map_label;
@ -391,11 +393,12 @@ impl Builder {
cmd::restore_state, cmd::restore_state,
cmd::filename cmd::filename
]) ])
.setup(|app, _api| { .setup(move |app, _api| {
let cache = load_saved_window_states(app, &filename).unwrap_or_default(); let cache = load_saved_window_states(app, &filename).unwrap_or_default();
app.manage(WindowStateCache(Arc::new(Mutex::new(cache)))); app.manage(WindowStateCache(Arc::new(Mutex::new(cache))));
app.manage(RestoringWindowState(Mutex::new(()))); app.manage(RestoringWindowState(Mutex::new(())));
app.manage(PluginState { app.manage(PluginState {
state_flags,
filename, filename,
map_label, map_label,
}); });
@ -423,14 +426,13 @@ impl Builder {
} }
if !self.skip_initial_state.contains(label) { if !self.skip_initial_state.contains(label) {
let _ = window.restore_state(self.state_flags); let _ = window.restore_state(state_flags);
} }
let cache = window.state::<WindowStateCache>(); let cache = window.state::<WindowStateCache>();
let cache = cache.0.clone(); let cache = cache.0.clone();
let label = label.to_string(); let label = label.to_string();
let window_clone = window.clone(); let window_clone = window.clone();
let flags = self.state_flags;
// insert a default state if this window should be tracked and // insert a default state if this window should be tracked and
// the disk cache doesn't have a state for it // the disk cache doesn't have a state for it
@ -446,11 +448,11 @@ impl Builder {
WindowEvent::CloseRequested { .. } => { WindowEvent::CloseRequested { .. } => {
let mut c = cache.lock().unwrap(); let mut c = cache.lock().unwrap();
if let Some(state) = c.get_mut(&label) { if let Some(state) = c.get_mut(&label) {
let _ = window_clone.update_state(state, flags); let _ = window_clone.update_state(state, state_flags);
} }
} }
WindowEvent::Moved(position) if flags.contains(StateFlags::POSITION) => { WindowEvent::Moved(position) if state_flags.contains(StateFlags::POSITION) => {
if window_clone if window_clone
.state::<RestoringWindowState>() .state::<RestoringWindowState>()
.0 .0
@ -468,7 +470,7 @@ impl Builder {
} }
} }
} }
WindowEvent::Resized(size) if flags.contains(StateFlags::SIZE) => { WindowEvent::Resized(size) if state_flags.contains(StateFlags::SIZE) => {
if window_clone if window_clone
.state::<RestoringWindowState>() .state::<RestoringWindowState>()
.0 .0
@ -499,7 +501,7 @@ impl Builder {
}) })
.on_event(move |app, event| { .on_event(move |app, event| {
if let RunEvent::Exit = event { if let RunEvent::Exit = event {
let _ = app.save_window_state(flags); let _ = app.save_window_state(state_flags);
} }
}) })
.build() .build()

Loading…
Cancel
Save