import { BrowserWindow , shell , Tray , Menu , ipcMain } from 'electron' ; import { rendererAppName , rendererAppPort } from './constants' ; import { join } from 'path' ; import { format } from 'url' ; import AutoLaunch from 'auto-launch' ; import { ipcLabels } from '@lunchpad/types' ; import * as Configstore from 'configstore' ; const config = new Configstore ( 'lunchpad' , { stayOnTop : false } ) ; export default class Lunchpad { static mainWindow : Electron . BrowserWindow ; static application : Electron . App ; static BrowserWindow ; static tray : Electron . Tray ; static isQuiting : boolean = false ; static stayOnTop : boolean ; static minimizeToTray : boolean ; static runAtStartup : boolean ; static Launcher : any ; public static isDevelopmentMode ( ) { const isEnvironmentSet = 'ELECTRON_IS_DEV' in process . env ; const getFromEnvironment = parseInt ( process . env . ELECTRON_IS_DEV , 10 ) === 1 ; return isEnvironmentSet ? getFromEnvironment : ! Lunchpad . application . isPackaged ; } private static onWindowAllClosed ( ) { if ( process . platform !== 'darwin' ) { Lunchpad . application . quit ( ) ; } } private static onClose ( ) { Lunchpad . mainWindow = null ; } private static onRedirect ( event , url ) { if ( url !== Lunchpad . mainWindow . webContents . getURL ( ) ) { event . preventDefault ( ) ; shell . openExternal ( url ) ; } } private static onReady ( ) { Lunchpad . initMainWindow ( ) ; Lunchpad . loadMainWindow ( ) ; } private static onActivate ( ) { if ( Lunchpad . mainWindow === null ) { Lunchpad . onReady ( ) ; } } private static SetStayOnTop ( value ) { Lunchpad . mainWindow . setAlwaysOnTop ( value ) ; config . set ( 'stayOnTop' , value ) ; Lunchpad . stayOnTop = value ; } private static SetMinimizeToTray ( value ) { config . set ( 'minimizeToTray' , value ) ; Lunchpad . minimizeToTray = value ; } private static SetRunAtStartup ( value ) { if ( value ) { Lunchpad . Launcher . enable ( ) ; } else { Lunchpad . Launcher . disable ( ) ; } config . set ( 'runAtStartup' , value ) ; Lunchpad . runAtStartup = value ; } private static initMainWindow ( ) { Lunchpad . mainWindow = new BrowserWindow ( { width : 756 , height : 756 , show : false , alwaysOnTop : Lunchpad . stayOnTop , backgroundColor : "#23272A" , useContentSize : true , webPreferences : { webSecurity : false , nodeIntegration : true , backgroundThrottling : false , defaultFontFamily : { monospace : "Exo 2" , serif : "Exo 2" , sansSerif : "Exo 2" , standard : "Exo 2" , } } , minHeight : 756 , minWidth : 756 , title : "Lunchpad" , icon : join ( __dirname , "assets/logo.ico" ) } ) ; Lunchpad . mainWindow . center ( ) ; Lunchpad . mainWindow . setMenuBarVisibility ( false ) ; if ( Lunchpad . minimizeToTray ) Lunchpad . mainWindow . hide ( ) ; Lunchpad . tray = new Tray ( join ( __dirname , "assets/logo.png" ) ) ; Lunchpad . tray . setContextMenu ( Menu . buildFromTemplate ( [ { label : "Join the Discord!" , icon : join ( __dirname , "assets/discord.png" ) , click : ( ) => shell . openExternal ( "https://discord.com/invite/4Ys9TRR" ) } , { type : "separator" } , { label : 'Show Lunchpad' , click : ( ) => { Lunchpad . mainWindow . show ( ) ; } } , { type : "separator" } , { type : "checkbox" , label : "Stay on top" , checked : Lunchpad . stayOnTop , click : ( e ) => Lunchpad . SetStayOnTop ( e . checked ) } , { type : "checkbox" , label : "Minimize to tray" , checked : Lunchpad . minimizeToTray , click : ( e ) => Lunchpad . SetMinimizeToTray ( e . checked ) } , { type : "checkbox" , label : "Run at startup" , checked : Lunchpad . runAtStartup , click : ( e ) => Lunchpad . SetRunAtStartup ( e . checked ) } , { type : "separator" } , { label : "Stop all running macros" , click : ( ) => Lunchpad . mainWindow . webContents . send ( ipcLabels . macros . stopAll ) , } , { type : "separator" } , { label : 'Quit' , click : ( ) => { Lunchpad . isQuiting = true ; Lunchpad . application . quit ( ) ; } } ] ) ) ; Lunchpad . tray . on ( 'double-click' , ( ) => Lunchpad . mainWindow . show ( ) ) ; Lunchpad . mainWindow . on ( 'minimize' , ( event ) => { if ( Lunchpad . minimizeToTray ) { event . preventDefault ( ) ; Lunchpad . mainWindow . hide ( ) ; } } ) ; Lunchpad . mainWindow . on ( 'close' , ( event ) => { if ( ! Lunchpad . isQuiting ) { event . returnValue = true ; event . preventDefault ( ) ; Lunchpad . mainWindow . hide ( ) ; } else { Lunchpad . mainWindow = null ; } } ) ; Lunchpad . mainWindow . once ( 'ready-to-show' , ( ) => { Lunchpad . mainWindow . show ( ) ; } ) ; } private static loadMainWindow ( ) { if ( Lunchpad . isDevelopmentMode ( ) ) { Lunchpad . mainWindow . loadURL ( `http://localhost:${ rendererAppPort }` ) ; } else { Lunchpad . mainWindow . loadURL ( format ( { pathname : join ( __dirname , '..' , rendererAppName , 'index.html' ) , protocol : 'file:' , slashes : true } ) ) ; } } static main ( app , browserWindow ) { Lunchpad . Launcher = new AutoLaunch ( { name : "Lunchpad" } ) ; Lunchpad . stayOnTop = config . get ( 'stayOnTop' ) ; Lunchpad . minimizeToTray = config . get ( 'minimizeToTray' ) ; Lunchpad . Launcher . isEnabled ( ) . then ( isEnabled => Lunchpad . runAtStartup = isEnabled ) ; Lunchpad . BrowserWindow = browserWindow ; Lunchpad . application = app ; Lunchpad . application . on ( 'window-all-closed' , Lunchpad . onWindowAllClosed ) ; Lunchpad . application . on ( 'ready' , Lunchpad . onReady ) ; Lunchpad . application . on ( 'activate' , Lunchpad . onActivate ) ; } }