import { app , BrowserWindow , ipcMain , Menu , nativeImage , Tray , screen } from 'electron' ; import * as path from 'path' ; import * as url from 'url' ; import * as os from 'os' ; if ( os . arch ( ) === 'arm' ) { app . disableHardwareAcceleration ( ) ; } const buildForSidechain = false ; const daemonName = buildForSidechain ? 'Stratis.CirrusD' : 'Stratis.StratisD' ; const args = process . argv . slice ( 1 ) ; args . push ( '--enableSignalR' ) ; console . log ( args ) ; const serve = args . some ( val => val === '--serve' || val === '-serve' ) ; const testnet = args . some ( val => val === '--testnet' || val === '-testnet' ) ; let sidechain = args . some ( val => val === '--sidechain' || val === '-sidechain' ) ; let nodaemon = args . some ( val => val === '--nodaemon' || val === '-nodaemon' ) ; const devtools = args . some ( val => val === '--devtools' || val === '-devtools' ) ; if ( buildForSidechain ) { sidechain = true ; } const applicationName = sidechain ? 'Cirrus Core' : 'Stratis Core' ; let apiPortDefault ; if ( testnet && ! sidechain ) { apiPortDefault = 38221 ; } else if ( ! testnet && ! sidechain ) { apiPortDefault = 37221 ; } else if ( sidechain && testnet ) { apiPortDefault = 38223 ; } else if ( sidechain && ! testnet ) { apiPortDefault = 37223 ; } const coreargs = require ( 'minimist' ) ( args , { default : { daemonip : 'localhost' , apiport : apiPortDefault } , } ) ; const daemonIP = coreargs . daemonip ; const apiPort = coreargs . apiport ; if ( daemonIP !== 'localhost' ) { nodaemon = true ; } ipcMain . on ( 'get-port' , ( event ) => { event . returnValue = apiPort ; } ) ; ipcMain . on ( 'get-testnet' , ( event ) => { event . returnValue = testnet ; } ) ; ipcMain . on ( 'get-sidechain' , ( event ) => { event . returnValue = sidechain ; } ) ; ipcMain . on ( 'get-daemonip' , ( event ) => { event . returnValue = daemonIP ; } ) ; require ( 'electron-context-menu' ) ( { showInspectElement : serve } ) ; let mainWindow = null ; function writeLog ( msg ) { console . log ( msg ) ; } function createMenu ( ) { const menuTemplate = [ { label : app . getName ( ) , submenu : [ { label : 'About ' + app . getName ( ) , selector : 'orderFrontStandardAboutPanel:' } , { label : 'Quit' , accelerator : 'Command+Q' , click : function ( ) : void { app . quit ( ) ; } } ] } , { label : 'Edit' , submenu : [ { label : 'Undo' , accelerator : 'CmdOrCtrl+Z' , selector : 'undo:' } , { label : 'Redo' , accelerator : 'Shift+CmdOrCtrl+Z' , selector : 'redo:' } , { label : 'Cut' , accelerator : 'CmdOrCtrl+X' , selector : 'cut:' } , { label : 'Copy' , accelerator : 'CmdOrCtrl+C' , selector : 'copy:' } , { label : 'Paste' , accelerator : 'CmdOrCtrl+V' , selector : 'paste:' } , { label : 'Select All' , accelerator : 'CmdOrCtrl+A' , selector : 'selectAll:' } ] } ] ; Menu . setApplicationMenu ( Menu . buildFromTemplate ( menuTemplate ) ) ; } function shutdownDaemon ( daemonAddr , portNumber ) { const http = require ( 'http' ) ; const body = JSON . stringify ( { } ) ; const request = new http . ClientRequest ( { method : 'POST' , hostname : daemonAddr , port : portNumber , path : '/api/node/shutdown' , headers : { 'Content-Type' : 'application/json' , 'Content-Length' : Buffer . byteLength ( body ) } } ) ; request . write ( 'true' ) ; request . on ( 'error' , function ( ) { } ) ; request . on ( 'timeout' , function ( ) { request . abort ( ) ; } ) ; request . on ( 'uncaughtException' , function ( ) { request . abort ( ) ; } ) ; request . end ( body ) ; } function startDaemon ( ) { const spawnDaemon = require ( 'child_process' ) . spawn ; let daemonPath ; if ( os . platform ( ) === 'win32' ) { daemonPath = path . resolve ( __dirname , '..\\..\\resources\\daemon\\' + daemonName + '.exe' ) ; } else if ( os . platform ( ) === 'linux' ) { daemonPath = path . resolve ( __dirname , '..//..//resources//daemon//' + daemonName ) ; } else { daemonPath = path . resolve ( __dirname , '..//..//resources//daemon//' + daemonName ) ; } const spawnArgs = args . filter ( arg => arg . startsWith ( '-' ) ) . join ( '&' ) . replace ( /--/g , '-' ) . split ( '&' ) ; console . log ( 'Starting daemon ' + daemonPath ) ; console . log ( spawnArgs ) ; const daemonProcess = spawnDaemon ( daemonPath , spawnArgs , { detached : true } ) ; daemonProcess . stdout . on ( 'data' , ( data ) => { writeLog ( `Stratis: ${ data }` ) ; } ) ; } function createTray ( ) { let iconPath = 'stratis/icon-16.png' ; if ( sidechain ) { iconPath = 'cirrus/icon-16.png' ; } let trayIcon ; if ( serve ) { trayIcon = nativeImage . createFromPath ( './src/assets/images/' + iconPath ) ; } else { trayIcon = nativeImage . createFromPath ( path . resolve ( __dirname , '../../resources/src/assets/images/' + iconPath ) ) ; } const systemTray = new Tray ( trayIcon ) ; const contextMenu = Menu . buildFromTemplate ( [ { label : 'Hide/Show' , click : function ( ) : void { mainWindow . isVisible ( ) ? mainWindow . hide ( ) : mainWindow . show ( ) ; } } , { label : 'Exit' , click : function ( ) : void { app . quit ( ) ; } } ] ) ; systemTray . setToolTip ( applicationName ) ; systemTray . setContextMenu ( contextMenu ) ; systemTray . on ( 'click' , function ( ) { if ( ! mainWindow . isVisible ( ) ) { mainWindow . show ( ) ; } if ( ! mainWindow . isFocused ( ) ) { mainWindow . focus ( ) ; } } ) ; app . on ( 'window-all-closed' , function ( ) { if ( systemTray ) { systemTray . destroy ( ) ; } } ) ; } function createWindow ( ) { const height = screen . getPrimaryDisplay ( ) . bounds . height - 100 ; const width = Math . round ( height * 1.1 ) ; console . log ( height ) ; console . log ( width ) ; mainWindow = new BrowserWindow ( { width : width , height : height , frame : true , minWidth : 900 , minHeight : 800 , title : applicationName , webPreferences : { nodeIntegration : true , } , } ) ; if ( serve ) { require ( 'electron-reload' ) ( __dirname , { } ) ; mainWindow . loadURL ( 'http://localhost:4200' ) ; } else { mainWindow . loadURL ( url . format ( { pathname : path . join ( __dirname , 'dist/index.html' ) , protocol : 'file:' , slashes : true } ) ) ; } if ( serve || devtools ) { mainWindow . webContents . openDevTools ( ) ; } mainWindow . on ( 'close' , ( ) => { if ( ! serve && ! nodaemon ) { shutdownDaemon ( daemonIP , apiPort ) ; } } ) ; mainWindow . on ( 'closed' , ( ) => { mainWindow = null ; } ) ; mainWindow . removeMenu ( ) ; } app . on ( 'ready' , ( ) => { if ( serve ) { console . log ( 'Stratis UI was started in development mode. This requires the user to be running the Stratis Full Node Daemon himself.' ) ; } else { if ( ! nodaemon ) { startDaemon ( ) ; } } createTray ( ) ; createWindow ( ) ; if ( os . platform ( ) === 'darwin' ) { createMenu ( ) ; } } ) ; app . on ( 'before-quit' , ( ) => { if ( ! serve && ! nodaemon ) { shutdownDaemon ( daemonIP , apiPort ) ; } } ) ; app . on ( 'quit' , ( ) => { if ( ! serve && ! nodaemon ) { shutdownDaemon ( daemonIP , apiPort ) ; } } ) ; app . on ( 'window-all-closed' , ( ) => { app . quit ( ) ; } ) ; app . on ( 'activate' , ( ) => { if ( mainWindow === null ) { createWindow ( ) ; } } ) ;