2020-08-07 19:03:54 -04:00
|
|
|
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";
|
2020-08-21 07:37:10 -04:00
|
|
|
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";
|
2020-08-07 19:03:54 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
export class ExternalModalController extends AbstractExternalModalController {
|
2020-08-07 19:03:54 -04:00
|
|
|
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({
|
2020-08-09 09:03:52 -04:00
|
|
|
/* parent: remote.getCurrentWindow(), */ /* do not link them together */
|
2020-08-07 19:03:54 -04:00
|
|
|
autoHideMenuBar: true,
|
|
|
|
|
|
|
|
webPreferences: {
|
2020-08-21 07:37:10 -04:00
|
|
|
nodeIntegration: true,
|
2020-08-07 19:03:54 -04:00
|
|
|
},
|
|
|
|
icon: path.join(__dirname, "..", "..", "resources", "logo.ico"),
|
|
|
|
minWidth: 600,
|
2020-08-21 07:37:10 -04:00
|
|
|
minHeight: 300,
|
|
|
|
|
|
|
|
frame: false,
|
|
|
|
transparent: true,
|
|
|
|
|
|
|
|
show: true
|
2020-08-07 19:03:54 -04:00
|
|
|
});
|
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
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();
|
|
|
|
|
2020-08-07 19:03:54 -04:00
|
|
|
const parameters = {
|
|
|
|
"loader-target": "manifest",
|
|
|
|
"chunk": "modal-external",
|
|
|
|
"modal-target": this.modalType,
|
|
|
|
"ipc-channel": this.ipcChannel.channelId,
|
|
|
|
"ipc-address": ipc.getInstance().getLocalAddress(),
|
2020-08-21 07:37:10 -04:00
|
|
|
"loader-abort": 0,
|
|
|
|
"animation-short": 1
|
2020-08-07 19:03:54 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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":
|
|
|
|
this.window?.minimize();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "hello-popout":
|
|
|
|
break;
|
|
|
|
}
|
2020-08-07 19:03:54 -04:00
|
|
|
}
|
2020-08-21 07:37:10 -04:00
|
|
|
}
|