Skip to content

Commit 6a28cd9

Browse files
authored
Fix keybinding configuration and menu service (microsoft#209970)
Configure Keybinding: fix when and modify label
1 parent b7cb65c commit 6a28cd9

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/vs/platform/actions/common/menuService.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { IAction, Separator, toAction } from 'vs/base/common/actions';
1414
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
1515
import { removeFastWithoutKeepingOrder } from 'vs/base/common/arrays';
1616
import { localize } from 'vs/nls';
17+
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
1718

1819
export class MenuService implements IMenuService {
1920

@@ -23,13 +24,14 @@ export class MenuService implements IMenuService {
2324

2425
constructor(
2526
@ICommandService private readonly _commandService: ICommandService,
27+
@IKeybindingService private readonly _keybindingService: IKeybindingService,
2628
@IStorageService storageService: IStorageService,
2729
) {
2830
this._hiddenStates = new PersistedMenuHideState(storageService);
2931
}
3032

3133
createMenu(id: MenuId, contextKeyService: IContextKeyService, options?: IMenuCreateOptions): IMenu {
32-
return new MenuImpl(id, this._hiddenStates, { emitEventsForSubmenuChanges: false, eventDebounceDelay: 50, ...options }, this._commandService, contextKeyService);
34+
return new MenuImpl(id, this._hiddenStates, { emitEventsForSubmenuChanges: false, eventDebounceDelay: 50, ...options }, this._commandService, this._keybindingService, contextKeyService);
3335
}
3436

3537
resetHiddenStates(ids?: MenuId[]): void {
@@ -162,6 +164,7 @@ class MenuInfo {
162164
private readonly _hiddenStates: PersistedMenuHideState,
163165
private readonly _collectContextKeysForSubmenus: boolean,
164166
@ICommandService private readonly _commandService: ICommandService,
167+
@IKeybindingService private readonly _keybindingService: IKeybindingService,
165168
@IContextKeyService private readonly _contextKeyService: IContextKeyService
166169
) {
167170
this.refresh();
@@ -245,11 +248,11 @@ class MenuInfo {
245248
const menuHide = createMenuHide(this._id, isMenuItem ? item.command : item, this._hiddenStates);
246249
if (isMenuItem) {
247250
// MenuItemAction
248-
const menuKeybinding = createMenuKeybindingAction(this._id, item.command, this._commandService);
251+
const menuKeybinding = createMenuKeybindingAction(this._id, item.command, item.when, this._commandService, this._keybindingService);
249252
activeActions.push(new MenuItemAction(item.command, item.alt, options, menuHide, menuKeybinding, this._contextKeyService, this._commandService));
250253
} else {
251254
// SubmenuItemAction
252-
const groups = new MenuInfo(item.submenu, this._hiddenStates, this._collectContextKeysForSubmenus, this._commandService, this._contextKeyService).createActionGroups(options);
255+
const groups = new MenuInfo(item.submenu, this._hiddenStates, this._collectContextKeysForSubmenus, this._commandService, this._keybindingService, this._contextKeyService).createActionGroups(options);
253256
const submenuActions = Separator.join(...groups.map(g => g[1]));
254257
if (submenuActions.length > 0) {
255258
activeActions.push(new SubmenuItemAction(item, menuHide, submenuActions));
@@ -336,9 +339,10 @@ class MenuImpl implements IMenu {
336339
hiddenStates: PersistedMenuHideState,
337340
options: Required<IMenuCreateOptions>,
338341
@ICommandService commandService: ICommandService,
342+
@IKeybindingService keybindingService: IKeybindingService,
339343
@IContextKeyService contextKeyService: IContextKeyService
340344
) {
341-
this._menuInfo = new MenuInfo(id, hiddenStates, options.emitEventsForSubmenuChanges, commandService, contextKeyService);
345+
this._menuInfo = new MenuInfo(id, hiddenStates, options.emitEventsForSubmenuChanges, commandService, keybindingService, contextKeyService);
342346

343347
// Rebuild this menu whenever the menu registry reports an event for this MenuId.
344348
// This usually happen while code and extensions are loaded and affects the over
@@ -438,17 +442,17 @@ function createMenuHide(menu: MenuId, command: ICommandAction | ISubmenuItem, st
438442
};
439443
}
440444

441-
function createMenuKeybindingAction(menu: MenuId, command: ICommandAction | ISubmenuItem, commandService: ICommandService): IAction | undefined {
445+
function createMenuKeybindingAction(menu: MenuId, command: ICommandAction | ISubmenuItem, when: ContextKeyExpression | undefined = undefined, commandService: ICommandService, keybindingService: IKeybindingService): IAction | undefined {
442446
if (isISubmenuItem(command)) {
443447
return undefined;
444448
}
445449

446450
const configureKeybindingAction = toAction({
447451
id: `configureKeybinding/${menu.id}/${command.id}`,
448-
label: localize('configure keybinding', "Configure Keybinding"),
452+
label: keybindingService.lookupKeybinding(command.id) ? localize('change keybinding', "Change Keybinding") : localize('configure keybinding', "Configure Keybinding"),
449453
run() {
450-
const when = command.precondition?.serialize();
451-
commandService.executeCommand('workbench.action.openGlobalKeybindings', `@command:${command.id}` + (when ? ` +when:${when}` : ''));
454+
const whenValue = when ? when.serialize() : undefined;
455+
commandService.executeCommand('workbench.action.openGlobalKeybindings', `@command:${command.id}` + (whenValue ? ` +when:${whenValue}` : ''));
452456
}
453457
});
454458

src/vs/platform/actions/test/common/menuService.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/uti
1010
import { isIMenuItem, MenuId, MenuRegistry } from 'vs/platform/actions/common/actions';
1111
import { MenuService } from 'vs/platform/actions/common/menuService';
1212
import { NullCommandService } from 'vs/platform/commands/test/common/nullCommandService';
13-
import { MockContextKeyService } from 'vs/platform/keybinding/test/common/mockKeybindingService';
13+
import { MockContextKeyService, MockKeybindingService } from 'vs/platform/keybinding/test/common/mockKeybindingService';
1414
import { InMemoryStorageService } from 'vs/platform/storage/common/storage';
1515

1616
// --- service instances
@@ -30,7 +30,7 @@ suite('MenuService', function () {
3030
let testMenuId: MenuId;
3131

3232
setup(function () {
33-
menuService = new MenuService(NullCommandService, new InMemoryStorageService());
33+
menuService = new MenuService(NullCommandService, new MockKeybindingService(), new InMemoryStorageService());
3434
testMenuId = new MenuId(`testo/${generateUuid()}`);
3535
disposables.clear();
3636
});

0 commit comments

Comments
 (0)