2019-10-24 15:37:33 +09:00
|
|
|
import { ipcRenderer, remote, IpcRendererEvent } from 'electron';
|
2019-09-18 15:02:21 +09:00
|
|
|
|
2019-10-24 15:37:33 +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';
|
2019-10-24 15:37:33 +09:00
|
|
|
import { share } from 'rxjs/operators';
|
2019-09-18 15:02:21 +09:00
|
|
|
|
|
|
|
export class ElectronNativeService implements NativeService {
|
2019-10-24 15:37:33 +09:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:37:33 +09:00
|
|
|
readFile(path: string): Observable<Buffer> {
|
|
|
|
return new Observable<Buffer>(subscriber => {
|
2019-09-24 09:03:36 +09:00
|
|
|
try {
|
|
|
|
subscriber.next(ipcRenderer.sendSync(Channel.readFile, path));
|
|
|
|
} catch (error) {
|
|
|
|
subscriber.error(error);
|
|
|
|
} finally {
|
|
|
|
subscriber.complete();
|
|
|
|
}
|
|
|
|
});
|
2019-09-18 15:02:21 +09:00
|
|
|
}
|
2019-09-24 09:03:36 +09:00
|
|
|
|
2019-11-07 11:37:33 +09:00
|
|
|
saveFile(
|
|
|
|
buffer: Buffer,
|
|
|
|
fileName: string,
|
|
|
|
path?: string
|
|
|
|
): Observable<string> {
|
|
|
|
return new Observable<string>(subscriber => {
|
2019-09-24 09:03:36 +09:00
|
|
|
try {
|
2019-11-07 11:37:33 +09:00
|
|
|
subscriber.next(
|
|
|
|
ipcRenderer.sendSync(Channel.saveFile, buffer, fileName, path)
|
|
|
|
);
|
2019-09-24 09:03:36 +09:00
|
|
|
} catch (error) {
|
|
|
|
subscriber.error(error);
|
|
|
|
} finally {
|
|
|
|
subscriber.complete();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-24 15:37:33 +09:00
|
|
|
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() {}
|
|
|
|
}
|