Skip to content

Commit bf6f163

Browse files
committed
Manage use of vscode.window.showInputBox
Fixes #302 by only showing one input box at a time, deduplicating per server
1 parent 06ff9b0 commit bf6f163

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

src/extension.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import {
7171
terminalWithDocker,
7272
notNull,
7373
currentFile,
74+
InputBoxManager,
7475
} from "./utils";
7576
import { ObjectScriptDiagnosticProvider } from "./providers/ObjectScriptDiagnosticProvider";
7677
import { DocumentRangeFormattingEditProvider } from "./providers/DocumentRangeFormattingEditProvider";
@@ -262,21 +263,23 @@ export function checkConnection(clearCookies = false, uri?: vscode.Uri): void {
262263
disableConnection(configName);
263264
}
264265
} else {
265-
vscode.window
266-
.showInputBox({
266+
InputBoxManager.showInputBox(
267+
{
267268
password: true,
268269
placeHolder: `Not Authorized. Enter password to connect as user '${username}' to ${connInfo}`,
269270
prompt: !api.externalServer ? "If no password is entered the connection will be disabled." : "",
270271
ignoreFocusOut: true,
271-
})
272-
.then((password) => {
272+
},
273+
(password) => {
273274
if (password) {
274275
workspaceState.update(configName + ":password", password);
275276
checkConnection(false, uri);
276277
} else if (!api.externalServer) {
277278
disableConnection(configName);
278279
}
279-
});
280+
},
281+
connInfo
282+
);
280283
}
281284
}, 1000);
282285
message = "Not Authorized";

src/utils/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export function outputConsole(data: string[]): void {
1313
});
1414
}
1515

16+
import { InputBoxManager } from "./inputBoxManager";
17+
export { InputBoxManager };
18+
1619
// tslint:disable-next-line: interface-name
1720
export interface CurrentFile {
1821
name: string;

src/utils/inputBoxManager.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import vscode = require("vscode");
2+
3+
import { InputBoxOptions } from "vscode";
4+
5+
// Used in situations where multiple input boxes may be displayed to ensure that we don't stomp on
6+
// the box that's already shown.
7+
export class InputBoxManager {
8+
private static shown = false;
9+
private static specs = new Array<InputBoxSpec>();
10+
private static currentKey: string;
11+
12+
public static showInputBox(options: InputBoxOptions, callback: (answer: string) => void, key?: string): void {
13+
const spec: InputBoxSpec = {
14+
options: options,
15+
callback: callback,
16+
};
17+
if (key) {
18+
if (
19+
InputBoxManager.currentKey === key ||
20+
InputBoxManager.specs.find((spec) => {
21+
return spec.key === key;
22+
})
23+
) {
24+
return;
25+
}
26+
spec.key = key;
27+
}
28+
this.specs.push(spec);
29+
if (!this.shown) {
30+
this.shown = true;
31+
this.next();
32+
}
33+
}
34+
35+
private static next() {
36+
if (this.specs.length === 0) {
37+
this.shown = false;
38+
this.currentKey = undefined;
39+
return;
40+
}
41+
const spec = this.specs.shift();
42+
this.currentKey = spec.key;
43+
vscode.window.showInputBox(spec.options).then(
44+
(result) => {
45+
spec.callback(result);
46+
InputBoxManager.next();
47+
},
48+
(ignored) => {
49+
InputBoxManager.next();
50+
}
51+
);
52+
}
53+
}
54+
55+
interface InputBoxSpec {
56+
key?: string;
57+
options: InputBoxOptions;
58+
callback: (answer: string) => void;
59+
}

0 commit comments

Comments
 (0)