120 lines
2.8 KiB
TypeScript
Raw Normal View History

import { ipcRenderer, remote, IpcRendererEvent } from 'electron';
2019-09-18 15:02:21 +09:00
import { Observable, Subject } from 'rxjs';
2019-09-19 10:40:16 +09:00
2019-11-01 11:25:54 +09:00
import {
NativeService,
WindowState,
NotiRequest
} from '@ucap-webmessenger/native';
2019-09-18 15:02:21 +09:00
import { Channel } from '../types/channel.type';
import { share } from 'rxjs/operators';
2019-09-18 15:02:21 +09:00
export class ElectronNativeService implements NativeService {
private windowStateChangedSubject: Subject<WindowState> | null = null;
private windowStateChanged$: Observable<WindowState> | null = null;
2019-11-01 11:25:54 +09:00
showNotify(noti: NotiRequest): void {
2019-09-18 15:02:21 +09:00
ipcRenderer.send(
Channel.showNotify,
2019-11-01 11:25:54 +09:00
noti.roomSeq,
noti.title,
noti.contents,
noti.image,
noti.useSound,
noti.interval
2019-09-18 15:02:21 +09:00
);
}
2019-09-19 10:40:16 +09:00
checkForUpdates(): Observable<boolean> {
return new Observable<boolean>(subscriber => {
2019-09-18 15:02:21 +09:00
try {
2019-09-19 10:40:16 +09:00
subscriber.next(ipcRenderer.sendSync(Channel.checkForUpdates));
2019-09-18 15:02:21 +09:00
} catch (error) {
2019-09-19 10:40:16 +09:00
subscriber.error(error);
} finally {
subscriber.complete();
2019-09-18 15:02:21 +09:00
}
});
}
showImageViewer(): void {
ipcRenderer.send(Channel.showImageViewer);
}
readFile(path: string): Observable<ArrayBuffer> {
return new Observable<ArrayBuffer>(subscriber => {
try {
subscriber.next(ipcRenderer.sendSync(Channel.readFile, path));
} catch (error) {
subscriber.error(error);
} finally {
subscriber.complete();
}
});
2019-09-18 15:02:21 +09:00
}
saveFile(path: string, buf: ArrayBuffer): Observable<boolean> {
return new Observable<boolean>(subscriber => {
try {
subscriber.next(ipcRenderer.sendSync(Channel.saveFile, path, buf));
} catch (error) {
subscriber.error(error);
} finally {
subscriber.complete();
}
});
}
windowStateChanged(): Observable<WindowState> {
if (!this.windowStateChangedSubject) {
this.windowStateChangedSubject = new Subject<WindowState>();
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();
}
}
2019-09-18 15:02:21 +09:00
constructor() {}
}