microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/appcenter/appCenterExtensionManager.ts
72lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import { Disposable, StatusBarAlignment, StatusBarItem, window } from "vscode"; |
| 5 | import Auth from "../appcenter/auth/auth"; |
| 6 | import { ACStrings } from "./appCenterStrings"; |
| 7 | import * as Q from "q"; |
| 8 | import { ACCommandNames, ACConstants } from "./appCenterConstants"; |
| 9 | |
| 10 | export class AppCenterExtensionManager implements Disposable { |
| 11 | private loginStatusBarItem: StatusBarItem; |
| 12 | private currentAppStatusBarItem: StatusBarItem; |
| 13 | |
| 14 | public setup(): Q.Promise<void> { |
| 15 | this.loginStatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left, 100); |
| 16 | this.currentAppStatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left, 99); |
| 17 | |
| 18 | return Auth.isAuthenticated().then((isAuthenticated: boolean) => { |
| 19 | if (!isAuthenticated) { |
| 20 | return this.setupNotAuthenticatedStatusBar(); |
| 21 | } else { |
| 22 | return Auth.whoAmI().then(userName => { |
| 23 | return this.setuAuthenticatedStatusBar(userName); |
| 24 | }); |
| 25 | } |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | public dispose() { |
| 30 | if (this.loginStatusBarItem) { |
| 31 | this.loginStatusBarItem.dispose(); |
| 32 | } |
| 33 | if (this.currentAppStatusBarItem) { |
| 34 | this.currentAppStatusBarItem.dispose(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public setupNotAuthenticatedStatusBar() { |
| 39 | this.setStatusBar(this.loginStatusBarItem, |
| 40 | `$(icon octicon-person) Login to App Center`, |
| 41 | ACStrings.UserMustSignIn, |
| 42 | `${ACConstants.ExtensionPrefixName}.${ACCommandNames.Login}` |
| 43 | ); |
| 44 | if (this.currentAppStatusBarItem) { |
| 45 | this.currentAppStatusBarItem.hide(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public setuAuthenticatedStatusBar(userName: string) { |
| 50 | this.setStatusBar(this.loginStatusBarItem, |
| 51 | `$(icon octicon-person) ${userName}`, |
| 52 | ACStrings.YouAreLoggedInMsg(userName), |
| 53 | `${ACConstants.ExtensionPrefixName}.${ACCommandNames.Logout}` |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | public setCurrentAppStatusBar(appName: string) { |
| 58 | return this.setStatusBar(this.currentAppStatusBarItem, |
| 59 | appName, |
| 60 | ACStrings.YourCurrentAppMsg(appName), |
| 61 | `${ACConstants.ExtensionPrefixName}.${ACCommandNames.SetCurrentApp}`); |
| 62 | } |
| 63 | |
| 64 | private setStatusBar(statusBar: StatusBarItem, text: string, tooltip: string, commandOnClick?: string): void { |
| 65 | if (statusBar !== undefined) { |
| 66 | statusBar.command = commandOnClick; // undefined clears the command |
| 67 | statusBar.text = text; |
| 68 | statusBar.tooltip = tooltip; |
| 69 | statusBar.show(); |
| 70 | } |
| 71 | } |
| 72 | } |