1
0
mirror of https://github.com/bitwarden/browser synced 2026-01-09 20:13:42 +00:00

more tray icon options

This commit is contained in:
Kyle Spearrin
2018-05-05 00:29:02 -04:00
parent 4fd597fc05
commit 9d969929de
8 changed files with 127 additions and 37 deletions

View File

@@ -21,6 +21,16 @@ export class MessagingMain {
break;
case 'updateAppMenu':
this.main.menuMain.updateApplicationMenuState(message.isAuthenticated, message.isLocked);
this.updateTrayMenu(message.isAuthenticated, message.isLocked);
break;
case 'showTray':
this.main.trayMain.showTray();
break;
case 'removeTray':
this.main.trayMain.removeTray();
break;
case 'hideToTray':
this.main.trayMain.hideToTray();
break;
default:
break;
@@ -42,4 +52,11 @@ export class MessagingMain {
});
}, SyncInterval);
}
private updateTrayMenu(isAuthenticated: boolean, isLocked: boolean) {
const lockNowTrayMenuItem = this.main.trayMain.contextMenu.getMenuItemById('lockNow');
if (lockNowTrayMenuItem != null) {
lockNowTrayMenuItem.enabled = isAuthenticated && !isLocked;
}
}
}

View File

@@ -1,21 +1,26 @@
import {
Menu,
MenuItem,
MenuItemConstructorOptions,
nativeImage,
Tray,
} from 'electron';
import * as path from 'path';
import { I18nService } from 'jslib/abstractions/i18n.service';
import { StorageService } from 'jslib/abstractions/storage.service';
import { ElectronConstants } from 'jslib/electron/electronConstants';
import { WindowMain } from 'jslib/electron/window.main';
import { DesktopConstants } from '../desktopConstants';
export class TrayMain {
contextMenu: Menu;
private tray: Tray;
private menu: Menu;
private icon: string | Electron.NativeImage;
private pressedIcon: Electron.NativeImage;
constructor(private windowMain: WindowMain, private appName: string, private minToTray: () => Promise<boolean>) {
constructor(private windowMain: WindowMain, private i18nService: I18nService,
private storageService: StorageService, private appName: string) {
if (process.platform === 'win32') {
this.icon = path.join(__dirname, '/images/icon.ico');
} else if (process.platform === 'darwin') {
@@ -28,52 +33,91 @@ export class TrayMain {
}
}
init() {
if (process.platform === 'linux') {
this.menu = Menu.buildFromTemplate([{
label: this.appName,
click: () => this.open(),
}]);
async init(additionalMenuItems: MenuItemConstructorOptions[] = null) {
const menuItemOptions: MenuItemConstructorOptions[] = [{
label: this.appName,
click: () => this.toggleWindow(),
},
{ type: 'separator' },
{
label: this.i18nService.t('exit'),
click: () => this.closeWindow(),
}];
if (additionalMenuItems != null) {
menuItemOptions.splice(1, 0, ...additionalMenuItems);
}
this.contextMenu = Menu.buildFromTemplate(menuItemOptions);
if (await this.storageService.get<boolean>(ElectronConstants.enableTrayKey)) {
this.showTray();
}
this.windowMain.win.on('minimize', async (e: Event) => {
if (await this.minToTray()) {
if (await this.storageService.get<boolean>(ElectronConstants.enableMinimizeToTrayKey)) {
e.preventDefault();
await this.handleHideEvent();
await this.hideToTray();
}
});
this.windowMain.win.on('show', async (e: Event) => {
await this.handleShowEvent();
const enableTray = await this.storageService.get<boolean>(ElectronConstants.enableTrayKey);
if (!enableTray) {
await this.removeTray(false);
}
});
}
private handleShowEvent() {
removeTray(showWindow = true) {
if (this.tray != null) {
this.tray.destroy();
this.tray = null;
}
if (this.windowMain.win != null && !this.windowMain.win.isVisible()) {
this.windowMain.win.show();
}
}
private handleHideEvent() {
hideToTray() {
this.showTray();
if (this.windowMain.win != null) {
this.windowMain.win.hide();
}
}
showTray() {
if (this.tray != null) {
return;
}
this.tray = new Tray(this.icon);
this.tray.setToolTip(this.appName);
this.tray.on('click', () => this.toggleWindow());
if (this.pressedIcon != null) {
this.tray.setPressedImage(this.pressedIcon);
}
if (this.menu != null) {
this.tray.setContextMenu(this.menu);
if (this.contextMenu != null) {
this.tray.setContextMenu(this.contextMenu);
}
this.tray.on('click', () => open());
this.windowMain.win.hide();
}
private open() {
private toggleWindow() {
if (this.windowMain.win == null) {
return;
}
if (this.windowMain.win.isVisible()) {
this.windowMain.win.hide();
} else {
this.windowMain.win.show();
}
}
private closeWindow() {
if (this.windowMain.win != null) {
this.windowMain.win.close();
}
}
}