Skip to content

Commit

Permalink
explorer: only pass uri as a context
Browse files Browse the repository at this point in the history
fixes #41157
  • Loading branch information
isidorn committed Jan 5, 2018
1 parent 6b8d750 commit c51c77c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 58 deletions.
71 changes: 25 additions & 46 deletions src/vs/workbench/parts/files/electron-browser/fileActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import 'vs/css!./media/fileactions';
import { TPromise } from 'vs/base/common/winjs.base';
import nls = require('vs/nls');
import { isWindows, isLinux, isMacintosh } from 'vs/base/common/platform';
import { isWindows, isLinux } from 'vs/base/common/platform';
import { sequence, ITask, always } from 'vs/base/common/async';
import paths = require('vs/base/common/paths');
import resources = require('vs/base/common/resources');
Expand Down Expand Up @@ -51,19 +51,12 @@ import { IModelService } from 'vs/editor/common/services/modelService';
import { ICommandService, CommandsRegistry } from 'vs/platform/commands/common/commands';
import { IListService, ListWidget } from 'vs/platform/list/browser/listService';
import { RawContextKey, IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IEvent } from 'vs/platform/contextview/browser/contextView';

export interface IEditableData {
action: IAction;
validator: IInputValidator;
}

export interface IExplorerContext {
viewletState: IFileViewletState;
event?: IEvent;
stat: FileStat;
}

export interface IFileViewletState {
getEditableData(stat: IFileStat): IEditableData;
setEditable(stat: IFileStat, editableData: IEditableData): void;
Expand Down Expand Up @@ -662,18 +655,6 @@ class BaseDeleteFileAction extends BaseFileAction {
this.tree.clearHighlight();
}

// Read context
if (context) {
if (context.event) {
const bypassTrash = (isMacintosh && context.event.altKey) || (!isMacintosh && context.event.shiftKey);
if (bypassTrash) {
this.useTrash = false;
}
} else if (typeof context.useTrash === 'boolean') {
this.useTrash = context.useTrash;
}
}

let primaryButton: string;
if (this.useTrash) {
primaryButton = isWindows ? nls.localize('deleteButtonLabelRecycleBin', "&&Move to Recycle Bin") : nls.localize({ key: 'deleteButtonLabelTrash', comment: ['&& denotes a mnemonic'] }, "&&Move to Trash");
Expand Down Expand Up @@ -1549,13 +1530,24 @@ if (!diag) {
});
}

interface IExplorerContext {
viewletState: IFileViewletState;
stat: FileStat;
}

function getContext(tree: ListWidget, viewletService: IViewletService): IExplorerContext {
// These commands can only be triggered when explorer viewlet is visible so get it using the active viewlet
return { stat: tree.getFocus(), viewletState: (<ExplorerViewlet>viewletService.getActiveViewlet()).getViewletState() };
}

// TODO@isidor these commands are calling into actions due to the complex inheritance action structure.
// It should be the other way around, that actions call into commands.
CommandsRegistry.registerCommand({
id: NEW_FILE_COMMAND_ID,
handler: (accessor, resource: URI, explorerContext: IExplorerContext) => {
handler: (accessor) => {
const instantationService = accessor.get(IInstantiationService);
const listService = accessor.get(IListService);
const explorerContext = getContext(listService.lastFocusedList, accessor.get(IViewletService));
const newFileAction = instantationService.createInstance(NewFileAction, listService.lastFocusedList, explorerContext.stat);

return newFileAction.run(explorerContext);
Expand All @@ -1564,69 +1556,56 @@ CommandsRegistry.registerCommand({

CommandsRegistry.registerCommand({
id: NEW_FOLDER_COMMAND_ID,
handler: (accessor, resource: URI, explorerContext: IExplorerContext) => {
handler: (accessor) => {
const instantationService = accessor.get(IInstantiationService);
const listService = accessor.get(IListService);
const explorerContext = getContext(listService.lastFocusedList, accessor.get(IViewletService));
const newFolderAction = instantationService.createInstance(NewFolderAction, listService.lastFocusedList, explorerContext.stat);

return newFolderAction.run(explorerContext);
}
});

function getContext(tree: ListWidget, viewletService: IViewletService): IExplorerContext {
return { stat: tree.getFocus(), viewletState: (<ExplorerViewlet>viewletService.getActiveViewlet()).getViewletState() };
}

export const renameHandler = (accessor: ServicesAccessor, resource: URI, explorerContext: IExplorerContext) => {
export const renameHandler = (accessor: ServicesAccessor) => {
const instantationService = accessor.get(IInstantiationService);
const listService = accessor.get(IListService);
if (!explorerContext) {
explorerContext = getContext(listService.lastFocusedList, accessor.get(IViewletService));
}
const explorerContext = getContext(listService.lastFocusedList, accessor.get(IViewletService));

const renameAction = instantationService.createInstance(TriggerRenameFileAction, listService.lastFocusedList, explorerContext.stat);
return renameAction.run(explorerContext);
};

export const moveFileToTrashHandler = (accessor, resource: URI, explorerContext: IExplorerContext) => {
export const moveFileToTrashHandler = (accessor) => {
const instantationService = accessor.get(IInstantiationService);
const listService = accessor.get(IListService);
if (!explorerContext) {
explorerContext = getContext(listService.lastFocusedList, accessor.get(IViewletService));
}
const explorerContext = getContext(listService.lastFocusedList, accessor.get(IViewletService));

const moveFileToTrashAction = instantationService.createInstance(BaseDeleteFileAction, listService.lastFocusedList, explorerContext.stat, true);
return moveFileToTrashAction.run(explorerContext);
};

export const deleteFileHandler = (accessor, resource: URI, explorerContext: IExplorerContext) => {
export const deleteFileHandler = (accessor) => {
const instantationService = accessor.get(IInstantiationService);
const listService = accessor.get(IListService);
if (!explorerContext) {
explorerContext = getContext(listService.lastFocusedList, accessor.get(IViewletService));
}
const explorerContext = getContext(listService.lastFocusedList, accessor.get(IViewletService));

const deleteFileAction = instantationService.createInstance(BaseDeleteFileAction, listService.lastFocusedList, explorerContext.stat, false);
return deleteFileAction.run(explorerContext);
};

export const copyFileHandler = (accessor, resource: URI, explorerContext: IExplorerContext) => {
export const copyFileHandler = (accessor) => {
const instantationService = accessor.get(IInstantiationService);
const listService = accessor.get(IListService);
if (!explorerContext) {
explorerContext = getContext(listService.lastFocusedList, accessor.get(IViewletService));
}
const explorerContext = getContext(listService.lastFocusedList, accessor.get(IViewletService));

const copyFileAction = instantationService.createInstance(CopyFileAction, listService.lastFocusedList, explorerContext.stat);
return copyFileAction.run();
};

export const pasteFileHandler = (accessor, resource: URI, explorerContext: IExplorerContext) => {
export const pasteFileHandler = (accessor) => {
const instantationService = accessor.get(IInstantiationService);
const listService = accessor.get(IListService);
if (!explorerContext) {
explorerContext = getContext(listService.lastFocusedList, accessor.get(IViewletService));
}
const explorerContext = getContext(listService.lastFocusedList, accessor.get(IViewletService));

const pasteFileAction = instantationService.createInstance(PasteFileAction, listService.lastFocusedList, explorerContext.stat);
return pasteFileAction.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ export class ExplorerView extends TreeViewsViewletPanel implements IExplorerView
public createViewer(container: Builder): WorkbenchTree {
const dataSource = this.instantiationService.createInstance(FileDataSource);
const renderer = this.instantiationService.createInstance(FileRenderer, this.viewletState);
const controller = this.instantiationService.createInstance(FileController, this.viewletState);
const controller = this.instantiationService.createInstance(FileController);
this.disposables.push(controller);
const sorter = this.instantiationService.createInstance(FileSorter);
this.disposables.push(sorter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,11 @@ export class FileAccessibilityProvider implements IAccessibilityProvider {

// Explorer Controller
export class FileController extends DefaultController implements IDisposable {
private state: FileViewletState;

private contributedContextMenu: IMenu;
private toDispose: IDisposable[];

constructor(state: FileViewletState,
@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
constructor( @IWorkbenchEditorService private editorService: IWorkbenchEditorService,
@IContextMenuService private contextMenuService: IContextMenuService,
@ITelemetryService private telemetryService: ITelemetryService,
@IMenuService menuService: IMenuService,
Expand All @@ -345,7 +343,6 @@ export class FileController extends DefaultController implements IDisposable {
this.contributedContextMenu = menuService.createMenu(MenuId.ExplorerContext, contextKeyService);
this.toDispose.push(this.contributedContextMenu);

this.state = state;
}

public onLeftClick(tree: ITree, stat: FileStat | Model, event: IMouseEvent, origin: string = 'mouse'): boolean {
Expand Down Expand Up @@ -430,13 +427,6 @@ export class FileController extends DefaultController implements IDisposable {
fillInActions(this.contributedContextMenu, { arg: stat instanceof FileStat ? stat.resource : undefined, shouldForwardArgs: true }, actions);
return TPromise.as(actions);
},
getActionsContext: (event) => {
return {
viewletState: this.state,
stat,
event
};
},
onHide: (wasCancelled?: boolean) => {
if (wasCancelled) {
tree.DOMFocus();
Expand Down

0 comments on commit c51c77c

Please sign in to comment.