1
0
mirror of https://github.com/bitwarden/desktop.git synced 2024-11-24 11:55:50 +01:00

move tray to jslib

This commit is contained in:
Kyle Spearrin 2018-05-08 12:05:05 -04:00
parent f149f98145
commit 786549c2c2
3 changed files with 4 additions and 129 deletions

2
jslib

@ -1 +1 @@
Subproject commit e7779759f25d315d35b20ae85371ebe86fed3eef Subproject commit 9de9c1655c9a325443fdabfc74630f06e20f8d9a

View File

@ -10,7 +10,6 @@ import { I18nService } from './services/i18n.service';
import { MenuMain } from './main/menu.main'; import { MenuMain } from './main/menu.main';
import { MessagingMain } from './main/messaging.main'; import { MessagingMain } from './main/messaging.main';
import { PowerMonitorMain } from './main/powerMonitor.main'; import { PowerMonitorMain } from './main/powerMonitor.main';
import { TrayMain } from './main/tray.main';
import { ConstantsService } from 'jslib/services/constants.service'; import { ConstantsService } from 'jslib/services/constants.service';
@ -18,6 +17,7 @@ import { KeytarStorageListener } from 'jslib/electron/keytarStorageListener';
import { ElectronLogService } from 'jslib/electron/services/electronLog.service'; import { ElectronLogService } from 'jslib/electron/services/electronLog.service';
import { ElectronMainMessagingService } from 'jslib/electron/services/electronMainMessaging.service'; import { ElectronMainMessagingService } from 'jslib/electron/services/electronMainMessaging.service';
import { ElectronStorageService } from 'jslib/electron/services/electronStorage.service'; import { ElectronStorageService } from 'jslib/electron/services/electronStorage.service';
import { TrayMain } from 'jslib/electron/tray.main';
import { UpdaterMain } from 'jslib/electron/updater.main'; import { UpdaterMain } from 'jslib/electron/updater.main';
import { WindowMain } from 'jslib/electron/window.main'; import { WindowMain } from 'jslib/electron/window.main';
@ -78,7 +78,7 @@ export class Main {
}); });
this.menuMain = new MenuMain(this); this.menuMain = new MenuMain(this);
this.powerMonitorMain = new PowerMonitorMain(this); this.powerMonitorMain = new PowerMonitorMain(this);
this.trayMain = new TrayMain(this.windowMain, this.i18nService, this.storageService, 'Bitwarden'); this.trayMain = new TrayMain(this.windowMain, this.i18nService, this.storageService);
this.messagingService = new ElectronMainMessagingService(this.windowMain, (message) => { this.messagingService = new ElectronMainMessagingService(this.windowMain, (message) => {
this.messagingMain.onMessage(message); this.messagingMain.onMessage(message);
@ -95,7 +95,7 @@ export class Main {
this.messagingMain.init(); this.messagingMain.init();
this.menuMain.init(); this.menuMain.init();
this.powerMonitorMain.init(); this.powerMonitorMain.init();
this.trayMain.init([{ await this.trayMain.init('Bitwarden', [{
label: this.i18nService.t('lockNow'), label: this.i18nService.t('lockNow'),
enabled: false, enabled: false,
id: 'lockNow', id: 'lockNow',

View File

@ -1,125 +0,0 @@
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';
export class TrayMain {
contextMenu: Menu;
private tray: Tray;
private icon: string | Electron.NativeImage;
private pressedIcon: Electron.NativeImage;
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') {
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(additionalMenuItems: MenuItemConstructorOptions[] = null) {
const menuItemOptions: MenuItemConstructorOptions[] = [{
label: this.i18nService.t('showHide'),
click: () => this.toggleWindow(),
},
{ type: 'separator' },
{
label: process.platform === 'darwin' ? this.i18nService.t('close') : 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();
}
if (process.platform === 'win32') {
this.windowMain.win.on('minimize', async (e: Event) => {
if (await this.storageService.get<boolean>(ElectronConstants.enableMinimizeToTrayKey)) {
e.preventDefault();
this.hideToTray();
}
});
}
this.windowMain.win.on('show', async (e: Event) => {
const enableTray = await this.storageService.get<boolean>(ElectronConstants.enableTrayKey);
if (!enableTray) {
this.removeTray(false);
}
});
}
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();
}
}
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.contextMenu != null) {
this.tray.setContextMenu(this.contextMenu);
}
}
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();
}
}
}