Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/dialogs/electron-browser/fileDialogService.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { SaveDialogOptions, OpenDialogOptions } from '../../../../base/parts/sandbox/common/electronTypes.js';
7
import { IHostService } from '../../host/browser/host.js';
8
import { IPickAndOpenOptions, ISaveDialogOptions, IOpenDialogOptions, IFileDialogService, IDialogService, INativeOpenDialogOptions } from '../../../../platform/dialogs/common/dialogs.js';
9
import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js';
10
import { IHistoryService } from '../../history/common/history.js';
11
import { IWorkbenchEnvironmentService } from '../../environment/common/environmentService.js';
12
import { URI } from '../../../../base/common/uri.js';
13
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
14
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
15
import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';
16
import { IFileService } from '../../../../platform/files/common/files.js';
17
import { IOpenerService } from '../../../../platform/opener/common/opener.js';
18
import { INativeHostOptions, INativeHostService } from '../../../../platform/native/common/native.js';
19
import { AbstractFileDialogService } from '../browser/abstractFileDialogService.js';
20
import { Schemas } from '../../../../base/common/network.js';
21
import { ILanguageService } from '../../../../editor/common/languages/language.js';
22
import { IWorkspacesService } from '../../../../platform/workspaces/common/workspaces.js';
23
import { ILabelService } from '../../../../platform/label/common/label.js';
24
import { IPathService } from '../../path/common/pathService.js';
25
import { ICommandService } from '../../../../platform/commands/common/commands.js';
26
import { ICodeEditorService } from '../../../../editor/browser/services/codeEditorService.js';
27
import { IEditorService } from '../../editor/common/editorService.js';
28
import { ILogService } from '../../../../platform/log/common/log.js';
29
import { getActiveWindow } from '../../../../base/browser/dom.js';
30
31
export class FileDialogService extends AbstractFileDialogService implements IFileDialogService {
32
33
constructor(
34
@IHostService hostService: IHostService,
35
@IWorkspaceContextService contextService: IWorkspaceContextService,
36
@IHistoryService historyService: IHistoryService,
37
@IWorkbenchEnvironmentService environmentService: IWorkbenchEnvironmentService,
38
@IInstantiationService instantiationService: IInstantiationService,
39
@IConfigurationService configurationService: IConfigurationService,
40
@IFileService fileService: IFileService,
41
@IOpenerService openerService: IOpenerService,
42
@INativeHostService private readonly nativeHostService: INativeHostService,
43
@IDialogService dialogService: IDialogService,
44
@ILanguageService languageService: ILanguageService,
45
@IWorkspacesService workspacesService: IWorkspacesService,
46
@ILabelService labelService: ILabelService,
47
@IPathService pathService: IPathService,
48
@ICommandService commandService: ICommandService,
49
@IEditorService editorService: IEditorService,
50
@ICodeEditorService codeEditorService: ICodeEditorService,
51
@ILogService logService: ILogService
52
) {
53
super(hostService, contextService, historyService, environmentService, instantiationService,
54
configurationService, fileService, openerService, dialogService, languageService, workspacesService, labelService, pathService, commandService, editorService, codeEditorService, logService);
55
}
56
57
private toNativeOpenDialogOptions(options: IPickAndOpenOptions): INativeOpenDialogOptions {
58
return {
59
forceNewWindow: options.forceNewWindow,
60
telemetryExtraData: options.telemetryExtraData,
61
defaultPath: options.defaultUri?.fsPath
62
};
63
}
64
65
private shouldUseSimplified(schema: string): { useSimplified: boolean; isSetting: boolean } {
66
const setting = (this.configurationService.getValue('files.simpleDialog.enable') === true);
67
const newWindowSetting = (this.configurationService.getValue('window.openFilesInNewWindow') === 'on');
68
return {
69
// - Only real files can be shown in the native file picker
70
// - If the simple file dialog is enabled
71
// - driver automation (like smoke tests) can use the simple file dialog but not native
72
useSimplified: ((schema !== Schemas.file) && (schema !== Schemas.vscodeUserData)) || setting || !!this.environmentService.enableSmokeTestDriver,
73
isSetting: newWindowSetting
74
};
75
}
76
77
async pickFileFolderAndOpen(options: IPickAndOpenOptions): Promise<void> {
78
const schema = this.getFileSystemSchema(options);
79
80
if (!options.defaultUri) {
81
options.defaultUri = await this.defaultFilePath(schema);
82
}
83
84
const shouldUseSimplified = this.shouldUseSimplified(schema);
85
if (shouldUseSimplified.useSimplified) {
86
return this.pickFileFolderAndOpenSimplified(schema, options, shouldUseSimplified.isSetting);
87
}
88
return this.nativeHostService.pickFileFolderAndOpen(this.toNativeOpenDialogOptions(options));
89
}
90
91
async pickFileAndOpen(options: IPickAndOpenOptions): Promise<void> {
92
const schema = this.getFileSystemSchema(options);
93
94
if (!options.defaultUri) {
95
options.defaultUri = await this.defaultFilePath(schema);
96
}
97
98
const shouldUseSimplified = this.shouldUseSimplified(schema);
99
if (shouldUseSimplified.useSimplified) {
100
return this.pickFileAndOpenSimplified(schema, options, shouldUseSimplified.isSetting);
101
}
102
return this.nativeHostService.pickFileAndOpen(this.toNativeOpenDialogOptions(options));
103
}
104
105
async pickFolderAndOpen(options: IPickAndOpenOptions): Promise<void> {
106
const schema = this.getFileSystemSchema(options);
107
108
if (!options.defaultUri) {
109
options.defaultUri = await this.defaultFolderPath(schema);
110
}
111
112
if (this.shouldUseSimplified(schema).useSimplified) {
113
return this.pickFolderAndOpenSimplified(schema, options);
114
}
115
return this.nativeHostService.pickFolderAndOpen(this.toNativeOpenDialogOptions(options));
116
}
117
118
async pickWorkspaceAndOpen(options: IPickAndOpenOptions): Promise<void> {
119
options.availableFileSystems = this.getWorkspaceAvailableFileSystems(options);
120
const schema = this.getFileSystemSchema(options);
121
122
if (!options.defaultUri) {
123
options.defaultUri = await this.defaultWorkspacePath(schema);
124
}
125
126
if (this.shouldUseSimplified(schema).useSimplified) {
127
return this.pickWorkspaceAndOpenSimplified(schema, options);
128
}
129
return this.nativeHostService.pickWorkspaceAndOpen(this.toNativeOpenDialogOptions(options));
130
}
131
132
async pickFileToSave(defaultUri: URI, availableFileSystems?: string[]): Promise<URI | undefined> {
133
const schema = this.getFileSystemSchema({ defaultUri, availableFileSystems });
134
const options = this.getPickFileToSaveDialogOptions(defaultUri, availableFileSystems);
135
if (this.shouldUseSimplified(schema).useSimplified) {
136
return this.pickFileToSaveSimplified(schema, options);
137
} else {
138
const result = await this.nativeHostService.showSaveDialog(this.toNativeSaveDialogOptions(options));
139
if (result && !result.canceled && result.filePath) {
140
const uri = URI.file(result.filePath);
141
142
this.addFileToRecentlyOpened(uri);
143
144
return uri;
145
}
146
}
147
return;
148
}
149
150
private toNativeSaveDialogOptions(options: ISaveDialogOptions): SaveDialogOptions & INativeHostOptions {
151
options.defaultUri = options.defaultUri ? URI.file(options.defaultUri.path) : undefined;
152
return {
153
defaultPath: options.defaultUri?.fsPath,
154
buttonLabel: typeof options.saveLabel === 'string' ? options.saveLabel : options.saveLabel?.withMnemonic,
155
filters: options.filters,
156
title: options.title,
157
targetWindowId: getActiveWindow().vscodeWindowId
158
};
159
}
160
161
async showSaveDialog(options: ISaveDialogOptions): Promise<URI | undefined> {
162
const schema = this.getFileSystemSchema(options);
163
if (this.shouldUseSimplified(schema).useSimplified) {
164
return this.showSaveDialogSimplified(schema, options);
165
}
166
167
const result = await this.nativeHostService.showSaveDialog(this.toNativeSaveDialogOptions(options));
168
if (result && !result.canceled && result.filePath) {
169
return URI.file(result.filePath);
170
}
171
172
return;
173
}
174
175
async showOpenDialog(options: IOpenDialogOptions): Promise<URI[] | undefined> {
176
const schema = this.getFileSystemSchema(options);
177
if (this.shouldUseSimplified(schema).useSimplified) {
178
return this.showOpenDialogSimplified(schema, options);
179
}
180
181
const newOptions: OpenDialogOptions & { properties: string[] } & INativeHostOptions = {
182
title: options.title,
183
defaultPath: options.defaultUri?.fsPath,
184
buttonLabel: typeof options.openLabel === 'string' ? options.openLabel : options.openLabel?.withMnemonic,
185
filters: options.filters,
186
properties: [],
187
targetWindowId: getActiveWindow().vscodeWindowId
188
};
189
190
newOptions.properties.push('createDirectory');
191
192
if (options.canSelectFiles) {
193
newOptions.properties.push('openFile');
194
}
195
196
if (options.canSelectFolders) {
197
newOptions.properties.push('openDirectory');
198
}
199
200
if (options.canSelectMany) {
201
newOptions.properties.push('multiSelections');
202
}
203
204
const result = await this.nativeHostService.showOpenDialog(newOptions);
205
return result && Array.isArray(result.filePaths) && result.filePaths.length > 0 ? result.filePaths.map(URI.file) : undefined;
206
}
207
}
208
209
registerSingleton(IFileDialogService, FileDialogService, InstantiationType.Delayed);
210
211