2021-09-25 11:22:14 +02:00
|
|
|
import {app, dialog, Menu, shell, Tray} from "electron";
|
2020-05-22 10:25:19 +02:00
|
|
|
import Meta from "./Meta";
|
|
|
|
import Config from "./Config";
|
|
|
|
import Updater from "./Updater";
|
|
|
|
import MainWindow from "./windows/MainWindow";
|
2020-06-15 14:48:52 +02:00
|
|
|
import * as os from "os";
|
2020-05-22 10:25:19 +02:00
|
|
|
|
|
|
|
export default class Application {
|
|
|
|
private readonly devMode: boolean;
|
|
|
|
private readonly config: Config;
|
|
|
|
private readonly updater: Updater;
|
|
|
|
private readonly mainWindow: MainWindow;
|
|
|
|
private tray?: Tray;
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
public constructor(devMode: boolean) {
|
2020-05-22 10:25:19 +02:00
|
|
|
this.devMode = devMode;
|
|
|
|
this.config = new Config();
|
2020-06-15 14:48:52 +02:00
|
|
|
this.updater = new Updater(this.config, this);
|
2020-05-22 10:25:19 +02:00
|
|
|
this.mainWindow = new MainWindow(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async start(): Promise<void> {
|
|
|
|
console.log('Starting app');
|
|
|
|
|
|
|
|
this.setupSystemTray();
|
|
|
|
this.mainWindow.setup();
|
|
|
|
this.setupElectronTweaks();
|
|
|
|
|
|
|
|
// Check for updates
|
2020-06-15 14:48:52 +02:00
|
|
|
if (os.platform() === 'win32') {
|
|
|
|
this.updater.checkAndPromptForUpdates(this.mainWindow.getWindow()).then(() => {
|
|
|
|
console.log('Update check successful.');
|
|
|
|
}).catch(console.error);
|
|
|
|
}
|
2020-05-22 10:25:19 +02:00
|
|
|
|
|
|
|
console.log('App started');
|
|
|
|
}
|
|
|
|
|
|
|
|
public async stop(): Promise<void> {
|
|
|
|
this.mainWindow.teardown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public getConfig(): Config {
|
|
|
|
return this.config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getUpdater(): Updater {
|
|
|
|
return this.updater;
|
|
|
|
}
|
|
|
|
|
|
|
|
public isDevMode(): boolean {
|
|
|
|
return this.devMode;
|
|
|
|
}
|
|
|
|
|
2021-09-25 11:22:14 +02:00
|
|
|
public async openExternalLink(url: string): Promise<void> {
|
|
|
|
if (url.startsWith('https://')) {
|
|
|
|
console.log('Opening link', url);
|
|
|
|
await shell.openExternal(url);
|
|
|
|
} else {
|
|
|
|
const {response} = await dialog.showMessageBox({
|
|
|
|
message: 'Are you sure you want to open this link?\n' + url,
|
|
|
|
type: 'question',
|
|
|
|
buttons: ['Cancel', 'Open link'],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response === 1) {
|
|
|
|
console.log('Opening link', url);
|
|
|
|
await shell.openExternal(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 10:25:19 +02:00
|
|
|
private setupElectronTweaks() {
|
|
|
|
// Open external links in default OS browser
|
|
|
|
app.on('web-contents-created', (e, contents) => {
|
|
|
|
if (contents.getType() === 'webview') {
|
|
|
|
console.log('Setting external links to open in default OS browser');
|
2021-09-25 11:22:14 +02:00
|
|
|
contents.setWindowOpenHandler(details => {
|
2022-03-17 12:56:40 +01:00
|
|
|
if (details.url.startsWith(details.referrer.url)) return {action: 'allow'};
|
|
|
|
|
2021-09-25 11:22:14 +02:00
|
|
|
const url = details.url;
|
|
|
|
this.openExternalLink(url)
|
|
|
|
.catch(console.error);
|
|
|
|
return {action: 'deny'};
|
2020-05-22 10:25:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-05-26 10:20:14 +02:00
|
|
|
|
|
|
|
// Disable unused features
|
|
|
|
app.on('web-contents-created', (e, contents) => {
|
2020-09-29 20:43:41 +02:00
|
|
|
contents.on('will-attach-webview', (e, webPreferences) => {
|
2020-05-26 10:20:14 +02:00
|
|
|
delete webPreferences.preload;
|
|
|
|
webPreferences.nodeIntegration = false;
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
// TODO: Here would be a good place to filter accessed urls (params.src).
|
|
|
|
// Also consider 'will-navigate' event on contents.
|
2020-05-26 10:20:14 +02:00
|
|
|
});
|
|
|
|
});
|
2020-05-22 10:25:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private setupSystemTray() {
|
|
|
|
console.log('Loading system Tray');
|
|
|
|
this.tray = new Tray(Meta.ICON_PATH);
|
|
|
|
this.tray.setToolTip('Tabs');
|
|
|
|
this.tray.setContextMenu(Menu.buildFromTemplate([
|
|
|
|
{label: 'Tabs', enabled: false},
|
|
|
|
{label: 'Open Tabs', click: () => this.mainWindow.getWindow().show()},
|
|
|
|
{type: 'separator'},
|
2020-09-29 20:43:41 +02:00
|
|
|
{label: 'Quit', role: 'quit'},
|
2020-05-22 10:25:19 +02:00
|
|
|
]));
|
|
|
|
this.tray.on('click', () => this.mainWindow.toggle());
|
|
|
|
}
|
2020-09-29 20:43:41 +02:00
|
|
|
}
|