@@ -23,13 +23,18 @@ use tauri_plugin_deep_link::DeepLinkExt;
2323use tauri_plugin_dialog:: { DialogExt , MessageDialogButtons , MessageDialogResult } ;
2424use tauri_plugin_shell:: process:: { CommandChild , CommandEvent } ;
2525use tauri_plugin_store:: StoreExt ;
26- use tokio:: sync:: oneshot;
26+ use tauri_plugin_window_state:: { AppHandleExt , StateFlags } ;
27+ use tokio:: sync:: { mpsc, oneshot} ;
2728
2829use crate :: window_customizer:: PinchZoomDisablePlugin ;
2930
3031const SETTINGS_STORE : & str = "opencode.settings.dat" ;
3132const DEFAULT_SERVER_URL_KEY : & str = "defaultServerUrl" ;
3233
34+ fn window_state_flags ( ) -> StateFlags {
35+ StateFlags :: all ( ) - StateFlags :: DECORATIONS
36+ }
37+
3338#[ derive( Clone , serde:: Serialize , specta:: Type ) ]
3439struct ServerReadyData {
3540 url : String ,
@@ -293,10 +298,7 @@ pub fn run() {
293298 . plugin ( tauri_plugin_os:: init ( ) )
294299 . plugin (
295300 tauri_plugin_window_state:: Builder :: new ( )
296- . with_state_flags (
297- tauri_plugin_window_state:: StateFlags :: all ( )
298- - tauri_plugin_window_state:: StateFlags :: DECORATIONS ,
299- )
301+ . with_state_flags ( window_state_flags ( ) )
300302 . build ( ) ,
301303 )
302304 . plugin ( tauri_plugin_store:: Builder :: new ( ) . build ( ) )
@@ -365,6 +367,8 @@ pub fn run() {
365367
366368 let window = window_builder. build ( ) . expect ( "Failed to create window" ) ;
367369
370+ setup_window_state_listener ( & app, & window) ;
371+
368372 #[ cfg( windows) ]
369373 let _ = window. create_overlay_titlebar ( ) ;
370374
@@ -560,3 +564,34 @@ async fn spawn_local_server(
560564 }
561565 }
562566}
567+
568+ fn setup_window_state_listener ( app : & tauri:: AppHandle , window : & tauri:: WebviewWindow ) {
569+ let ( tx, mut rx) = mpsc:: channel :: < ( ) > ( 1 ) ;
570+
571+ window. on_window_event ( move |event| {
572+ if !matches ! ( event, WindowEvent :: Moved ( _) | WindowEvent :: Resized ( _) ) {
573+ return ;
574+ }
575+ let _ = tx. try_send ( ( ) ) ;
576+ } ) ;
577+
578+ tauri:: async_runtime:: spawn ( {
579+ let app = app. clone ( ) ;
580+
581+ async move {
582+ let save = || {
583+ let handle = app. clone ( ) ;
584+ let app = app. clone ( ) ;
585+ let _ = handle. run_on_main_thread ( move || {
586+ let _ = app. save_window_state ( window_state_flags ( ) ) ;
587+ } ) ;
588+ } ;
589+
590+ while rx. recv ( ) . await . is_some ( ) {
591+ tokio:: time:: sleep ( Duration :: from_millis ( 200 ) ) . await ;
592+
593+ save ( ) ;
594+ }
595+ }
596+ } ) ;
597+ }
0 commit comments