Path: blob/main/src/vs/workbench/contrib/chat/browser/enablementActions.ts
13401 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 { Action, IAction } from '../../../../base/common/actions.js';6import { localize } from '../../../../nls.js';7import { IWorkspaceContextService, WorkbenchState } from '../../../../platform/workspace/common/workspace.js';8import { ContributionEnablementState, IEnablementModel, isContributionDisabled } from '../common/enablement.js';910/**11* Creates the four standard enablement actions (Enable, Enable Workspace,12* Disable, Disable Workspace) for a contribution identified by a string key.13*/14export function createEnablementActions(15key: string,16enablementModel: IEnablementModel,17idPrefix: string,18): [enable: Action, enableWorkspace: Action, disable: Action, disableWorkspace: Action] {19return [20new Action(`${idPrefix}.enable`, localize('enable', "Enable"), undefined, true,21() => { enablementModel.setEnabled(key, ContributionEnablementState.EnabledProfile); return Promise.resolve(); }),22new Action(`${idPrefix}.enableForWorkspace`, localize('enableForWorkspace', "Enable (Workspace)"), undefined, true,23() => { enablementModel.setEnabled(key, ContributionEnablementState.EnabledWorkspace); return Promise.resolve(); }),24new Action(`${idPrefix}.disable`, localize('disable', "Disable"), undefined, true,25() => { enablementModel.setEnabled(key, ContributionEnablementState.DisabledProfile); return Promise.resolve(); }),26new Action(`${idPrefix}.disableForWorkspace`, localize('disableForWorkspace', "Disable (Workspace)"), undefined, true,27() => { enablementModel.setEnabled(key, ContributionEnablementState.DisabledWorkspace); return Promise.resolve(); }),28];29}3031/**32* Builds the standard enablement context-menu action group for a33* contribution. Returns either the enable or disable actions depending34* on the current state, with workspace variants included only when a35* workspace is open.36*/37export function buildEnablementContextMenuGroup(38enablementState: ContributionEnablementState,39key: string,40enablementModel: IEnablementModel,41workspaceContextService: IWorkspaceContextService,42idPrefix: string,43): IAction[] {44const hasWorkspace = workspaceContextService.getWorkbenchState() !== WorkbenchState.EMPTY;45const [enable, enableWorkspace, disable, disableWorkspace] = createEnablementActions(key, enablementModel, idPrefix);46const actions: IAction[] = [];47if (isContributionDisabled(enablementState)) {48actions.push(enable);49if (hasWorkspace) {50actions.push(enableWorkspace);51}52} else {53actions.push(disable);54if (hasWorkspace) {55actions.push(disableWorkspace);56}57}58return actions;59}606162