import { app , BrowserWindow , clipboard , ipcMain , screen , session , shell } from 'electron' ; import { auth } from '../src/uitls/stackexchange-auth' ; import { InvokeEnum } from '../src/interfaces/InvokeEnum' ; import stackoverflow from '../src/uitls/stackexchange-api' ; let mainWindow ; let splashScreenWindow ; let overlayWindow ; declare const MAIN_WINDOW_WEBPACK_ENTRY ; declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY ; declare const SPLASH_SCREEN_WINDOW_WEBPACK_ENTRY ; declare const SPLASH_SCREEN_WINDOW_PRELOAD_WEBPACK_ENTRY ; declare const OVERLAY_WINDOW_WEBPACK_ENTRY ; declare const OVERLAY_WINDOW_PRELOAD_WEBPACK_ENTRY ; function createWindows ( ) { const display = screen . getPrimaryDisplay ( ) ; mainWindow = new BrowserWindow ( { width : 1000 , height : 600 , minWidth : 750 , minHeight : 400 , show : false , title : 'StackOverflow' , titleBarStyle : 'hidden' , trafficLightPosition : { x : 16 , y : 13 } , webPreferences : { sandbox : true , nodeIntegration : false , contextIsolation : true , preload : MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY } } ) ; splashScreenWindow = new BrowserWindow ( { width : 300 , height : 300 , show : false , title : 'StackOverflow starting…' , frame : false , resizable : false , closable : false , backgroundColor : '#2d3748' , webPreferences : { sandbox : true , nodeIntegration : false , contextIsolation : true , preload : SPLASH_SCREEN_WINDOW_PRELOAD_WEBPACK_ENTRY } } ) ; overlayWindow = new BrowserWindow ( { title : 'Preview' , alwaysOnTop : true , x : display . bounds . x , y : display . bounds . y , width : display . bounds . width , height : display . bounds . height , frame : false , show : false , transparent : true , roundedCorners : true , resizable : false , movable : false , minimizable : false , maximizable : false , hasShadow : false , webPreferences : { sandbox : true , nodeIntegration : false , contextIsolation : true , preload : OVERLAY_WINDOW_PRELOAD_WEBPACK_ENTRY } } ) ; initEmptyWindows ( ) ; mainWindow . on ( 'close' , ( event ) => { event . preventDefault ( ) ; mainWindow ?. hide ( ) ; } ) ; mainWindow . on ( 'ready-to-show' , ( ) => { auth ( async ( token , expires ) => { const user = await stackoverflow . getLoggedInUser ( token ) ; const partialSidebarCounts = await stackoverflow . getSidebarCounts ( user . user_id , token ) ; mainWindow ?. webContents . send ( 'stackexchange:on-auth' , { user , sidebarCounts : { ... partialSidebarCounts , questions : user . question_count , answers : user . answer_count } , token } ) ; } ) ; } ) ; mainWindow . webContents . on ( 'will-navigate' , ( event , url ) => { if ( process . env . NODE_ENV === 'development' && url . includes ( 'localhost' ) ) { return ; } event . preventDefault ( ) ; shell . openExternal ( url ) ; } ) ; splashScreenWindow . on ( 'ready-to-show' , ( ) => { splashScreenWindow ?. show ( ) ; } ) ; } async function registerListeners ( ) { ipcMain . on ( 'stackexchange-logout' , ( event ) => { session . defaultSession . clearStorageData ( { storages : [ 'cookies' ] } ) . then ( ( ) => { initEmptyWindows ( ) ; } ) ; } ) ; ipcMain . handle ( InvokeEnum . OPEN_CODE_IN_PREVIEW , ( event , html ) => { overlayWindow ?. webContents . send ( 'init-html' , html ) ; overlayWindow ?. show ( ) ; } ) ; ipcMain . handle ( InvokeEnum . OPEN_IMAGE_IN_PREVIEW , ( event , url ) => { overlayWindow ?. webContents . send ( 'init-image' , url ) ; overlayWindow ?. show ( ) ; } ) ; ipcMain . handle ( InvokeEnum . HIDE_OVERLAY , ( ) => { overlayWindow ?. hide ( ) ; } ) ; ipcMain . on ( 'main-window-ready' , ( ) => { mainWindow ?. show ( ) ; splashScreenWindow ?. destroy ( ) ; splashScreenWindow = null ; } ) ; ipcMain . on ( 'online' , ( event ) => { event . reply ( 'online' ) ; } ) ; ipcMain . on ( 'offline' , ( event ) => { event . reply ( 'offline' ) ; } ) ; ipcMain . handle ( InvokeEnum . COPY_TO_CLIPBOARD , ( event , text ) => { clipboard . writeText ( text ) ; } ) ; } app . on ( 'ready' , createWindows ) . whenReady ( ) . then ( registerListeners ) . catch ( ( e ) => console . error ( e ) ) ; app . on ( 'window-all-closed' , ( ) => { if ( process . platform !== 'darwin' ) { app . quit ( ) ; } } ) ; app . on ( 'activate' , ( ) => { if ( ! mainWindow ?. isVisible ( ) && ! splashScreenWindow ) { mainWindow ?. show ( ) ; } } ) ; function initEmptyWindows ( ) { mainWindow ?. loadURL ( MAIN_WINDOW_WEBPACK_ENTRY ) ; splashScreenWindow ?. loadURL ( SPLASH_SCREEN_WINDOW_WEBPACK_ENTRY ) ; overlayWindow ?. loadURL ( OVERLAY_WINDOW_WEBPACK_ENTRY ) ; }