TeaSpeak-Client/modules/core/main-window/index.ts

173 lines
5.7 KiB
TypeScript
Raw Normal View History

2020-10-01 04:56:22 -04:00
import {BrowserWindow, app, dialog} from "electron";
2019-10-25 19:51:40 -04:00
import * as path from "path";
export let is_debug: boolean;
export let allow_dev_tools: boolean;
2020-10-01 04:56:22 -04:00
import {Arguments, processArguments} from "../../shared/process-arguments";
import * as updater from "./../app-updater";
import * as loader from "./../ui-loader";
2020-07-28 14:01:25 -04:00
import * as url from "url";
2020-10-01 04:56:22 -04:00
import {loadWindowBounds, startTrackWindowBounds} from "../../shared/window";
import {referenceApp, dereferenceApp} from "../AppInstance";
2019-10-25 19:51:40 -04:00
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
2020-10-01 04:56:22 -04:00
export let mainWindow: BrowserWindow = null;
function spawnMainWindow(rendererEntryPoint: string) {
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
console.log("Allowing untrusted certificate for %o", url);
event.preventDefault();
callback(true);
});
2019-10-25 19:51:40 -04:00
// Create the browser window.
console.log("Spawning main window");
2020-10-01 04:56:22 -04:00
referenceApp(); /* main browser window references the app */
mainWindow = new BrowserWindow({
2019-10-25 19:51:40 -04:00
width: 800,
height: 600,
minHeight: 600,
minWidth: 600,
show: false,
webPreferences: {
webSecurity: false,
nodeIntegrationInWorker: true,
2020-10-01 04:56:22 -04:00
nodeIntegration: true,
preload: path.join(__dirname, "preload.js")
2019-10-25 19:51:40 -04:00
},
2020-10-01 04:56:22 -04:00
icon: path.join(__dirname, "..", "..", "resources", "logo.ico"),
2019-10-25 19:51:40 -04:00
});
2020-10-01 04:56:22 -04:00
mainWindow.webContents.on('devtools-closed', () => {
2019-10-25 19:51:40 -04:00
console.log("Dev tools destroyed!");
});
2020-10-01 04:56:22 -04:00
mainWindow.on('closed', () => {
app.releaseSingleInstanceLock();
2020-10-01 04:56:22 -04:00
require("../url-preview").close();
mainWindow = null;
2020-04-01 19:19:55 -04:00
2020-10-01 04:56:22 -04:00
dereferenceApp();
2019-10-25 19:51:40 -04:00
});
2020-10-01 04:56:22 -04:00
mainWindow.loadURL(url.pathToFileURL(loader.ui.preloading_page(rendererEntryPoint)).toString()).catch(error => {
console.error("Failed to load UI entry point: %o", error);
handleUILoadingError("UI entry point failed to load");
});
2019-10-25 19:51:40 -04:00
2020-10-01 04:56:22 -04:00
mainWindow.once('ready-to-show', () => {
mainWindow.show();
loadWindowBounds('main-window', mainWindow).then(() => {
startTrackWindowBounds('main-window', mainWindow);
2019-10-25 19:51:40 -04:00
2020-10-01 04:56:22 -04:00
mainWindow.focus();
2019-10-25 19:51:40 -04:00
loader.ui.cleanup();
2020-10-01 04:56:22 -04:00
if(allow_dev_tools && !mainWindow.webContents.isDevToolsOpened())
mainWindow.webContents.openDevTools();
2019-10-25 19:51:40 -04:00
});
});
2020-10-01 04:56:22 -04:00
mainWindow.webContents.on('new-window', (event, url_str, frameName, disposition, options, additionalFeatures) => {
if(frameName.startsWith("__modal_external__")) {
return;
}
2019-10-25 19:51:40 -04:00
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!";
}
}
console.log("Got new window " + frameName);
const url_preview = require("./url-preview");
url_preview.open_preview(url_str);
} 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);
}
});
2020-10-01 04:56:22 -04:00
mainWindow.webContents.on('crashed', () => {
2019-10-25 19:51:40 -04:00
console.error("UI thread crashed! Closing app!");
2020-10-01 04:56:22 -04:00
if(!processArguments.has_flag(Arguments.DEBUG)) {
mainWindow.close();
}
2019-10-25 19:51:40 -04:00
});
}
2020-10-01 04:56:22 -04:00
function handleUILoadingError(message: string) {
referenceApp();
2019-10-25 19:51:40 -04:00
console.log("Caught loading error: %s", message);
2020-10-01 04:56:22 -04:00
if(mainWindow) {
mainWindow.close();
mainWindow = undefined;
}
2019-10-25 19:51:40 -04:00
dialog.showMessageBox({
type: "error",
buttons: ["exit"],
title: "A critical error happened while loading TeaClient!",
2020-04-02 05:13:44 -04:00
message: (message || "no error").toString()
2020-10-01 04:56:22 -04:00
}).then(dereferenceApp);
2019-10-25 19:51:40 -04:00
loader.ui.cancel();
}
export function execute() {
console.log("Main app executed!");
2020-10-01 04:56:22 -04:00
is_debug = processArguments.has_flag(...Arguments.DEBUG);
allow_dev_tools = processArguments.has_flag(...Arguments.DEV_TOOLS);
2019-10-25 19:51:40 -04:00
if(is_debug) {
console.log("Enabled debug!");
2020-10-01 04:56:22 -04:00
console.log("Arguments: %o", processArguments);
2019-10-25 19:51:40 -04:00
}
console.log("Setting up render backend");
2020-10-01 04:56:22 -04:00
require("../render-backend");
2019-10-25 19:51:40 -04:00
console.log("Spawn loading screen");
loader.ui.execute_loader().then(async (entry_point: string) => {
/* test if the updater may have an update found */
let awaiting_update_set = false;
while(updater.update_question_open) {
if(!awaiting_update_set) {
awaiting_update_set = true;
loader.ui.show_await_update();
console.log("Awaiting update stuff to be finished");
}
await new Promise(resolve => setTimeout(resolve, 100));
}
if(updater.update_restart_pending)
return undefined;
return entry_point;
}).then((entry_point: string) => {
2020-10-01 04:56:22 -04:00
referenceApp(); /* because we've no windows when we close the loader UI */
2019-10-25 19:51:40 -04:00
loader.ui.cleanup(); /* close the window */
if(entry_point) //has not been canceled
2020-10-01 04:56:22 -04:00
spawnMainWindow(entry_point);
2019-10-25 19:51:40 -04:00
else {
2020-10-01 04:56:22 -04:00
handleUILoadingError("Missing UI entry point");
2019-10-25 19:51:40 -04:00
}
2020-10-01 04:56:22 -04:00
dereferenceApp();
}).catch(handleUILoadingError);
2019-10-25 19:51:40 -04:00
}