TeaSpeak-Client/modules/renderer/ExternalModalHandler.ts

109 lines
3.5 KiB
TypeScript

import {AbstractExternalModalController} from "tc-shared/ui/react-elements/external-modal/Controller";
import * as ipc from "tc-shared/ipc/BrowserIPC";
import * as log from "tc-shared/log";
import {LogCategory} from "tc-shared/log";
import {BrowserWindow, remote} from "electron";
import {tr} from "tc-shared/i18n/localize";
import * as path from "path";
import {Arguments, process_args} from "../shared/process-arguments";
import {Popout2ControllerMessages, PopoutIPCMessage} from "tc-shared/ui/react-elements/external-modal/IPCMessage";
import {loadWindowBounds, startTrackWindowBounds} from "../shared/window";
export class ExternalModalController extends AbstractExternalModalController {
private window: BrowserWindow;
constructor(a, b, c) {
super(a, b, c);
}
protected async spawnWindow(): Promise<boolean> {
if(this.window) {
return true;
}
this.window = new remote.BrowserWindow({
/* parent: remote.getCurrentWindow(), */ /* do not link them together */
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
},
icon: path.join(__dirname, "..", "..", "resources", "logo.ico"),
minWidth: 600,
minHeight: 300,
frame: false,
transparent: true,
show: true
});
loadWindowBounds("modal-" + this.modalType, this.window).then(() => {
startTrackWindowBounds("modal-" + this.modalType, this.window);
});
if(process_args.has_flag(Arguments.DEV_TOOLS))
this.window.webContents.openDevTools();
const parameters = {
"loader-target": "manifest",
"chunk": "modal-external",
"modal-target": this.modalType,
"ipc-channel": this.ipcChannel.channelId,
"ipc-address": ipc.getInstance().getLocalAddress(),
"loader-abort": 0,
"animation-short": 1
};
const baseUrl = location.origin + location.pathname + "?";
const url = baseUrl + Object.keys(parameters).map(e => e + "=" + encodeURIComponent(parameters[e])).join("&");
try {
await this.window.loadURL(url);
} catch (error) {
log.warn(LogCategory.GENERAL, tr("Failed to load external modal main page: %o"), error);
this.window.close();
this.window = undefined;
return false;
}
this.window.on("closed", () => {
this.window = undefined;
this.handleWindowClosed();
});
return true;
}
protected destroyWindow(): void {
if(this.window) {
this.window.close();
this.window = undefined;
}
}
protected focusWindow(): void {
this.window?.focus();
}
protected handleTypedIPCMessage<T extends Popout2ControllerMessages>(type: T, payload: PopoutIPCMessage[T]) {
super.handleTypedIPCMessage(type, payload);
switch (type) {
case "invoke-modal-action":
const data = payload as PopoutIPCMessage["invoke-modal-action"];
switch (data.action) {
case "close":
this.destroy();
break;
case "minimize":
this.window?.minimize();
break;
}
break;
case "hello-popout":
break;
}
}
}