2019-10-25 19:51:40 -04:00
|
|
|
import {BrowserWindow, Menu, MenuItem, MessageBoxOptions, app, dialog} from "electron";
|
|
|
|
import * as electron from "electron";
|
|
|
|
import * as winmgr from "./window";
|
|
|
|
import * as path from "path";
|
|
|
|
|
2020-04-01 19:19:55 -04:00
|
|
|
let app_references = 0;
|
|
|
|
export function reference_app() {
|
|
|
|
app_references++;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function unreference_app() {
|
|
|
|
app_references--;
|
|
|
|
test_app_should_exit();
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export let is_debug: boolean;
|
|
|
|
export let allow_dev_tools: boolean;
|
|
|
|
|
|
|
|
import {Arguments, parse_arguments, process_args} from "../shared/process-arguments";
|
|
|
|
import * as updater from "./app-updater";
|
|
|
|
import * as loader from "./ui-loader";
|
|
|
|
import * as crash_handler from "../crash_handler";
|
2020-07-28 14:01:25 -04:00
|
|
|
import * as url from "url";
|
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.
|
|
|
|
export let main_window: BrowserWindow = null;
|
|
|
|
|
|
|
|
function spawn_main_window(entry_point: string) {
|
|
|
|
// Create the browser window.
|
|
|
|
console.log("Spawning main window");
|
2020-04-01 19:19:55 -04:00
|
|
|
reference_app(); /* main browser window references the app */
|
2019-10-25 19:51:40 -04:00
|
|
|
main_window = new BrowserWindow({
|
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
|
|
|
|
minHeight: 600,
|
|
|
|
minWidth: 600,
|
|
|
|
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webSecurity: false,
|
|
|
|
nodeIntegrationInWorker: true,
|
|
|
|
nodeIntegration: true
|
|
|
|
},
|
|
|
|
icon: path.join(__dirname, "..", "..", "resources", "logo.ico")
|
|
|
|
});
|
|
|
|
|
|
|
|
main_window.webContents.on('devtools-closed', event => {
|
|
|
|
console.log("Dev tools destroyed!");
|
|
|
|
});
|
|
|
|
|
|
|
|
main_window.on('closed', () => {
|
2019-10-27 17:39:59 -04:00
|
|
|
app.releaseSingleInstanceLock();
|
2019-10-25 19:51:40 -04:00
|
|
|
require("./url-preview").close();
|
|
|
|
main_window = null;
|
2020-04-01 19:19:55 -04:00
|
|
|
|
|
|
|
unreference_app();
|
2019-10-25 19:51:40 -04:00
|
|
|
});
|
|
|
|
|
2020-07-28 14:01:25 -04:00
|
|
|
main_window.loadURL(url.pathToFileURL(loader.ui.preloading_page(entry_point)).toString());
|
2019-10-25 19:51:40 -04:00
|
|
|
|
|
|
|
main_window.once('ready-to-show', () => {
|
|
|
|
main_window.show();
|
|
|
|
winmgr.apply_bounds('main-window', main_window).then(() => {
|
|
|
|
winmgr.track_bounds('main-window', main_window);
|
|
|
|
|
|
|
|
main_window.focus();
|
|
|
|
loader.ui.cleanup();
|
|
|
|
if(allow_dev_tools && !main_window.webContents.isDevToolsOpened())
|
|
|
|
main_window.webContents.openDevTools();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
main_window.webContents.on('new-window', (event, url_str, frameName, disposition, options, additionalFeatures) => {
|
2020-08-07 19:03:54 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
main_window.webContents.on('crashed', event => {
|
|
|
|
console.error("UI thread crashed! Closing app!");
|
2020-04-01 19:19:55 -04:00
|
|
|
if(!process_args.has_flag(Arguments.DEBUG))
|
2019-10-25 19:51:40 -04:00
|
|
|
main_window.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-04 08:17:07 -04:00
|
|
|
function handle_ui_load_error(message: string) {
|
2019-10-25 19:51:40 -04:00
|
|
|
console.log("Caught loading error: %s", message);
|
|
|
|
//"A critical error happened while loading TeaClient!", "A critical error happened while loading TeaClient!<br>" + message
|
2020-04-01 19:19:55 -04:00
|
|
|
reference_app();
|
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-04-01 19:19:55 -04:00
|
|
|
}).then(unreference_app);
|
2019-10-25 19:51:40 -04:00
|
|
|
loader.ui.cancel();
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:19:55 -04:00
|
|
|
function test_app_should_exit() {
|
|
|
|
if(app_references > 0) return;
|
|
|
|
|
|
|
|
console.log("All windows have been closed, closing app.");
|
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
|
2019-10-25 19:51:40 -04:00
|
|
|
function init_listener() {
|
|
|
|
app.on('quit', () => {
|
2020-04-01 19:19:55 -04:00
|
|
|
console.debug("Shutting down app.");
|
2019-10-25 19:51:40 -04:00
|
|
|
crash_handler.finalize_handler();
|
|
|
|
loader.ui.cleanup();
|
2020-04-01 19:19:55 -04:00
|
|
|
console.log("App has been finalized.");
|
2019-10-25 19:51:40 -04:00
|
|
|
});
|
|
|
|
|
2020-04-01 19:19:55 -04:00
|
|
|
|
2019-10-25 19:51:40 -04:00
|
|
|
app.on('window-all-closed', () => {
|
2020-04-01 19:19:55 -04:00
|
|
|
console.log("All windows have been closed. App reference count: %d", app_references);
|
|
|
|
test_app_should_exit();
|
2019-10-25 19:51:40 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
// On macOS it's common to re-create a window in the app when the
|
|
|
|
// dock icon is clicked and there are no other windows open.
|
|
|
|
if (main_window === null) {
|
|
|
|
//spawn_loading_screen();
|
|
|
|
//createWindow()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
|
|
|
|
console.log("Allowing untrusted certificate for %o", url);
|
|
|
|
event.preventDefault();
|
|
|
|
callback(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function execute() {
|
|
|
|
console.log("Main app executed!");
|
|
|
|
|
|
|
|
parse_arguments();
|
|
|
|
is_debug = process_args.has_flag(...Arguments.DEBUG);
|
|
|
|
allow_dev_tools = process_args.has_flag(...Arguments.DEV_TOOLS);
|
|
|
|
if(is_debug) {
|
|
|
|
console.log("Enabled debug!");
|
|
|
|
console.log("Arguments: %o", process_args);
|
|
|
|
}
|
|
|
|
|
|
|
|
Menu.setApplicationMenu(null);
|
|
|
|
init_listener();
|
|
|
|
|
|
|
|
console.log("Setting up render backend");
|
|
|
|
require("./render-backend");
|
|
|
|
|
|
|
|
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-04-01 19:19:55 -04:00
|
|
|
reference_app(); /* 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
|
|
|
|
spawn_main_window(entry_point);
|
|
|
|
else {
|
2020-04-04 08:17:07 -04:00
|
|
|
handle_ui_load_error("Missing UI entry point");
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
2020-04-01 19:19:55 -04:00
|
|
|
unreference_app();
|
2020-04-04 08:17:07 -04:00
|
|
|
}).catch(handle_ui_load_error);
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|