Skip to content

Commit 6a8f255

Browse files
feat(window-state): make flags optional in js side (#2619)
1 parent 8ac494d commit 6a8f255

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'window-state': 'minor'
3+
'window-state-js': 'minor'
4+
---
5+
6+
Making `flags` optional in the `saveWindowState`, `restoreState`, `restoreStateCurrent` JavaScripts APIs, leaving it empty will make it use plugin's default flags

plugins/window-state/guest-js/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export enum StateFlags {
1818
/**
1919
* Save the state of all open windows to disk.
2020
*/
21-
async function saveWindowState(flags: StateFlags): Promise<void> {
21+
async function saveWindowState(flags?: StateFlags): Promise<void> {
2222
await invoke('plugin:window-state|save_window_state', { flags })
2323
}
2424

@@ -27,15 +27,15 @@ async function saveWindowState(flags: StateFlags): Promise<void> {
2727
*/
2828
async function restoreState(
2929
label: WindowLabel,
30-
flags: StateFlags
30+
flags?: StateFlags
3131
): Promise<void> {
3232
await invoke('plugin:window-state|restore_state', { label, flags })
3333
}
3434

3535
/**
3636
* Restore the state for the current window from disk.
3737
*/
38-
async function restoreStateCurrent(flags: StateFlags): Promise<void> {
38+
async function restoreStateCurrent(flags?: StateFlags): Promise<void> {
3939
await restoreState(getCurrentWindow().label, flags)
4040
}
4141
/**

plugins/window-state/src/cmd.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@
55
use crate::{AppHandleExt, StateFlags, WindowExt};
66
use tauri::{command, AppHandle, Manager, Runtime};
77

8+
fn get_state_flags<R: Runtime>(
9+
app: &AppHandle<R>,
10+
flags: Option<u32>,
11+
) -> std::result::Result<StateFlags, String> {
12+
let flags = if let Some(flags) = flags {
13+
StateFlags::from_bits(flags).ok_or_else(|| format!("Invalid state flags bits: {flags}"))?
14+
} else {
15+
let plugin_state = app.state::<crate::PluginState>();
16+
plugin_state.state_flags
17+
};
18+
Ok(flags)
19+
}
20+
821
#[command]
922
pub async fn save_window_state<R: Runtime>(
1023
app: AppHandle<R>,
11-
flags: u32,
24+
flags: Option<u32>,
1225
) -> std::result::Result<(), String> {
13-
let flags = StateFlags::from_bits(flags)
14-
.ok_or_else(|| format!("Invalid state flags bits: {}", flags))?;
26+
let flags = get_state_flags(&app, flags)?;
1527
app.save_window_state(flags).map_err(|e| e.to_string())?;
1628
Ok(())
1729
}
@@ -20,12 +32,11 @@ pub async fn save_window_state<R: Runtime>(
2032
pub async fn restore_state<R: Runtime>(
2133
app: AppHandle<R>,
2234
label: String,
23-
flags: u32,
35+
flags: Option<u32>,
2436
) -> std::result::Result<(), String> {
25-
let flags = StateFlags::from_bits(flags)
26-
.ok_or_else(|| format!("Invalid state flags bits: {}", flags))?;
37+
let flags = get_state_flags(&app, flags)?;
2738
app.get_webview_window(&label)
28-
.ok_or_else(|| format!("Couldn't find window with label: {}", label))?
39+
.ok_or_else(|| format!("Couldn't find window with label: {label}"))?
2940
.restore_state(flags)
3041
.map_err(|e| e.to_string())?;
3142
Ok(())

plugins/window-state/src/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ bitflags! {
6060
}
6161

6262
impl Default for StateFlags {
63+
/// Default to [`all`](Self::all)
6364
fn default() -> Self {
6465
Self::all()
6566
}
6667
}
6768

6869
struct PluginState {
70+
pub(crate) state_flags: StateFlags,
6971
filename: String,
7072
map_label: Option<Box<LabelMapperFn>>,
7173
}
@@ -381,7 +383,7 @@ impl Builder {
381383
}
382384

383385
pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
384-
let flags = self.state_flags;
386+
let state_flags = self.state_flags;
385387
let filename = self.filename.unwrap_or_else(|| DEFAULT_FILENAME.into());
386388
let map_label = self.map_label;
387389

@@ -391,11 +393,12 @@ impl Builder {
391393
cmd::restore_state,
392394
cmd::filename
393395
])
394-
.setup(|app, _api| {
396+
.setup(move |app, _api| {
395397
let cache = load_saved_window_states(app, &filename).unwrap_or_default();
396398
app.manage(WindowStateCache(Arc::new(Mutex::new(cache))));
397399
app.manage(RestoringWindowState(Mutex::new(())));
398400
app.manage(PluginState {
401+
state_flags,
399402
filename,
400403
map_label,
401404
});
@@ -423,14 +426,13 @@ impl Builder {
423426
}
424427

425428
if !self.skip_initial_state.contains(label) {
426-
let _ = window.restore_state(self.state_flags);
429+
let _ = window.restore_state(state_flags);
427430
}
428431

429432
let cache = window.state::<WindowStateCache>();
430433
let cache = cache.0.clone();
431434
let label = label.to_string();
432435
let window_clone = window.clone();
433-
let flags = self.state_flags;
434436

435437
// insert a default state if this window should be tracked and
436438
// the disk cache doesn't have a state for it
@@ -446,11 +448,11 @@ impl Builder {
446448
WindowEvent::CloseRequested { .. } => {
447449
let mut c = cache.lock().unwrap();
448450
if let Some(state) = c.get_mut(&label) {
449-
let _ = window_clone.update_state(state, flags);
451+
let _ = window_clone.update_state(state, state_flags);
450452
}
451453
}
452454

453-
WindowEvent::Moved(position) if flags.contains(StateFlags::POSITION) => {
455+
WindowEvent::Moved(position) if state_flags.contains(StateFlags::POSITION) => {
454456
if window_clone
455457
.state::<RestoringWindowState>()
456458
.0
@@ -468,7 +470,7 @@ impl Builder {
468470
}
469471
}
470472
}
471-
WindowEvent::Resized(size) if flags.contains(StateFlags::SIZE) => {
473+
WindowEvent::Resized(size) if state_flags.contains(StateFlags::SIZE) => {
472474
if window_clone
473475
.state::<RestoringWindowState>()
474476
.0
@@ -499,7 +501,7 @@ impl Builder {
499501
})
500502
.on_event(move |app, event| {
501503
if let RunEvent::Exit = event {
502-
let _ = app.save_window_state(flags);
504+
let _ = app.save_window_state(state_flags);
503505
}
504506
})
505507
.build()

0 commit comments

Comments
 (0)