TeaSpeak-Client/modules/core/url-preview/index.ts

110 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-10-25 19:51:40 -04:00
import * as electron from "electron";
import * as path from "path";
2020-08-21 07:37:10 -04:00
import {loadWindowBounds, startTrackWindowBounds} from "../../shared/window";
2019-10-25 19:51:40 -04:00
let global_window: electron.BrowserWindow;
let global_window_promise: Promise<void>;
2020-10-05 09:10:49 -04:00
export async function closeURLPreview() {
2019-10-25 19:51:40 -04:00
while(global_window_promise) {
try {
await global_window_promise;
break;
} catch(error) {} /* error will be already logged */
}
2020-08-23 05:35:52 -04:00
2019-10-25 19:51:40 -04:00
if(global_window) {
global_window.close();
global_window = undefined;
global_window_promise = undefined;
}
}
2020-10-05 09:10:49 -04:00
export async function openURLPreview(url: string) {
2019-10-25 19:51:40 -04:00
while(global_window_promise) {
try {
await global_window_promise;
break;
} catch(error) {} /* error will be already logged */
}
2020-10-05 09:10:49 -04:00
2019-10-25 19:51:40 -04:00
if(!global_window) {
global_window_promise = (async () => {
global_window = new electron.BrowserWindow({
webPreferences: {
nodeIntegration: true,
webviewTag: true
},
center: true,
show: false,
2020-04-02 09:12:09 -04:00
minHeight: 400,
minWidth: 400
2019-10-25 19:51:40 -04:00
});
global_window.setMenuBarVisibility(false);
global_window.setMenu(null);
2020-07-28 14:01:25 -04:00
global_window.loadURL(require("url").pathToFileURL(path.join(__dirname, "html", "index.html")).toString()).then(() => {
2020-04-02 10:51:12 -04:00
//global_window.webContents.openDevTools();
2019-10-25 19:51:40 -04:00
});
global_window.on('close', event => {
global_window = undefined;
});
try {
2020-08-21 07:37:10 -04:00
await loadWindowBounds('url-preview', global_window);
startTrackWindowBounds('url-preview', global_window);
2019-10-25 19:51:40 -04:00
await new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject("timeout"), 5000);
global_window.on('ready-to-show', () => {
clearTimeout(timeout);
resolve();
});
});
} catch(error) {
console.warn("Failed to initialize preview window. Dont show preview! Error: %o", error);
throw "failed to initialize";
}
global_window.show();
})();
try {
await global_window_promise;
} catch(error) {
console.log("Failed to create preview window! Error: %o", error);
try {
global_window.close();
} finally {
global_window = undefined;
}
global_window_promise = undefined;
return;
}
}
console.log("Opening URL '%s' as preview.", url);
global_window.webContents.send('preview', url);
if(!global_window.isFocused())
global_window.focus();
}
electron.ipcMain.on('preview-action', (event, args) => {
const sender: electron.WebContents = event.sender;
if(!args || !args.action) {
console.warn("Received preview action without a valid action type!");
return;
}
if(args.action === "open-url") {
console.log("Opening " +args.url);
electron.shell.openExternal(args.url, {
activate: true
});
const browser = electron.BrowserWindow.fromWebContents(sender);
if(!browser)
console.warn("Failed to find browser handle");
else
browser.close();
}
});