From 197ac4ce6431ad8acdc46cb09639be5b445bce39 Mon Sep 17 00:00:00 2001 From: Rachel Macfarlane Date: Wed, 15 Apr 2020 16:18:04 -0700 Subject: [PATCH] Show a context menu item when not signed into any accounts --- .../api/browser/mainThreadAuthentication.ts | 9 +++++- .../browser/authenticationService.ts | 31 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/vs/workbench/api/browser/mainThreadAuthentication.ts b/src/vs/workbench/api/browser/mainThreadAuthentication.ts index 5ba33c07cf640..491f634f001cf 100644 --- a/src/vs/workbench/api/browser/mainThreadAuthentication.ts +++ b/src/vs/workbench/api/browser/mainThreadAuthentication.ts @@ -68,8 +68,14 @@ export class MainThreadAuthenticationProvider extends Disposable { private readonly notificationService: INotificationService ) { super(); + } + + public async initialize(): Promise { + return this.registerCommandsAndContextMenuItems(); + } - this.registerCommandsAndContextMenuItems(); + public hasSessions(): boolean { + return !!this._sessions.size; } private manageTrustedExtensions(quickInputService: IQuickInputService, storageService: IStorageService, accountName: string) { @@ -309,6 +315,7 @@ export class MainThreadAuthentication extends Disposable implements MainThreadAu async $registerAuthenticationProvider(id: string, displayName: string, supportsMultipleAccounts: boolean): Promise { const provider = new MainThreadAuthenticationProvider(this._proxy, id, displayName, supportsMultipleAccounts, this.notificationService); + await provider.initialize(); this.authenticationService.registerAuthenticationProvider(id, provider); } diff --git a/src/vs/workbench/services/authentication/browser/authenticationService.ts b/src/vs/workbench/services/authentication/browser/authenticationService.ts index 1409dd9baaa9a..e49d60371d0bf 100644 --- a/src/vs/workbench/services/authentication/browser/authenticationService.ts +++ b/src/vs/workbench/services/authentication/browser/authenticationService.ts @@ -34,6 +34,7 @@ export interface IAuthenticationService { export class AuthenticationService extends Disposable implements IAuthenticationService { _serviceBrand: undefined; private _placeholderMenuItem: IDisposable | undefined; + private _noAccountsMenuItem: IDisposable | undefined; private _authenticationProviders: Map = new Map(); @@ -56,6 +57,28 @@ export class AuthenticationService extends Disposable implements IAuthentication }); } + private updateAccountsMenuItem(): void { + let hasSession = false; + this._authenticationProviders.forEach(async provider => { + hasSession = hasSession || provider.hasSessions(); + }); + + if (hasSession && this._noAccountsMenuItem) { + this._noAccountsMenuItem.dispose(); + this._noAccountsMenuItem = undefined; + } + + if (!hasSession && !this._noAccountsMenuItem) { + this._noAccountsMenuItem = MenuRegistry.appendMenuItem(MenuId.AccountsContext, { + group: '0_accounts', + command: { + id: 'noAccounts', + title: nls.localize('noAccounts', "You are not signed in to any accounts") + }, + }); + } + } + registerAuthenticationProvider(id: string, authenticationProvider: MainThreadAuthenticationProvider): void { this._authenticationProviders.set(id, authenticationProvider); this._onDidRegisterAuthenticationProvider.fire(id); @@ -64,6 +87,8 @@ export class AuthenticationService extends Disposable implements IAuthentication this._placeholderMenuItem.dispose(); this._placeholderMenuItem = undefined; } + + this.updateAccountsMenuItem(); } unregisterAuthenticationProvider(id: string): void { @@ -72,6 +97,7 @@ export class AuthenticationService extends Disposable implements IAuthentication provider.dispose(); this._authenticationProviders.delete(id); this._onDidUnregisterAuthenticationProvider.fire(id); + this.updateAccountsMenuItem(); } if (!this._authenticationProviders.size) { @@ -84,11 +110,12 @@ export class AuthenticationService extends Disposable implements IAuthentication } } - sessionsUpdate(id: string, event: AuthenticationSessionsChangeEvent): void { + async sessionsUpdate(id: string, event: AuthenticationSessionsChangeEvent): Promise { this._onDidChangeSessions.fire({ providerId: id, event: event }); const provider = this._authenticationProviders.get(id); if (provider) { - provider.updateSessionItems(event); + await provider.updateSessionItems(event); + this.updateAccountsMenuItem(); } }