Improved the external modal support
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
import {ObjectProxyServer} from "../../shared/proxy/Server";
|
||||
import {ExternalModal, kIPCChannelExternalModal} from "../../shared/ipc/ExternalModal";
|
||||
import {ProxiedClass} from "../../shared/proxy/Definitions";
|
||||
import {BrowserWindow, dialog} from "electron";
|
||||
import {loadWindowBounds, startTrackWindowBounds} from "../../shared/window";
|
||||
import {Arguments, process_args} from "../../shared/process-arguments";
|
||||
import {open_preview} from "../url-preview";
|
||||
import * as path from "path";
|
||||
|
||||
class ProxyImplementation extends ProxiedClass<ExternalModal> implements ExternalModal {
|
||||
private windowInstance: BrowserWindow;
|
||||
|
||||
public constructor(props) {
|
||||
super(props);
|
||||
}
|
||||
|
||||
async focus(): Promise<any> {
|
||||
this.windowInstance?.focusOnWebView();
|
||||
}
|
||||
|
||||
async minimize(): Promise<any> {
|
||||
this.windowInstance?.minimize();
|
||||
}
|
||||
|
||||
async spawnWindow(modalTarget: string, url: string): Promise<boolean> {
|
||||
if(this.windowInstance) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.windowInstance = new 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-" + modalTarget, this.windowInstance).then(() => {
|
||||
startTrackWindowBounds("modal-" + modalTarget, this.windowInstance);
|
||||
});
|
||||
|
||||
this.windowInstance.webContents.on('new-window', (event, url_str, frameName, disposition, options, additionalFeatures) => {
|
||||
console.error("Open: %O", frameName);
|
||||
if(frameName.startsWith("__modal_external__")) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
try {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(url_str);
|
||||
} catch(error) {
|
||||
throw "failed to parse URL";
|
||||
}
|
||||
|
||||
{
|
||||
let protocol = url.protocol.endsWith(":") ? url.protocol.substring(0, url.protocol.length - 1) : url.protocol;
|
||||
if(protocol !== "https" && protocol !== "http") {
|
||||
throw "invalid protocol (" + protocol + "). HTTP(S) are only supported!";
|
||||
}
|
||||
}
|
||||
|
||||
open_preview(url.toString());
|
||||
} catch(error) {
|
||||
console.error("Failed to open preview window for URL %s: %o", url_str, error);
|
||||
dialog.showErrorBox("Failed to open preview", "Failed to open preview URL: " + url_str + "\nError: " + error);
|
||||
}
|
||||
});
|
||||
|
||||
if(process_args.has_flag(Arguments.DEV_TOOLS))
|
||||
this.windowInstance.webContents.openDevTools();
|
||||
|
||||
try {
|
||||
await this.windowInstance.loadURL(url);
|
||||
} catch (error) {
|
||||
console.error("Failed to load external modal main page: %o", error);
|
||||
this.windowInstance.close();
|
||||
this.windowInstance = undefined;
|
||||
return false;
|
||||
}
|
||||
|
||||
this.windowInstance.on("closed", () => {
|
||||
this.windowInstance = undefined;
|
||||
this.events.onClose();
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
destroy() {
|
||||
if(!this.windowInstance) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.windowInstance.close();
|
||||
this.windowInstance = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
const server = new ObjectProxyServer<ExternalModal>(kIPCChannelExternalModal, ProxyImplementation);
|
||||
server.initialize();
|
||||
@@ -8,6 +8,10 @@ import {open as open_changelog} from "../app-updater/changelog";
|
||||
import * as updater from "../app-updater";
|
||||
import {execute_connect_urls} from "../instance_handler";
|
||||
import {process_args} from "../../shared/process-arguments";
|
||||
import {open_preview} from "../url-preview";
|
||||
import {dialog} from "electron";
|
||||
|
||||
import "./ExternalModal";
|
||||
|
||||
ipcMain.on('basic-action', (event, action, ...args: any[]) => {
|
||||
const window = BrowserWindow.fromWebContents(event.sender);
|
||||
|
||||
Reference in New Issue
Block a user