Path: blob/main/src/vs/workbench/electron-browser/parts/dialogs/dialogHandler.ts
3296 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 { IClipboardService } from '../../../../platform/clipboard/common/clipboardService.js';7import { AbstractDialogHandler, IConfirmation, IConfirmationResult, IPrompt, IAsyncPromptResult } from '../../../../platform/dialogs/common/dialogs.js';8import { ILogService } from '../../../../platform/log/common/log.js';9import { INativeHostService } from '../../../../platform/native/common/native.js';10import { getActiveWindow } from '../../../../base/browser/dom.js';1112export class NativeDialogHandler extends AbstractDialogHandler {1314constructor(15@ILogService private readonly logService: ILogService,16@INativeHostService private readonly nativeHostService: INativeHostService,17@IClipboardService private readonly clipboardService: IClipboardService18) {19super();20}2122async prompt<T>(prompt: IPrompt<T>): Promise<IAsyncPromptResult<T>> {23this.logService.trace('DialogService#prompt', prompt.message);2425const buttons = this.getPromptButtons(prompt);2627const { response, checkboxChecked } = await this.nativeHostService.showMessageBox({28type: this.getDialogType(prompt.type),29title: prompt.title,30message: prompt.message,31detail: prompt.detail,32buttons,33cancelId: prompt.cancelButton ? buttons.length - 1 : -1 /* Disabled */,34checkboxLabel: prompt.checkbox?.label,35checkboxChecked: prompt.checkbox?.checked,36targetWindowId: getActiveWindow().vscodeWindowId37});3839return this.getPromptResult(prompt, response, checkboxChecked);40}4142async confirm(confirmation: IConfirmation): Promise<IConfirmationResult> {43this.logService.trace('DialogService#confirm', confirmation.message);4445const buttons = this.getConfirmationButtons(confirmation);4647const { response, checkboxChecked } = await this.nativeHostService.showMessageBox({48type: this.getDialogType(confirmation.type) ?? 'question',49title: confirmation.title,50message: confirmation.message,51detail: confirmation.detail,52buttons,53cancelId: buttons.length - 1,54checkboxLabel: confirmation.checkbox?.label,55checkboxChecked: confirmation.checkbox?.checked,56targetWindowId: getActiveWindow().vscodeWindowId57});5859return { confirmed: response === 0, checkboxChecked };60}6162input(): never {63throw new Error('Unsupported'); // we have no native API for password dialogs in Electron64}6566async about(title: string, details: string, detailsToCopy: string): Promise<void> {67const { response } = await this.nativeHostService.showMessageBox({68type: 'info',69message: title,70detail: `\n${details}`,71buttons: [72localize({ key: 'copy', comment: ['&& denotes a mnemonic'] }, "&&Copy"),73localize('okButton', "OK")74],75targetWindowId: getActiveWindow().vscodeWindowId76});7778if (response === 0) {79this.clipboardService.writeText(detailsToCopy);80}81}82}838485