Skip to content

Commit e5cbf75

Browse files
committed
commands use precondition for enablement
fixes microsoft#41103
1 parent 64b5169 commit e5cbf75

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

src/vs/workbench/parts/files/electron-browser/fileActions.contribution.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { revertLocalChangesCommand, acceptLocalChangesCommand, CONFLICT_RESOLUTI
1111
import { SyncActionDescriptor, MenuId, MenuRegistry } from 'vs/platform/actions/common/actions';
1212
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions';
1313
import { KeyMod, KeyChord, KeyCode } from 'vs/base/common/keyCodes';
14-
import { openWindowCommand, 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, ResourceSelectedForCompareContext, REVEAL_IN_OS_LABEL } from 'vs/workbench/parts/files/electron-browser/fileCommands';
14+
import { openWindowCommand, 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, ResourceSelectedForCompareContext, REVEAL_IN_OS_LABEL, DirtyEditorContext } from 'vs/workbench/parts/files/electron-browser/fileCommands';
1515
import { CommandsRegistry, ICommandHandler } from 'vs/platform/commands/common/commands';
1616
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
1717
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
@@ -195,7 +195,8 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
195195
order: 10,
196196
command: {
197197
id: SAVE_FILE_COMMAND_ID,
198-
title: SAVE_FILE_LABEL
198+
title: SAVE_FILE_LABEL,
199+
precondition: DirtyEditorContext
199200
},
200201
when: ContextKeyExpr.and(ResourceContextKey.IsFile, AutoSaveContext.notEqualsTo('afterDelay'))
201202
});
@@ -205,7 +206,8 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
205206
order: 20,
206207
command: {
207208
id: REVERT_FILE_COMMAND_ID,
208-
title: nls.localize('revert', "Revert File")
209+
title: nls.localize('revert', "Revert File"),
210+
precondition: DirtyEditorContext
209211
},
210212
when: ContextKeyExpr.and(ResourceContextKey.IsFile, AutoSaveContext.notEqualsTo('afterDelay'))
211213
});
@@ -233,9 +235,10 @@ MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
233235
order: 10,
234236
command: {
235237
id: COMPARE_WITH_SAVED_COMMAND_ID,
236-
title: nls.localize('compareWithSaved', "Compare with Saved")
238+
title: nls.localize('compareWithSaved', "Compare with Saved"),
239+
precondition: DirtyEditorContext
237240
},
238-
when: ResourceContextKey.IsFile
241+
when: ContextKeyExpr.and(ResourceContextKey.IsFile, AutoSaveContext.notEqualsTo('afterDelay'))
239242
});
240243

241244
const compareResourceCommand = {
@@ -363,9 +366,10 @@ MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
363366
order: 20,
364367
command: {
365368
id: PASTE_FILE_ID,
366-
title: PASTE_FILE_LABEL
369+
title: PASTE_FILE_LABEL,
370+
precondition: FileCopiedContext
367371
},
368-
when: ContextKeyExpr.and(ExplorerFolderContext, FileCopiedContext)
372+
when: ExplorerFolderContext
369373
});
370374

371375
MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {

src/vs/workbench/parts/files/electron-browser/fileCommands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export const SAVE_FILES_COMMAND_ID = 'workbench.command.files.saveFiles';
6666
export const SAVE_FILES_LABEL = nls.localize('saveFiles', "Save All Files");
6767

6868
export const OpenEditorsGroupContext = new RawContextKey<boolean>('groupFocusedInOpenEditors', false);
69+
export const DirtyEditorContext = new RawContextKey<boolean>('dirtyEditor', false);
6970
export const ResourceSelectedForCompareContext = new RawContextKey<boolean>('resourceSelectedForCompare', false);
7071

7172
export const openWindowCommand = (accessor: ServicesAccessor, paths: string[], forceNewWindow: boolean) => {

src/vs/workbench/parts/files/electron-browser/views/openEditorsView.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { KeyCode } from 'vs/base/common/keyCodes';
4040
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
4141
import { fillInActions } from 'vs/platform/actions/browser/menuItemActionItem';
4242
import { IMenuService, MenuId, IMenu } from 'vs/platform/actions/common/actions';
43-
import { OpenEditorsGroupContext } from 'vs/workbench/parts/files/electron-browser/fileCommands';
43+
import { OpenEditorsGroupContext, DirtyEditorContext } from 'vs/workbench/parts/files/electron-browser/fileCommands';
4444
import { ResourceContextKey } from 'vs/workbench/common/resources';
4545
import { DataTransfers } from 'vs/base/browser/dnd';
4646
import { getPathLabel, getBaseLabel } from 'vs/base/common/labels';
@@ -64,6 +64,7 @@ export class OpenEditorsView extends ViewsViewletPanel {
6464
private needsRefresh: boolean;
6565
private resourceContext: ResourceContextKey;
6666
private groupFocusedContext: IContextKey<boolean>;
67+
private dirtyEditorFocusedContext: IContextKey<boolean>;
6768

6869
constructor(
6970
options: IViewletViewOptions,
@@ -159,13 +160,16 @@ export class OpenEditorsView extends ViewsViewletPanel {
159160

160161
this.resourceContext = this.instantiationService.createInstance(ResourceContextKey);
161162
this.groupFocusedContext = OpenEditorsGroupContext.bindTo(this.contextKeyService);
163+
this.dirtyEditorFocusedContext = DirtyEditorContext.bindTo(this.contextKeyService);
162164

163165
this.disposables.push(this.list.onContextMenu(e => this.onListContextMenu(e)));
164166
this.list.onFocusChange(e => {
165167
this.resourceContext.reset();
166168
this.groupFocusedContext.reset();
169+
this.dirtyEditorFocusedContext.reset();
167170
const element = e.elements.length ? e.elements[0] : undefined;
168171
if (element instanceof OpenEditor) {
172+
this.dirtyEditorFocusedContext.set(this.textFileService.isDirty(element.getResource()));
169173
this.resourceContext.set(element.getResource());
170174
} else if (!!element) {
171175
this.groupFocusedContext.set(true);

0 commit comments

Comments
 (0)