1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { BrowserWindow, dialog, FileFilter, MessageBoxOptions, MessageBoxReturnValue, OpenDialogOptions, OpenDialogReturnValue, SaveDialogOptions, SaveDialogReturnValue } from 'electron';
import { Queue } from 'vs/base/common/async';
import { hash } from 'vs/base/common/hash';
import { mnemonicButtonLabel } from 'vs/base/common/labels';
import { Disposable, dispose, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { normalizeNFC } from 'vs/base/common/normalization';
import { dirname } from 'vs/base/common/path';
import { isMacintosh } from 'vs/base/common/platform';
import { withNullAsUndefined } from 'vs/base/common/types';
import { Promises } from 'vs/base/node/pfs';
import { localize } from 'vs/nls';
import { INativeOpenDialogOptions } from 'vs/platform/dialogs/common/dialogs';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { ILogService } from 'vs/platform/log/common/log';
import { IStateMainService } from 'vs/platform/state/electron-main/state';
import { WORKSPACE_FILTER } from 'vs/platform/workspace/common/workspace';
export const IDialogMainService = createDecorator<IDialogMainService>('dialogMainService');
export interface IDialogMainService {
readonly _serviceBrand: undefined;
pickFileFolder(options: INativeOpenDialogOptions, window?: BrowserWindow): Promise<string[] | undefined>;
pickFolder(options: INativeOpenDialogOptions, window?: BrowserWindow): Promise<string[] | undefined>;
pickFile(options: INativeOpenDialogOptions, window?: BrowserWindow): Promise<string[] | undefined>;
pickWorkspace(options: INativeOpenDialogOptions, window?: BrowserWindow): Promise<string[] | undefined>;
showMessageBox(options: MessageBoxOptions, window?: BrowserWindow): Promise<MessageBoxReturnValue>;
showSaveDialog(options: SaveDialogOptions, window?: BrowserWindow): Promise<SaveDialogReturnValue>;
showOpenDialog(options: OpenDialogOptions, window?: BrowserWindow): Promise<OpenDialogReturnValue>;
}
interface IInternalNativeOpenDialogOptions extends INativeOpenDialogOptions {
pickFolders?: boolean;
pickFiles?: boolean;
title: string;
buttonLabel?: string;
filters?: FileFilter[];
}
export class DialogMainService implements IDialogMainService {
declare readonly _serviceBrand: undefined;
private static readonly workingDirPickerStorageKey = 'pickerWorkingDir';
private readonly windowFileDialogLocks = new Map<number, Set<number>>();
private readonly windowDialogQueues = new Map<number, Queue<MessageBoxReturnValue | SaveDialogReturnValue | OpenDialogReturnValue>>();
private readonly noWindowDialogueQueue = new Queue<MessageBoxReturnValue | SaveDialogReturnValue | OpenDialogReturnValue>();
constructor(
@IStateMainService private readonly stateMainService: IStateMainService,
@ILogService private readonly logService: ILogService
) {
}
pickFileFolder(options: INativeOpenDialogOptions, window?: BrowserWindow): Promise<string[] | undefined> {
return this.doPick({ ...options, pickFolders: true, pickFiles: true, title: localize('open', "Open") }, window);
}
pickFolder(options: INativeOpenDialogOptions, window?: BrowserWindow): Promise<string[] | undefined> {
return this.doPick({ ...options, pickFolders: true, title: localize('openFolder', "Open Folder") }, window);
}
pickFile(options: INativeOpenDialogOptions, window?: BrowserWindow): Promise<string[] | undefined> {
return this.doPick({ ...options, pickFiles: true, title: localize('openFile', "Open File") }, window);
}
pickWorkspace(options: INativeOpenDialogOptions, window?: BrowserWindow): Promise<string[] | undefined> {
const title = localize('openWorkspaceTitle', "Open Workspace from File");
const buttonLabel = mnemonicButtonLabel(localize({ key: 'openWorkspace', comment: ['&& denotes a mnemonic'] }, "&&Open"));
const filters = WORKSPACE_FILTER;
return this.doPick({ ...options, pickFiles: true, title, filters, buttonLabel }, window);
}
private async doPick(options: IInternalNativeOpenDialogOptions, window?: BrowserWindow): Promise<string[] | undefined> {
// Ensure dialog options
const dialogOptions: OpenDialogOptions = {
title: options.title,
buttonLabel: options.buttonLabel,
filters: options.filters
};
// Ensure defaultPath
dialogOptions.defaultPath = options.defaultPath || this.stateMainService.getItem<string>(DialogMainService.workingDirPickerStorageKey);
// Ensure properties
if (typeof options.pickFiles === 'boolean' || typeof options.pickFolders === 'boolean') {
dialogOptions.properties = undefined; // let it override based on the booleans
if (options.pickFiles && options.pickFolders) {
dialogOptions.properties = ['multiSelections', 'openDirectory', 'openFile', 'createDirectory'];
}
}
if (!dialogOptions.properties) {
dialogOptions.properties = ['multiSelections', options.pickFolders ? 'openDirectory' : 'openFile', 'createDirectory'];
}
if (isMacintosh) {
dialogOptions.properties.push('treatPackageAsDirectory'); // always drill into .app files
}
// Show Dialog
const windowToUse = window || BrowserWindow.getFocusedWindow();
const result = await this.showOpenDialog(dialogOptions, withNullAsUndefined(windowToUse));
if (result && result.filePaths && result.filePaths.length > 0) {
// Remember path in storage for next time
this.stateMainService.setItem(DialogMainService.workingDirPickerStorageKey, dirname(result.filePaths[0]));
return result.filePaths;
}
return;
}
private getWindowDialogQueue<T extends MessageBoxReturnValue | SaveDialogReturnValue | OpenDialogReturnValue>(window?: BrowserWindow): Queue<T> {
// Queue message box requests per window so that one can show
// after the other.
if (window) {
let windowDialogQueue = this.windowDialogQueues.get(window.id);
if (!windowDialogQueue) {
windowDialogQueue = new Queue<MessageBoxReturnValue | SaveDialogReturnValue | OpenDialogReturnValue>();
this.windowDialogQueues.set(window.id, windowDialogQueue);
}
return windowDialogQueue as unknown as Queue<T>;
} else {
return this.noWindowDialogueQueue as unknown as Queue<T>;
}
}
showMessageBox(options: MessageBoxOptions, window?: BrowserWindow): Promise<MessageBoxReturnValue> {
return this.getWindowDialogQueue<MessageBoxReturnValue>(window).queue(async () => {
if (window) {
return dialog.showMessageBox(window, options);
}
return dialog.showMessageBox(options);
});
}
async showSaveDialog(options: SaveDialogOptions, window?: BrowserWindow): Promise<SaveDialogReturnValue> {
// prevent duplicates of the same dialog queueing at the same time
const fileDialogLock = this.acquireFileDialogLock(options, window);
if (!fileDialogLock) {
this.logService.error('[DialogMainService]: file save dialog is already or will be showing for the window with the same configuration');
return { canceled: true };
}
try {
return await this.getWindowDialogQueue<SaveDialogReturnValue>(window).queue(async () => {
let result: SaveDialogReturnValue;
if (window) {
result = await dialog.showSaveDialog(window, options);
} else {
result = await dialog.showSaveDialog(options);
}
result.filePath = this.normalizePath(result.filePath);
return result;
});
} finally {
dispose(fileDialogLock);
}
}
private normalizePath(path: string): string;
private normalizePath(path: string | undefined): string | undefined;
private normalizePath(path: string | undefined): string | undefined {
if (path && isMacintosh) {
path = normalizeNFC(path); // macOS only: normalize paths to NFC form
}
return path;
}
private normalizePaths(paths: string[]): string[] {
return paths.map(path => this.normalizePath(path));
}
async showOpenDialog(options: OpenDialogOptions, window?: BrowserWindow): Promise<OpenDialogReturnValue> {
// Ensure the path exists (if provided)
if (options.defaultPath) {
const pathExists = await Promises.exists(options.defaultPath);
if (!pathExists) {
options.defaultPath = undefined;
}
}
// prevent duplicates of the same dialog queueing at the same time
const fileDialogLock = this.acquireFileDialogLock(options, window);
if (!fileDialogLock) {
this.logService.error('[DialogMainService]: file open dialog is already or will be showing for the window with the same configuration');
return { canceled: true, filePaths: [] };
}
try {
return await this.getWindowDialogQueue<OpenDialogReturnValue>(window).queue(async () => {
let result: OpenDialogReturnValue;
if (window) {
result = await dialog.showOpenDialog(window, options);
} else {
result = await dialog.showOpenDialog(options);
}
result.filePaths = this.normalizePaths(result.filePaths);
return result;
});
} finally {
dispose(fileDialogLock);
}
}
private acquireFileDialogLock(options: SaveDialogOptions | OpenDialogOptions, window?: BrowserWindow): IDisposable | undefined {
// if no window is provided, allow as many dialogs as
// needed since we consider them not modal per window
if (!window) {
return Disposable.None;
}
// if a window is provided, only allow a single dialog
// at the same time because dialogs are modal and we
// do not want to open one dialog after the other
// (https://github.com/microsoft/vscode/issues/114432)
// we figure this out by `hashing` the configuration
// options for the dialog to prevent duplicates
this.logService.trace('[DialogMainService]: request to acquire file dialog lock', options);
let windowFileDialogLocks = this.windowFileDialogLocks.get(window.id);
if (!windowFileDialogLocks) {
windowFileDialogLocks = new Set();
this.windowFileDialogLocks.set(window.id, windowFileDialogLocks);
}
const optionsHash = hash(options);
if (windowFileDialogLocks.has(optionsHash)) {
return undefined; // prevent duplicates, return
}
this.logService.trace('[DialogMainService]: new file dialog lock created', options);
windowFileDialogLocks.add(optionsHash);
return toDisposable(() => {
this.logService.trace('[DialogMainService]: file dialog lock disposed', options);
windowFileDialogLocks?.delete(optionsHash);
// if the window has no more dialog locks, delete it from the set of locks
if (windowFileDialogLocks?.size === 0) {
this.windowFileDialogLocks.delete(window.id);
}
});
}
}
|