1010
1111// $FlowFixMe[unclear-type] We have no Flow types for the Electron API.
1212const { BrowserWindow, Menu, app, shell, ipcMain} = require ( 'electron' ) as any ;
13+ const Store = require ( 'electron-store' ) ;
1314const path = require ( 'path' ) ;
1415const util = require ( 'util' ) ;
1516
17+ const appSettings = new Store ( ) ;
1618const windowMetadata = new WeakMap <
1719 typeof BrowserWindow ,
1820 $ReadOnly < {
@@ -53,10 +55,11 @@ function handleLaunchArgs(argv: string[]) {
5355 } , 1000 ) ;
5456 }
5557 } else {
56- // Create the browser window.
5758 frontendWindow = new BrowserWindow ( {
58- width : 1200 ,
59- height : 600 ,
59+ ...( getSavedWindowPosition ( windowKey ) ?? {
60+ width : 1200 ,
61+ height : 600 ,
62+ } ) ,
6063 webPreferences : {
6164 partition : 'persist:react-native-devtools' ,
6265 preload : require . resolve ( './preload.js' ) ,
@@ -66,6 +69,8 @@ function handleLaunchArgs(argv: string[]) {
6669 } ) ;
6770 // Auto-hide the Windows/Linux menu bar
6871 frontendWindow . setMenuBarVisibility ( false ) ;
72+ // Observe and update saved window position
73+ setupWindowResizeListeners ( frontendWindow , windowKey ) ;
6974 }
7075
7176 // Open links in the default browser instead of in new Electron windows.
@@ -119,6 +124,37 @@ function configureAppMenu() {
119124 Menu . setApplicationMenu ( menu ) ;
120125}
121126
127+ function getSavedWindowPosition (
128+ windowKey : string ,
129+ ) : ?{ width: number , height : number , x ? : number , y ? : number } {
130+ return appSettings . get ( 'windowArrangements' , { } ) [ windowKey ] ;
131+ }
132+
133+ function saveWindowPosition (
134+ windowKey : string ,
135+ position : { x : number , y : number , width : number , height : number } ,
136+ ) {
137+ const windowArrangements = appSettings . get ( 'windowArrangements' , { } ) ;
138+ windowArrangements [ windowKey ] = position ;
139+ appSettings . set ( 'windowArrangements' , windowArrangements ) ;
140+ }
141+
142+ function setupWindowResizeListeners (
143+ browserWindow : typeof BrowserWindow ,
144+ windowKey : string ,
145+ ) {
146+ const savePosition = ( ) => {
147+ if ( ! browserWindow . isDestroyed ( ) ) {
148+ const [ x , y ] = browserWindow . getPosition ( ) ;
149+ const [ width , height ] = browserWindow . getSize ( ) ;
150+ saveWindowPosition ( windowKey , { x, y, width, height} ) ;
151+ }
152+ } ;
153+ browserWindow . on ( 'moved' , savePosition ) ;
154+ browserWindow . on ( 'resized' , savePosition ) ;
155+ browserWindow . on ( 'closed' , savePosition ) ;
156+ }
157+
122158app . whenReady ( ) . then ( ( ) => {
123159 handleLaunchArgs ( process . argv . slice ( app . isPackaged ? 1 : 2 ) ) ;
124160 configureAppMenu ( ) ;
0 commit comments