-
Notifications
You must be signed in to change notification settings - Fork 565
/
Copy pathawsCredentialsStatusBarItem.ts
92 lines (77 loc) · 3.28 KB
/
awsCredentialsStatusBarItem.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*!
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import * as nls from 'vscode-nls'
const localize = nls.loadMessageBundle()
import * as vscode from 'vscode'
import { AwsContext, ContextChangeEventsArgs } from '../shared/awsContext'
import { getIdeProperties } from '../shared/extensionUtilities'
import globals from '../shared/extensionGlobals'
import { DevSettings } from '../shared/settings'
const STATUSBAR_PRIORITY = 1
const STATUSBAR_CONNECTED_MSG = localize('AWS.credentials.statusbar.connected', '(connected)')
const STATUSBAR_CONNECTED_DELAY = 1000
// This is a module global since this code doesn't really warrant its own class
let timeoutID: NodeJS.Timeout
export async function initializeAwsCredentialsStatusBarItem(
awsContext: AwsContext,
context: vscode.ExtensionContext
): Promise<void> {
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, STATUSBAR_PRIORITY)
statusBarItem.command = 'aws.login'
statusBarItem.show()
updateCredentialsStatusBarItem(statusBarItem)
context.subscriptions.push(statusBarItem)
const devSettings = DevSettings.instance
handleDevSettings(devSettings, statusBarItem)
context.subscriptions.push(
awsContext.onDidChangeContext(async (ev: ContextChangeEventsArgs) => {
updateCredentialsStatusBarItem(statusBarItem, ev.profileName)
handleDevSettings(devSettings, statusBarItem)
}),
devSettings.onDidChangeActiveSettings(() => handleDevSettings(devSettings, statusBarItem))
)
}
function handleDevSettings(devSettings: DevSettings, statusBarItem: vscode.StatusBarItem) {
const developerMode = Object.keys(devSettings.activeSettings)
if (developerMode.length > 0) {
;(statusBarItem as any).backgroundColor = new vscode.ThemeColor('statusBarItem.errorBackground')
const devSettingsStr = developerMode.join(' \n')
statusBarItem.tooltip = `Toolkit developer settings:\n${devSettingsStr}`
} else {
;(statusBarItem as any).backgroundColor = undefined
}
}
// Resolves when the status bar reaches its final state
export async function updateCredentialsStatusBarItem(
statusBarItem: vscode.StatusBarItem,
credentialsId?: string
): Promise<void> {
globals.clock.clearTimeout(timeoutID)
const connectedMsg = localize(
'AWS.credentials.statusbar.connected',
'Connected to {0} with "{1}" (click to change)',
getIdeProperties().company,
credentialsId
)
const disconnectedMsg = localize(
'AWS.credentials.statusbar.disconnected',
'Click to connect to {0}',
getIdeProperties().company
)
statusBarItem.tooltip = credentialsId ? connectedMsg : disconnectedMsg
// Shows "connected" message briefly.
let delay = 0
if (credentialsId) {
delay = STATUSBAR_CONNECTED_DELAY
statusBarItem.text = `${getIdeProperties().company}: ${STATUSBAR_CONNECTED_MSG}`
}
return new Promise<void>(
resolve =>
(timeoutID = globals.clock.setTimeout(() => {
const company = getIdeProperties().company
;(statusBarItem.text = credentialsId ? `${company}: ${credentialsId}` : company), resolve()
}, delay))
)
}