Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor file dialog service to fix layer breaker #81171

Merged
merged 6 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { IRemoteAuthorityResolverService } from 'vs/platform/remote/common/remot
import { IHostService } from 'vs/workbench/services/host/browser/host';
import { RemoteConnectionState, Deprecated_RemoteAuthorityContext, RemoteFileDialogContext } from 'vs/workbench/browser/contextkeys';
import { IDownloadService } from 'vs/platform/download/common/download';
import { OpenLocalFileFolderCommand, OpenLocalFileCommand, OpenLocalFolderCommand, SaveLocalFileCommand } from 'vs/workbench/services/dialogs/browser/remoteFileDialog';
import { OpenLocalFileFolderCommand, OpenLocalFileCommand, OpenLocalFolderCommand, SaveLocalFileCommand } from 'vs/workbench/services/dialogs/browser/simpleFileDialog';

const WINDOW_ACTIONS_COMMAND_ID = 'remote.showActions';
const CLOSE_REMOTE_COMMAND_ID = 'remote.closeRemote';
Expand Down
194 changes: 194 additions & 0 deletions src/vs/workbench/services/dialogs/browser/abstractFileDialogService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as nls from 'vs/nls';
import { IWindowService, IURIToOpen, FileFilter } from 'vs/platform/windows/common/windows';
import { IPickAndOpenOptions, ISaveDialogOptions, IOpenDialogOptions } from 'vs/platform/dialogs/common/dialogs';
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
import { IHistoryService } from 'vs/workbench/services/history/common/history';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { URI } from 'vs/base/common/uri';
import { Schemas } from 'vs/base/common/network';
import * as resources from 'vs/base/common/resources';
import { IInstantiationService, } from 'vs/platform/instantiation/common/instantiation';
import { SimpleFileDialog as SimpleFileDialog } from 'vs/workbench/services/dialogs/browser/simpleFileDialog';
import { WORKSPACE_EXTENSION } from 'vs/platform/workspaces/common/workspaces';
import { REMOTE_HOST_SCHEME } from 'vs/platform/remote/common/remoteHosts';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IFileService } from 'vs/platform/files/common/files';
import { isWeb } from 'vs/base/common/platform';
import { IOpenerService } from 'vs/platform/opener/common/opener';

export class AbstractFileDialogService {

_serviceBrand: undefined;

constructor(
@IWindowService protected readonly windowService: IWindowService,
@IWorkspaceContextService protected readonly contextService: IWorkspaceContextService,
@IHistoryService protected readonly historyService: IHistoryService,
@IWorkbenchEnvironmentService protected readonly environmentService: IWorkbenchEnvironmentService,
@IInstantiationService protected readonly instantiationService: IInstantiationService,
@IConfigurationService protected readonly configurationService: IConfigurationService,
@IFileService protected readonly fileService: IFileService,
@IOpenerService protected readonly openerService: IOpenerService,
) { }

defaultFilePath(schemeFilter = this.getSchemeFilterForWindow()): URI | undefined {

// Check for last active file first...
let candidate = this.historyService.getLastActiveFile(schemeFilter);

// ...then for last active file root
if (!candidate) {
candidate = this.historyService.getLastActiveWorkspaceRoot(schemeFilter);
} else {
candidate = candidate && resources.dirname(candidate);
}

return candidate || undefined;
}

defaultFolderPath(schemeFilter = this.getSchemeFilterForWindow()): URI | undefined {

// Check for last active file root first...
let candidate = this.historyService.getLastActiveWorkspaceRoot(schemeFilter);

// ...then for last active file
if (!candidate) {
candidate = this.historyService.getLastActiveFile(schemeFilter);
}

return candidate && resources.dirname(candidate) || undefined;
}

defaultWorkspacePath(schemeFilter = this.getSchemeFilterForWindow()): URI | undefined {

// Check for current workspace config file first...
if (this.contextService.getWorkbenchState() === WorkbenchState.WORKSPACE) {
const configuration = this.contextService.getWorkspace().configuration;
if (configuration && !isUntitledWorkspace(configuration, this.environmentService)) {
return resources.dirname(configuration) || undefined;
}
}

// ...then fallback to default file path
return this.defaultFilePath(schemeFilter);
}

protected addFileSchemaIfNeeded(schema: string): string[] {
// Include File schema unless the schema is web
// Don't allow untitled schema through.
if (isWeb) {
return schema === Schemas.untitled ? [Schemas.file] : [schema];
} else {
return schema === Schemas.untitled ? [Schemas.file] : (schema !== Schemas.file ? [schema, Schemas.file] : [schema]);
}
}

protected async pickFileFolderAndOpenSimplified(schema: string, options: IPickAndOpenOptions, preferCurrentWindow: boolean): Promise<any> {
const title = nls.localize('openFileOrFolder.title', 'Open File Or Folder');
const availableFileSystems = this.addFileSchemaIfNeeded(schema);

const uri = await this.pickResource({ canSelectFiles: true, canSelectFolders: true, canSelectMany: false, defaultUri: options.defaultUri, title, availableFileSystems });

if (uri) {
const stat = await this.fileService.resolve(uri);

const toOpen: IURIToOpen = stat.isDirectory ? { folderUri: uri } : { fileUri: uri };
if (stat.isDirectory || options.forceNewWindow || preferCurrentWindow) {
return this.windowService.openWindow([toOpen], { forceNewWindow: options.forceNewWindow });
} else {
return this.openerService.open(uri);
}
}
}

protected async pickFileAndOpenSimplified(schema: string, options: IPickAndOpenOptions, preferCurrentWindow: boolean): Promise<any> {
const title = nls.localize('openFile.title', 'Open File');
const availableFileSystems = this.addFileSchemaIfNeeded(schema);

const uri = await this.pickResource({ canSelectFiles: true, canSelectFolders: false, canSelectMany: false, defaultUri: options.defaultUri, title, availableFileSystems });
if (uri) {
if (options.forceNewWindow || preferCurrentWindow) {
return this.windowService.openWindow([{ fileUri: uri }], { forceNewWindow: options.forceNewWindow });
} else {
return this.openerService.open(uri);
}
}
}

protected async pickFolderAndOpenSimplified(schema: string, options: IPickAndOpenOptions): Promise<any> {
const title = nls.localize('openFolder.title', 'Open Folder');
const availableFileSystems = this.addFileSchemaIfNeeded(schema);

const uri = await this.pickResource({ canSelectFiles: false, canSelectFolders: true, canSelectMany: false, defaultUri: options.defaultUri, title, availableFileSystems });
if (uri) {
return this.windowService.openWindow([{ folderUri: uri }], { forceNewWindow: options.forceNewWindow });
}
}

protected async pickWorkspaceAndOpenSimplified(schema: string, options: IPickAndOpenOptions): Promise<any> {
const title = nls.localize('openWorkspace.title', 'Open Workspace');
const filters: FileFilter[] = [{ name: nls.localize('filterName.workspace', 'Workspace'), extensions: [WORKSPACE_EXTENSION] }];
const availableFileSystems = this.addFileSchemaIfNeeded(schema);

const uri = await this.pickResource({ canSelectFiles: true, canSelectFolders: false, canSelectMany: false, defaultUri: options.defaultUri, title, filters, availableFileSystems });
if (uri) {
return this.windowService.openWindow([{ workspaceUri: uri }], { forceNewWindow: options.forceNewWindow });
}
}

protected async pickFileToSaveSimplified(schema: string, options: ISaveDialogOptions): Promise<URI | undefined> {
if (!options.availableFileSystems) {
options.availableFileSystems = this.addFileSchemaIfNeeded(schema);
}

options.title = nls.localize('saveFileAs.title', 'Save As');
return this.saveRemoteResource(options);
}

protected async showSaveDialogSimplified(schema: string, options: ISaveDialogOptions): Promise<URI | undefined> {
if (!options.availableFileSystems) {
options.availableFileSystems = this.addFileSchemaIfNeeded(schema);
}

return this.saveRemoteResource(options);
}

protected async showOpenDialogSimplified(schema: string, options: IOpenDialogOptions): Promise<URI[] | undefined> {
if (!options.availableFileSystems) {
options.availableFileSystems = this.addFileSchemaIfNeeded(schema);
}

const uri = await this.pickResource(options);

return uri ? [uri] : undefined;
}

private pickResource(options: IOpenDialogOptions): Promise<URI | undefined> {
const simpleFileDialog = this.instantiationService.createInstance(SimpleFileDialog);

return simpleFileDialog.showOpenDialog(options);
}

private saveRemoteResource(options: ISaveDialogOptions): Promise<URI | undefined> {
const remoteFileDialog = this.instantiationService.createInstance(SimpleFileDialog);

return remoteFileDialog.showSaveDialog(options);
}

protected getSchemeFilterForWindow(): string {
return !this.environmentService.configuration.remoteAuthority ? Schemas.file : REMOTE_HOST_SCHEME;
}

protected getFileSystemSchema(options: { availableFileSystems?: string[], defaultUri?: URI }): string {
return options.availableFileSystems && options.availableFileSystems[0] || this.getSchemeFilterForWindow();
}
}

function isUntitledWorkspace(path: URI, environmentService: IWorkbenchEnvironmentService): boolean {
return resources.isEqualOrParent(path, environmentService.untitledWorkspacesHome);
}
Loading