import { Injectable } from '@angular/core' ; import * as electron from 'electron' ; import { IPCInvokeEvents , IPCEmitEvents } from '../../../electron/shared/IPCHandler' ; @ Injectable ( { providedIn : 'root' } ) export class ElectronService { electron : typeof electron ; get isElectron ( ) { return ! ! ( window && window . process && window . process . type ) ; } constructor ( ) { if ( this . isElectron ) { this . electron = window . require ( 'electron' ) ; this . receiveIPC ( 'log' , results => results . forEach ( result => console . log ( result ) ) ) ; } } get currentWindow ( ) { return this . electron . remote . getCurrentWindow ( ) ; } param event param data returns async invoke < E extends keyof IPCInvokeEvents > ( event , data ) { return this . electron . ipcRenderer . invoke ( event , data ) as Promise < IPCInvokeEvents [ E ] [ 'output' ] > ; } param event param data sendIPC < E extends keyof IPCEmitEvents > ( event , data ) { this . electron . ipcRenderer . send ( event , data ) ; } param event param callback receiveIPC < E extends keyof IPCEmitEvents > ( event , callback ) { this . electron . ipcRenderer . on ( event , ( _event , ... results ) => { callback ( results [ 0 ] ) ; } ) ; } quit ( ) { this . electron . remote . app . exit ( ) ; } openFolder ( filepath ) { this . electron . shell . openPath ( filepath ) ; } showFolder ( filepath ) { this . electron . shell . showItemInFolder ( filepath ) ; } showOpenDialog ( options ) { return this . electron . remote . dialog . showOpenDialog ( this . currentWindow , options ) ; } get defaultSession ( ) { return this . electron . remote . session . defaultSession ; } }