Path: blob/main/src/vs/workbench/api/browser/mainThreadDialogs.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 { URI } from '../../../base/common/uri.js';6import { MainThreadDiaglogsShape, MainContext, MainThreadDialogOpenOptions, MainThreadDialogSaveOptions } from '../common/extHost.protocol.js';7import { extHostNamedCustomer, IExtHostContext } from '../../services/extensions/common/extHostCustomers.js';8import { IFileDialogService, IOpenDialogOptions, ISaveDialogOptions } from '../../../platform/dialogs/common/dialogs.js';910@extHostNamedCustomer(MainContext.MainThreadDialogs)11export class MainThreadDialogs implements MainThreadDiaglogsShape {1213constructor(14context: IExtHostContext,15@IFileDialogService private readonly _fileDialogService: IFileDialogService,16) {17//18}1920dispose(): void {21//22}2324async $showOpenDialog(options?: MainThreadDialogOpenOptions): Promise<URI[] | undefined> {25const convertedOptions = MainThreadDialogs._convertOpenOptions(options);26if (!convertedOptions.defaultUri) {27convertedOptions.defaultUri = await this._fileDialogService.defaultFilePath();28}29return Promise.resolve(this._fileDialogService.showOpenDialog(convertedOptions));30}3132async $showSaveDialog(options?: MainThreadDialogSaveOptions): Promise<URI | undefined> {33const convertedOptions = MainThreadDialogs._convertSaveOptions(options);34if (!convertedOptions.defaultUri) {35convertedOptions.defaultUri = await this._fileDialogService.defaultFilePath();36}37return Promise.resolve(this._fileDialogService.showSaveDialog(convertedOptions));38}3940private static _convertOpenOptions(options?: MainThreadDialogOpenOptions): IOpenDialogOptions {41const result: IOpenDialogOptions = {42openLabel: options?.openLabel || undefined,43canSelectFiles: options?.canSelectFiles || (!options?.canSelectFiles && !options?.canSelectFolders),44canSelectFolders: options?.canSelectFolders,45canSelectMany: options?.canSelectMany,46defaultUri: options?.defaultUri ? URI.revive(options.defaultUri) : undefined,47title: options?.title || undefined,48availableFileSystems: []49};50if (options?.filters) {51result.filters = [];52for (const [key, value] of Object.entries(options.filters)) {53result.filters.push({ name: key, extensions: value });54}55}56return result;57}5859private static _convertSaveOptions(options?: MainThreadDialogSaveOptions): ISaveDialogOptions {60const result: ISaveDialogOptions = {61defaultUri: options?.defaultUri ? URI.revive(options.defaultUri) : undefined,62saveLabel: options?.saveLabel || undefined,63title: options?.title || undefined64};65if (options?.filters) {66result.filters = [];67for (const [key, value] of Object.entries(options.filters)) {68result.filters.push({ name: key, extensions: value });69}70}71return result;72}73}747576