Skip to content

Commit 0e9541f

Browse files
authored
feat(window-state): support using a custom filename (#1138)
* feat(window-state): support using a custom filename ref: #1079 * generate api * fmt
1 parent f9bcc1c commit 0e9541f

6 files changed

Lines changed: 53 additions & 6 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"window-state": "patch"
3+
"window-state-js": "patch"
4+
---
5+
6+
Add `Builder::with_filename` to support using a custom filename. Also add `AppHandleExt::file_name` and a similar function in JS, to retrieve it later.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"window-state": "patch"
3+
---
4+
5+
**Breaking change**: Renamed `STATE_FILENAME` const to `DEFAULT_FILENAME`.

plugins/window-state/api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,11 @@ async function restoreState(
3838
async function restoreStateCurrent(flags: StateFlags): Promise<void> {
3939
return restoreState(getCurrent().label, flags);
4040
}
41+
/**
42+
* Get the name of the file used to store window state.
43+
*/
44+
async function filename(): Promise<string> {
45+
return invoke("plugin:window-state|filename");
46+
}
4147

42-
export { restoreState, restoreStateCurrent, saveWindowState };
48+
export { restoreState, restoreStateCurrent, saveWindowState, filename };

plugins/window-state/src/cmd.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ pub async fn restore_state<R: Runtime>(
3232
.map_err(|e| e.to_string())?;
3333
Ok(())
3434
}
35+
36+
#[command]
37+
pub fn filename<R: Runtime>(app: AppHandle<R>) -> String {
38+
app.filename()
39+
}

plugins/window-state/src/lib.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ use std::{
2828

2929
mod cmd;
3030

31-
pub const STATE_FILENAME: &str = ".window-state.json";
31+
/// Default filename used to store window state.
32+
///
33+
/// If using a custom filename, you should probably use [`AppHandleExt::filename`] instead.
34+
pub const DEFAULT_FILENAME: &str = ".window-state.json";
3235

3336
#[derive(Debug, thiserror::Error)]
3437
pub enum Error {
@@ -60,6 +63,10 @@ impl Default for StateFlags {
6063
}
6164
}
6265

66+
struct PluginState {
67+
filename: String,
68+
}
69+
6370
#[derive(Debug, Deserialize, Serialize, PartialEq)]
6471
struct WindowState {
6572
width: f64,
@@ -98,12 +105,15 @@ struct WindowStateCache(Arc<Mutex<HashMap<String, WindowState>>>);
98105
pub trait AppHandleExt {
99106
/// Saves all open windows state to disk
100107
fn save_window_state(&self, flags: StateFlags) -> Result<()>;
108+
/// Get the name of the file used to store window state.
109+
fn filename(&self) -> String;
101110
}
102111

103112
impl<R: Runtime> AppHandleExt for tauri::AppHandle<R> {
104113
fn save_window_state(&self, flags: StateFlags) -> Result<()> {
105114
if let Ok(app_dir) = self.path().app_config_dir() {
106-
let state_path = app_dir.join(STATE_FILENAME);
115+
let plugin_state = self.state::<PluginState>();
116+
let state_path = app_dir.join(&plugin_state.filename);
107117
let cache = self.state::<WindowStateCache>();
108118
let mut state = cache.0.lock().unwrap();
109119
for (label, s) in state.iter_mut() {
@@ -120,6 +130,10 @@ impl<R: Runtime> AppHandleExt for tauri::AppHandle<R> {
120130
Ok(())
121131
}
122132
}
133+
134+
fn filename(&self) -> String {
135+
self.state::<PluginState>().filename.clone()
136+
}
123137
}
124138

125139
pub trait WindowExt {
@@ -286,6 +300,7 @@ pub struct Builder {
286300
denylist: HashSet<String>,
287301
skip_initial_state: HashSet<String>,
288302
state_flags: StateFlags,
303+
filename: Option<String>,
289304
}
290305

291306
impl Builder {
@@ -299,6 +314,12 @@ impl Builder {
299314
self
300315
}
301316

317+
/// Sets a custom filename to use when saving and restoring window states from disk.
318+
pub fn with_filename(mut self, filename: impl Into<String>) -> Self {
319+
self.filename.replace(filename.into());
320+
self
321+
}
322+
302323
/// Sets a list of windows that shouldn't be tracked and managed by this plugin
303324
/// for example splash screen windows.
304325
pub fn with_denylist(mut self, denylist: &[&str]) -> Self {
@@ -314,15 +335,18 @@ impl Builder {
314335

315336
pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
316337
let flags = self.state_flags;
338+
let filename = self.filename.unwrap_or_else(|| DEFAULT_FILENAME.into());
339+
317340
PluginBuilder::new("window-state")
318341
.invoke_handler(tauri::generate_handler![
319342
cmd::save_window_state,
320-
cmd::restore_state
343+
cmd::restore_state,
344+
cmd::filename
321345
])
322346
.setup(|app, _api| {
323347
let cache: Arc<Mutex<HashMap<String, WindowState>>> =
324348
if let Ok(app_dir) = app.path().app_config_dir() {
325-
let state_path = app_dir.join(STATE_FILENAME);
349+
let state_path = app_dir.join(&filename);
326350
if state_path.exists() {
327351
Arc::new(Mutex::new(
328352
std::fs::read(state_path)
@@ -339,6 +363,7 @@ impl Builder {
339363
Default::default()
340364
};
341365
app.manage(WindowStateCache(cache));
366+
app.manage(PluginState { filename });
342367
Ok(())
343368
})
344369
.on_window_ready(move |window| {

0 commit comments

Comments
 (0)