Path: blob/main/src/vs/workbench/browser/parts/views/viewMenuActions.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { IAction } from '../../../../base/common/actions.js';6import { Emitter, Event } from '../../../../base/common/event.js';7import { Disposable } from '../../../../base/common/lifecycle.js';8import { getActionBarActions, PrimaryAndSecondaryActions } from '../../../../platform/actions/browser/menuEntryActionViewItem.js';9import { MenuId, IMenuActionOptions, IMenuService, IMenu } from '../../../../platform/actions/common/actions.js';10import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';11import { IViewDescriptorService, ViewContainer, ViewContainerLocationToString } from '../../../common/views.js';1213export class ViewMenuActions extends Disposable {1415private readonly menu: IMenu;1617private readonly _onDidChange = this._register(new Emitter<void>());18readonly onDidChange = this._onDidChange.event;1920constructor(21readonly menuId: MenuId,22private readonly contextMenuId: MenuId | undefined,23private readonly options: IMenuActionOptions | undefined,24@IContextKeyService private readonly contextKeyService: IContextKeyService,25@IMenuService private readonly menuService: IMenuService,26) {27super();28this.menu = this._register(menuService.createMenu(menuId, contextKeyService, { emitEventsForSubmenuChanges: true }));29this._register(this.menu.onDidChange(() => {30this.actions = undefined;31this._onDidChange.fire();32}));33}3435private actions: PrimaryAndSecondaryActions | undefined;36private getActions(): PrimaryAndSecondaryActions {37if (!this.actions) {38this.actions = getActionBarActions(this.menu.getActions(this.options));39}40return this.actions;41}4243getPrimaryActions(): IAction[] {44return this.getActions().primary;45}4647getSecondaryActions(): IAction[] {48return this.getActions().secondary;49}5051getContextMenuActions(): IAction[] {52if (this.contextMenuId) {53const menu = this.menuService.getMenuActions(this.contextMenuId, this.contextKeyService, this.options);54return getActionBarActions(menu).secondary;55}56return [];57}58}5960export class ViewContainerMenuActions extends ViewMenuActions {61constructor(62element: HTMLElement,63viewContainer: ViewContainer,64@IViewDescriptorService viewDescriptorService: IViewDescriptorService,65@IContextKeyService contextKeyService: IContextKeyService,66@IMenuService menuService: IMenuService,67) {68const scopedContextKeyService = contextKeyService.createScoped(element);69scopedContextKeyService.createKey('viewContainer', viewContainer.id);70const viewContainerLocationKey = scopedContextKeyService.createKey('viewContainerLocation', ViewContainerLocationToString(viewDescriptorService.getViewContainerLocation(viewContainer)!));71super(MenuId.ViewContainerTitle, MenuId.ViewContainerTitleContext, { shouldForwardArgs: true, renderShortTitle: true }, scopedContextKeyService, menuService);72this._register(scopedContextKeyService);73this._register(Event.filter(viewDescriptorService.onDidChangeContainerLocation, e => e.viewContainer === viewContainer)(() => viewContainerLocationKey.set(ViewContainerLocationToString(viewDescriptorService.getViewContainerLocation(viewContainer)!))));74}75}767778