quickInput.ts 49.1 KB
Newer Older
C
Christof Marti 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

B
Benjamin Pasero 已提交
6
import 'vs/css!./media/quickInput';
C
Christof Marti 已提交
7
import { Component } from 'vs/workbench/common/component';
C
Christof Marti 已提交
8
import { IQuickInputService, IQuickPickItem, IPickOptions, IInputOptions, IQuickNavigateConfiguration, IQuickPick, IQuickInput, IQuickInputButton, IInputBox, IQuickPickItemButtonEvent, QuickPickInput, IQuickPickSeparator, IKeyMods } from 'vs/platform/quickinput/common/quickInput';
9
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
C
Christof Marti 已提交
10
import * as dom from 'vs/base/browser/dom';
11
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
12
import { IThemeService } from 'vs/platform/theme/common/themeService';
13
import { contrastBorder, widgetShadow } from 'vs/platform/theme/common/colorRegistry';
14
import { QUICK_INPUT_BACKGROUND, QUICK_INPUT_FOREGROUND } from 'vs/workbench/common/theme';
15
import { IQuickOpenService } from 'vs/platform/quickOpen/common/quickOpen';
16
import { CancellationToken } from 'vs/base/common/cancellation';
17
import { QuickInputList } from './quickInputList';
18
import { QuickInputBox } from './quickInputBox';
C
Christof Marti 已提交
19 20
import { KeyCode } from 'vs/base/common/keyCodes';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
21
import { localize } from 'vs/nls';
22 23
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { CLOSE_ON_FOCUS_LOST_CONFIG } from 'vs/workbench/browser/quickopen';
C
Christof Marti 已提交
24
import { CountBadge } from 'vs/base/browser/ui/countBadge/countBadge';
25
import { attachBadgeStyler, attachProgressBarStyler, attachButtonStyler } from 'vs/platform/theme/common/styler';
26
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
C
Christof Marti 已提交
27
import { ProgressBar } from 'vs/base/browser/ui/progressbar/progressbar';
J
Joao Moreno 已提交
28
import { Emitter, Event } from 'vs/base/common/event';
29
import { Button } from 'vs/base/browser/ui/button/button';
30
import { dispose, Disposable, DisposableStore } from 'vs/base/common/lifecycle';
C
Christof Marti 已提交
31
import Severity from 'vs/base/common/severity';
32
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
33
import { IContextKeyService, RawContextKey, IContextKey } from 'vs/platform/contextkey/common/contextkey';
34
import { ICommandAndKeybindingRule, KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
35
import { inQuickOpenContext, InQuickOpenContextKey } from 'vs/workbench/browser/parts/quickopen/quickopen';
36
import { ActionBar, ActionViewItem } from 'vs/base/browser/ui/actionbar/actionbar';
C
Christof Marti 已提交
37
import { Action } from 'vs/base/common/actions';
38
import { URI } from 'vs/base/common/uri';
C
Christof Marti 已提交
39
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
40
import { equals } from 'vs/base/common/arrays';
41
import { TimeoutTimer } from 'vs/base/common/async';
C
Christof Marti 已提交
42
import { getIconClass } from 'vs/workbench/browser/parts/quickinput/quickInputUtils';
43
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
B
Benjamin Pasero 已提交
44
import { IStorageService } from 'vs/platform/storage/common/storage';
45
import { IAccessibilityService, AccessibilitySupport } from 'vs/platform/accessibility/common/accessibility';
46
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
I
isidor 已提交
47
import { registerAndGetAmdImageURL } from 'vs/base/common/amd';
C
Christof Marti 已提交
48 49 50

const $ = dom.$;

C
Christof Marti 已提交
51 52
type Writeable<T> = { -readonly [P in keyof T]: T[P] };

C
Christof Marti 已提交
53 54
const backButton = {
	iconPath: {
I
isidor 已提交
55 56
		dark: URI.parse(registerAndGetAmdImageURL('vs/workbench/browser/parts/quickinput/media/arrow-left-dark.svg')),
		light: URI.parse(registerAndGetAmdImageURL('vs/workbench/browser/parts/quickinput/media/arrow-left-light.svg'))
C
Christof Marti 已提交
57 58 59 60 61
	},
	tooltip: localize('quickInput.back', "Back"),
	handle: -1 // TODO
};

62
interface QuickInputUI {
C
Christof Marti 已提交
63
	container: HTMLElement;
C
Christof Marti 已提交
64
	leftActionBar: ActionBar;
65
	titleBar: HTMLElement;
C
Christof Marti 已提交
66
	title: HTMLElement;
C
Christof Marti 已提交
67
	rightActionBar: ActionBar;
68
	checkAll: HTMLInputElement;
69
	filterContainer: HTMLElement;
70
	inputBox: QuickInputBox;
71
	visibleCountContainer: HTMLElement;
72
	visibleCount: CountBadge;
73
	countContainer: HTMLElement;
74
	count: CountBadge;
75 76
	okContainer: HTMLElement;
	ok: Button;
77
	message: HTMLElement;
78
	customButtonContainer: HTMLElement;
79
	customButton: Button;
80
	progressBar: ProgressBar;
81
	list: QuickInputList;
82
	onDidAccept: Event<void>;
83
	onDidCustom: Event<void>;
C
Christof Marti 已提交
84
	onDidTriggerButton: Event<IQuickInputButton>;
85
	ignoreFocusOut: boolean;
C
Christof Marti 已提交
86
	keyMods: Writeable<IKeyMods>;
87
	isScreenReaderOptimized(): boolean;
88 89
	show(controller: QuickInput): void;
	setVisibilities(visibilities: Visibilities): void;
90
	setComboboxAccessibility(enabled: boolean): void;
C
Christof Marti 已提交
91
	setEnabled(enabled: boolean): void;
C
Christof Marti 已提交
92
	setContextKey(contextKey?: string): void;
93
	hide(): void;
94 95
}

96
type Visibilities = {
C
Christof Marti 已提交
97
	title?: boolean;
98 99
	checkAll?: boolean;
	inputBox?: boolean;
100
	visibleCount?: boolean;
101 102 103 104
	count?: boolean;
	message?: boolean;
	list?: boolean;
	ok?: boolean;
105
	customButton?: boolean;
106 107
};

108
class QuickInput extends Disposable implements IQuickInput {
109

110 111 112
	private _title: string | undefined;
	private _steps: number | undefined;
	private _totalSteps: number | undefined;
113 114
	protected visible = false;
	private _enabled = true;
115
	private _contextKey: string | undefined;
116 117
	private _busy = false;
	private _ignoreFocusOut = false;
C
Christof Marti 已提交
118 119
	private _buttons: IQuickInputButton[] = [];
	private buttonsUpdated = false;
120 121
	private readonly onDidTriggerButtonEmitter = this._register(new Emitter<IQuickInputButton>());
	private readonly onDidHideEmitter = this._register(new Emitter<void>());
122

123
	protected readonly visibleDisposables = this._register(new DisposableStore());
124

125
	private busyDelay: TimeoutTimer | undefined;
126

127 128 129 130
	constructor(
		protected ui: QuickInputUI
	) {
		super();
131 132
	}

C
Christof Marti 已提交
133 134 135 136
	get title() {
		return this._title;
	}

137
	set title(title: string | undefined) {
C
Christof Marti 已提交
138 139 140 141 142 143 144 145
		this._title = title;
		this.update();
	}

	get step() {
		return this._steps;
	}

146
	set step(step: number | undefined) {
C
Christof Marti 已提交
147 148 149 150 151 152 153 154
		this._steps = step;
		this.update();
	}

	get totalSteps() {
		return this._totalSteps;
	}

155
	set totalSteps(totalSteps: number | undefined) {
C
Christof Marti 已提交
156 157 158 159
		this._totalSteps = totalSteps;
		this.update();
	}

160 161 162 163 164 165
	get enabled() {
		return this._enabled;
	}

	set enabled(enabled: boolean) {
		this._enabled = enabled;
C
Christof Marti 已提交
166
		this.update();
167 168
	}

C
Christof Marti 已提交
169 170 171 172
	get contextKey() {
		return this._contextKey;
	}

173
	set contextKey(contextKey: string | undefined) {
C
Christof Marti 已提交
174 175 176 177
		this._contextKey = contextKey;
		this.update();
	}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	get busy() {
		return this._busy;
	}

	set busy(busy: boolean) {
		this._busy = busy;
		this.update();
	}

	get ignoreFocusOut() {
		return this._ignoreFocusOut;
	}

	set ignoreFocusOut(ignoreFocusOut: boolean) {
		this._ignoreFocusOut = ignoreFocusOut;
		this.update();
	}

C
Christof Marti 已提交
196 197 198 199 200 201 202 203 204 205 206 207
	get buttons() {
		return this._buttons;
	}

	set buttons(buttons: IQuickInputButton[]) {
		this._buttons = buttons;
		this.buttonsUpdated = true;
		this.update();
	}

	onDidTriggerButton = this.onDidTriggerButtonEmitter.event;

208 209 210 211
	show(): void {
		if (this.visible) {
			return;
		}
212
		this.visibleDisposables.add(
C
Christof Marti 已提交
213 214 215 216 217 218
			this.ui.onDidTriggerButton(button => {
				if (this.buttons.indexOf(button) !== -1) {
					this.onDidTriggerButtonEmitter.fire(button);
				}
			}),
		);
219 220 221 222 223 224 225 226 227 228 229 230 231 232
		this.ui.show(this);
		this.visible = true;
		this.update();
	}

	hide(): void {
		if (!this.visible) {
			return;
		}
		this.ui.hide();
	}

	didHide(): void {
		this.visible = false;
233
		this.visibleDisposables.clear();
234 235 236 237 238 239 240 241 242
		this.onDidHideEmitter.fire();
	}

	onDidHide = this.onDidHideEmitter.event;

	protected update() {
		if (!this.visible) {
			return;
		}
C
Christof Marti 已提交
243 244 245 246
		const title = this.getTitle();
		if (this.ui.title.textContent !== title) {
			this.ui.title.textContent = title;
		}
247
		if (this.busy && !this.busyDelay) {
248 249
			this.busyDelay = new TimeoutTimer();
			this.busyDelay.setIfNotSet(() => {
C
Christof Marti 已提交
250 251 252
				if (this.visible) {
					this.ui.progressBar.infinite();
				}
253
			}, 800);
254 255 256 257
		}
		if (!this.busy && this.busyDelay) {
			this.ui.progressBar.stop();
			this.busyDelay.cancel();
258
			this.busyDelay = undefined;
259
		}
C
Christof Marti 已提交
260 261
		if (this.buttonsUpdated) {
			this.buttonsUpdated = false;
C
Christof Marti 已提交
262 263 264
			this.ui.leftActionBar.clear();
			const leftButtons = this.buttons.filter(button => button === backButton);
			this.ui.leftActionBar.push(leftButtons.map((button, index) => {
265
				const action = new Action(`id-${index}`, '', button.iconClass || getIconClass(button.iconPath), true, () => {
M
Matt Bierner 已提交
266 267 268
					this.onDidTriggerButtonEmitter.fire(button);
					return Promise.resolve(null);
				});
269
				action.tooltip = button.tooltip || '';
C
Christof Marti 已提交
270 271 272 273 274
				return action;
			}), { icon: true, label: false });
			this.ui.rightActionBar.clear();
			const rightButtons = this.buttons.filter(button => button !== backButton);
			this.ui.rightActionBar.push(rightButtons.map((button, index) => {
275
				const action = new Action(`id-${index}`, '', button.iconClass || getIconClass(button.iconPath), true, () => {
M
Matt Bierner 已提交
276 277 278
					this.onDidTriggerButtonEmitter.fire(button);
					return Promise.resolve(null);
				});
279
				action.tooltip = button.tooltip || '';
C
Christof Marti 已提交
280 281 282
				return action;
			}), { icon: true, label: false });
		}
283
		this.ui.ignoreFocusOut = this.ignoreFocusOut;
C
Christof Marti 已提交
284
		this.ui.setEnabled(this.enabled);
C
Christof Marti 已提交
285
		this.ui.setContextKey(this.contextKey);
286 287
	}

C
Christof Marti 已提交
288 289
	private getTitle() {
		if (this.title && this.step) {
C
Christof Marti 已提交
290
			return `${this.title} (${this.getSteps()})`;
C
Christof Marti 已提交
291 292 293 294 295 296 297 298 299 300 301 302
		}
		if (this.title) {
			return this.title;
		}
		if (this.step) {
			return this.getSteps();
		}
		return '';
	}

	private getSteps() {
		if (this.step && this.totalSteps) {
C
Christof Marti 已提交
303
			return localize('quickInput.steps', "{0}/{1}", this.step, this.totalSteps);
C
Christof Marti 已提交
304 305 306 307 308 309 310
		}
		if (this.step) {
			return String(this.step);
		}
		return '';
	}

311 312 313 314
	protected showMessageDecoration(severity: Severity) {
		this.ui.inputBox.showDecoration(severity);
		if (severity === Severity.Error) {
			const styles = this.ui.inputBox.stylesForType(severity);
M
Matt Bierner 已提交
315 316
			this.ui.message.style.backgroundColor = styles.background ? `${styles.background}` : '';
			this.ui.message.style.border = styles.border ? `1px solid ${styles.border}` : '';
A
Alex Ross 已提交
317
			this.ui.message.style.paddingBottom = '4px';
318 319 320
		} else {
			this.ui.message.style.backgroundColor = '';
			this.ui.message.style.border = '';
A
Alex Ross 已提交
321
			this.ui.message.style.paddingBottom = '';
322 323 324
		}
	}

325 326
	public dispose(): void {
		this.hide();
327
		super.dispose();
328
	}
329 330
}

331
class QuickPick<T extends IQuickPickItem> extends QuickInput implements IQuickPick<T> {
332

333
	private static readonly INPUT_BOX_ARIA_LABEL = localize('quickInputBox.ariaLabel', "Type to narrow down results.");
334

335
	private _value = '';
336
	private _placeholder: string | undefined;
337 338 339
	private readonly onDidChangeValueEmitter = this._register(new Emitter<string>());
	private readonly onDidAcceptEmitter = this._register(new Emitter<void>());
	private readonly onDidCustomEmitter = this._register(new Emitter<void>());
340
	private _items: Array<T | IQuickPickSeparator> = [];
341 342
	private itemsUpdated = false;
	private _canSelectMany = false;
C
Christof Marti 已提交
343 344
	private _matchOnDescription = false;
	private _matchOnDetail = false;
A
Alex Ross 已提交
345 346
	private _matchOnLabel = true;
	private _autoFocusOnList = true;
347
	private _activeItems: T[] = [];
348
	private activeItemsUpdated = false;
349
	private activeItemsToConfirm: T[] | null = [];
350
	private readonly onDidChangeActiveEmitter = this._register(new Emitter<T[]>());
351
	private _selectedItems: T[] = [];
352
	private selectedItemsUpdated = false;
353
	private selectedItemsToConfirm: T[] | null = [];
354 355
	private readonly onDidChangeSelectionEmitter = this._register(new Emitter<T[]>());
	private readonly onDidTriggerItemButtonEmitter = this._register(new Emitter<IQuickPickItemButtonEvent<T>>());
356
	private _valueSelection: Readonly<[number, number]> | undefined;
A
Alex Ross 已提交
357
	private valueSelectionUpdated = true;
358 359 360 361 362
	private _validationMessage: string | undefined;
	private _ok = false;
	private _customButton = false;
	private _customButtonLabel: string | undefined;
	private _customButtonHover: string | undefined;
C
Christof Marti 已提交
363

364
	quickNavigate: IQuickNavigateConfiguration | undefined;
365 366


367 368 369
	get value() {
		return this._value;
	}
370

371 372 373 374
	set value(value: string) {
		this._value = value || '';
		this.update();
	}
375

376 377 378 379
	get placeholder() {
		return this._placeholder;
	}

380
	set placeholder(placeholder: string | undefined) {
381
		this._placeholder = placeholder;
382 383
		this.update();
	}
384

385
	onDidChangeValue = this.onDidChangeValueEmitter.event;
386 387 388

	onDidAccept = this.onDidAcceptEmitter.event;

389 390
	onDidCustom = this.onDidCustomEmitter.event;

391 392 393 394
	get items() {
		return this._items;
	}

395
	set items(items: Array<T | IQuickPickSeparator>) {
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
		this._items = items;
		this.itemsUpdated = true;
		this.update();
	}

	get canSelectMany() {
		return this._canSelectMany;
	}

	set canSelectMany(canSelectMany: boolean) {
		this._canSelectMany = canSelectMany;
		this.update();
	}

	get matchOnDescription() {
		return this._matchOnDescription;
	}

	set matchOnDescription(matchOnDescription: boolean) {
		this._matchOnDescription = matchOnDescription;
		this.update();
	}

	get matchOnDetail() {
		return this._matchOnDetail;
	}

	set matchOnDetail(matchOnDetail: boolean) {
		this._matchOnDetail = matchOnDetail;
		this.update();
	}

A
Alex Ross 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	get matchOnLabel() {
		return this._matchOnLabel;
	}

	set matchOnLabel(matchOnLabel: boolean) {
		this._matchOnLabel = matchOnLabel;
		this.update();
	}

	get autoFocusOnList() {
		return this._autoFocusOnList;
	}

	set autoFocusOnList(autoFocusOnList: boolean) {
		this._autoFocusOnList = autoFocusOnList;
		this.update();
	}

446 447 448 449
	get activeItems() {
		return this._activeItems;
	}

450
	set activeItems(activeItems: T[]) {
451 452 453 454 455
		this._activeItems = activeItems;
		this.activeItemsUpdated = true;
		this.update();
	}

456 457 458 459 460 461
	onDidChangeActive = this.onDidChangeActiveEmitter.event;

	get selectedItems() {
		return this._selectedItems;
	}

462
	set selectedItems(selectedItems: T[]) {
463 464 465 466 467
		this._selectedItems = selectedItems;
		this.selectedItemsUpdated = true;
		this.update();
	}

C
Christof Marti 已提交
468 469 470 471
	get keyMods() {
		return this.ui.keyMods;
	}

A
Alex Ross 已提交
472 473 474 475 476 477
	set valueSelection(valueSelection: Readonly<[number, number]>) {
		this._valueSelection = valueSelection;
		this.valueSelectionUpdated = true;
		this.update();
	}

478 479 480 481
	get validationMessage() {
		return this._validationMessage;
	}

482
	set validationMessage(validationMessage: string | undefined) {
483 484 485 486
		this._validationMessage = validationMessage;
		this.update();
	}

487 488 489 490 491 492 493 494 495 496 497 498 499
	get customButton() {
		return this._customButton;
	}

	set customButton(showCustomButton: boolean) {
		this._customButton = showCustomButton;
		this.update();
	}

	get customLabel() {
		return this._customButtonLabel;
	}

500
	set customLabel(label: string | undefined) {
501 502 503 504
		this._customButtonLabel = label;
		this.update();
	}

A
Alex Ross 已提交
505 506 507 508
	get customHover() {
		return this._customButtonHover;
	}

509
	set customHover(hover: string | undefined) {
A
Alex Ross 已提交
510 511 512 513
		this._customButtonHover = hover;
		this.update();
	}

514 515 516 517 518 519 520 521 522
	get ok() {
		return this._ok;
	}

	set ok(showOkButton: boolean) {
		this._ok = showOkButton;
		this.update();
	}

523 524 525 526
	public inputHasFocus(): boolean {
		return this.visible ? this.ui.inputBox.hasFocus() : false;
	}

527 528
	onDidChangeSelection = this.onDidChangeSelectionEmitter.event;

C
Christof Marti 已提交
529 530
	onDidTriggerItemButton = this.onDidTriggerItemButtonEmitter.event;

A
Alex Ross 已提交
531 532 533 534 535 536 537 538
	private trySelectFirst() {
		if (this.autoFocusOnList) {
			if (!this.ui.isScreenReaderOptimized() && !this.canSelectMany) {
				this.ui.list.focus('First');
			}
		}
	}

539 540
	show() {
		if (!this.visible) {
541
			this.visibleDisposables.add(
542 543 544
				this.ui.inputBox.onDidChange(value => {
					if (value === this.value) {
						return;
545
					}
546 547
					this._value = value;
					this.ui.list.filter(this.ui.inputBox.value);
A
Alex Ross 已提交
548
					this.trySelectFirst();
549
					this.onDidChangeValueEmitter.fire(value);
550 551 552 553 554 555 556 557 558 559 560 561
				}));
			this.visibleDisposables.add(this.ui.inputBox.onMouseDown(event => {
				if (!this.autoFocusOnList) {
					this.ui.list.clearFocus();
				}
			}));
			this.visibleDisposables.add(this.ui.inputBox.onKeyDown(event => {
				switch (event.keyCode) {
					case KeyCode.DownArrow:
						this.ui.list.focus('Next');
						if (this.canSelectMany) {
							this.ui.list.domFocus();
562
						}
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
						event.preventDefault();
						break;
					case KeyCode.UpArrow:
						if (this.ui.list.getFocusedElements().length) {
							this.ui.list.focus('Previous');
						} else {
							this.ui.list.focus('Last');
						}
						if (this.canSelectMany) {
							this.ui.list.domFocus();
						}
						event.preventDefault();
						break;
					case KeyCode.PageDown:
						if (this.ui.list.getFocusedElements().length) {
							this.ui.list.focus('NextPage');
						} else {
							this.ui.list.focus('First');
						}
						if (this.canSelectMany) {
							this.ui.list.domFocus();
						}
						event.preventDefault();
						break;
					case KeyCode.PageUp:
						if (this.ui.list.getFocusedElements().length) {
							this.ui.list.focus('PreviousPage');
						} else {
							this.ui.list.focus('Last');
						}
						if (this.canSelectMany) {
							this.ui.list.domFocus();
						}
						event.preventDefault();
						break;
				}
			}));
			this.visibleDisposables.add(this.ui.onDidAccept(() => {
				if (!this.canSelectMany && this.activeItems[0]) {
					this._selectedItems = [this.activeItems[0]];
					this.onDidChangeSelectionEmitter.fire(this.selectedItems);
				}
				this.onDidAcceptEmitter.fire(undefined);
			}));
			this.visibleDisposables.add(this.ui.onDidCustom(() => {
				this.onDidCustomEmitter.fire(undefined);
			}));
			this.visibleDisposables.add(this.ui.list.onDidChangeFocus(focusedItems => {
				if (this.activeItemsUpdated) {
					return; // Expect another event.
				}
				if (this.activeItemsToConfirm !== this._activeItems && equals(focusedItems, this._activeItems, (a, b) => a === b)) {
					return;
				}
				this._activeItems = focusedItems as T[];
				this.onDidChangeActiveEmitter.fire(focusedItems as T[]);
			}));
			this.visibleDisposables.add(this.ui.list.onDidChangeSelection(selectedItems => {
				if (this.canSelectMany) {
622
					if (selectedItems.length) {
623
						this.ui.list.setSelectedElements([]);
624
					}
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
					return;
				}
				if (this.selectedItemsToConfirm !== this._selectedItems && equals(selectedItems, this._selectedItems, (a, b) => a === b)) {
					return;
				}
				this._selectedItems = selectedItems as T[];
				this.onDidChangeSelectionEmitter.fire(selectedItems as T[]);
				if (selectedItems.length) {
					this.onDidAcceptEmitter.fire(undefined);
				}
			}));
			this.visibleDisposables.add(this.ui.list.onChangedCheckedElements(checkedItems => {
				if (!this.canSelectMany) {
					return;
				}
				if (this.selectedItemsToConfirm !== this._selectedItems && equals(checkedItems, this._selectedItems, (a, b) => a === b)) {
					return;
				}
				this._selectedItems = checkedItems as T[];
				this.onDidChangeSelectionEmitter.fire(checkedItems as T[]);
			}));
			this.visibleDisposables.add(this.ui.list.onButtonTriggered(event => this.onDidTriggerItemButtonEmitter.fire(event as IQuickPickItemButtonEvent<T>)));
			this.visibleDisposables.add(this.registerQuickNavigation());
A
Alex Ross 已提交
648
			this.valueSelectionUpdated = true;
649
		}
650
		super.show(); // TODO: Why have show() bubble up while update() trickles down? (Could move setComboboxAccessibility() here.)
651 652
	}

C
Christof Marti 已提交
653
	private registerQuickNavigation() {
654
		return dom.addDisposableListener(this.ui.container, dom.EventType.KEY_UP, e => {
655
			if (this.canSelectMany || !this.quickNavigate) {
C
Christof Marti 已提交
656 657 658
				return;
			}

659
			const keyboardEvent: StandardKeyboardEvent = new StandardKeyboardEvent(e);
C
Christof Marti 已提交
660 661 662 663
			const keyCode = keyboardEvent.keyCode;

			// Select element when keys are pressed that signal it
			const quickNavKeys = this.quickNavigate.keybindings;
C
Christof Marti 已提交
664
			const wasTriggerKeyPressed = quickNavKeys.some(k => {
C
Christof Marti 已提交
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
				const [firstPart, chordPart] = k.getParts();
				if (chordPart) {
					return false;
				}

				if (firstPart.shiftKey && keyCode === KeyCode.Shift) {
					if (keyboardEvent.ctrlKey || keyboardEvent.altKey || keyboardEvent.metaKey) {
						return false; // this is an optimistic check for the shift key being used to navigate back in quick open
					}

					return true;
				}

				if (firstPart.altKey && keyCode === KeyCode.Alt) {
					return true;
				}

				if (firstPart.ctrlKey && keyCode === KeyCode.Ctrl) {
					return true;
				}

				if (firstPart.metaKey && keyCode === KeyCode.Meta) {
					return true;
				}

				return false;
			});

			if (wasTriggerKeyPressed && this.activeItems[0]) {
				this._selectedItems = [this.activeItems[0]];
				this.onDidChangeSelectionEmitter.fire(this.selectedItems);
696
				this.onDidAcceptEmitter.fire(undefined);
C
Christof Marti 已提交
697 698 699 700
			}
		});
	}

701 702 703 704
	protected update() {
		if (!this.visible) {
			return;
		}
705 706
		this.ui.setVisibilities(this.canSelectMany ? { title: !!this.title || !!this.step, checkAll: true, inputBox: true, visibleCount: true, count: true, ok: true, list: true, message: !!this.validationMessage } : { title: !!this.title || !!this.step, inputBox: true, visibleCount: true, list: true, message: !!this.validationMessage, customButton: this.customButton, ok: this.ok });
		super.update();
707 708 709
		if (this.ui.inputBox.value !== this.value) {
			this.ui.inputBox.value = this.value;
		}
A
Alex Ross 已提交
710 711 712 713
		if (this.valueSelectionUpdated) {
			this.valueSelectionUpdated = false;
			this.ui.inputBox.select(this._valueSelection && { start: this._valueSelection[0], end: this._valueSelection[1] });
		}
714 715
		if (this.ui.inputBox.placeholder !== (this.placeholder || '')) {
			this.ui.inputBox.placeholder = (this.placeholder || '');
716 717
		}
		if (this.itemsUpdated) {
718
			this.itemsUpdated = false;
719 720 721
			this.ui.list.setElements(this.items);
			this.ui.list.filter(this.ui.inputBox.value);
			this.ui.checkAll.checked = this.ui.list.getAllVisibleChecked();
722
			this.ui.visibleCount.setCount(this.ui.list.getVisibleCount());
723
			this.ui.count.setCount(this.ui.list.getCheckedCount());
A
Alex Ross 已提交
724
			this.trySelectFirst();
725
		}
726
		if (this.ui.container.classList.contains('show-checkboxes') !== !!this.canSelectMany) {
727 728
			if (this.canSelectMany) {
				this.ui.list.clearFocus();
A
Alex Ross 已提交
729 730
			} else {
				this.trySelectFirst();
731 732
			}
		}
733 734
		if (this.activeItemsUpdated) {
			this.activeItemsUpdated = false;
735
			this.activeItemsToConfirm = this._activeItems;
736
			this.ui.list.setFocusedElements(this.activeItems);
737 738 739
			if (this.activeItemsToConfirm === this._activeItems) {
				this.activeItemsToConfirm = null;
			}
740 741 742
		}
		if (this.selectedItemsUpdated) {
			this.selectedItemsUpdated = false;
743
			this.selectedItemsToConfirm = this._selectedItems;
744 745 746 747 748
			if (this.canSelectMany) {
				this.ui.list.setCheckedElements(this.selectedItems);
			} else {
				this.ui.list.setSelectedElements(this.selectedItems);
			}
749 750 751
			if (this.selectedItemsToConfirm === this._selectedItems) {
				this.selectedItemsToConfirm = null;
			}
752
		}
753 754
		if (this.validationMessage) {
			this.ui.message.textContent = this.validationMessage;
755
			this.showMessageDecoration(Severity.Error);
756 757
		} else {
			this.ui.message.textContent = null;
758
			this.showMessageDecoration(Severity.Ignore);
759
		}
760 761
		this.ui.customButton.label = this.customLabel || '';
		this.ui.customButton.element.title = this.customHover || '';
762 763
		this.ui.list.matchOnDescription = this.matchOnDescription;
		this.ui.list.matchOnDetail = this.matchOnDetail;
A
Alex Ross 已提交
764
		this.ui.list.matchOnLabel = this.matchOnLabel;
765
		this.ui.setComboboxAccessibility(true);
766
		this.ui.inputBox.setAttribute('aria-label', QuickPick.INPUT_BOX_ARIA_LABEL);
767
	}
768
}
C
Christof Marti 已提交
769

770 771
class InputBox extends QuickInput implements IInputBox {

772
	private static readonly noPromptMessage = localize('inputModeEntry', "Press 'Enter' to confirm your input or 'Escape' to cancel");
773 774

	private _value = '';
775
	private _valueSelection: Readonly<[number, number]> | undefined;
776
	private valueSelectionUpdated = true;
777
	private _placeholder: string | undefined;
778
	private _password = false;
779
	private _prompt: string | undefined;
780
	private noValidationMessage = InputBox.noPromptMessage;
781
	private _validationMessage: string | undefined;
782 783
	private readonly onDidValueChangeEmitter = this._register(new Emitter<string>());
	private readonly onDidAcceptEmitter = this._register(new Emitter<void>());
784

785 786 787
	get value() {
		return this._value;
	}
788

789 790 791 792
	set value(value: string) {
		this._value = value || '';
		this.update();
	}
793

794 795 796 797
	set valueSelection(valueSelection: Readonly<[number, number]>) {
		this._valueSelection = valueSelection;
		this.valueSelectionUpdated = true;
		this.update();
798
	}
799

800 801
	get placeholder() {
		return this._placeholder;
802
	}
803

804
	set placeholder(placeholder: string | undefined) {
805
		this._placeholder = placeholder;
806 807 808 809 810 811 812 813
		this.update();
	}

	get password() {
		return this._password;
	}

	set password(password: boolean) {
814
		this._password = password;
815 816 817 818 819 820 821
		this.update();
	}

	get prompt() {
		return this._prompt;
	}

822
	set prompt(prompt: string | undefined) {
823
		this._prompt = prompt;
824 825 826 827 828 829 830 831 832 833
		this.noValidationMessage = prompt
			? localize('inputModeEntryDescription', "{0} (Press 'Enter' to confirm or 'Escape' to cancel)", prompt)
			: InputBox.noPromptMessage;
		this.update();
	}

	get validationMessage() {
		return this._validationMessage;
	}

834
	set validationMessage(validationMessage: string | undefined) {
835
		this._validationMessage = validationMessage;
836 837 838
		this.update();
	}

839
	readonly onDidChangeValue = this.onDidValueChangeEmitter.event;
840

841
	readonly onDidAccept = this.onDidAcceptEmitter.event;
842 843 844

	show() {
		if (!this.visible) {
845
			this.visibleDisposables.add(
846 847 848
				this.ui.inputBox.onDidChange(value => {
					if (value === this.value) {
						return;
849
					}
850 851
					this._value = value;
					this.onDidValueChangeEmitter.fire(value);
852 853
				}));
			this.visibleDisposables.add(this.ui.onDidAccept(() => this.onDidAcceptEmitter.fire(undefined)));
854
			this.valueSelectionUpdated = true;
855
		}
856
		super.show();
857 858
	}

859 860 861 862
	protected update() {
		if (!this.visible) {
			return;
		}
863 864
		this.ui.setVisibilities({ title: !!this.title || !!this.step, inputBox: true, message: true });
		super.update();
865 866 867 868 869
		if (this.ui.inputBox.value !== this.value) {
			this.ui.inputBox.value = this.value;
		}
		if (this.valueSelectionUpdated) {
			this.valueSelectionUpdated = false;
870
			this.ui.inputBox.select(this._valueSelection && { start: this._valueSelection[0], end: this._valueSelection[1] });
871
		}
872 873
		if (this.ui.inputBox.placeholder !== (this.placeholder || '')) {
			this.ui.inputBox.placeholder = (this.placeholder || '');
874 875 876 877 878 879
		}
		if (this.ui.inputBox.password !== this.password) {
			this.ui.inputBox.password = this.password;
		}
		if (!this.validationMessage && this.ui.message.textContent !== this.noValidationMessage) {
			this.ui.message.textContent = this.noValidationMessage;
880
			this.showMessageDecoration(Severity.Ignore);
881 882 883
		}
		if (this.validationMessage && this.ui.message.textContent !== this.validationMessage) {
			this.ui.message.textContent = this.validationMessage;
884
			this.showMessageDecoration(Severity.Error);
885
		}
886 887 888
	}
}

C
Christof Marti 已提交
889 890
export class QuickInputService extends Component implements IQuickInputService {

891
	public _serviceBrand: undefined;
C
Christof Marti 已提交
892 893

	private static readonly ID = 'workbench.component.quickinput';
894
	private static readonly MAX_WIDTH = 600; // Max total width of quick open widget
C
Christof Marti 已提交
895

896
	private idPrefix = 'quickInput_'; // Constant since there is still only one.
897
	private ui: QuickInputUI | undefined;
C
Christof Marti 已提交
898
	private dimension?: dom.Dimension;
899
	private comboboxAccessibility = false;
C
Christof Marti 已提交
900
	private enabled = true;
C
Christof Marti 已提交
901
	private inQuickOpenWidgets: Record<string, boolean> = {};
902
	private inQuickOpenContext: IContextKey<boolean>;
903
	private contexts: Map<string, IContextKey<boolean>> = new Map();
904 905 906
	private readonly onDidAcceptEmitter = this._register(new Emitter<void>());
	private readonly onDidCustomEmitter = this._register(new Emitter<void>());
	private readonly onDidTriggerButtonEmitter = this._register(new Emitter<IQuickInputButton>());
907
	private keyMods: Writeable<IKeyMods> = { ctrlCmd: false, alt: false };
C
Christof Marti 已提交
908

909
	private controller: QuickInput | null = null;
C
Christof Marti 已提交
910 911

	constructor(
912 913 914 915 916 917 918
		@IEnvironmentService private readonly environmentService: IEnvironmentService,
		@IConfigurationService private readonly configurationService: IConfigurationService,
		@IInstantiationService private readonly instantiationService: IInstantiationService,
		@IQuickOpenService private readonly quickOpenService: IQuickOpenService,
		@IEditorGroupsService private readonly editorGroupService: IEditorGroupsService,
		@IKeybindingService private readonly keybindingService: IKeybindingService,
		@IContextKeyService private readonly contextKeyService: IContextKeyService,
B
Benjamin Pasero 已提交
919
		@IThemeService themeService: IThemeService,
920
		@IStorageService storageService: IStorageService,
921
		@IAccessibilityService private readonly accessibilityService: IAccessibilityService,
922
		@IWorkbenchLayoutService private readonly layoutService: IWorkbenchLayoutService
C
Christof Marti 已提交
923
	) {
924
		super(QuickInputService.ID, themeService, storageService);
925
		this.inQuickOpenContext = InQuickOpenContextKey.bindTo(contextKeyService);
B
Benjamin Pasero 已提交
926 927
		this._register(this.quickOpenService.onShow(() => this.inQuickOpen('quickOpen', true)));
		this._register(this.quickOpenService.onHide(() => this.inQuickOpen('quickOpen', false)));
928
		this._register(this.layoutService.onLayout(dimension => this.layout(dimension)));
929
		this.registerKeyModsListeners();
930 931
	}

C
Christof Marti 已提交
932 933 934
	private inQuickOpen(widget: 'quickInput' | 'quickOpen', open: boolean) {
		if (open) {
			this.inQuickOpenWidgets[widget] = true;
935
		} else {
C
Christof Marti 已提交
936 937 938 939 940 941 942 943 944 945
			delete this.inQuickOpenWidgets[widget];
		}
		if (Object.keys(this.inQuickOpenWidgets).length) {
			if (!this.inQuickOpenContext.get()) {
				this.inQuickOpenContext.set(true);
			}
		} else {
			if (this.inQuickOpenContext.get()) {
				this.inQuickOpenContext.reset();
			}
946
		}
C
Christof Marti 已提交
947 948
	}

C
Christof Marti 已提交
949
	private setContextKey(id?: string) {
950
		let key: IContextKey<boolean> | undefined;
C
Christof Marti 已提交
951
		if (id) {
952
			key = this.contexts.get(id);
C
Christof Marti 已提交
953 954 955
			if (!key) {
				key = new RawContextKey<boolean>(id, false)
					.bindTo(this.contextKeyService);
956
				this.contexts.set(id, key);
C
Christof Marti 已提交
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
			}
		}

		if (key && key.get()) {
			return; // already active context
		}

		this.resetContextKeys();

		if (key) {
			key.set(true);
		}
	}

	private resetContextKeys() {
972 973 974
		this.contexts.forEach(context => {
			if (context.get()) {
				context.reset();
C
Christof Marti 已提交
975
			}
976
		});
C
Christof Marti 已提交
977 978
	}

979
	private registerKeyModsListeners() {
980
		const workbench = this.layoutService.getWorkbenchElement();
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
		this._register(dom.addDisposableListener(workbench, dom.EventType.KEY_DOWN, (e: KeyboardEvent) => {
			const event = new StandardKeyboardEvent(e);
			switch (event.keyCode) {
				case KeyCode.Ctrl:
				case KeyCode.Meta:
					this.keyMods.ctrlCmd = true;
					break;
				case KeyCode.Alt:
					this.keyMods.alt = true;
					break;
			}
		}));
		this._register(dom.addDisposableListener(workbench, dom.EventType.KEY_UP, (e: KeyboardEvent) => {
			const event = new StandardKeyboardEvent(e);
			switch (event.keyCode) {
				case KeyCode.Ctrl:
				case KeyCode.Meta:
					this.keyMods.ctrlCmd = false;
					break;
				case KeyCode.Alt:
					this.keyMods.alt = false;
					break;
			}
		}));
	}

1007
	private getUI() {
C
Christof Marti 已提交
1008
		if (this.ui) {
1009
			return this.ui;
C
Christof Marti 已提交
1010 1011
		}

1012
		const workbench = this.layoutService.getWorkbenchElement();
C
Christof Marti 已提交
1013
		const container = dom.append(workbench, $('.quick-input-widget.show-file-icons'));
C
Christof Marti 已提交
1014 1015
		container.tabIndex = -1;
		container.style.display = 'none';
C
Christof Marti 已提交
1016

1017
		const titleBar = dom.append(container, $('.quick-input-titlebar'));
1018

1019
		const leftActionBar = this._register(new ActionBar(titleBar));
C
Christof Marti 已提交
1020 1021
		leftActionBar.domNode.classList.add('quick-input-left-action-bar');

1022
		const title = dom.append(titleBar, $('.quick-input-title'));
1023

1024
		const rightActionBar = this._register(new ActionBar(titleBar));
C
Christof Marti 已提交
1025
		rightActionBar.domNode.classList.add('quick-input-right-action-bar');
C
Christof Marti 已提交
1026

C
Christof Marti 已提交
1027
		const headerContainer = dom.append(container, $('.quick-input-header'));
1028

1029 1030
		const checkAll = <HTMLInputElement>dom.append(headerContainer, $('input.quick-input-check-all'));
		checkAll.type = 'checkbox';
B
Benjamin Pasero 已提交
1031
		this._register(dom.addStandardDisposableListener(checkAll, dom.EventType.CHANGE, e => {
1032
			const checked = checkAll.checked;
1033
			list.setAllVisibleChecked(checked);
C
Christof Marti 已提交
1034
		}));
B
Benjamin Pasero 已提交
1035
		this._register(dom.addDisposableListener(checkAll, dom.EventType.CLICK, e => {
1036
			if (e.x || e.y) { // Avoid 'click' triggered by 'space'...
1037
				inputBox.setFocus();
1038 1039
			}
		}));
C
Christof Marti 已提交
1040

1041
		const extraContainer = dom.append(headerContainer, $('.quick-input-and-message'));
1042
		const filterContainer = dom.append(extraContainer, $('.quick-input-filter'));
1043

1044
		const inputBox = this._register(new QuickInputBox(filterContainer));
1045
		inputBox.setAttribute('aria-describedby', `${this.idPrefix}message`);
1046

1047 1048 1049 1050
		const visibleCountContainer = dom.append(filterContainer, $('.quick-input-visible-count'));
		visibleCountContainer.setAttribute('aria-live', 'polite');
		visibleCountContainer.setAttribute('aria-atomic', 'true');
		const visibleCount = new CountBadge(visibleCountContainer, { countFormat: localize({ key: 'quickInput.visibleCount', comment: ['This tells the user how many items are shown in a list of items to select from. The items can be anything. Currently not visible, but read by screen readers.'] }, "{0} Results") });
1051

1052 1053 1054
		const countContainer = dom.append(filterContainer, $('.quick-input-count'));
		countContainer.setAttribute('aria-live', 'polite');
		const count = new CountBadge(countContainer, { countFormat: localize({ key: 'quickInput.countSelected', comment: ['This tells the user how many items are selected in a list of items to select from. The items can be anything.'] }, "{0} Selected") });
B
Benjamin Pasero 已提交
1055
		this._register(attachBadgeStyler(count, this.themeService));
1056

1057 1058 1059 1060 1061
		const okContainer = dom.append(headerContainer, $('.quick-input-action'));
		const ok = new Button(okContainer);
		attachButtonStyler(ok, this.themeService);
		ok.label = localize('ok', "OK");
		this._register(ok.onDidClick(e => {
C
Christof Marti 已提交
1062
			this.onDidAcceptEmitter.fire();
C
Christof Marti 已提交
1063 1064
		}));

1065 1066
		const customButtonContainer = dom.append(headerContainer, $('.quick-input-action'));
		const customButton = new Button(customButtonContainer);
1067 1068 1069 1070 1071 1072
		attachButtonStyler(customButton, this.themeService);
		customButton.label = localize('custom', "Custom");
		this._register(customButton.onDidClick(e => {
			this.onDidCustomEmitter.fire();
		}));

1073
		const message = dom.append(extraContainer, $(`#${this.idPrefix}message.quick-input-message`));
1074

1075 1076
		const progressBar = new ProgressBar(container);
		dom.addClass(progressBar.getContainer(), 'quick-input-progress');
B
Benjamin Pasero 已提交
1077
		this._register(attachProgressBarStyler(progressBar, this.themeService));
1078

1079
		const list = this._register(this.instantiationService.createInstance(QuickInputList, container, this.idPrefix + 'list'));
B
Benjamin Pasero 已提交
1080
		this._register(list.onChangedAllVisibleChecked(checked => {
1081
			checkAll.checked = checked;
C
Christof Marti 已提交
1082
		}));
1083 1084 1085
		this._register(list.onChangedVisibleCount(c => {
			visibleCount.setCount(c);
		}));
B
Benjamin Pasero 已提交
1086
		this._register(list.onChangedCheckedCount(c => {
1087
			count.setCount(c);
C
Christof Marti 已提交
1088
		}));
B
Benjamin Pasero 已提交
1089
		this._register(list.onLeave(() => {
1090 1091
			// Defer to avoid the input field reacting to the triggering key.
			setTimeout(() => {
1092
				inputBox.setFocus();
C
Christof Marti 已提交
1093 1094 1095
				if (this.controller instanceof QuickPick && this.controller.canSelectMany) {
					list.clearFocus();
				}
1096 1097
			}, 0);
		}));
1098 1099
		this._register(list.onDidChangeFocus(() => {
			if (this.comboboxAccessibility) {
1100
				this.getUI().inputBox.setAttribute('aria-activedescendant', this.getUI().list.getActiveDescendant() || '');
1101 1102
			}
		}));
C
Christof Marti 已提交
1103

1104 1105 1106
		const focusTracker = dom.trackFocus(container);
		this._register(focusTracker);
		this._register(focusTracker.onDidBlur(() => {
1107
			if (!this.getUI().ignoreFocusOut && !this.environmentService.args['sticky-quickopen'] && this.configurationService.getValue(CLOSE_ON_FOCUS_LOST_CONFIG)) {
1108
				this.hide(true);
1109
			}
C
Christof Marti 已提交
1110
		}));
1111 1112 1113
		this._register(dom.addDisposableListener(container, dom.EventType.FOCUS, (e: FocusEvent) => {
			inputBox.setFocus();
		}));
B
Benjamin Pasero 已提交
1114
		this._register(dom.addDisposableListener(container, dom.EventType.KEY_DOWN, (e: KeyboardEvent) => {
C
Christof Marti 已提交
1115 1116 1117
			const event = new StandardKeyboardEvent(e);
			switch (event.keyCode) {
				case KeyCode.Enter:
1118 1119
					dom.EventHelper.stop(e, true);
					this.onDidAcceptEmitter.fire();
C
Christof Marti 已提交
1120 1121 1122
					break;
				case KeyCode.Escape:
					dom.EventHelper.stop(e, true);
1123
					this.hide();
C
Christof Marti 已提交
1124
					break;
1125 1126
				case KeyCode.Tab:
					if (!event.altKey && !event.ctrlKey && !event.metaKey) {
C
Christof Marti 已提交
1127
						const selectors = ['.action-label.codicon'];
C
Christof Marti 已提交
1128
						if (container.classList.contains('show-checkboxes')) {
1129
							selectors.push('input');
C
Christof Marti 已提交
1130
						} else {
1131
							selectors.push('input[type=text]');
C
Christof Marti 已提交
1132
						}
1133
						if (this.getUI().list.isDisplayed()) {
1134 1135 1136 1137
							selectors.push('.monaco-list');
						}
						const stops = container.querySelectorAll<HTMLElement>(selectors.join(', '));
						if (event.shiftKey && event.target === stops[0]) {
1138
							dom.EventHelper.stop(e, true);
1139 1140
							stops[stops.length - 1].focus();
						} else if (!event.shiftKey && event.target === stops[stops.length - 1]) {
1141
							dom.EventHelper.stop(e, true);
1142
							stops[0].focus();
1143 1144 1145
						}
					}
					break;
C
Christof Marti 已提交
1146 1147
			}
		}));
1148

B
Benjamin Pasero 已提交
1149
		this._register(this.quickOpenService.onShow(() => this.hide(true)));
1150 1151 1152

		this.ui = {
			container,
C
Christof Marti 已提交
1153
			leftActionBar,
1154
			titleBar,
C
Christof Marti 已提交
1155
			title,
C
Christof Marti 已提交
1156
			rightActionBar,
1157
			checkAll,
1158
			filterContainer,
1159
			inputBox,
1160
			visibleCountContainer,
1161
			visibleCount,
1162
			countContainer,
1163
			count,
1164 1165
			okContainer,
			ok,
1166
			message,
1167
			customButtonContainer,
1168
			customButton,
1169 1170 1171
			progressBar,
			list,
			onDidAccept: this.onDidAcceptEmitter.event,
1172
			onDidCustom: this.onDidCustomEmitter.event,
C
Christof Marti 已提交
1173
			onDidTriggerButton: this.onDidTriggerButtonEmitter.event,
1174
			ignoreFocusOut: false,
1175
			keyMods: this.keyMods,
1176
			isScreenReaderOptimized: () => this.isScreenReaderOptimized(),
1177 1178
			show: controller => this.show(controller),
			hide: () => this.hide(),
C
Christof Marti 已提交
1179
			setVisibilities: visibilities => this.setVisibilities(visibilities),
1180
			setComboboxAccessibility: enabled => this.setComboboxAccessibility(enabled),
C
Christof Marti 已提交
1181
			setEnabled: enabled => this.setEnabled(enabled),
C
Christof Marti 已提交
1182
			setContextKey: contextKey => this.setContextKey(contextKey),
1183
		};
1184
		this.updateStyles();
1185
		return this.ui;
C
Christof Marti 已提交
1186 1187
	}

C
Christof Marti 已提交
1188 1189
	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> {
		return new Promise<O extends { canPickMany: true } ? T[] : T>((doResolve, reject) => {
C
Christof Marti 已提交
1190 1191 1192 1193 1194 1195 1196
			let resolve = (result: any) => {
				resolve = doResolve;
				if (options.onKeyMods) {
					options.onKeyMods(input.keyMods);
				}
				doResolve(result);
			};
1197 1198 1199
			if (token.isCancellationRequested) {
				resolve(undefined);
				return;
1200
			}
1201
			const input = this.createQuickPick<T>();
1202
			let activeItem: T | undefined;
1203 1204 1205 1206
			const disposables = [
				input,
				input.onDidAccept(() => {
					if (input.canSelectMany) {
1207
						resolve(<any>input.selectedItems.slice());
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
						input.hide();
					} else {
						const result = input.activeItems[0];
						if (result) {
							resolve(<any>result);
							input.hide();
						}
					}
				}),
				input.onDidChangeActive(items => {
					const focused = items[0];
J
Joao Moreno 已提交
1219 1220
					if (focused && options.onDidFocus) {
						options.onDidFocus(focused);
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
					}
				}),
				input.onDidChangeSelection(items => {
					if (!input.canSelectMany) {
						const result = items[0];
						if (result) {
							resolve(<any>result);
							input.hide();
						}
					}
				}),
C
Christof Marti 已提交
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
				input.onDidTriggerItemButton(event => options.onDidTriggerItemButton && options.onDidTriggerItemButton({
					...event,
					removeItem: () => {
						const index = input.items.indexOf(event.item);
						if (index !== -1) {
							const items = input.items.slice();
							items.splice(index, 1);
							input.items = items;
						}
					}
				})),
1243 1244 1245 1246 1247
				input.onDidChangeValue(value => {
					if (activeItem && !value && (input.activeItems.length !== 1 || input.activeItems[0] !== activeItem)) {
						input.activeItems = [activeItem];
					}
				}),
1248 1249 1250 1251 1252 1253 1254 1255
				token.onCancellationRequested(() => {
					input.hide();
				}),
				input.onDidHide(() => {
					dispose(disposables);
					resolve(undefined);
				}),
			];
1256
			input.canSelectMany = !!options.canPickMany;
1257
			input.placeholder = options.placeHolder;
1258 1259 1260
			input.ignoreFocusOut = !!options.ignoreFocusLost;
			input.matchOnDescription = !!options.matchOnDescription;
			input.matchOnDetail = !!options.matchOnDetail;
A
Alex Ross 已提交
1261 1262
			input.matchOnLabel = (options.matchOnLabel === undefined) || options.matchOnLabel; // default to true
			input.autoFocusOnList = (options.autoFocusOnList === undefined) || options.autoFocusOnList; // default to true
C
Christof Marti 已提交
1263
			input.quickNavigate = options.quickNavigate;
C
Christof Marti 已提交
1264
			input.contextKey = options.contextKey;
1265
			input.busy = true;
1266
			Promise.all<QuickPickInput<T>[], T | undefined>([picks, options.activeItem])
1267 1268
				.then(([items, _activeItem]) => {
					activeItem = _activeItem;
C
Christof Marti 已提交
1269 1270 1271
					input.busy = false;
					input.items = items;
					if (input.canSelectMany) {
C
Christof Marti 已提交
1272
						input.selectedItems = items.filter(item => item.type !== 'separator' && item.picked) as T[];
C
Christof Marti 已提交
1273 1274 1275 1276 1277
					}
					if (activeItem) {
						input.activeItems = [activeItem];
					}
				});
1278
			input.show();
R
Rob Lourens 已提交
1279
			Promise.resolve(picks).then(undefined, err => {
1280 1281 1282 1283
				reject(err);
				input.hide();
			});
		});
C
Christof Marti 已提交
1284 1285
	}

C
Christof Marti 已提交
1286 1287
	input(options: IInputOptions = {}, token: CancellationToken = CancellationToken.None): Promise<string> {
		return new Promise<string>((resolve, reject) => {
1288 1289 1290 1291 1292
			if (token.isCancellationRequested) {
				resolve(undefined);
				return;
			}
			const input = this.createInputBox();
J
Johannes Rieken 已提交
1293
			const validateInput = options.validateInput || (() => <Promise<undefined>>Promise.resolve(undefined));
J
Joao Moreno 已提交
1294
			const onDidValueChange = Event.debounce(input.onDidChangeValue, (last, cur) => cur, 100);
1295
			let validationValue = options.value || '';
C
Christof Marti 已提交
1296
			let validation = Promise.resolve(validateInput(validationValue));
1297 1298 1299 1300
			const disposables = [
				input,
				onDidValueChange(value => {
					if (value !== validationValue) {
C
Christof Marti 已提交
1301
						validation = Promise.resolve(validateInput(value));
1302
						validationValue = value;
1303 1304
					}
					validation.then(result => {
1305
						if (value === validationValue) {
1306
							input.validationMessage = result || undefined;
1307
						}
1308 1309 1310 1311 1312
					});
				}),
				input.onDidAccept(() => {
					const value = input.value;
					if (value !== validationValue) {
C
Christof Marti 已提交
1313
						validation = Promise.resolve(validateInput(value));
1314
						validationValue = value;
1315 1316 1317 1318 1319
					}
					validation.then(result => {
						if (!result) {
							resolve(value);
							input.hide();
1320 1321
						} else if (value === validationValue) {
							input.validationMessage = result;
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
						}
					});
				}),
				token.onCancellationRequested(() => {
					input.hide();
				}),
				input.onDidHide(() => {
					dispose(disposables);
					resolve(undefined);
				}),
			];
1333
			input.value = options.value || '';
1334 1335 1336
			input.valueSelection = options.valueSelection;
			input.prompt = options.prompt;
			input.placeholder = options.placeHolder;
1337 1338
			input.password = !!options.password;
			input.ignoreFocusOut = !!options.ignoreFocusLost;
1339 1340
			input.show();
		});
1341 1342
	}

C
Christof Marti 已提交
1343
	backButton = backButton;
C
Christof Marti 已提交
1344

1345
	createQuickPick<T extends IQuickPickItem>(): IQuickPick<T> {
1346 1347
		const ui = this.getUI();
		return new QuickPick<T>(ui);
C
Christof Marti 已提交
1348 1349
	}

1350
	createInputBox(): IInputBox {
1351 1352
		const ui = this.getUI();
		return new InputBox(ui);
1353 1354
	}

1355
	private show(controller: QuickInput) {
1356
		const ui = this.getUI();
1357
		this.quickOpenService.close();
1358 1359 1360 1361
		const oldController = this.controller;
		this.controller = controller;
		if (oldController) {
			oldController.didHide();
C
Christof Marti 已提交
1362
		}
C
Christof Marti 已提交
1363

C
Christof Marti 已提交
1364
		this.setEnabled(true);
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
		ui.leftActionBar.clear();
		ui.title.textContent = '';
		ui.rightActionBar.clear();
		ui.checkAll.checked = false;
		// ui.inputBox.value = ''; Avoid triggering an event.
		ui.inputBox.placeholder = '';
		ui.inputBox.password = false;
		ui.inputBox.showDecoration(Severity.Ignore);
		ui.visibleCount.setCount(0);
		ui.count.setCount(0);
		ui.message.textContent = '';
		ui.progressBar.stop();
		ui.list.setElements([]);
		ui.list.matchOnDescription = false;
		ui.list.matchOnDetail = false;
		ui.list.matchOnLabel = true;
		ui.ignoreFocusOut = false;
1382
		this.setComboboxAccessibility(false);
1383
		ui.inputBox.removeAttribute('aria-label');
1384

C
Christof Marti 已提交
1385 1386 1387
		const keybinding = this.keybindingService.lookupKeybinding(BackAction.ID);
		backButton.tooltip = keybinding ? localize('quickInput.backWithKeybinding', "Back ({0})", keybinding.getLabel()) : localize('quickInput.back', "Back");

1388
		this.inQuickOpen('quickInput', true);
C
Christof Marti 已提交
1389
		this.resetContextKeys();
C
Christof Marti 已提交
1390

1391
		ui.container.style.display = '';
C
Christof Marti 已提交
1392
		this.updateLayout();
1393
		ui.inputBox.setFocus();
C
Christof Marti 已提交
1394 1395
	}

1396
	private setVisibilities(visibilities: Visibilities) {
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
		const ui = this.getUI();
		ui.title.style.display = visibilities.title ? '' : 'none';
		ui.checkAll.style.display = visibilities.checkAll ? '' : 'none';
		ui.filterContainer.style.display = visibilities.inputBox ? '' : 'none';
		ui.visibleCountContainer.style.display = visibilities.visibleCount ? '' : 'none';
		ui.countContainer.style.display = visibilities.count ? '' : 'none';
		ui.okContainer.style.display = visibilities.ok ? '' : 'none';
		ui.customButtonContainer.style.display = visibilities.customButton ? '' : 'none';
		ui.message.style.display = visibilities.message ? '' : 'none';
		ui.list.display(!!visibilities.list);
		ui.container.classList[visibilities.checkAll ? 'add' : 'remove']('show-checkboxes');
1408
		this.updateLayout(); // TODO
1409 1410
	}

1411 1412
	private setComboboxAccessibility(enabled: boolean) {
		if (enabled !== this.comboboxAccessibility) {
1413
			const ui = this.getUI();
1414 1415
			this.comboboxAccessibility = enabled;
			if (this.comboboxAccessibility) {
1416 1417 1418 1419
				ui.inputBox.setAttribute('role', 'combobox');
				ui.inputBox.setAttribute('aria-haspopup', 'true');
				ui.inputBox.setAttribute('aria-autocomplete', 'list');
				ui.inputBox.setAttribute('aria-activedescendant', ui.list.getActiveDescendant() || '');
1420
			} else {
1421 1422 1423 1424
				ui.inputBox.removeAttribute('role');
				ui.inputBox.removeAttribute('aria-haspopup');
				ui.inputBox.removeAttribute('aria-autocomplete');
				ui.inputBox.removeAttribute('aria-activedescendant');
1425 1426 1427 1428
			}
		}
	}

1429
	private isScreenReaderOptimized() {
1430
		const detected = this.accessibilityService.getAccessibilitySupport() === AccessibilitySupport.Enabled;
1431 1432 1433 1434
		const config = this.configurationService.getValue<IEditorOptions>('editor').accessibilitySupport;
		return config === 'on' || (config === 'auto' && detected);
	}

C
Christof Marti 已提交
1435 1436 1437
	private setEnabled(enabled: boolean) {
		if (enabled !== this.enabled) {
			this.enabled = enabled;
1438
			for (const item of this.getUI().leftActionBar.viewItems) {
1439
				(item as ActionViewItem).getAction().enabled = enabled;
C
Christof Marti 已提交
1440
			}
1441
			for (const item of this.getUI().rightActionBar.viewItems) {
1442
				(item as ActionViewItem).getAction().enabled = enabled;
C
Christof Marti 已提交
1443
			}
1444 1445 1446 1447
			this.getUI().checkAll.disabled = !enabled;
			// this.getUI().inputBox.enabled = enabled; Avoid loosing focus.
			this.getUI().ok.enabled = enabled;
			this.getUI().list.enabled = enabled;
C
Christof Marti 已提交
1448 1449 1450
		}
	}

1451 1452 1453 1454 1455
	private hide(focusLost?: boolean) {
		const controller = this.controller;
		if (controller) {
			this.controller = null;
			this.inQuickOpen('quickInput', false);
C
Christof Marti 已提交
1456
			this.resetContextKeys();
1457
			this.getUI().container.style.display = 'none';
1458 1459 1460 1461
			if (!focusLost) {
				this.editorGroupService.activeGroup.focus();
			}
			controller.didHide();
C
Christof Marti 已提交
1462 1463 1464
		}
	}

C
Christof Marti 已提交
1465
	focus() {
C
Christof Marti 已提交
1466
		if (this.isDisplayed()) {
1467
			this.getUI().inputBox.setFocus();
C
Christof Marti 已提交
1468 1469 1470
		}
	}

C
Christof Marti 已提交
1471
	toggle() {
1472
		if (this.isDisplayed() && this.controller instanceof QuickPick && this.controller.canSelectMany) {
1473
			this.getUI().list.toggleCheckbox();
C
Christof Marti 已提交
1474 1475 1476
		}
	}

C
Christof Marti 已提交
1477
	navigate(next: boolean, quickNavigate?: IQuickNavigateConfiguration) {
1478 1479
		if (this.isDisplayed() && this.getUI().list.isDisplayed()) {
			this.getUI().list.focus(next ? 'Next' : 'Previous');
1480
			if (quickNavigate && this.controller instanceof QuickPick) {
C
Christof Marti 已提交
1481
				this.controller.quickNavigate = quickNavigate;
C
Christof Marti 已提交
1482
			}
1483 1484 1485
		}
	}

C
Christof Marti 已提交
1486
	accept() {
1487
		this.onDidAcceptEmitter.fire();
C
Christof Marti 已提交
1488
		return Promise.resolve(undefined);
C
Christof Marti 已提交
1489 1490
	}

C
Christof Marti 已提交
1491 1492
	back() {
		this.onDidTriggerButtonEmitter.fire(this.backButton);
C
Christof Marti 已提交
1493
		return Promise.resolve(undefined);
C
Christof Marti 已提交
1494 1495
	}

C
Christof Marti 已提交
1496
	cancel() {
1497
		this.hide();
C
Christof Marti 已提交
1498
		return Promise.resolve(undefined);
C
Christof Marti 已提交
1499 1500 1501
	}

	layout(dimension: dom.Dimension): void {
C
Christof Marti 已提交
1502
		this.dimension = dimension;
C
Christof Marti 已提交
1503 1504 1505 1506
		this.updateLayout();
	}

	private updateLayout() {
1507
		if (this.ui) {
1508
			const titlebarOffset = this.layoutService.getTitleBarOffset();
C
Christof Marti 已提交
1509
			this.ui.container.style.top = `${titlebarOffset}px`;
C
Christof Marti 已提交
1510

C
Christof Marti 已提交
1511
			const style = this.ui.container.style;
1512
			const width = Math.min(this.layoutService.dimension.width * 0.62 /* golden cut */, QuickInputService.MAX_WIDTH);
C
Christof Marti 已提交
1513 1514 1515
			style.width = width + 'px';
			style.marginLeft = '-' + (width / 2) + 'px';

1516
			this.ui.inputBox.layout();
C
Christof Marti 已提交
1517
			this.ui.list.layout(this.dimension && this.dimension.height * 0.6);
C
Christof Marti 已提交
1518 1519
		}
	}
1520 1521

	protected updateStyles() {
1522
		const theme = this.themeService.getTheme();
1523
		if (this.ui) {
C
Christof Marti 已提交
1524 1525
			// TODO
			const titleColor = { dark: 'rgba(255, 255, 255, 0.105)', light: 'rgba(0,0,0,.06)', hc: 'black' }[theme.type];
1526
			this.ui.titleBar.style.backgroundColor = titleColor ? titleColor.toString() : '';
1527
			this.ui.inputBox.style(theme);
1528
			const quickInputBackground = theme.getColor(QUICK_INPUT_BACKGROUND);
M
Matt Bierner 已提交
1529
			this.ui.container.style.backgroundColor = quickInputBackground ? quickInputBackground.toString() : '';
1530 1531
			const quickInputForeground = theme.getColor(QUICK_INPUT_FOREGROUND);
			this.ui.container.style.color = quickInputForeground ? quickInputForeground.toString() : null;
1532
			const contrastBorderColor = theme.getColor(contrastBorder);
M
Matt Bierner 已提交
1533
			this.ui.container.style.border = contrastBorderColor ? `1px solid ${contrastBorderColor}` : '';
1534
			const widgetShadowColor = theme.getColor(widgetShadow);
M
Matt Bierner 已提交
1535
			this.ui.container.style.boxShadow = widgetShadowColor ? `0 5px 8px ${widgetShadowColor}` : '';
1536
		}
1537
	}
1538 1539

	private isDisplayed() {
C
Christof Marti 已提交
1540
		return this.ui && this.ui.container.style.display !== 'none';
1541
	}
C
Christof Marti 已提交
1542
}
C
Christof Marti 已提交
1543

1544 1545
export const QuickPickManyToggle: ICommandAndKeybindingRule = {
	id: 'workbench.action.quickPickManyToggle',
1546
	weight: KeybindingWeight.WorkbenchContrib,
1547
	when: inQuickOpenContext,
1548
	primary: 0,
1549 1550 1551
	handler: accessor => {
		const quickInputService = accessor.get(IQuickInputService);
		quickInputService.toggle();
C
Christof Marti 已提交
1552
	}
1553
};
C
Christof Marti 已提交
1554 1555 1556 1557 1558 1559

export class BackAction extends Action {

	public static readonly ID = 'workbench.action.quickInputBack';
	public static readonly LABEL = localize('back', "Back");

1560
	constructor(id: string, label: string, @IQuickInputService private readonly quickInputService: IQuickInputService) {
C
Christof Marti 已提交
1561 1562 1563
		super(id, label);
	}

C
Christof Marti 已提交
1564
	public run(): Promise<any> {
C
Christof Marti 已提交
1565
		this.quickInputService.back();
1566
		return Promise.resolve();
C
Christof Marti 已提交
1567 1568
	}
}
1569

1570
registerSingleton(IQuickInputService, QuickInputService, true);