import { ipcRenderer, remote, IpcRendererEvent } from 'electron'; import { Observable, Subject } from 'rxjs'; import { NativeService, WindowState, NotiRequest } from '@ucap-webmessenger/native'; import { Channel } from '../types/channel.type'; import { share } from 'rxjs/operators'; export class ElectronNativeService implements NativeService { private windowStateChangedSubject: Subject | null = null; private windowStateChanged$: Observable | null = null; showNotify(noti: NotiRequest): void { ipcRenderer.send( Channel.showNotify, noti.roomSeq, noti.title, noti.contents, noti.image, noti.useSound, noti.interval ); } checkForUpdates(): Observable { return new Observable(subscriber => { try { subscriber.next(ipcRenderer.sendSync(Channel.checkForUpdates)); } catch (error) { subscriber.error(error); } finally { subscriber.complete(); } }); } showImageViewer(): void { ipcRenderer.send(Channel.showImageViewer); } readFile(path: string): Observable { return new Observable(subscriber => { try { subscriber.next(ipcRenderer.sendSync(Channel.readFile, path)); } catch (error) { subscriber.error(error); } finally { subscriber.complete(); } }); } saveFile(path: string, buf: ArrayBuffer): Observable { return new Observable(subscriber => { try { subscriber.next(ipcRenderer.sendSync(Channel.saveFile, path, buf)); } catch (error) { subscriber.error(error); } finally { subscriber.complete(); } }); } windowStateChanged(): Observable { if (!this.windowStateChangedSubject) { this.windowStateChangedSubject = new Subject(); this.windowStateChanged$ = this.windowStateChangedSubject .asObservable() .pipe(share()); } ipcRenderer.on( Channel.windowStateChanged, (event: IpcRendererEvent, windowState: WindowState) => { console.log('windowStateChanged', windowState); this.windowStateChangedSubject.next(windowState); } ); return this.windowStateChanged$; } windowClose(): void { const currentWindow = remote.getCurrentWindow(); if (!currentWindow) { return; } currentWindow.close(); } windowMinimize(): void { const currentWindow = remote.getCurrentWindow(); if (!currentWindow) { return; } currentWindow.minimize(); } windowMaximize(): void { const currentWindow = remote.getCurrentWindow(); if (!currentWindow) { return; } if (currentWindow.isMaximized()) { currentWindow.unmaximize(); } else { currentWindow.maximize(); } } constructor() {} }