1
0
mirror of https://github.com/bitwarden/desktop.git synced 2024-06-30 11:15:42 +02:00

update menu items on event messages

This commit is contained in:
Kyle Spearrin 2018-02-14 00:26:32 -05:00
parent 216c77fa25
commit 3bffbbbeb3
4 changed files with 89 additions and 11 deletions

View File

@ -30,6 +30,7 @@ import { CryptoService } from 'jslib/abstractions/crypto.service';
import { FolderService } from 'jslib/abstractions/folder.service'; import { FolderService } from 'jslib/abstractions/folder.service';
import { I18nService } from 'jslib/abstractions/i18n.service'; import { I18nService } from 'jslib/abstractions/i18n.service';
import { LockService } from 'jslib/abstractions/lock.service'; import { LockService } from 'jslib/abstractions/lock.service';
import { MessagingService } from 'jslib/abstractions/messaging.service';
import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration.service'; import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service'; import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { SettingsService } from 'jslib/abstractions/settings.service'; import { SettingsService } from 'jslib/abstractions/settings.service';
@ -72,9 +73,14 @@ export class AppComponent implements OnInit {
private toasterService: ToasterService, private i18nService: I18nService, private toasterService: ToasterService, private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService, private ngZone: NgZone, private platformUtilsService: PlatformUtilsService, private ngZone: NgZone,
private lockService: LockService, private storageService: StorageService, private lockService: LockService, private storageService: StorageService,
private cryptoService: CryptoService, private componentFactoryResolver: ComponentFactoryResolver) { } private cryptoService: CryptoService, private componentFactoryResolver: ComponentFactoryResolver,
private messagingService: MessagingService) { }
ngOnInit() { ngOnInit() {
setTimeout(async () => {
await this.updateAppMenu();
}, 1000);
window.onmousemove = () => this.recordActivity(); window.onmousemove = () => this.recordActivity();
window.onmousedown = () => this.recordActivity(); window.onmousedown = () => this.recordActivity();
window.ontouchstart = () => this.recordActivity(); window.ontouchstart = () => this.recordActivity();
@ -82,7 +88,11 @@ export class AppComponent implements OnInit {
window.onscroll = () => this.recordActivity(); window.onscroll = () => this.recordActivity();
window.onkeypress = () => this.recordActivity(); window.onkeypress = () => this.recordActivity();
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => { this.broadcasterService.subscribe(BroadcasterSubscriptionId, async (message: any) => {
if (message.command !== 'updateAppMenu') {
await this.updateAppMenu();
}
this.ngZone.run(async () => { this.ngZone.run(async () => {
switch (message.command) { switch (message.command) {
case 'loggedIn': case 'loggedIn':
@ -115,6 +125,13 @@ export class AppComponent implements OnInit {
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId); this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
} }
private async updateAppMenu() {
this.messagingService.send('updateAppMenu', {
isAuthenticated: await this.userService.isAuthenticated(),
isLocked: (await this.cryptoService.getKey()) == null,
});
}
private async logOut(expired: boolean) { private async logOut(expired: boolean) {
const userId = await this.userService.getUserId(); const userId = await this.userService.getUserId();
@ -129,13 +146,14 @@ export class AppComponent implements OnInit {
this.passwordGenerationService.clear(), this.passwordGenerationService.clear(),
]); ]);
this.authService.logOut(() => { this.authService.logOut(async () => {
this.analytics.eventTrack.next({ action: 'Logged Out' }); this.analytics.eventTrack.next({ action: 'Logged Out' });
if (expired) { if (expired) {
this.toasterService.popAsync('warning', this.i18nService.t('loggedOut'), this.toasterService.popAsync('warning', this.i18nService.t('loggedOut'),
this.i18nService.t('loginExpired')); this.i18nService.t('loginExpired'));
} }
this.router.navigate(['login']); await this.router.navigate(['login']);
this.messagingService.send('loggedOut');
}); });
} }

View File

@ -13,11 +13,57 @@ import {
import { Main } from '../main'; import { Main } from '../main';
export class MenuMain { export class MenuMain {
menu: Menu;
updateMenuItem: MenuItem;
addNewLogin: MenuItem;
addNewItem: MenuItem;
addNewFolder: MenuItem;
syncVault: MenuItem;
settings: MenuItem;
lockNow: MenuItem;
logOut: MenuItem;
twoStepLogin: MenuItem;
changeEmail: MenuItem;
changeMasterPass: MenuItem;
premiumMembership: MenuItem;
passwordGenerator: MenuItem;
searchVault: MenuItem;
unlockedRequiredMenuItems: MenuItem[] = [];
constructor(private main: Main) { } constructor(private main: Main) { }
init() { init() {
this.initContextMenu(); this.initContextMenu();
this.initApplicationMenu(); this.initApplicationMenu();
this.updateMenuItem = this.menu.getMenuItemById('checkForUpdates');
this.addNewLogin = this.menu.getMenuItemById('addNewLogin');
this.addNewItem = this.menu.getMenuItemById('addNewItem');
this.addNewFolder = this.menu.getMenuItemById('addNewFolder');
this.syncVault = this.menu.getMenuItemById('syncVault');
this.settings = this.menu.getMenuItemById('settings');
this.lockNow = this.menu.getMenuItemById('lockNow');
this.logOut = this.menu.getMenuItemById('logOut');
this.twoStepLogin = this.menu.getMenuItemById('twoStepLogin');
this.changeEmail = this.menu.getMenuItemById('changeEmail');
this.changeMasterPass = this.menu.getMenuItemById('changeMasterPass');
this.premiumMembership = this.menu.getMenuItemById('premiumMembership');
this.passwordGenerator = this.menu.getMenuItemById('passwordGenerator');
this.searchVault = this.menu.getMenuItemById('searchVault');
this.unlockedRequiredMenuItems = [
this.addNewLogin, this.addNewItem, this.addNewFolder,
this.syncVault, this.settings, this.lockNow, this.twoStepLogin, this.changeEmail,
this.changeMasterPass, this.premiumMembership, this.passwordGenerator, this.searchVault];
this.updateApplicationMenuState(false, true);
}
updateApplicationMenuState(isAuthenticated: boolean, isLocked: boolean) {
this.unlockedRequiredMenuItems.forEach((mi: MenuItem) => {
mi.enabled = isAuthenticated && !isLocked;
});
this.logOut.enabled = isAuthenticated;
} }
private initContextMenu() { private initContextMenu() {
@ -67,9 +113,11 @@ export class MenuMain {
label: this.main.i18nService.t('addNewLogin'), label: this.main.i18nService.t('addNewLogin'),
click: () => this.main.messagingService.send('newLogin'), click: () => this.main.messagingService.send('newLogin'),
accelerator: 'CmdOrCtrl+N', accelerator: 'CmdOrCtrl+N',
id: 'addNewLogin',
}, },
{ {
label: this.main.i18nService.t('addNewItem'), label: this.main.i18nService.t('addNewItem'),
id: 'addNewItem',
submenu: [ submenu: [
{ {
label: this.main.i18nService.t('typeLogin'), label: this.main.i18nService.t('typeLogin'),
@ -95,11 +143,13 @@ export class MenuMain {
}, },
{ {
label: this.main.i18nService.t('addNewFolder'), label: this.main.i18nService.t('addNewFolder'),
id: 'addNewFolder',
click: () => this.main.messagingService.send('newFolder'), click: () => this.main.messagingService.send('newFolder'),
}, },
{ type: 'separator' }, { type: 'separator' },
{ {
label: this.main.i18nService.t('syncVault'), label: this.main.i18nService.t('syncVault'),
id: 'syncVault',
click: () => this.main.messagingService.send('syncVault'), click: () => this.main.messagingService.send('syncVault'),
}, },
], ],
@ -122,11 +172,13 @@ export class MenuMain {
submenu: [ submenu: [
{ {
label: this.main.i18nService.t('passwordGenerator'), label: this.main.i18nService.t('passwordGenerator'),
id: 'passwordGenerator',
click: () => this.main.messagingService.send('openPasswordGenerator'), click: () => this.main.messagingService.send('openPasswordGenerator'),
accelerator: 'CmdOrCtrl+G', accelerator: 'CmdOrCtrl+G',
}, },
{ {
label: this.main.i18nService.t('searchVault'), label: this.main.i18nService.t('searchVault'),
id: 'searchVault',
click: () => this.main.messagingService.send('focusSearch'), click: () => this.main.messagingService.send('focusSearch'),
accelerator: 'CmdOrCtrl+F', accelerator: 'CmdOrCtrl+F',
}, },
@ -148,9 +200,11 @@ export class MenuMain {
{ {
label: this.main.i18nService.t('premiumMembership'), label: this.main.i18nService.t('premiumMembership'),
click: () => this.main.messagingService.send('premiumMembership'), click: () => this.main.messagingService.send('premiumMembership'),
id: 'premiumMembership',
}, },
{ {
label: this.main.i18nService.t('changeMasterPass'), label: this.main.i18nService.t('changeMasterPass'),
id: 'changeMasterPass',
click: () => { click: () => {
const result = dialog.showMessageBox(this.main.windowMain.win, { const result = dialog.showMessageBox(this.main.windowMain.win, {
title: this.main.i18nService.t('changeMasterPass'), title: this.main.i18nService.t('changeMasterPass'),
@ -167,6 +221,7 @@ export class MenuMain {
}, },
{ {
label: this.main.i18nService.t('changeEmail'), label: this.main.i18nService.t('changeEmail'),
id: 'changeEmail',
click: () => { click: () => {
const result = dialog.showMessageBox(this.main.windowMain.win, { const result = dialog.showMessageBox(this.main.windowMain.win, {
title: this.main.i18nService.t('changeEmail'), title: this.main.i18nService.t('changeEmail'),
@ -183,6 +238,7 @@ export class MenuMain {
}, },
{ {
label: this.main.i18nService.t('twoStepLogin'), label: this.main.i18nService.t('twoStepLogin'),
id: 'twoStepLogin',
click: () => { click: () => {
const result = dialog.showMessageBox(this.main.windowMain.win, { const result = dialog.showMessageBox(this.main.windowMain.win, {
title: this.main.i18nService.t('twoStepLogin'), title: this.main.i18nService.t('twoStepLogin'),
@ -200,6 +256,7 @@ export class MenuMain {
{ type: 'separator' }, { type: 'separator' },
{ {
label: this.main.i18nService.t('logOut'), label: this.main.i18nService.t('logOut'),
id: 'logOut',
click: () => { click: () => {
const result = dialog.showMessageBox(this.main.windowMain.win, { const result = dialog.showMessageBox(this.main.windowMain.win, {
title: this.main.i18nService.t('logOut'), title: this.main.i18nService.t('logOut'),
@ -336,10 +393,12 @@ export class MenuMain {
{ type: 'separator' }, { type: 'separator' },
{ {
label: this.main.i18nService.t('settings'), label: this.main.i18nService.t('settings'),
id: 'settings',
click: () => this.main.messagingService.send('openSettings'), click: () => this.main.messagingService.send('openSettings'),
}, },
{ {
label: this.main.i18nService.t('lockNow'), label: this.main.i18nService.t('lockNow'),
id: 'lockNow',
click: () => this.main.messagingService.send('lockVault'), click: () => this.main.messagingService.send('lockVault'),
accelerator: 'CmdOrCtrl+L', accelerator: 'CmdOrCtrl+L',
}, },
@ -413,7 +472,7 @@ export class MenuMain {
]); ]);
} }
const menu = Menu.buildFromTemplate(template); this.menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu); Menu.setApplicationMenu(this.menu);
} }
} }

View File

@ -54,6 +54,9 @@ export class MessagingMain {
case 'scheduleNextSync': case 'scheduleNextSync':
this.scheduleNextSync(); this.scheduleNextSync();
break; break;
case 'updateAppMenu':
this.main.menuMain.updateApplicationMenuState(message.isAuthenticated, message.isLocked);
break;
default: default:
break; break;
} }

View File

@ -14,17 +14,15 @@ const UpdaterCheckInterval = 12 * 60 * 60 * 1000; // 12 hours
export class UpdaterMain { export class UpdaterMain {
private doingUpdateCheck = false; private doingUpdateCheck = false;
private doingUpdateCheckWithFeedback = false; private doingUpdateCheckWithFeedback = false;
private updateMenuItem: MenuItem;
constructor(private main: Main) { } constructor(private main: Main) { }
async init() { async init() {
global.setTimeout(async () => await this.checkForUpdate(), UpdaterCheckInitalDelay); global.setTimeout(async () => await this.checkForUpdate(), UpdaterCheckInitalDelay);
global.setInterval(async () => await this.checkForUpdate(), UpdaterCheckInterval); global.setInterval(async () => await this.checkForUpdate(), UpdaterCheckInterval);
this.updateMenuItem = Menu.getApplicationMenu().getMenuItemById('checkForUpdates');
autoUpdater.on('checking-for-update', () => { autoUpdater.on('checking-for-update', () => {
this.updateMenuItem.enabled = false; this.main.menuMain.updateMenuItem.enabled = false;
this.doingUpdateCheck = true; this.doingUpdateCheck = true;
}); });
@ -60,7 +58,7 @@ export class UpdaterMain {
}); });
autoUpdater.on('update-downloaded', (info) => { autoUpdater.on('update-downloaded', (info) => {
this.updateMenuItem.label = this.main.i18nService.t('restartToUpdate'); this.main.menuMain.updateMenuItem.label = this.main.i18nService.t('restartToUpdate');
const result = dialog.showMessageBox(this.main.windowMain.win, { const result = dialog.showMessageBox(this.main.windowMain.win, {
type: 'info', type: 'info',
@ -103,7 +101,7 @@ export class UpdaterMain {
private reset() { private reset() {
autoUpdater.autoDownload = true; autoUpdater.autoDownload = true;
this.updateMenuItem.enabled = true; this.main.menuMain.updateMenuItem.enabled = true;
this.doingUpdateCheck = false; this.doingUpdateCheck = false;
} }
} }