import { BrowserWindow } from 'electron' ; import path from 'path' ; import { IPCKey } from '../common/Constants' ; /**
 * Current window list.
 */ const currentWindows = new Map ( ) ; param excludeId const notifyUpdateWindowIDs = ( excludeId ) => { const windowIds = Array . from ( currentWindows . keys ( ) ) ; currentWindows . forEach ( ( w ) => { if ( w . id === excludeId ) { return ; } w . webContents . send ( IPCKey . UpdateWindowIds , windowIds ) ; } ) ; } ; /**
 * Create a window and add it to the list.
 */ export const createNewWindow = ( ) => { const newWindow = new BrowserWindow ( { width : 800 , height : 600 , minWidth : 480 , minHeight : 320 , resizable : true , webPreferences : { nativeWindowOpen : false , nodeIntegration : false , contextIsolation : true , preload : path . join ( __dirname , 'preload.js' ) } } ) ; const windowId = newWindow . id ; newWindow . on ( 'closed' , ( ) => { console . log ( `Window was closed, id = ${ windowId }` ) ; currentWindows . delete ( windowId ) ; notifyUpdateWindowIDs ( windowId ) ; } ) ; const filePath = path . join ( __dirname , 'index.html' ) ; newWindow . loadURL ( `file://${ filePath }#${ windowId }` ) ; currentWindows . set ( windowId , newWindow ) ; notifyUpdateWindowIDs ( windowId ) ; } ; param targetWindowId param message returns export const sendMessage = ( targetWindowId , message ) : boolean => { const w = currentWindows . get ( targetWindowId ) ; if ( w ) { w . webContents . send ( IPCKey . UpdateMessage , message ) ; return true ; } return false ; } ; /**
 * Get a current window identifiers.
 */ export const getWindowIds = ( ) : number [ ] => { return Array . from ( currentWindows . keys ( ) ) ; } ;