Path: blob/main/src/vs/workbench/browser/parts/dialogs/dialogHandler.ts
5263 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { localize } from '../../../../nls.js';6import { IConfirmation, IConfirmationResult, IInputResult, ICheckbox, IInputElement, ICustomDialogOptions, IInput, AbstractDialogHandler, DialogType, IPrompt, IAsyncPromptResult } from '../../../../platform/dialogs/common/dialogs.js';7import { ILayoutService } from '../../../../platform/layout/browser/layoutService.js';8import { ILogService } from '../../../../platform/log/common/log.js';9import Severity from '../../../../base/common/severity.js';10import { Dialog, IDialogResult } from '../../../../base/browser/ui/dialog/dialog.js';11import { DisposableStore } from '../../../../base/common/lifecycle.js';12import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';13import { IClipboardService } from '../../../../platform/clipboard/common/clipboardService.js';14import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';15import { IMarkdownRendererService, openLinkFromMarkdown } from '../../../../platform/markdown/browser/markdownRenderer.js';16import { IOpenerService } from '../../../../platform/opener/common/opener.js';17import { createWorkbenchDialogOptions } from '../../../../platform/dialogs/browser/dialog.js';1819export class BrowserDialogHandler extends AbstractDialogHandler {2021private static readonly ALLOWABLE_COMMANDS = new Set([22'copy',23'cut',24'editor.action.selectAll',25'editor.action.clipboardCopyAction',26'editor.action.clipboardCutAction',27'editor.action.clipboardPasteAction'28]);2930constructor(31@ILogService private readonly logService: ILogService,32@ILayoutService private readonly layoutService: ILayoutService,33@IKeybindingService private readonly keybindingService: IKeybindingService,34@IInstantiationService instantiationService: IInstantiationService,35@IClipboardService private readonly clipboardService: IClipboardService,36@IOpenerService private readonly openerService: IOpenerService,37@IMarkdownRendererService private readonly markdownRendererService: IMarkdownRendererService,38) {39super();40}4142async prompt<T>(prompt: IPrompt<T>): Promise<IAsyncPromptResult<T>> {43this.logService.trace('DialogService#prompt', prompt.message);4445const buttons = this.getPromptButtons(prompt);4647const { button, checkboxChecked } = await this.doShow(prompt.type, prompt.message, buttons, prompt.detail, prompt.cancelButton ? buttons.length - 1 : -1 /* Disabled */, prompt.checkbox, undefined, typeof prompt?.custom === 'object' ? prompt.custom : undefined);4849return this.getPromptResult(prompt, button, checkboxChecked);50}5152async confirm(confirmation: IConfirmation): Promise<IConfirmationResult> {53this.logService.trace('DialogService#confirm', confirmation.message);5455const buttons = this.getConfirmationButtons(confirmation);5657const { button, checkboxChecked } = await this.doShow(confirmation.type ?? 'question', confirmation.message, buttons, confirmation.detail, buttons.length - 1, confirmation.checkbox, undefined, typeof confirmation?.custom === 'object' ? confirmation.custom : undefined);5859return { confirmed: button === 0, checkboxChecked };60}6162async input(input: IInput): Promise<IInputResult> {63this.logService.trace('DialogService#input', input.message);6465const buttons = this.getInputButtons(input);6667const { button, checkboxChecked, values } = await this.doShow(input.type ?? 'question', input.message, buttons, input.detail, buttons.length - 1, input?.checkbox, input.inputs, typeof input.custom === 'object' ? input.custom : undefined);6869return { confirmed: button === 0, checkboxChecked, values };70}7172async about(title: string, details: string, detailsToCopy: string): Promise<void> {7374const { button } = await this.doShow(75Severity.Info,76title,77[78localize({ key: 'copy', comment: ['&& denotes a mnemonic'] }, "&&Copy"),79localize('ok', "OK")80],81details,82183);8485if (button === 0) {86this.clipboardService.writeText(detailsToCopy);87}88}8990private async doShow(type: Severity | DialogType | undefined, message: string, buttons?: string[], detail?: string, cancelId?: number, checkbox?: ICheckbox, inputs?: IInputElement[], customOptions?: ICustomDialogOptions): Promise<IDialogResult> {91const dialogDisposables = new DisposableStore();9293const renderBody = customOptions ? (parent: HTMLElement) => {94parent.classList.add(...(customOptions.classes || []));95customOptions.markdownDetails?.forEach(markdownDetail => {96const result = dialogDisposables.add(this.markdownRendererService.render(markdownDetail.markdown, {97actionHandler: markdownDetail.actionHandler || ((link, mdStr) => {98return openLinkFromMarkdown(this.openerService, link, mdStr.isTrusted, true /* skip URL validation to prevent another dialog from showing which is unsupported */);99}),100}));101parent.appendChild(result.element);102result.element.classList.add(...(markdownDetail.classes || []));103});104} : undefined;105106const dialog = new Dialog(107this.layoutService.activeContainer,108message,109buttons,110createWorkbenchDialogOptions({111detail,112cancelId,113type: this.getDialogType(type),114renderBody,115icon: customOptions?.icon,116disableCloseAction: customOptions?.disableCloseAction,117buttonOptions: customOptions?.buttonDetails?.map(detail => ({ sublabel: detail })),118checkboxLabel: checkbox?.label,119checkboxChecked: checkbox?.checked,120inputs121}, this.keybindingService, this.layoutService, BrowserDialogHandler.ALLOWABLE_COMMANDS)122);123124dialogDisposables.add(dialog);125126const result = await dialog.show();127dialogDisposables.dispose();128129return result;130}131}132133134