Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/contextview/browser/contextMenuService.ts
3296 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 { IContextMenuDelegate } from '../../../base/browser/contextmenu.js';
7
import { ModifierKeyEmitter } from '../../../base/browser/dom.js';
8
import { IAction, Separator } from '../../../base/common/actions.js';
9
import { Emitter } from '../../../base/common/event.js';
10
import { Disposable } from '../../../base/common/lifecycle.js';
11
import { getFlatContextMenuActions } from '../../actions/browser/menuEntryActionViewItem.js';
12
import { IMenuService, MenuId } from '../../actions/common/actions.js';
13
import { IContextKeyService } from '../../contextkey/common/contextkey.js';
14
import { IKeybindingService } from '../../keybinding/common/keybinding.js';
15
import { INotificationService } from '../../notification/common/notification.js';
16
import { ITelemetryService } from '../../telemetry/common/telemetry.js';
17
import { ContextMenuHandler, IContextMenuHandlerOptions } from './contextMenuHandler.js';
18
import { IContextMenuMenuDelegate, IContextMenuService, IContextViewService } from './contextView.js';
19
20
export class ContextMenuService extends Disposable implements IContextMenuService {
21
22
declare readonly _serviceBrand: undefined;
23
24
private _contextMenuHandler: ContextMenuHandler | undefined = undefined;
25
private get contextMenuHandler(): ContextMenuHandler {
26
if (!this._contextMenuHandler) {
27
this._contextMenuHandler = new ContextMenuHandler(this.contextViewService, this.telemetryService, this.notificationService, this.keybindingService);
28
}
29
30
return this._contextMenuHandler;
31
}
32
33
private readonly _onDidShowContextMenu = this._store.add(new Emitter<void>());
34
readonly onDidShowContextMenu = this._onDidShowContextMenu.event;
35
36
private readonly _onDidHideContextMenu = this._store.add(new Emitter<void>());
37
readonly onDidHideContextMenu = this._onDidHideContextMenu.event;
38
39
constructor(
40
@ITelemetryService private readonly telemetryService: ITelemetryService,
41
@INotificationService private readonly notificationService: INotificationService,
42
@IContextViewService private readonly contextViewService: IContextViewService,
43
@IKeybindingService private readonly keybindingService: IKeybindingService,
44
@IMenuService private readonly menuService: IMenuService,
45
@IContextKeyService private readonly contextKeyService: IContextKeyService,
46
) {
47
super();
48
}
49
50
configure(options: IContextMenuHandlerOptions): void {
51
this.contextMenuHandler.configure(options);
52
}
53
54
// ContextMenu
55
56
showContextMenu(delegate: IContextMenuDelegate | IContextMenuMenuDelegate): void {
57
58
delegate = ContextMenuMenuDelegate.transform(delegate, this.menuService, this.contextKeyService);
59
60
this.contextMenuHandler.showContextMenu({
61
...delegate,
62
onHide: (didCancel) => {
63
delegate.onHide?.(didCancel);
64
65
this._onDidHideContextMenu.fire();
66
}
67
});
68
ModifierKeyEmitter.getInstance().resetKeyStatus();
69
this._onDidShowContextMenu.fire();
70
}
71
}
72
73
export namespace ContextMenuMenuDelegate {
74
75
function is(thing: IContextMenuDelegate | IContextMenuMenuDelegate): thing is IContextMenuMenuDelegate {
76
return thing && (<IContextMenuMenuDelegate>thing).menuId instanceof MenuId;
77
}
78
79
export function transform(delegate: IContextMenuDelegate | IContextMenuMenuDelegate, menuService: IMenuService, globalContextKeyService: IContextKeyService): IContextMenuDelegate {
80
if (!is(delegate)) {
81
return delegate;
82
}
83
const { menuId, menuActionOptions, contextKeyService } = delegate;
84
return {
85
...delegate,
86
getActions: () => {
87
let target: IAction[] = [];
88
if (menuId) {
89
const menu = menuService.getMenuActions(menuId, contextKeyService ?? globalContextKeyService, menuActionOptions);
90
target = getFlatContextMenuActions(menu);
91
}
92
if (!delegate.getActions) {
93
return target;
94
} else {
95
return Separator.join(delegate.getActions(), target);
96
}
97
}
98
};
99
}
100
}
101
102