bitwarden-estensione-browser/src/electron/tray.main.ts

176 lines
5.3 KiB
TypeScript
Raw Normal View History

2018-05-08 18:05:00 +02:00
import {
app,
BrowserWindow,
2018-05-08 18:05:00 +02:00
Menu,
MenuItem,
MenuItemConstructorOptions,
nativeImage,
Tray,
} from 'electron';
import * as path from 'path';
import { I18nService } from '../abstractions/i18n.service';
import { StorageService } from '../abstractions/storage.service';
import { ElectronConstants } from './electronConstants';
import { WindowMain } from './window.main';
export class TrayMain {
contextMenu: Menu;
private appName: string;
private tray: Tray;
private icon: string | Electron.NativeImage;
private pressedIcon: Electron.NativeImage;
constructor(private windowMain: WindowMain, private i18nService: I18nService,
private storageService: StorageService) {
if (process.platform === 'win32') {
this.icon = path.join(__dirname, '/images/icon.ico');
} else if (process.platform === 'darwin') {
const nImage = nativeImage.createFromPath(path.join(__dirname, '/images/icon-template.png'));
nImage.setTemplateImage(true);
this.icon = nImage;
this.pressedIcon = nativeImage.createFromPath(path.join(__dirname, '/images/icon-highlight.png'));
} else {
this.icon = path.join(__dirname, '/images/icon.png');
}
}
async init(appName: string, additionalMenuItems: MenuItemConstructorOptions[] = null) {
this.appName = appName;
const menuItemOptions: MenuItemConstructorOptions[] = [{
label: this.i18nService.t('showHide'),
click: () => this.toggleWindow(),
},
{ type: 'separator' },
{
label: this.i18nService.t('exit'),
2018-05-08 18:05:00 +02:00
click: () => this.closeWindow(),
}];
if (additionalMenuItems != null) {
menuItemOptions.splice(1, 0, ...additionalMenuItems);
}
this.contextMenu = Menu.buildFromTemplate(menuItemOptions);
2018-05-08 18:05:00 +02:00
if (await this.storageService.get<boolean>(ElectronConstants.enableTrayKey)) {
this.showTray();
}
}
2018-05-08 18:05:00 +02:00
setupWindowListeners(win: BrowserWindow) {
win.on('minimize', async (e: Event) => {
if (await this.storageService.get<boolean>(ElectronConstants.enableMinimizeToTrayKey)) {
e.preventDefault();
this.hideToTray();
}
});
win.on('close', async (e: Event) => {
if (await this.storageService.get<boolean>(ElectronConstants.enableCloseToTrayKey)) {
if (!this.windowMain.isQuitting) {
2018-05-08 18:05:00 +02:00
e.preventDefault();
this.hideToTray();
}
}
});
win.on('show', async (e: Event) => {
2018-05-08 18:05:00 +02:00
const enableTray = await this.storageService.get<boolean>(ElectronConstants.enableTrayKey);
if (!enableTray) {
setTimeout(() => this.removeTray(false), 100);
2018-05-08 18:05:00 +02:00
}
});
}
removeTray(showWindow = true) {
if (this.tray != null) {
this.tray.destroy();
this.tray = null;
}
if (showWindow && this.windowMain.win != null && !this.windowMain.win.isVisible()) {
this.windowMain.win.show();
}
}
async hideToTray() {
2018-05-08 18:05:00 +02:00
this.showTray();
if (this.windowMain.win != null) {
this.windowMain.win.hide();
}
if (this.isDarwin() && !await this.storageService.get<boolean>(ElectronConstants.alwaysShowDock)) {
this.hideDock();
}
}
restoreFromTray() {
if (this.windowMain.win == null || !this.windowMain.win.isVisible()) {
this.toggleWindow();
}
2018-05-08 18:05:00 +02:00
}
showTray() {
if (this.tray != null) {
return;
}
this.tray = new Tray(this.icon);
this.tray.setToolTip(this.appName);
this.tray.on('click', () => this.toggleWindow());
this.tray.on('right-click', () => this.tray.popUpContextMenu(this.contextMenu));
2018-05-08 18:05:00 +02:00
if (this.pressedIcon != null) {
this.tray.setPressedImage(this.pressedIcon);
}
if (this.contextMenu != null && !this.isDarwin()) {
2018-05-08 18:05:00 +02:00
this.tray.setContextMenu(this.contextMenu);
}
}
private hideDock() {
app.dock.hide();
}
private showDock() {
app.dock.show();
}
private isDarwin() {
return process.platform === 'darwin';
}
private async toggleWindow() {
2019-05-16 04:47:58 +02:00
if (this.windowMain.win == null) {
if (this.isDarwin()) {
// On MacOS, closing the window via the red button destroys the BrowserWindow instance.
this.windowMain.createWindow().then(() => {
this.windowMain.win.show();
this.showDock();
});
}
2019-05-16 04:47:58 +02:00
return;
}
if (this.windowMain.win.isVisible()) {
this.windowMain.win.hide();
if (this.isDarwin() && !await this.storageService.get<boolean>(ElectronConstants.alwaysShowDock)) {
this.hideDock();
}
2018-05-08 18:05:00 +02:00
} else {
2019-05-16 04:47:58 +02:00
this.windowMain.win.show();
if (this.isDarwin()) {
this.showDock();
}
2018-05-08 18:05:00 +02:00
}
}
private closeWindow() {
this.windowMain.isQuitting = true;
2018-05-08 18:05:00 +02:00
if (this.windowMain.win != null) {
this.windowMain.win.close();
}
}
}