import { ipcBackend } from './ipc' ; import { RC_Config } from '../shared/shared-types' ; import { setLogHandler } from '../shared/logger' ; import type { dialog , app , shell , clipboard } from 'electron' ; import { basename , join } from 'path' ; const { openExternal , openPath , write_clipboard_text , read_clipboard_text , app_getPath , } = ( window as any ) . electron_functions as { ipcRenderer : import ( 'electron' ) . IpcRenderer ; openExternal : typeof shell . openExternal ; openPath : typeof shell . openPath ; app_getPath : typeof app . getPath ; write_clipboard_text : typeof clipboard . writeText ; read_clipboard_text : typeof clipboard . readText ; } ; /**
 * Offers an abstraction Layer to make it easier to make browser client in the future
 */ interface Runtime { /**
     * initializes runtime stuff
     * - sets the LogHandler
     * - event listeners on runtime events
     */ initialize ( ) : void ; reloadWebContent ( ) : void ; openLogFile ( ) : void ; getCurrentLogLocation ( ) : string ; openHelpWindow ( ) : void ; updateBadge ( ) : void ; /**
     * get the comandline arguments
     */ getRC_Config ( ) : RC_Config ; param link openLink ( link ) : void ; showOpenFileDialog ( options ) : Promise < string | null > ; downloadFile ( pathToFile ) : Promise < void > ; transformBlobURL ( blob ) : string ; readClipboardText ( ) : Promise < string > ; writeClipboardText ( text ) : Promise < void > ; getAppPath ( name ) : string ; openPath ( path ) : Promise < string > ; } class Browser implements Runtime { openPath ( _path ) { throw new Error ( 'Method not implemented.' ) ; } getAppPath ( _name ) { throw new Error ( 'Method not implemented.' ) ; } downloadFile ( _pathToFile ) { throw new Error ( 'Method not implemented.' ) ; } async readClipboardText ( ) { throw new Error ( 'Method not implemented.' ) ; } writeClipboardText ( _text ) { throw new Error ( 'Method not implemented.' ) ; } transformBlobURL ( _blob ) { throw new Error ( 'Method not implemented.' ) ; } async showOpenFileDialog ( _options ) { throw new Error ( 'Method not implemented.' ) ; } openLink ( _link ) { throw new Error ( 'Method not implemented.' ) ; } initialize ( ) { throw new Error ( 'Method not implemented.' ) ; } getRC_Config ( ) { throw new Error ( 'Method not implemented.' ) ; } updateBadge ( ) { throw new Error ( 'Method not implemented.' ) ; } openHelpWindow ( ) { throw new Error ( 'Method not implemented.' ) ; } openLogFile ( ) { throw new Error ( 'Method not implemented.' ) ; } getCurrentLogLocation ( ) { return 'not implemented.' ; } reloadWebContent ( ) { window . location . reload ( ) ; } } class Electron implements Runtime { openPath ( path ) { return openPath ( path ) ; } getAppPath ( name ) { return app_getPath ( name ) ; } async downloadFile ( pathToFile ) { await ipcBackend . invoke ( 'saveFile' , pathToFile , { defaultPath : join ( app_getPath ( 'downloads' ) , basename ( pathToFile ) ) , } ) ; } readClipboardText ( ) { return Promise . resolve ( read_clipboard_text ( ) ) ; } writeClipboardText ( text ) { return Promise . resolve ( write_clipboard_text ( text ) ) ; } private rc_config : RC_Config | null = null ; transformBlobURL ( blob ) { return blob ; } async showOpenFileDialog ( options ) { const { filePaths } = await ( < ReturnType < typeof dialog . showOpenDialog > > ( ipcBackend . invoke ( 'fileChooser' , options ) ) ) ; return filePaths && filePaths [ 0 ] ; } openLink ( link ) { openExternal ( link ) ; } getRC_Config ( ) { if ( ! this . rc_config ) { this . rc_config = ipcBackend . sendSync ( 'get-rc-config' ) ; } return this . rc_config as RC_Config ; } initialize ( ) { setLogHandler ( ( ... args ) => { ipcBackend . send ( 'handleLogMessage' , ... args . map ( arg => { if ( typeof arg === 'object' ) { return JSON . parse ( JSON . stringify ( arg ) ) ; } else if ( typeof arg === 'function' ) { return arg . toString ( ) ; } else { return arg ; } } ) ) ; } , this . getRC_Config ( ) ) ; ipcBackend . on ( 'showHelpDialog' , this . openHelpWindow ) ; } openHelpWindow ( ) { ipcBackend . send ( 'help' , window . localeData . locale ) ; } openLogFile ( ) { openPath ( this . getCurrentLogLocation ( ) ) ; } getCurrentLogLocation ( ) { return ipcBackend . sendSync ( 'get-log-path' ) ; } reloadWebContent ( ) { ipcBackend . send ( 'reload-main-window' ) ; } updateBadge ( ) { ipcBackend . send ( 'update-badge' ) ; } } const IS_ELECTRON = true ; export const runtime = IS_ELECTRON ? new Electron ( ) : new Browser ( ) ; ( window as any ) . r = runtime ;