A fully free and unlocked alternative to Franz.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.9 KiB

  1. import {app, Menu, shell, Tray} from "electron";
  2. import Meta from "./Meta";
  3. import Config from "./Config";
  4. import Updater from "./Updater";
  5. import MainWindow from "./windows/MainWindow";
  6. import * as os from "os";
  7. export default class Application {
  8. private readonly devMode: boolean;
  9. private readonly config: Config;
  10. private readonly updater: Updater;
  11. private readonly mainWindow: MainWindow;
  12. private tray?: Tray;
  13. public constructor(devMode: boolean) {
  14. this.devMode = devMode;
  15. this.config = new Config();
  16. this.updater = new Updater(this.config, this);
  17. this.mainWindow = new MainWindow(this);
  18. }
  19. public async start(): Promise<void> {
  20. console.log('Starting app');
  21. this.setupSystemTray();
  22. this.mainWindow.setup();
  23. this.setupElectronTweaks();
  24. // Check for updates
  25. if (os.platform() === 'win32') {
  26. this.updater.checkAndPromptForUpdates(this.mainWindow.getWindow()).then(() => {
  27. console.log('Update check successful.');
  28. }).catch(console.error);
  29. }
  30. console.log('App started');
  31. }
  32. public async stop(): Promise<void> {
  33. this.mainWindow.teardown();
  34. }
  35. public getConfig(): Config {
  36. return this.config;
  37. }
  38. public getUpdater(): Updater {
  39. return this.updater;
  40. }
  41. public isDevMode(): boolean {
  42. return this.devMode;
  43. }
  44. private setupElectronTweaks() {
  45. // Open external links in default OS browser
  46. app.on('web-contents-created', (e, contents) => {
  47. if (contents.getType() === 'webview') {
  48. console.log('Setting external links to open in default OS browser');
  49. contents.on('new-window', (e, url) => {
  50. e.preventDefault();
  51. if (url.startsWith('https://')) {
  52. shell.openExternal(url).catch(console.error);
  53. }
  54. });
  55. }
  56. });
  57. // Disable unused features
  58. app.on('web-contents-created', (e, contents) => {
  59. contents.on('will-attach-webview', (e, webPreferences) => {
  60. delete webPreferences.preload;
  61. webPreferences.nodeIntegration = false;
  62. // TODO: Here would be a good place to filter accessed urls (params.src).
  63. // Also consider 'will-navigate' event on contents.
  64. });
  65. });
  66. }
  67. private setupSystemTray() {
  68. console.log('Loading system Tray');
  69. this.tray = new Tray(Meta.ICON_PATH);
  70. this.tray.setToolTip('Tabs');
  71. this.tray.setContextMenu(Menu.buildFromTemplate([
  72. {label: 'Tabs', enabled: false},
  73. {label: 'Open Tabs', click: () => this.mainWindow.getWindow().show()},
  74. {type: 'separator'},
  75. {label: 'Quit', role: 'quit'},
  76. ]));
  77. this.tray.on('click', () => this.mainWindow.toggle());
  78. }
  79. }