154 lines
3.7 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,
2019-11-09 17:29:02 +09:00
NotificationRequest,
WindowIdle
2019-11-01 11:25:54 +09:00
} from '@ucap-webmessenger/native';
import { share } from 'rxjs/operators';
2019-11-09 17:29:02 +09:00
import {
NotificationChannel,
UpdaterChannel,
FileChannel,
WindowStateChannel,
IdleStateChannel
} from '../types/channel.type';
2019-11-11 15:53:39 +09:00
import { Injectable } from '@angular/core';
2019-09-18 15:02:21 +09:00
2019-11-11 15:53:39 +09:00
@Injectable({
providedIn: 'root'
})
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;
private idleStateChangedSubject: Subject<WindowIdle> | null = null;
private idleStateChanged$: Observable<WindowIdle> | null = null;
2019-11-09 17:29:02 +09:00
notify(noti: NotificationRequest): void {
ipcRenderer.send(NotificationChannel.Notify, noti);
}
closeAllNotify(): void {
ipcRenderer.send(NotificationChannel.CloseAllNotify);
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-11-09 17:29:02 +09:00
subscriber.next(ipcRenderer.sendSync(UpdaterChannel.Check));
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 {
2019-11-09 17:29:02 +09:00
ipcRenderer.send(FileChannel.ShowImageViewer);
2019-09-18 15:02:21 +09:00
}
2019-11-07 11:37:33 +09:00
readFile(path: string): Observable<Buffer> {
return new Observable<Buffer>(subscriber => {
try {
2019-11-09 17:29:02 +09:00
subscriber.next(ipcRenderer.sendSync(FileChannel.ReadFile, path));
} catch (error) {
subscriber.error(error);
} finally {
subscriber.complete();
}
});
2019-09-18 15:02:21 +09:00
}
2019-11-07 11:37:33 +09:00
saveFile(
buffer: Buffer,
fileName: string,
path?: string
): Observable<string> {
return new Observable<string>(subscriber => {
try {
2019-11-07 11:37:33 +09:00
subscriber.next(
2019-11-09 17:29:02 +09:00
ipcRenderer.sendSync(FileChannel.SaveFile, buffer, fileName, path)
2019-11-07 11:37:33 +09:00
);
} 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(
2019-11-09 17:29:02 +09:00
WindowStateChannel.Changed,
(event: IpcRendererEvent, windowState: 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();
}
}
idleStateChanged(): Observable<WindowIdle> {
if (!this.idleStateChangedSubject) {
this.idleStateChangedSubject = new Subject<WindowIdle>();
this.idleStateChanged$ = this.idleStateChangedSubject
.asObservable()
.pipe(share());
}
2019-11-09 17:29:02 +09:00
ipcRenderer.send(IdleStateChannel.StartCheck);
ipcRenderer.on(
2019-11-09 17:29:02 +09:00
IdleStateChannel.Changed,
(event: IpcRendererEvent, idleState: WindowIdle) => {
this.idleStateChangedSubject.next(idleState);
}
);
return this.idleStateChanged$;
}
2019-09-18 15:02:21 +09:00
constructor() {}
}