import { Injectable , NgZone } from '@angular/core' ; import { remote } from 'electron' ; import { Observable } from 'rxjs' ; import { enterZone } from '../../libs/rx' ; const { BrowserWindow , dialog } = remote ; export enum NativeDialogProperties { OPEN_FILE = 1 , OPEN_DIRECTORY = 2 , MULTI_SELECTIONS = 4 , SHOW_HIDDEN_FILES = 8 } export const nativeDialogFileFilters = { ALL : { name : 'All' , extensions : [ '*' ] } , IMAGES : { name : 'Images' , extensions : [ 'png' , 'jpg' , 'jpeg' , 'svg' , 'gif' ] } , } ; export class NativeDialogConfig { /** [macOS] Message to display above input boxes. */ message ? : string ; /** Allows the dialog to attach itself to a parent window. */ isModal ? : boolean = false ; /** Specifies an array of file types that can be displayed or selected. */ fileFilters ? : { name : string ; extensions : string [ ] ; } [ ] = [ ] ; /** Contains which features the dialog should use. */ properties ? : NativeDialogProperties ; } export interface NativeDialogOpenResult { isSelected : boolean ; filePaths ? : string [ ] ; } /**
 * Service for using native dialog.
 */ @ Injectable ( ) export class NativeDialog { constructor ( private ngZone ) { } example showOpenDialog ( config ) { const _config = this . withDefaultConfig ( config ) ; return new Observable < NativeDialogOpenResult > ( ( observer ) => { dialog . showOpenDialog ( _config . isModal ? undefined : BrowserWindow . getFocusedWindow ( ) , { message : _config . message , filters : _config . fileFilters , properties : this . makeProperties ( _config ) , } , ( filePaths ) => { const result = { isSelected : filePaths !== undefined } ; if ( result . isSelected ) { result . filePaths = filePaths ; } observer . next ( result ) ; observer . complete ( ) ; } ) ; } ) . pipe ( enterZone ( this . ngZone ) ) ; } private makeProperties ( config ) { const properties = [ ] ; if ( config . properties & NativeDialogProperties . OPEN_FILE ) { properties . push ( 'openFile' ) ; } if ( config . properties & NativeDialogProperties . OPEN_DIRECTORY ) { properties . push ( 'openDirectory' ) ; } if ( config . properties & NativeDialogProperties . MULTI_SELECTIONS ) { properties . push ( 'multiSelections' ) ; } if ( config . properties & NativeDialogProperties . SHOW_HIDDEN_FILES ) { properties . push ( 'showHiddenFiles' ) ; } return properties ; } private withDefaultConfig ( config ) { return { ... new NativeDialogConfig ( ) , ... config , } ; } }