Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to hide menu items from their respective menus #154801

Merged
merged 5 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/vs/base/common/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ export function equals<T>(one: ReadonlyArray<T> | undefined, other: ReadonlyArra
return true;
}

/**
* Remove the element at `index` by replacing it with the last element. This is faster than `splice`
* but changes the order of the array
*/
export function removeFastWithoutKeepingOrder<T>(array: T[], index: number) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to learn about this new utility!

const last = array.length - 1;
if (index < last) {
array[index] = array[last];
}
array.pop();
}

/**
* Performs a binary search algorithm over a sorted array.
*
Expand Down
13 changes: 13 additions & 0 deletions src/vs/base/test/common/arrays.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import * as assert from 'assert';
import * as arrays from 'vs/base/common/arrays';

suite('Arrays', () => {

test('removeFastWithoutKeepingOrder', () => {
const array = [1, 4, 5, 7, 55, 59, 60, 61, 64, 69];
arrays.removeFastWithoutKeepingOrder(array, 1);
assert.deepStrictEqual(array, [1, 69, 5, 7, 55, 59, 60, 61, 64]);

arrays.removeFastWithoutKeepingOrder(array, 0);
assert.deepStrictEqual(array, [64, 69, 5, 7, 55, 59, 60, 61]);

arrays.removeFastWithoutKeepingOrder(array, 7);
assert.deepStrictEqual(array, [64, 69, 5, 7, 55, 59, 60]);
});

test('findFirst', () => {
const array = [1, 4, 5, 7, 55, 59, 60, 61, 64, 69];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { IContextMenuProvider } from 'vs/base/browser/contextmenu';
import * as DOM from 'vs/base/browser/dom';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { ActionViewItem, BaseActionViewItem } from 'vs/base/browser/ui/actionbar/actionViewItems';
Expand All @@ -18,6 +17,7 @@ import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';

export interface IDropdownWithPrimaryActionViewItemOptions {
getKeyBinding?: (action: IAction) => ResolvedKeybinding | undefined;
Expand All @@ -38,15 +38,15 @@ export class DropdownWithPrimaryActionViewItem extends BaseActionViewItem {
dropdownAction: IAction,
dropdownMenuActions: IAction[],
className: string,
private readonly _contextMenuProvider: IContextMenuProvider,
private readonly _contextMenuProvider: IContextMenuService,
private readonly _options: IDropdownWithPrimaryActionViewItemOptions | undefined,
@IKeybindingService _keybindingService: IKeybindingService,
@INotificationService _notificationService: INotificationService,
@IContextKeyService _contextKeyService: IContextKeyService,
@IThemeService _themeService: IThemeService
) {
super(null, primaryAction);
this._primaryAction = new MenuEntryActionViewItem(primaryAction, undefined, _keybindingService, _notificationService, _contextKeyService, _themeService);
this._primaryAction = new MenuEntryActionViewItem(primaryAction, undefined, _keybindingService, _notificationService, _contextKeyService, _themeService, _contextMenuProvider);
this._dropdown = new DropdownMenuActionViewItem(dropdownAction, dropdownMenuActions, this._contextMenuProvider, {
menuAsChild: true,
classNames: ['codicon', 'codicon-chevron-down'],
Expand Down
20 changes: 18 additions & 2 deletions src/vs/platform/actions/browser/menuEntryActionViewItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ export class MenuEntryActionViewItem extends ActionViewItem {
@IKeybindingService protected readonly _keybindingService: IKeybindingService,
@INotificationService protected _notificationService: INotificationService,
@IContextKeyService protected _contextKeyService: IContextKeyService,
@IThemeService protected _themeService: IThemeService
@IThemeService protected _themeService: IThemeService,
@IContextMenuService protected _contextMenuService: IContextMenuService
) {
super(undefined, action, { icon: !!(action.class || action.item.icon), label: !action.class && !action.item.icon, draggable: options?.draggable, keybinding: options?.keybinding, hoverDelegate: options?.hoverDelegate });
this._altKey = ModifierKeyEmitter.getInstance();
Expand Down Expand Up @@ -202,6 +203,21 @@ export class MenuEntryActionViewItem extends ActionViewItem {
mouseOver = true;
updateAltState();
}));


this._register(addDisposableListener(container, 'contextmenu', event => {
if (!this._menuItemAction.hideActions) {
return;
}

event.preventDefault();
event.stopPropagation();

this._contextMenuService.showContextMenu({
getAnchor: () => container,
getActions: () => this._menuItemAction.hideActions!.asList()
});
}, true));
}

override updateLabel(): void {
Expand Down Expand Up @@ -356,7 +372,7 @@ export class DropdownWithDefaultActionViewItem extends BaseActionViewItem {
) {
super(null, submenuAction);
this._options = options;
this._storageKey = `${submenuAction.item.submenu._debugName}_lastActionId`;
this._storageKey = `${submenuAction.item.submenu.id}_lastActionId`;

// determine default action
let defaultAction: IAction | undefined;
Expand Down
34 changes: 27 additions & 7 deletions src/vs/platform/actions/common/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function isISubmenuItem(item: IMenuItem | ISubmenuItem): item is ISubmenu

export class MenuId {

private static _idPool = 0;
private static readonly _idPool = new Set<string>();

static readonly CommandPalette = new MenuId('CommandPalette');
static readonly DebugBreakpointsContext = new MenuId('DebugBreakpointsContext');
Expand Down Expand Up @@ -162,12 +162,15 @@ export class MenuId {
static readonly NewFile = new MenuId('NewFile');
static readonly MergeToolbar = new MenuId('MergeToolbar');

readonly id: number;
readonly _debugName: string;

constructor(debugName: string) {
this.id = MenuId._idPool++;
this._debugName = debugName;
readonly id: string;

constructor(identifier: string) {
if (MenuId._idPool.has(identifier)) {
throw new Error(`Duplicate menu identifier ${identifier}`);
}
MenuId._idPool.add(identifier);
this.id = identifier;
}
}

Expand Down Expand Up @@ -350,6 +353,22 @@ export class SubmenuItemAction extends SubmenuAction {
}
}

export class MenuItemActionManageActions {
constructor(
private readonly _hideThis: IAction,
private readonly _toggleAny: IAction[][],
) { }

asList(): IAction[] {
let result: IAction[] = [this._hideThis];
for (const n of this._toggleAny) {
result.push(new Separator());
result = result.concat(n);
}
return result;
}
}

// implements IAction, does NOT extend Action, so that no one
// subscribes to events of Action or modified properties
export class MenuItemAction implements IAction {
Expand All @@ -370,6 +389,7 @@ export class MenuItemAction implements IAction {
item: ICommandAction,
alt: ICommandAction | undefined,
options: IMenuActionOptions | undefined,
readonly hideActions: MenuItemActionManageActions | undefined,
@IContextKeyService contextKeyService: IContextKeyService,
@ICommandService private _commandService: ICommandService
) {
Expand All @@ -396,7 +416,7 @@ export class MenuItemAction implements IAction {
}

this.item = item;
this.alt = alt ? new MenuItemAction(alt, undefined, options, contextKeyService, _commandService) : undefined;
this.alt = alt ? new MenuItemAction(alt, undefined, options, hideActions, contextKeyService, _commandService) : undefined;
this._options = options;
if (ThemeIcon.isThemeIcon(item.icon)) {
this.class = CSSIcon.asClassName(item.icon);
Expand Down
Loading