TeaSpeak-Client/modules/renderer/ExternalModalHandler.ts

70 lines
2.6 KiB
TypeScript
Raw Normal View History

import {AbstractExternalModalController} from "tc-shared/ui/react-elements/external-modal/Controller";
2020-08-21 07:37:10 -04:00
import {Popout2ControllerMessages, PopoutIPCMessage} from "tc-shared/ui/react-elements/external-modal/IPCMessage";
2020-08-23 15:26:27 -04:00
import {ExternalModal, kIPCChannelExternalModal} from "../shared/ipc/ExternalModal";
import {ObjectProxyClient} from "../shared/proxy/Client";
import {ProxiedClass} from "../shared/proxy/Definitions";
2020-12-13 08:51:10 -05:00
import {getIpcInstance} from "tc-shared/ipc/BrowserIPC";
2021-02-06 13:42:29 -05:00
import {ModalOptions} from "tc-shared/ui/react-elements/modal/Definitions";
2020-08-23 15:26:27 -04:00
const modalClient = new ObjectProxyClient<ExternalModal>(kIPCChannelExternalModal);
modalClient.initialize();
2020-08-21 07:37:10 -04:00
export class ExternalModalController extends AbstractExternalModalController {
2020-08-23 15:26:27 -04:00
private handle: ProxiedClass<ExternalModal> & ExternalModal;
2021-02-06 13:42:29 -05:00
constructor(modalType: string, constructorArguments?: any[], options?: ModalOptions) {
super(modalType, constructorArguments);
}
protected async spawnWindow(): Promise<boolean> {
2020-08-23 15:26:27 -04:00
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,
2020-12-13 08:51:10 -05:00
"ipc-address": getIpcInstance().getLocalAddress(),
2020-08-21 07:37:10 -04:00
"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("&");
2020-08-23 15:26:27 -04:00
return await this.handle.spawnWindow(this.modalType, url);
}
protected destroyWindow(): void {
2020-08-23 15:26:27 -04:00
this.handle?.destroy();
this.handle = undefined;
}
protected focusWindow(): void {
2020-08-23 15:26:27 -04:00
this.handle?.focus().then(() => {});
}
2020-08-21 07:37:10 -04:00
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":
2020-08-23 15:26:27 -04:00
this.handle?.minimize().then(() => {});
2020-08-21 07:37:10 -04:00
break;
}
break;
case "hello-popout":
break;
}
}
2020-08-21 07:37:10 -04:00
}