Skip to content

Commit

Permalink
also add showSaveDialog-api, #13807
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Sep 7, 2017
1 parent 550e32e commit a731263
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
10 changes: 8 additions & 2 deletions src/vs/vscode.proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@
declare module 'vscode' {

export interface OpenDialogOptions {
uri?: Uri;
defaultResource?: Uri;
openLabel?: string;
openFiles?: boolean;
openFolders?: boolean;
openMany?: boolean;
}

export namespace window {
export interface SaveDialogOptions {
defaultResource?: Uri;
saveLabel?: string;
}

export namespace window {
export function showOpenDialog(options: OpenDialogOptions): Thenable<Uri[]>;
export function showSaveDialog(options: OpenDialogOptions): Thenable<Uri>;
}

// todo@joh discover files etc
Expand Down
45 changes: 36 additions & 9 deletions src/vs/workbench/api/electron-browser/mainThreadDialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { TPromise } from 'vs/base/common/winjs.base';
import { isFalsyOrEmpty } from 'vs/base/common/arrays';
import { MainThreadDiaglogsShape, MainContext, IExtHostContext, MainThreadDialogOptions } from '../node/extHost.protocol';
import { MainThreadDiaglogsShape, MainContext, IExtHostContext, MainThreadDialogOpenOptions, MainThreadDialogSaveOptions } from '../node/extHost.protocol';
import { extHostNamedCustomer } from 'vs/workbench/api/electron-browser/extHostCustomers';
import { IWindowService } from 'vs/platform/windows/common/windows';

Expand All @@ -24,27 +24,41 @@ export class MainThreadDialogs implements MainThreadDiaglogsShape {
//
}

$showOpenDialog(options: MainThreadDialogOptions): TPromise<string[]> {
$showOpenDialog(options: MainThreadDialogOpenOptions): TPromise<string[]> {
// TODO@joh what about remote dev setup?
if (options.uri && options.uri.scheme !== 'file') {
if (options.defaultResource && options.defaultResource.scheme !== 'file') {
return TPromise.wrapError(new Error('bad path'));
}
return new TPromise<string[]>(resolve => {
this._windowService.showOpenDialog(MainThreadDialogs._convertOptions(options), filenames => {
resolve(isFalsyOrEmpty(filenames) ? undefined : filenames);
});
this._windowService.showOpenDialog(
MainThreadDialogs._convertOpenOptions(options),
filenames => resolve(isFalsyOrEmpty(filenames) ? undefined : filenames)
);
});
}

private static _convertOptions(options: MainThreadDialogOptions): Electron.OpenDialogOptions {
$showSaveDialog(options: MainThreadDialogSaveOptions): TPromise<string> {
// TODO@joh what about remote dev setup?
if (options.defaultResource && options.defaultResource.scheme !== 'file') {
return TPromise.wrapError(new Error('bad path'));
}
return new TPromise<string>(resolve => {
this._windowService.showSaveDialog(
MainThreadDialogs._convertSaveOptions(options),
filename => resolve(!filename ? undefined : filename)
);
});
}

private static _convertOpenOptions(options: MainThreadDialogOpenOptions): Electron.OpenDialogOptions {
const result: Electron.OpenDialogOptions = {
properties: ['createDirectory']
};
if (options.openLabel) {
result.buttonLabel = options.openLabel;
}
if (options.uri) {
result.defaultPath = options.uri.fsPath;
if (options.defaultResource) {
result.defaultPath = options.defaultResource.fsPath;
}
if (!options.openFiles && !options.openFolders) {
options.openFiles = true;
Expand All @@ -60,4 +74,17 @@ export class MainThreadDialogs implements MainThreadDiaglogsShape {
}
return result;
}

private static _convertSaveOptions(options: MainThreadDialogSaveOptions): Electron.SaveDialogOptions {
const result: Electron.SaveDialogOptions = {

};
if (options.defaultResource) {
result.defaultPath = options.defaultResource.fsPath;
}
if (options.saveLabel) {
result.buttonLabel = options.saveLabel;
}
return result;
}
}
3 changes: 3 additions & 0 deletions src/vs/workbench/api/node/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ export function createApiFactory(
}),
showOpenDialog: proposedApiFunction(extension, options => {
return extHostDialogs.showOpenDialog(options);
}),
showSaveDialog: proposedApiFunction(extension, options => {
return extHostDialogs.showSaveDialog(options);
})
};

Expand Down
12 changes: 9 additions & 3 deletions src/vs/workbench/api/node/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,22 @@ export interface MainThreadDiagnosticsShape extends IDisposable {
$clear(owner: string): TPromise<any>;
}

export interface MainThreadDialogOptions {
uri?: URI;
export interface MainThreadDialogOpenOptions {
defaultResource?: URI;
openLabel?: string;
openFiles?: boolean;
openFolders?: boolean;
openMany?: boolean;
}

export interface MainThreadDialogSaveOptions {
defaultResource?: URI;
saveLabel?: string;
}

export interface MainThreadDiaglogsShape extends IDisposable {
$showOpenDialog(options: MainThreadDialogOptions): TPromise<string[]>;
$showOpenDialog(options: MainThreadDialogOpenOptions): TPromise<string[]>;
$showSaveDialog(options: MainThreadDialogSaveOptions): TPromise<string>;
}

export interface MainThreadDocumentContentProvidersShape extends IDisposable {
Expand Down
6 changes: 6 additions & 0 deletions src/vs/workbench/api/node/extHostDialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ export class ExtHostDialogs {
return filepaths && filepaths.map(URI.file);
});
}

showSaveDialog(options: vscode.SaveDialogOptions): Thenable<URI> {
return this._proxy.$showSaveDialog(<any>options).then(filepath => {
return filepath && URI.file(filepath);
});
}
}

0 comments on commit a731263

Please sign in to comment.