TeaSpeak-Client/modules/renderer/ExternalModalHandler.ts
2021-02-06 19:42:29 +01:00

70 lines
2.6 KiB
TypeScript

import {AbstractExternalModalController} from "tc-shared/ui/react-elements/external-modal/Controller";
import {Popout2ControllerMessages, PopoutIPCMessage} from "tc-shared/ui/react-elements/external-modal/IPCMessage";
import {ExternalModal, kIPCChannelExternalModal} from "../shared/ipc/ExternalModal";
import {ObjectProxyClient} from "../shared/proxy/Client";
import {ProxiedClass} from "../shared/proxy/Definitions";
import {getIpcInstance} from "tc-shared/ipc/BrowserIPC";
import {ModalOptions} from "tc-shared/ui/react-elements/modal/Definitions";
const modalClient = new ObjectProxyClient<ExternalModal>(kIPCChannelExternalModal);
modalClient.initialize();
export class ExternalModalController extends AbstractExternalModalController {
private handle: ProxiedClass<ExternalModal> & ExternalModal;
constructor(modalType: string, constructorArguments?: any[], options?: ModalOptions) {
super(modalType, constructorArguments);
}
protected async spawnWindow(): Promise<boolean> {
if(!this.handle) {
this.handle = await modalClient.createNewInstance();
}
const parameters = {
"loader-target": "manifest",
"chunk": "modal-external",
"modal-target": this.modalType,
"ipc-channel": this.ipcChannel.channelId,
"ipc-address": getIpcInstance().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("&");
return await this.handle.spawnWindow(this.modalType, url);
}
protected destroyWindow(): void {
this.handle?.destroy();
this.handle = undefined;
}
protected focusWindow(): void {
this.handle?.focus().then(() => {});
}
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.handle?.minimize().then(() => {});
break;
}
break;
case "hello-popout":
break;
}
}
}