forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickInput.ts
More file actions
220 lines (183 loc) · 7.81 KB
/
quickInput.ts
File metadata and controls
220 lines (183 loc) · 7.81 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IListRenderer, IListVirtualDelegate } from 'vs/base/browser/ui/list/list';
import { List } from 'vs/base/browser/ui/list/listWidget';
import { CancellationToken } from 'vs/base/common/cancellation';
import { Emitter } from 'vs/base/common/event';
import { IQuickInputOptions, IQuickInputStyles, QuickInputController } from 'vs/base/parts/quickinput/browser/quickInput';
import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility';
import { IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { ILayoutService } from 'vs/platform/layout/browser/layoutService';
import { IWorkbenchListOptions, WorkbenchList } from 'vs/platform/list/browser/listService';
import { QuickAccessController } from 'vs/platform/quickinput/browser/quickAccess';
import { IQuickAccessController } from 'vs/platform/quickinput/common/quickAccess';
import { IInputBox, IInputOptions, IKeyMods, IPickOptions, IQuickInputButton, IQuickInputService, IQuickNavigateConfiguration, IQuickPick, IQuickPickItem, QuickPickInput } from 'vs/platform/quickinput/common/quickInput';
import { defaultButtonStyles, defaultInputBoxStyles, defaultKeybindingLabelStyles, defaultProgressBarStyles, defaultToggleStyles } from 'vs/platform/theme/browser/defaultStyles';
import { activeContrastBorder, badgeBackground, badgeForeground, contrastBorder, pickerGroupBorder, pickerGroupForeground, quickInputBackground, quickInputForeground, quickInputListFocusBackground, quickInputListFocusForeground, quickInputListFocusIconForeground, quickInputTitleBackground, widgetShadow } from 'vs/platform/theme/common/colorRegistry';
import { computeStyles } from 'vs/platform/theme/common/styler';
import { IThemeService, Themable } from 'vs/platform/theme/common/themeService';
export interface IQuickInputControllerHost extends ILayoutService { }
export class QuickInputService extends Themable implements IQuickInputService {
declare readonly _serviceBrand: undefined;
get backButton(): IQuickInputButton { return this.controller.backButton; }
private readonly _onShow = this._register(new Emitter<void>());
readonly onShow = this._onShow.event;
private readonly _onHide = this._register(new Emitter<void>());
readonly onHide = this._onHide.event;
private _controller: QuickInputController | undefined;
private get controller(): QuickInputController {
if (!this._controller) {
this._controller = this._register(this.createController());
}
return this._controller;
}
private get hasController() { return !!this._controller; }
private _quickAccess: IQuickAccessController | undefined;
get quickAccess(): IQuickAccessController {
if (!this._quickAccess) {
this._quickAccess = this._register(this.instantiationService.createInstance(QuickAccessController));
}
return this._quickAccess;
}
private readonly contexts = new Map<string, IContextKey<boolean>>();
constructor(
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IContextKeyService protected readonly contextKeyService: IContextKeyService,
@IThemeService themeService: IThemeService,
@IAccessibilityService private readonly accessibilityService: IAccessibilityService,
@ILayoutService protected readonly layoutService: ILayoutService
) {
super(themeService);
}
protected createController(host: IQuickInputControllerHost = this.layoutService, options?: Partial<IQuickInputOptions>): QuickInputController {
const defaultOptions: IQuickInputOptions = {
idPrefix: 'quickInput_', // Constant since there is still only one.
container: host.container,
ignoreFocusOut: () => false,
isScreenReaderOptimized: () => this.accessibilityService.isScreenReaderOptimized(),
backKeybindingLabel: () => undefined,
setContextKey: (id?: string) => this.setContextKey(id),
returnFocus: () => host.focus(),
createList: <T>(
user: string,
container: HTMLElement,
delegate: IListVirtualDelegate<T>,
renderers: IListRenderer<T, any>[],
options: IWorkbenchListOptions<T>,
) => this.instantiationService.createInstance(WorkbenchList, user, container, delegate, renderers, options) as List<T>,
styles: this.computeStyles()
};
const controller = this._register(new QuickInputController({
...defaultOptions,
...options
}));
controller.layout(host.dimension, host.offset.quickPickTop);
// Layout changes
this._register(host.onDidLayout(dimension => controller.layout(dimension, host.offset.quickPickTop)));
// Context keys
this._register(controller.onShow(() => {
this.resetContextKeys();
this._onShow.fire();
}));
this._register(controller.onHide(() => {
this.resetContextKeys();
this._onHide.fire();
}));
return controller;
}
private setContextKey(id?: string) {
let key: IContextKey<boolean> | undefined;
if (id) {
key = this.contexts.get(id);
if (!key) {
key = new RawContextKey<boolean>(id, false)
.bindTo(this.contextKeyService);
this.contexts.set(id, key);
}
}
if (key && key.get()) {
return; // already active context
}
this.resetContextKeys();
key?.set(true);
}
private resetContextKeys() {
this.contexts.forEach(context => {
if (context.get()) {
context.reset();
}
});
}
pick<T extends IQuickPickItem, O extends IPickOptions<T>>(picks: Promise<QuickPickInput<T>[]> | QuickPickInput<T>[], options: O = <O>{}, token: CancellationToken = CancellationToken.None): Promise<(O extends { canPickMany: true } ? T[] : T) | undefined> {
return this.controller.pick(picks, options, token);
}
input(options: IInputOptions = {}, token: CancellationToken = CancellationToken.None): Promise<string | undefined> {
return this.controller.input(options, token);
}
createQuickPick<T extends IQuickPickItem>(): IQuickPick<T> {
return this.controller.createQuickPick();
}
createInputBox(): IInputBox {
return this.controller.createInputBox();
}
focus() {
this.controller.focus();
}
toggle() {
this.controller.toggle();
}
navigate(next: boolean, quickNavigate?: IQuickNavigateConfiguration) {
this.controller.navigate(next, quickNavigate);
}
accept(keyMods?: IKeyMods) {
return this.controller.accept(keyMods);
}
back() {
return this.controller.back();
}
cancel() {
return this.controller.cancel();
}
override updateStyles() {
if (this.hasController) {
this.controller.applyStyles(this.computeStyles());
}
}
private computeStyles(): IQuickInputStyles {
return {
widget: {
...computeStyles(this.theme, {
quickInputBackground,
quickInputForeground,
quickInputTitleBackground,
contrastBorder,
widgetShadow
}),
},
inputBox: defaultInputBoxStyles,
toggle: defaultToggleStyles,
countBadge: computeStyles(this.theme, {
badgeBackground,
badgeForeground,
badgeBorder: contrastBorder
}),
button: defaultButtonStyles,
progressBar: defaultProgressBarStyles, // default uses progressBarBackground
keybindingLabel: defaultKeybindingLabelStyles,
list: computeStyles(this.theme, {
listBackground: quickInputBackground,
// Look like focused when inactive.
listInactiveFocusForeground: quickInputListFocusForeground,
listInactiveSelectionIconForeground: quickInputListFocusIconForeground,
listInactiveFocusBackground: quickInputListFocusBackground,
listFocusOutline: activeContrastBorder,
listInactiveFocusOutline: activeContrastBorder,
pickerGroupBorder,
pickerGroupForeground
})
};
}
}