提交 087c67ff 编写于 作者: B Benjamin Pasero

clean up context keys

上级 3756000c
......@@ -439,6 +439,10 @@ export class RawContextKey<T> extends ContextKeyDefinedExpr {
public isEqualTo(value: string): ContextKeyExpr {
return ContextKeyExpr.equals(this.key, value);
}
public notEqualsTo(value: string): ContextKeyExpr {
return ContextKeyExpr.notEquals(this.key, value);
}
}
export interface IContext {
......
......@@ -10,6 +10,7 @@ import paths = require('vs/base/common/paths');
import { basename } from 'vs/base/common/paths';
import { RawContextKey, IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IModeService } from 'vs/editor/common/services/modeService';
import { IFileService } from 'vs/platform/files/common/files';
export class ResourceContextKey implements IContextKey<URI> {
......@@ -18,22 +19,29 @@ export class ResourceContextKey implements IContextKey<URI> {
static LangId = new RawContextKey<string>('resourceLangId', undefined);
static Resource = new RawContextKey<URI>('resource', undefined);
static Extension = new RawContextKey<string>('resourceExtname', undefined);
static HasResource = new RawContextKey<boolean>('resourceSet', false);
static IsFile = new RawContextKey<boolean>('resourceIsFile', false);
private _resourceKey: IContextKey<URI>;
private _schemeKey: IContextKey<string>;
private _filenameKey: IContextKey<string>;
private _langIdKey: IContextKey<string>;
private _extensionKey: IContextKey<string>;
private _hasResource: IContextKey<boolean>;
private _isFile: IContextKey<boolean>;
constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@IModeService private _modeService: IModeService
@IModeService private _modeService: IModeService,
@IFileService private _fileService: IFileService
) {
this._schemeKey = ResourceContextKey.Scheme.bindTo(contextKeyService);
this._filenameKey = ResourceContextKey.Filename.bindTo(contextKeyService);
this._langIdKey = ResourceContextKey.LangId.bindTo(contextKeyService);
this._resourceKey = ResourceContextKey.Resource.bindTo(contextKeyService);
this._extensionKey = ResourceContextKey.Extension.bindTo(contextKeyService);
this._hasResource = ResourceContextKey.HasResource.bindTo(contextKeyService);
this._isFile = ResourceContextKey.IsFile.bindTo(contextKeyService);
}
set(value: URI) {
......@@ -42,6 +50,8 @@ export class ResourceContextKey implements IContextKey<URI> {
this._filenameKey.set(value && basename(value.fsPath));
this._langIdKey.set(value && this._modeService.getModeIdByFilenameOrFirstLine(value.fsPath));
this._extensionKey.set(value && paths.extname(value.fsPath));
this._hasResource.set(!!value);
this._isFile.set(value && this._fileService.canHandleResource(value));
}
reset(): void {
......@@ -50,6 +60,8 @@ export class ResourceContextKey implements IContextKey<URI> {
this._resourceKey.reset();
this._langIdKey.reset();
this._extensionKey.reset();
this._hasResource.reset();
this._isFile.reset();
}
public get(): URI {
......
......@@ -25,7 +25,6 @@ import { DEFAULT_TERMINAL_WINDOWS, DEFAULT_TERMINAL_LINUX_READY, DEFAULT_TERMINA
import { WinTerminalService, MacTerminalService, LinuxTerminalService } from 'vs/workbench/parts/execution/electron-browser/terminalService';
import { IHistoryService } from 'vs/workbench/services/history/common/history';
import { CommandsRegistry, ICommandService } from 'vs/platform/commands/common/commands';
import { EditorWithResourceFocusedInOpenEditorsContext } from 'vs/workbench/parts/files/electron-browser/fileCommands';
import { ResourceContextKey } from 'vs/workbench/common/resources';
if (env.isWindows) {
......@@ -166,7 +165,7 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
group: '1_files',
order: 30,
command: openConsoleCommand,
when: EditorWithResourceFocusedInOpenEditorsContext
when: ResourceContextKey.Scheme.isEqualTo('file')
});
MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
......
......@@ -11,7 +11,7 @@ import { revertLocalChangesCommand, acceptLocalChangesCommand, CONFLICT_RESOLUTI
import { SyncActionDescriptor, MenuId, MenuRegistry } from 'vs/platform/actions/common/actions';
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions';
import { KeyMod, KeyChord, KeyCode } from 'vs/base/common/keyCodes';
import { copyFocusedFilesExplorerViewItem, openWindowCommand, deleteFocusedFilesExplorerViewItemCommand, moveFocusedFilesExplorerViewItemToTrashCommand, renameFocusedFilesExplorerViewItemCommand, REVEAL_IN_OS_COMMAND_ID, COPY_PATH_COMMAND_ID, REVEAL_IN_EXPLORER_COMMAND_ID, OPEN_TO_SIDE_COMMAND_ID, EditorWithResourceFocusedInOpenEditorsContext, REVERT_FILE_COMMAND_ID, SAVE_FILE_COMMAND_ID, SAVE_FILE_LABEL, UntitledEditorFocusedInOpenEditorsContext, SAVE_FILE_AS_COMMAND_ID, SAVE_FILE_AS_LABEL, SAVE_ALL_IN_GROUP_COMMAND_ID, GroupFocusedInOpenEditorsContext, COMPARE_WITH_SAVED_COMMAND_ID, COMPARE_RESOURCE_COMMAND_ID, SELECT_FOR_COMPARE_COMMAND_ID, EditorFocusedInOpenEditorsContext } from 'vs/workbench/parts/files/electron-browser/fileCommands';
import { copyFocusedFilesExplorerViewItem, openWindowCommand, deleteFocusedFilesExplorerViewItemCommand, moveFocusedFilesExplorerViewItemToTrashCommand, renameFocusedFilesExplorerViewItemCommand, REVEAL_IN_OS_COMMAND_ID, COPY_PATH_COMMAND_ID, REVEAL_IN_EXPLORER_COMMAND_ID, OPEN_TO_SIDE_COMMAND_ID, REVERT_FILE_COMMAND_ID, SAVE_FILE_COMMAND_ID, SAVE_FILE_LABEL, SAVE_FILE_AS_COMMAND_ID, SAVE_FILE_AS_LABEL, SAVE_ALL_IN_GROUP_COMMAND_ID, OpenEditorsGroupContext, COMPARE_WITH_SAVED_COMMAND_ID, COMPARE_RESOURCE_COMMAND_ID, SELECT_FOR_COMPARE_COMMAND_ID } from 'vs/workbench/parts/files/electron-browser/fileCommands';
import { CommandsRegistry, ICommandHandler } from 'vs/platform/commands/common/commands';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
......@@ -145,7 +145,7 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
group: '1_files',
order: 10,
command: openToSideCommand,
when: EditorWithResourceFocusedInOpenEditorsContext
when: ResourceContextKey.HasResource
});
const revealInOsCommand = {
......@@ -156,7 +156,7 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
group: '1_files',
order: 20,
command: revealInOsCommand,
when: EditorWithResourceFocusedInOpenEditorsContext
when: ResourceContextKey.Scheme.isEqualTo('file')
});
const copyPathCommand = {
......@@ -167,7 +167,7 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
group: '1_files',
order: 40,
command: copyPathCommand,
when: EditorWithResourceFocusedInOpenEditorsContext
when: ResourceContextKey.HasResource
});
MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
......@@ -177,7 +177,7 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
id: SAVE_FILE_COMMAND_ID,
title: SAVE_FILE_LABEL
},
when: ContextKeyExpr.and(EditorWithResourceFocusedInOpenEditorsContext, AutoSaveNotAfterDelayContext)
when: ContextKeyExpr.and(ResourceContextKey.IsFile, AutoSaveNotAfterDelayContext)
});
MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
......@@ -187,7 +187,7 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
id: REVERT_FILE_COMMAND_ID,
title: nls.localize('revert', "Revert File")
},
when: ContextKeyExpr.and(EditorWithResourceFocusedInOpenEditorsContext, AutoSaveNotAfterDelayContext, UntitledEditorFocusedInOpenEditorsContext.toNegated())
when: ContextKeyExpr.and(ResourceContextKey.IsFile, AutoSaveNotAfterDelayContext)
});
MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
......@@ -196,7 +196,7 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
id: SAVE_FILE_AS_COMMAND_ID,
title: SAVE_FILE_AS_LABEL
},
when: ContextKeyExpr.and(EditorWithResourceFocusedInOpenEditorsContext, UntitledEditorFocusedInOpenEditorsContext)
when: ResourceContextKey.Scheme.isEqualTo('untitled')
});
MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
......@@ -205,7 +205,7 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
id: SAVE_ALL_IN_GROUP_COMMAND_ID,
title: nls.localize('saveAll', "Save All")
},
when: ContextKeyExpr.and(GroupFocusedInOpenEditorsContext, AutoSaveNotAfterDelayContext)
when: ContextKeyExpr.and(OpenEditorsGroupContext, AutoSaveNotAfterDelayContext)
});
MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
......@@ -215,7 +215,7 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
id: COMPARE_WITH_SAVED_COMMAND_ID,
title: nls.localize('compareWithSaved', "Compare with Saved")
},
when: ContextKeyExpr.and(EditorWithResourceFocusedInOpenEditorsContext, UntitledEditorFocusedInOpenEditorsContext.toNegated())
when: ResourceContextKey.IsFile
});
const compareResourceCommand = {
......@@ -226,7 +226,7 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
group: '3_compare',
order: 20,
command: compareResourceCommand,
when: ContextKeyExpr.and(EditorWithResourceFocusedInOpenEditorsContext, )
when: ResourceContextKey.HasResource
});
const selectForCompareCommand = {
......@@ -237,7 +237,7 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
group: '3_compare',
order: 30,
command: selectForCompareCommand,
when: EditorWithResourceFocusedInOpenEditorsContext
when: ResourceContextKey.HasResource
});
MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
......@@ -247,7 +247,7 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
id: CLOSE_EDITOR_COMMAND_ID,
title: nls.localize('close', "Close")
},
when: EditorFocusedInOpenEditorsContext
when: OpenEditorsGroupContext.toNegated()
});
MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
......@@ -257,7 +257,7 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
id: CLOSE_OTHER_EDITORS_IN_GROUP_COMMAND_ID,
title: nls.localize('closeOthers', "Close Others")
},
when: EditorFocusedInOpenEditorsContext
when: OpenEditorsGroupContext.toNegated()
});
MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
......@@ -311,7 +311,7 @@ MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
group: '1_files',
order: 20,
command: revealInOsCommand,
when: ResourceContextKey.Scheme.isEqualTo('file')
when: ResourceContextKey.HasResource
});
MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
......
......@@ -66,10 +66,7 @@ export const SAVE_ALL_IN_GROUP_COMMAND_ID = 'workbench.action.files.saveAllInGro
export const SAVE_FILES_COMMAND_ID = 'workbench.command.files.saveFiles';
export const SAVE_FILES_LABEL = nls.localize('saveFiles', "Save All Files");
export const EditorFocusedInOpenEditorsContext = new RawContextKey<boolean>('editorFocusedInOpenEditors', false);
export const EditorWithResourceFocusedInOpenEditorsContext = new RawContextKey<boolean>('editorWithResourceFocusedInOpenEditors', false);
export const UntitledEditorFocusedInOpenEditorsContext = new RawContextKey<boolean>('untitledEditorFocusedInOpenEditors', false);
export const GroupFocusedInOpenEditorsContext = new RawContextKey<boolean>('groupFocusedInOpenEditors', false);
export const OpenEditorsGroupContext = new RawContextKey<boolean>('groupFocusedInOpenEditors', false);
export const openWindowCommand = (accessor: ServicesAccessor, paths: string[], forceNewWindow: boolean) => {
const windowsService = accessor.get(IWindowsService);
......
......@@ -40,7 +40,8 @@ import { KeyCode } from 'vs/base/common/keyCodes';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { fillInActions } from 'vs/platform/actions/browser/menuItemActionItem';
import { IMenuService, MenuId, IMenu } from 'vs/platform/actions/common/actions';
import { EditorFocusedInOpenEditorsContext, UntitledEditorFocusedInOpenEditorsContext, GroupFocusedInOpenEditorsContext, EditorWithResourceFocusedInOpenEditorsContext } from 'vs/workbench/parts/files/electron-browser/fileCommands';
import { OpenEditorsGroupContext } from 'vs/workbench/parts/files/electron-browser/fileCommands';
import { ResourceContextKey } from 'vs/workbench/common/resources';
const $ = dom.$;
......@@ -58,9 +59,7 @@ export class OpenEditorsView extends ViewsViewletPanel {
private list: WorkbenchList<OpenEditor | IEditorGroup>;
private contributedContextMenu: IMenu;
private needsRefresh: boolean;
private editorFocusedContext: IContextKey<boolean>;
private editorWithResourceFocusedContext: IContextKey<boolean>;
private untitledEditorFocusedContext: IContextKey<boolean>;
private resourceContext: ResourceContextKey;
private groupFocusedContext: IContextKey<boolean>;
constructor(
......@@ -150,25 +149,21 @@ export class OpenEditorsView extends ViewsViewletPanel {
}, this.contextKeyService, this.listService, this.themeService);
this.updateSize();
// Bind context keys
OpenEditorsFocusedContext.bindTo(this.list.contextKeyService);
ExplorerFocusedContext.bindTo(this.list.contextKeyService);
this.editorFocusedContext = EditorFocusedInOpenEditorsContext.bindTo(this.contextKeyService);
this.editorWithResourceFocusedContext = EditorWithResourceFocusedInOpenEditorsContext.bindTo(this.contextKeyService);
this.untitledEditorFocusedContext = UntitledEditorFocusedInOpenEditorsContext.bindTo(this.contextKeyService);
this.groupFocusedContext = GroupFocusedInOpenEditorsContext.bindTo(this.contextKeyService);
this.resourceContext = this.instantiationService.createInstance(ResourceContextKey);
this.groupFocusedContext = OpenEditorsGroupContext.bindTo(this.contextKeyService);
this.disposables.push(this.list.onContextMenu(e => this.onListContextMenu(e)));
this.list.onFocusChange(e => {
this.editorFocusedContext.reset();
this.editorWithResourceFocusedContext.reset();
this.resourceContext.reset();
this.groupFocusedContext.reset();
this.untitledEditorFocusedContext.reset();
const element = e.elements.length ? e.elements[0] : undefined;
if (element instanceof OpenEditor) {
this.editorFocusedContext.set(true);
this.editorWithResourceFocusedContext.set(!!element.getResource());
this.untitledEditorFocusedContext.set(element.isUntitled());
this.resourceContext.set(element.getResource());
} else if (!!element) {
this.groupFocusedContext.set(true);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册