Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/actions/browser/actionViewItemService.ts
3294 views
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 { IActionViewItem } from '../../../base/browser/ui/actionbar/actionbar.js';
7
import { IActionViewItemOptions } from '../../../base/browser/ui/actionbar/actionViewItems.js';
8
import { IAction } from '../../../base/common/actions.js';
9
import { Emitter, Event } from '../../../base/common/event.js';
10
import { Disposable, IDisposable, toDisposable } from '../../../base/common/lifecycle.js';
11
import { InstantiationType, registerSingleton } from '../../instantiation/common/extensions.js';
12
import { createDecorator, IInstantiationService } from '../../instantiation/common/instantiation.js';
13
import { MenuId } from '../common/actions.js';
14
15
16
export const IActionViewItemService = createDecorator<IActionViewItemService>('IActionViewItemService');
17
18
19
export interface IActionViewItemFactory {
20
(action: IAction, options: IActionViewItemOptions, instantiationService: IInstantiationService, windowId: number): IActionViewItem | undefined;
21
}
22
23
export interface IActionViewItemService {
24
25
_serviceBrand: undefined;
26
27
onDidChange: Event<MenuId>;
28
29
register(menu: MenuId, submenu: MenuId, provider: IActionViewItemFactory, event?: Event<unknown>): IDisposable;
30
register(menu: MenuId, commandId: string, provider: IActionViewItemFactory, event?: Event<unknown>): IDisposable;
31
32
lookUp(menu: MenuId, submenu: MenuId): IActionViewItemFactory | undefined;
33
lookUp(menu: MenuId, commandId: string): IActionViewItemFactory | undefined;
34
}
35
36
export class NullActionViewItemService implements IActionViewItemService {
37
_serviceBrand: undefined;
38
39
onDidChange: Event<MenuId> = Event.None;
40
41
register(menu: MenuId, commandId: string | MenuId, provider: IActionViewItemFactory, event?: Event<unknown>): IDisposable {
42
return Disposable.None;
43
}
44
45
lookUp(menu: MenuId, commandId: string | MenuId): IActionViewItemFactory | undefined {
46
return undefined;
47
}
48
}
49
50
class ActionViewItemService implements IActionViewItemService {
51
52
declare _serviceBrand: undefined;
53
54
private readonly _providers = new Map<string, IActionViewItemFactory>();
55
56
private readonly _onDidChange = new Emitter<MenuId>();
57
readonly onDidChange: Event<MenuId> = this._onDidChange.event;
58
59
dispose(): void {
60
this._onDidChange.dispose();
61
}
62
63
register(menu: MenuId, commandOrSubmenuId: string | MenuId, provider: IActionViewItemFactory, event?: Event<unknown>): IDisposable {
64
const id = this._makeKey(menu, commandOrSubmenuId);
65
if (this._providers.has(id)) {
66
throw new Error(`A provider for the command ${commandOrSubmenuId} and menu ${menu} is already registered.`);
67
}
68
this._providers.set(id, provider);
69
70
const listener = event?.(() => {
71
this._onDidChange.fire(menu);
72
});
73
74
return toDisposable(() => {
75
listener?.dispose();
76
this._providers.delete(id);
77
});
78
}
79
80
lookUp(menu: MenuId, commandOrMenuId: string | MenuId): IActionViewItemFactory | undefined {
81
return this._providers.get(this._makeKey(menu, commandOrMenuId));
82
}
83
84
private _makeKey(menu: MenuId, commandOrMenuId: string | MenuId) {
85
return `${menu.id}/${(commandOrMenuId instanceof MenuId ? commandOrMenuId.id : commandOrMenuId)}`;
86
}
87
}
88
89
registerSingleton(IActionViewItemService, ActionViewItemService, InstantiationType.Delayed);
90
91