TeaSpeak-Client/modules/renderer/ExternalModalHandler.ts

84 lines
2.7 KiB
TypeScript

import {AbstractExternalModalController} from "tc-shared/ui/react-elements/external-modal/Controller";
import {setExternalModalControllerFactory} from "tc-shared/ui/react-elements/external-modal";
import * as ipc from "tc-shared/ipc/BrowserIPC";
import * as log from "tc-shared/log";
import {LogCategory} from "tc-shared/log";
import * as loader from "tc-loader";
import {Stage} from "tc-loader";
import {BrowserWindow, remote} from "electron";
import {tr} from "tc-shared/i18n/localize";
import * as path from "path";
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(),
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true
},
icon: path.join(__dirname, "..", "..", "resources", "logo.ico"),
minWidth: 600,
minHeight: 300
});
const parameters = {
"loader-target": "manifest",
"chunk": "modal-external",
"modal-target": this.modalType,
"ipc-channel": this.ipcChannel.channelId,
"ipc-address": ipc.getInstance().getLocalAddress(),
//"disableGlobalContextMenu": is_debug ? 1 : 0,
//"loader-abort": is_debug ? 1 : 0,
};
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.show();
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();
}
}
loader.register_task(Stage.JAVASCRIPT_INITIALIZING, {
priority: 50,
name: "external modal controller factory setup",
function: async () => {
setExternalModalControllerFactory((modal, events, userData) => new ExternalModalController(modal, events, userData));
}
});