import { clipboard , ipcRenderer , shell } from "electron" ; import { isDev , isMacAppStore } from "../utils" ; import { DeviceType } from "jslib-common/enums/deviceType" ; import { ThemeType } from "jslib-common/enums/themeType" ; import { I18nService } from "jslib-common/abstractions/i18n.service" ; import { MessagingService } from "jslib-common/abstractions/messaging.service" ; import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service" ; import { StateService } from "jslib-common/abstractions/state.service" ; export class ElectronPlatformUtilsService implements PlatformUtilsService { identityClientId : string ; private deviceCache : DeviceType = null ; constructor ( protected i18nService , private messagingService , private isDesktopApp , private stateService ) { this . identityClientId = isDesktopApp ? "desktop" : "connector" ; } getDevice ( ) { if ( ! this . deviceCache ) { switch ( process . platform ) { case "win32" : this . deviceCache = DeviceType . WindowsDesktop ; break ; case "darwin" : this . deviceCache = DeviceType . MacOsDesktop ; break ; case "linux" : default : this . deviceCache = DeviceType . LinuxDesktop ; break ; } } return this . deviceCache ; } getDeviceString ( ) { const device = DeviceType [ this . getDevice ( ) ] . toLowerCase ( ) ; return device . replace ( "desktop" , "" ) ; } isFirefox ( ) { return false ; } isChrome ( ) { return true ; } isEdge ( ) { return false ; } isOpera ( ) { return false ; } isVivaldi ( ) { return false ; } isSafari ( ) { return false ; } isIE ( ) { return false ; } isMacAppStore ( ) { return isMacAppStore ( ) ; } isViewOpen ( ) { return Promise . resolve ( false ) ; } launchUri ( uri , options ? ) { shell . openExternal ( uri ) ; } saveFile ( win , blobData , blobOptions , fileName ) { const blob = new Blob ( [ blobData ] , blobOptions ) ; const a = win . document . createElement ( "a" ) ; a . href = URL . createObjectURL ( blob ) ; a . download = fileName ; win . document . body . appendChild ( a ) ; a . click ( ) ; win . document . body . removeChild ( a ) ; } getApplicationVersion ( ) { return ipcRenderer . invoke ( "appVersion" ) ; } supportsWebAuthn ( win ) { return process . platform === "win32" ; } supportsDuo ( ) { return true ; } showToast ( type , title , text , options ? ) { this . messagingService . send ( "showToast" , { text : text , title : title , type : type , options : options , } ) ; } async showDialog ( text , title ? , confirmText ? , cancelText ? , type ? ) { const buttons = [ confirmText == null ? this . i18nService . t ( "ok" ) : confirmText ] ; if ( cancelText != null ) { buttons . push ( cancelText ) ; } const result = await ipcRenderer . invoke ( "showMessageBox" , { type : type , title : title , message : title , detail : text , buttons : buttons , cancelId : buttons . length === 2 ? 1 : null , defaultId : 0 , noLink : true , } ) ; return Promise . resolve ( result . response === 0 ) ; } isDev ( ) { return isDev ( ) ; } isSelfHost ( ) { return false ; } copyToClipboard ( text , options ? ) { const type = options ? options . type : null ; const clearing = options ? ! ! options . clearing : false ; const clearMs = options && options . clearMs ? options . clearMs : null ; clipboard . writeText ( text , type ) ; if ( ! clearing ) { this . messagingService . send ( "copiedToClipboard" , { clipboardValue : text , clearMs : clearMs , type : type , clearing : clearing , } ) ; } } readFromClipboard ( options ? ) { const type = options ? options . type : null ; return Promise . resolve ( clipboard . readText ( type ) ) ; } async supportsBiometric ( ) { return await this . stateService . getEnableBiometric ( ) ; } authenticateBiometric ( ) { return new Promise ( ( resolve ) => { const val = ipcRenderer . sendSync ( "biometric" , { action : "authenticate" , } ) ; resolve ( val ) ; } ) ; } getDefaultSystemTheme ( ) { return ipcRenderer . invoke ( "systemTheme" ) ; } onDefaultSystemThemeChange ( callback ) { ipcRenderer . on ( "systemThemeUpdated" , ( event , theme ) => callback ( theme ) ) ; } async getEffectiveTheme ( ) { const theme = await this . stateService . getTheme ( ) ; if ( theme == null || theme === ThemeType . System ) { return this . getDefaultSystemTheme ( ) ; } else { return theme ; } } supportsSecureStorage ( ) { return true ; } }