TeaSpeak-Client/modules/core/ui-loader/graphical.ts

135 lines
3.9 KiB
TypeScript
Raw Normal View History

2019-06-26 16:09:01 -04:00
import * as electron from "electron";
import * as path from "path";
import {screen} from "electron";
import {Arguments, process_args} from "../../shared/process-arguments";
import * as loader from "./loader";
import * as updater from "../app-updater";
export namespace ui {
let gui: electron.BrowserWindow;
let promise: Promise<String>;
let resolve: any;
let reject: any;
export function running() : boolean {
return promise !== undefined;
}
export function cancel() : boolean {
if(resolve)
resolve();
cleanup();
return true;
}
export function cleanup() {
if(gui) {
gui.destroy();
gui = undefined;
promise = undefined;
resolve = undefined;
reject = error => {
if(error)
console.error("Received error from loader after it had been closed... Error: %o", error);
};
}
}
async function load_files() {
const channel = await updater.selected_channel();
try {
const entry_point = await loader.load_files(channel, (status, index) => {
if(gui) {
gui.webContents.send('progress-update', index);
}
});
const resolved = () => {
resolve(entry_point);
promise = undefined;
resolve = undefined;
reject = error => {
if(error)
console.error("Received error from loader after it had been closed... Error: %o", error);
};
};
if(!process_args.has_flag(...Arguments.DISABLE_ANIMATION))
setTimeout(resolved, 250);
else
setImmediate(resolved);
} catch (error) {
throw error;
}
}
export function show_await_update() {
if(gui)
gui.webContents.send('await-update');
}
function spawn_gui(close_callback: () => any) {
console.log("Spawn window!");
const WINDOW_WIDTH = 340;
const WINDOW_HEIGHT = 400;
let bounds = screen.getPrimaryDisplay().bounds;
let x = (bounds.width - WINDOW_WIDTH) / 2;
let y = (bounds.height - WINDOW_HEIGHT) / 2;
let dev_tools = false;
gui = new electron.BrowserWindow({
width: dev_tools ? WINDOW_WIDTH + 1000 : WINDOW_WIDTH,
height: WINDOW_HEIGHT + (process.platform == "win32" ? 40 : 0),
frame: true,
resizable: dev_tools,
show: false,
autoHideMenuBar: true,
//frame: false,
webPreferences: {
webSecurity: false,
nodeIntegrationInWorker: true
}
});
gui.setMenu(null);
gui.loadFile(path.join(path.dirname(module.filename), "ui", "loading_screen.html"));
gui.on('closed', close_callback);
gui.on('ready-to-show', () => {
gui.show();
const call_loader = () => load_files().catch(reject);
if(!process_args.has_flag(...Arguments.DISABLE_ANIMATION))
setTimeout(call_loader, 1000);
else
setImmediate(call_loader);
if(dev_tools)
gui.webContents.openDevTools();
});
}
export async function execute_loader() : Promise<String> {
return promise = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject || (error => {
console.error("Failed to load UI files! Error: %o", error)
});
spawn_gui(() => reject(undefined));
});
}
export function preloading_page(entry_point: string) : string {
global["browser-root"] = entry_point; /* setup entry point */
return path.join(path.dirname(module.filename), "ui", "preload_page.html");
}
}