Skip to content

Commit 261e65f

Browse files
authored
joh/issue154804 (microsoft#154909)
* add action to reset menu hidden states, add actions.contribution file for service and command registration * some 💄
1 parent 491a834 commit 261e65f

File tree

6 files changed

+73
-15
lines changed

6 files changed

+73
-15
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { IMenuService, registerAction2 } from 'vs/platform/actions/common/actions';
7+
import { MenuHiddenStatesReset } from 'vs/platform/actions/common/menuResetAction';
8+
import { MenuService } from 'vs/platform/actions/common/menuService';
9+
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
10+
11+
12+
registerSingleton(IMenuService, MenuService, true);
13+
14+
registerAction2(MenuHiddenStatesReset);

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,18 @@ export interface IMenuService {
196196

197197
readonly _serviceBrand: undefined;
198198

199+
/**
200+
* Create a new menu for the given menu identifier. A menu sends events when it's entries
201+
* have changed (placement, enablement, checked-state). By default it does not send events for
202+
* submenu entries. That is more expensive and must be explicitly enabled with the
203+
* `emitEventsForSubmenuChanges` flag.
204+
*/
199205
createMenu(id: MenuId, contextKeyService: IContextKeyService, options?: IMenuCreateOptions): IMenu;
206+
207+
/**
208+
* Reset **all** menu item hidden states.
209+
*/
210+
resetHiddenStates(): void;
200211
}
201212

202213
export type ICommandsMap = Map<string, ICommandAction>;
@@ -355,13 +366,13 @@ export class SubmenuItemAction extends SubmenuAction {
355366

356367
export class MenuItemActionManageActions {
357368
constructor(
358-
private readonly _hideThis: IAction,
359-
private readonly _toggleAny: IAction[][],
369+
readonly hideThis: IAction,
370+
readonly toggleAny: readonly IAction[][],
360371
) { }
361372

362373
asList(): IAction[] {
363-
let result: IAction[] = [this._hideThis];
364-
for (const n of this._toggleAny) {
374+
let result: IAction[] = [this.hideThis];
375+
for (const n of this.toggleAny) {
365376
result.push(new Separator());
366377
result = result.concat(n);
367378
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { localize } from 'vs/nls';
7+
import { Action2, IMenuService } from 'vs/platform/actions/common/actions';
8+
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
9+
import { ILogService } from 'vs/platform/log/common/log';
10+
11+
export class MenuHiddenStatesReset extends Action2 {
12+
13+
constructor() {
14+
super({
15+
id: 'menu.resetHiddenStates',
16+
title: {
17+
value: localize('title', 'Reset Hidden Menus'),
18+
original: 'Reset Hidden Menus'
19+
},
20+
category: localize('cat', 'View'),
21+
f1: true
22+
});
23+
}
24+
25+
run(accessor: ServicesAccessor): void {
26+
accessor.get(IMenuService).resetHiddenStates();
27+
accessor.get(ILogService).info('did RESET all menu hidden states');
28+
}
29+
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ export class MenuService implements IMenuService {
2828
this._hiddenStates = new PersistedMenuHideState(storageService);
2929
}
3030

31-
/**
32-
* Create a new menu for the given menu identifier. A menu sends events when it's entries
33-
* have changed (placement, enablement, checked-state). By default it does not send events for
34-
* submenu entries. That is more expensive and must be explicitly enabled with the
35-
* `emitEventsForSubmenuChanges` flag.
36-
*/
3731
createMenu(id: MenuId, contextKeyService: IContextKeyService, options?: IMenuCreateOptions): IMenu {
3832
return new Menu(id, this._hiddenStates, { emitEventsForSubmenuChanges: false, eventDebounceDelay: 50, ...options }, this._commandService, contextKeyService, this);
3933
}
34+
35+
resetHiddenStates(): void {
36+
this._hiddenStates.reset();
37+
}
4038
}
4139

4240
class PersistedMenuHideState {
@@ -110,6 +108,11 @@ class PersistedMenuHideState {
110108
this._persist();
111109
}
112110

111+
reset(): void {
112+
this._data = Object.create(null);
113+
this._persist();
114+
}
115+
113116
private _persist(): void {
114117
try {
115118
this._ignoreChangeEvent = true;
@@ -264,9 +267,8 @@ class Menu implements IMenu {
264267
action.dispose();
265268
action = undefined;
266269
}
267-
// add toggle submenu
270+
// add toggle submenu - this re-creates ToggleMenuItemAction-instances for submenus but that's OK...
268271
if (action) {
269-
// todo@jrieken this isn't good and O(n2) because this recurses for each submenu...
270272
const makeToggleCommand = (id: MenuId, action: IAction): IAction => {
271273
if (action instanceof SubmenuItemAction) {
272274
return new SubmenuAction(action.id, action.label, action.actions.map(a => makeToggleCommand(action.item.submenu, a)));

src/vs/workbench/test/browser/workbenchTestServices.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,10 @@ export class TestMenuService implements IMenuService {
519519
getActions: () => []
520520
};
521521
}
522+
523+
resetHiddenStates(): void {
524+
// nothing
525+
}
522526
}
523527

524528
export class TestHistoryService implements IHistoryService {

src/vs/workbench/workbench.common.main.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import 'vs/workbench/browser/parts/views/viewsService';
5252

5353
//#region --- workbench services
5454

55+
import 'vs/platform/actions/common/actions.contribution';
5556
import 'vs/platform/undoRedo/common/undoRedoService';
5657
import 'vs/workbench/services/extensions/browser/extensionUrlHandler';
5758
import 'vs/workbench/services/keybinding/common/keybindingEditing';
@@ -116,8 +117,6 @@ import { ContextKeyService } from 'vs/platform/contextkey/browser/contextKeyServ
116117
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
117118
import { ITextResourceConfigurationService } from 'vs/editor/common/services/textResourceConfiguration';
118119
import { TextResourceConfigurationService } from 'vs/editor/common/services/textResourceConfigurationService';
119-
import { IMenuService } from 'vs/platform/actions/common/actions';
120-
import { MenuService } from 'vs/platform/actions/common/menuService';
121120
import { IDownloadService } from 'vs/platform/download/common/download';
122121
import { DownloadService } from 'vs/platform/download/common/downloadService';
123122
import { OpenerService } from 'vs/editor/browser/services/openerService';
@@ -140,7 +139,6 @@ registerSingleton(IMarkerDecorationsService, MarkerDecorationsService);
140139
registerSingleton(IMarkerService, MarkerService, true);
141140
registerSingleton(IContextKeyService, ContextKeyService);
142141
registerSingleton(ITextResourceConfigurationService, TextResourceConfigurationService);
143-
registerSingleton(IMenuService, MenuService, true);
144142
registerSingleton(IDownloadService, DownloadService, true);
145143
registerSingleton(IOpenerService, OpenerService, true);
146144
registerSingleton(IExtensionsProfileScannerService, ExtensionsProfileScannerService);

0 commit comments

Comments
 (0)