import { BrowserWindow , shell , dialog , app } from 'electron' ; import { indexFilePath , iconPath } from '../constants/paths' ; import { setWindowMenu } from '../util/windowMenu' ; import { setHotkeys } from '../util/hotkeys' ; import * as EVENTS from '../../common/constants/events' ; import * as PATHS from '../../common/constants/paths' ; import { IPlugin } from '../../common/types/plugins' ; import { startFullscreenSetting , startMaximizedSetting , showMenuBarWhenFullscreenSetting , useGnomeStyleHeaderbarSetting } from '../../common/code/settings' ; import { ipcMainListen } from '../events/limitedIPC' ; import { setIPCHandlers } from './mainWindowIPC' ; let showExitPrompt = true ; export function createMainWindow ( isDev , plugins , splashWin ) { const win = new BrowserWindow ( { webPreferences : { devTools : isDev , enableRemoteModule : true , nodeIntegration : true , contextIsolation : false } , show : false , fullscreenable : true , frame : ! useGnomeStyleHeaderbarSetting . get ( ) , fullscreen : startFullscreenSetting . get ( ) , icon : iconPath } ) ; if ( startMaximizedSetting . get ( ) ) win . maximize ( ) ; ipcMainListen ( EVENTS . GET_PLUGINS , ( e ) => { e . returnValue = plugins . filter ( plugin => plugin . usable ) ; } ) ; setIPCHandlers ( win , app , isDev ) ; win . loadFile ( indexFilePath ) ; setWindowMenu ( win , isDev , `/${ PATHS . HOME }` ) ; setHotkeys ( win ) ; win . on ( 'enter-full-screen' , ( ) => { win . setMenuBarVisibility ( showMenuBarWhenFullscreenSetting . get ( ) ) ; } ) ; win . on ( 'leave-full-screen' , ( ) => { win . setMenuBarVisibility ( true ) ; } ) ; win . webContents . on ( 'new-window' , ( e , url ) => { e . preventDefault ( ) ; if ( ! url . includes ( indexFilePath ) ) shell . openExternal ( url ) ; } ) ; win . on ( 'close' , ( e ) => { if ( showExitPrompt ) { if ( win . webContents . getURL ( ) . toLowerCase ( ) . includes ( `/#${ PATHS . WHITEBOARD }` ) ) { e . preventDefault ( ) ; dialog . showMessageBox ( win , { type : 'question' , buttons : [ 'No' , 'Yes' ] , title : 'Quit?' , message : 'Unsaved data will be lost. Are you sure you want to quit?' } ) . then ( ( { response } ) => { if ( response === 1 ) { showExitPrompt = false ; win . close ( ) ; } } ) ; } } } ) ; win . webContents . on ( 'did-finish-load' , ( ) => { win . show ( ) ; } ) ; }