Path: blob/main/src/vs/platform/actions/browser/actionViewItemService.ts
3294 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 { IActionViewItem } from '../../../base/browser/ui/actionbar/actionbar.js';6import { IActionViewItemOptions } from '../../../base/browser/ui/actionbar/actionViewItems.js';7import { IAction } from '../../../base/common/actions.js';8import { Emitter, Event } from '../../../base/common/event.js';9import { Disposable, IDisposable, toDisposable } from '../../../base/common/lifecycle.js';10import { InstantiationType, registerSingleton } from '../../instantiation/common/extensions.js';11import { createDecorator, IInstantiationService } from '../../instantiation/common/instantiation.js';12import { MenuId } from '../common/actions.js';131415export const IActionViewItemService = createDecorator<IActionViewItemService>('IActionViewItemService');161718export interface IActionViewItemFactory {19(action: IAction, options: IActionViewItemOptions, instantiationService: IInstantiationService, windowId: number): IActionViewItem | undefined;20}2122export interface IActionViewItemService {2324_serviceBrand: undefined;2526onDidChange: Event<MenuId>;2728register(menu: MenuId, submenu: MenuId, provider: IActionViewItemFactory, event?: Event<unknown>): IDisposable;29register(menu: MenuId, commandId: string, provider: IActionViewItemFactory, event?: Event<unknown>): IDisposable;3031lookUp(menu: MenuId, submenu: MenuId): IActionViewItemFactory | undefined;32lookUp(menu: MenuId, commandId: string): IActionViewItemFactory | undefined;33}3435export class NullActionViewItemService implements IActionViewItemService {36_serviceBrand: undefined;3738onDidChange: Event<MenuId> = Event.None;3940register(menu: MenuId, commandId: string | MenuId, provider: IActionViewItemFactory, event?: Event<unknown>): IDisposable {41return Disposable.None;42}4344lookUp(menu: MenuId, commandId: string | MenuId): IActionViewItemFactory | undefined {45return undefined;46}47}4849class ActionViewItemService implements IActionViewItemService {5051declare _serviceBrand: undefined;5253private readonly _providers = new Map<string, IActionViewItemFactory>();5455private readonly _onDidChange = new Emitter<MenuId>();56readonly onDidChange: Event<MenuId> = this._onDidChange.event;5758dispose(): void {59this._onDidChange.dispose();60}6162register(menu: MenuId, commandOrSubmenuId: string | MenuId, provider: IActionViewItemFactory, event?: Event<unknown>): IDisposable {63const id = this._makeKey(menu, commandOrSubmenuId);64if (this._providers.has(id)) {65throw new Error(`A provider for the command ${commandOrSubmenuId} and menu ${menu} is already registered.`);66}67this._providers.set(id, provider);6869const listener = event?.(() => {70this._onDidChange.fire(menu);71});7273return toDisposable(() => {74listener?.dispose();75this._providers.delete(id);76});77}7879lookUp(menu: MenuId, commandOrMenuId: string | MenuId): IActionViewItemFactory | undefined {80return this._providers.get(this._makeKey(menu, commandOrMenuId));81}8283private _makeKey(menu: MenuId, commandOrMenuId: string | MenuId) {84return `${menu.id}/${(commandOrMenuId instanceof MenuId ? commandOrMenuId.id : commandOrMenuId)}`;85}86}8788registerSingleton(IActionViewItemService, ActionViewItemService, InstantiationType.Delayed);899091