async show()

in src/vs/base/browser/ui/dialog/dialog.ts [141:274]


	async show(): Promise<IDialogResult> {
		this.focusToReturn = document.activeElement as HTMLElement;

		return new Promise<IDialogResult>((resolve) => {
			if (!this.element || !this.buttonsContainer || !this.iconElement || !this.toolbarContainer) {
				resolve({ button: 0 });
				return;
			}

			clearNode(this.buttonsContainer);

			let focusedButton = 0;
			const buttonGroup = this.buttonGroup = new ButtonGroup(this.buttonsContainer, this.buttons.length, { title: true });
			const buttonMap = this.rearrangeButtons(this.buttons, this.options.cancelId);

			// Set focused button to UI index
			buttonMap.forEach((value, index) => {
				if (value.index === 0) {
					focusedButton = index;
				}
			});

			buttonGroup.buttons.forEach((button, index) => {
				button.label = mnemonicButtonLabel(buttonMap[index].label, true);

				this._register(button.onDidClick(e => {
					EventHelper.stop(e);
					resolve({ button: buttonMap[index].index, checkboxChecked: this.checkbox ? this.checkbox.checked : undefined });
				}));
			});

			this._register(domEvent(window, 'keydown', true)((e: KeyboardEvent) => {
				const evt = new StandardKeyboardEvent(e);
				if (evt.equals(KeyCode.Enter) || evt.equals(KeyCode.Space)) {
					return;
				}

				let eventHandled = false;
				if (evt.equals(KeyMod.Shift | KeyCode.Tab) || evt.equals(KeyCode.LeftArrow)) {
					if (!this.checkboxHasFocus && focusedButton === 0) {
						if (this.checkbox) {
							this.checkbox.domNode.focus();
						}
						this.checkboxHasFocus = true;
					} else {
						focusedButton = (this.checkboxHasFocus ? 0 : focusedButton) + buttonGroup.buttons.length - 1;
						focusedButton = focusedButton % buttonGroup.buttons.length;
						buttonGroup.buttons[focusedButton].focus();
						this.checkboxHasFocus = false;
					}

					eventHandled = true;
				} else if (evt.equals(KeyCode.Tab) || evt.equals(KeyCode.RightArrow)) {
					if (!this.checkboxHasFocus && focusedButton === buttonGroup.buttons.length - 1) {
						if (this.checkbox) {
							this.checkbox.domNode.focus();
						}
						this.checkboxHasFocus = true;
					} else {
						focusedButton = this.checkboxHasFocus ? 0 : focusedButton + 1;
						focusedButton = focusedButton % buttonGroup.buttons.length;
						buttonGroup.buttons[focusedButton].focus();
						this.checkboxHasFocus = false;
					}
					eventHandled = true;
				}

				if (eventHandled) {
					EventHelper.stop(e, true);
				} else if (this.options.keyEventProcessor) {
					this.options.keyEventProcessor(evt);
				}
			}));

			this._register(domEvent(window, 'keyup', true)((e: KeyboardEvent) => {
				EventHelper.stop(e, true);
				const evt = new StandardKeyboardEvent(e);

				if (evt.equals(KeyCode.Escape)) {
					resolve({ button: this.options.cancelId || 0, checkboxChecked: this.checkbox ? this.checkbox.checked : undefined });
				}
			}));

			this._register(domEvent(this.element, 'focusout', false)((e: FocusEvent) => {
				if (!!e.relatedTarget && !!this.element) {
					if (!isAncestor(e.relatedTarget as HTMLElement, this.element)) {
						this.focusToReturn = e.relatedTarget as HTMLElement;

						if (e.target) {
							(e.target as HTMLElement).focus();
							EventHelper.stop(e, true);
						}
					}
				}
			}));

			removeClasses(this.iconElement, dialogErrorIcon.classNames, dialogWarningIcon.classNames, dialogInfoIcon.classNames, Codicon.loading.classNames);

			switch (this.options.type) {
				case 'error':
					addClasses(this.iconElement, dialogErrorIcon.classNames);
					break;
				case 'warning':
					addClasses(this.iconElement, dialogWarningIcon.classNames);
					break;
				case 'pending':
					addClasses(this.iconElement, Codicon.loading.classNames, 'codicon-animation-spin');
					break;
				case 'none':
				case 'info':
				case 'question':
				default:
					addClasses(this.iconElement, dialogInfoIcon.classNames);
					break;
			}

			const actionBar = new ActionBar(this.toolbarContainer, {});

			const action = new Action('dialog.close', nls.localize('dialogClose', "Close Dialog"), dialogCloseIcon.classNames, true, () => {
				resolve({ button: this.options.cancelId || 0, checkboxChecked: this.checkbox ? this.checkbox.checked : undefined });
				return Promise.resolve();
			});

			actionBar.push(action, { icon: true, label: false, });

			this.applyStyles();

			this.element.setAttribute('aria-label', this.getAriaLabel());
			show(this.element);

			// Focus first element
			buttonGroup.buttons[focusedButton].focus();
		});
	}