@@ -28,7 +28,10 @@ use std::{
2828
2929mod 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 ) ]
3437pub 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 ) ]
6471struct WindowState {
6572 width : f64 ,
@@ -98,12 +105,15 @@ struct WindowStateCache(Arc<Mutex<HashMap<String, WindowState>>>);
98105pub 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
103112impl < 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
125139pub 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
291306impl 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