diff --git a/electron/electronAPI.d.ts b/electron/electronAPI.d.ts index d6c2dfb15e..3fb401b737 100644 --- a/electron/electronAPI.d.ts +++ b/electron/electronAPI.d.ts @@ -6,6 +6,7 @@ export interface ElectronAPI { // IPC STUFF ipcEvent$(evName: string): Observable; + send(channel: string, ...args: any[]): void; invoke(channel: string, ...args: any[]): Promise; @@ -26,4 +27,16 @@ export interface ElectronAPI { openPath(path: string): Promise; openExternal(url: string, options?: OpenExternalOptions): Promise; + + isMacOS(): boolean; + + // TODO implement + reloadMainWin(): void; + + openDevTools(): void; + + relaunch(): void; + + exit(exitCode: number): void; + isSystemDarkMode(): boolean; } diff --git a/electron/electronAPI.ts b/electron/electronAPI.ts index a920a6fc63..f4ff6b9043 100644 --- a/electron/electronAPI.ts +++ b/electron/electronAPI.ts @@ -1,12 +1,15 @@ import { + app, IpcRenderer, ipcRenderer, IpcRendererEvent, + nativeTheme, OpenExternalOptions, shell, webFrame, } from 'electron'; import { ElectronAPI } from './electronAPI.d'; +import { getWin } from './main-window'; export const electronAPI: Partial = { // TODO use full interface @@ -32,4 +35,10 @@ export const electronAPI: Partial = { getZoomFactor: () => webFrame.getZoomFactor(), openPath: (path: string) => shell.openPath(path), openExternal: (url: string, options?: OpenExternalOptions) => shell.openExternal(url), + isMacOS: () => process.platform === 'darwin', + reloadMainWin: () => getWin().reload(), + openDevTools: () => getWin().webContents.openDevTools(), + relaunch: () => app.relaunch(), + exit: (exitCode: number) => app.exit(exitCode), + isSystemDarkMode: () => nativeTheme.shouldUseDarkColors, }; diff --git a/src/app/core/electron/electron.service.ts b/src/app/core/electron/electron.service.ts index 73035fe6f0..d0ddc8e40d 100644 --- a/src/app/core/electron/electron.service.ts +++ b/src/app/core/electron/electron.service.ts @@ -29,8 +29,7 @@ const getResponseChannels = ( @Injectable({ providedIn: 'root' }) export class ElectronService { - ipcRenderer?: typeof ipcRenderer; - remote!: typeof remote; + private ipcRenderer?: typeof ipcRenderer; // fs: typeof fs; @@ -40,45 +39,7 @@ export class ElectronService { // this.ipcRenderer = electron.ipcRenderer; this.ipcRenderer = window.electronAPI.ipcRenderer(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.remote = getElectronRemoteModule()!; - // NOTE: works for non-sandboxed electron only - - // log to file for production - // if (environment.production || environment.stage) { - // const log = (this.remote as typeof remote).require('electron-log'); - // log.transports.maxSize = 1024 * 1024 * 20; - // console.error = log.error; - // console.log = log.log; - // console.warn = log.warn; - // } } - - // NOTE: useful in case we want to disable the node integration - // NOTE: global-error-handler.class.ts also needs to be adjusted - // this.ipcRenderer = { - // on: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void) => { - // }, - // send: () => { - // } - // }; - // this.webFrame = { - // setZoomFactor(factor: number): void { - // }, - // getZoomFactor: () => 1 - // }; - } - - public get isElectronApp(): boolean { - return !!window.navigator.userAgent.match(/Electron/); - } - - // TODO move to a better place - public get isMacOS(): boolean { - return this.isElectronApp && this.process && this.process.platform === 'darwin'; - } - - public get process(): any { - return this.remote ? this.remote.process : null; } public callMain(channel: string, data: unknown): Promise { diff --git a/src/app/core/electron/exec-before-close.service.ts b/src/app/core/electron/exec-before-close.service.ts index b47dba0965..dbd1247264 100644 --- a/src/app/core/electron/exec-before-close.service.ts +++ b/src/app/core/electron/exec-before-close.service.ts @@ -4,13 +4,10 @@ import { IPC } from '../../../../electron/shared-with-frontend/ipc-events.const' import { Observable, of } from 'rxjs'; import { map } from 'rxjs/operators'; import { IS_ELECTRON } from '../../app.constants'; -import { ipcRenderer } from 'electron'; import { ipcEvent$ } from '../../util/ipc-event'; @Injectable({ providedIn: 'root' }) export class ExecBeforeCloseService { - ipcRenderer: typeof ipcRenderer = this._electronService - .ipcRenderer as typeof ipcRenderer; onBeforeClose$: Observable = IS_ELECTRON ? ipcEvent$(IPC.NOTIFY_ON_CLOSE).pipe(map(([, ids]: any) => ids)) : of([]); @@ -18,14 +15,14 @@ export class ExecBeforeCloseService { constructor(private _electronService: ElectronService) {} schedule(id: string): void { - this.ipcRenderer.send(IPC.REGISTER_BEFORE_CLOSE, { id }); + window.electronAPI.send(IPC.REGISTER_BEFORE_CLOSE, { id }); } unschedule(id: string): void { - this.ipcRenderer.send(IPC.UNREGISTER_BEFORE_CLOSE, { id }); + window.electronAPI.send(IPC.UNREGISTER_BEFORE_CLOSE, { id }); } setDone(id: string): void { - this.ipcRenderer.send(IPC.BEFORE_CLOSE_DONE, { id }); + window.electronAPI.send(IPC.BEFORE_CLOSE_DONE, { id }); } } diff --git a/src/app/core/error-handler/global-error-handler.util.ts b/src/app/core/error-handler/global-error-handler.util.ts index 0babef1830..77af5bab05 100644 --- a/src/app/core/error-handler/global-error-handler.util.ts +++ b/src/app/core/error-handler/global-error-handler.util.ts @@ -105,7 +105,7 @@ export const createErrorAlert = ( btnReload.innerText = 'Reload App'; btnReload.addEventListener('click', () => { if (IS_ELECTRON) { - eSvc.remote.getCurrentWindow().webContents.reload(); + window.electronAPI.reloadMainWin(); } else { window.location.reload(); } @@ -155,7 +155,7 @@ export const createErrorAlert = ( }, 1500); if (IS_ELECTRON) { - eSvc.remote.getCurrentWindow().webContents.openDevTools(); + window.electronAPI.openDevTools(); } }; diff --git a/src/app/core/persistence/database.service.ts b/src/app/core/persistence/database.service.ts index f00b4c36d9..1da91fdda0 100644 --- a/src/app/core/persistence/database.service.ts +++ b/src/app/core/persistence/database.service.ts @@ -100,8 +100,8 @@ export class DatabaseService { private _restartApp(): void { if (IS_ELECTRON) { - this._electronService.remote.app.relaunch(); - this._electronService.remote.app.exit(0); + window.electronAPI.relaunch(); + window.electronAPI.exit(0); } else { window.location.reload(); } diff --git a/src/app/core/theme/global-theme.service.ts b/src/app/core/theme/global-theme.service.ts index 7ff535e7ce..36cb538d98 100644 --- a/src/app/core/theme/global-theme.service.ts +++ b/src/app/core/theme/global-theme.service.ts @@ -21,16 +21,16 @@ import { IS_MOUSE_PRIMARY, IS_TOUCH_PRIMARY } from '../../util/is-mouse-primary' @Injectable({ providedIn: 'root' }) export class GlobalThemeService { isDarkTheme$: Observable = - IS_ELECTRON && this._electronService.isMacOS + IS_ELECTRON && window.electronAPI.isMacOS() ? new Observable((subscriber) => { - subscriber.next(this._electronService.remote.nativeTheme.shouldUseDarkColors); - this._electronService.remote.systemPreferences.subscribeNotification( - 'AppleInterfaceThemeChangedNotification', - () => - subscriber.next( - this._electronService.remote.nativeTheme.shouldUseDarkColors, - ), - ); + subscriber.next(window.electronAPI.isSystemDarkMode()); + // this._electronService.remote.systemPreferences.subscribeNotification( + // 'AppleInterfaceThemeChangedNotification', + // () => + // subscriber.next( + // this._electronService.remote.nativeTheme.shouldUseDarkColors, + // ), + // ); }) : this._globalConfigService.misc$.pipe( map((cfg) => cfg.isDarkMode), @@ -50,7 +50,6 @@ export class GlobalThemeService { constructor( @Inject(DOCUMENT) private document: Document, private _materialCssVarsService: MaterialCssVarsService, - private _electronService: ElectronService, private _workContextService: WorkContextService, private _globalConfigService: GlobalConfigService, private _matIconRegistry: MatIconRegistry, diff --git a/src/app/features/issue/providers/jira/jira-api.service.ts b/src/app/features/issue/providers/jira/jira-api.service.ts index 8f51a37f7d..7b8a27d1ef 100644 --- a/src/app/features/issue/providers/jira/jira-api.service.ts +++ b/src/app/features/issue/providers/jira/jira-api.service.ts @@ -428,7 +428,7 @@ export class JiraApiService { }); const requestToSend = { requestId, requestInit, url }; - if (this._electronService.isElectronApp) { + if (IS_ELECTRON) { window.electronAPI.send(IPC.JIRA_MAKE_REQUEST_EVENT, { ...requestToSend, jiraCfg,