Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/comments/browser/commentFormActions.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 { Button, ButtonWithDropdown } from '../../../../base/browser/ui/button/button.js';
7
import { ActionRunner, IAction } from '../../../../base/common/actions.js';
8
import { DisposableStore, IDisposable } from '../../../../base/common/lifecycle.js';
9
import { IMenu, SubmenuItemAction } from '../../../../platform/actions/common/actions.js';
10
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';
11
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.js';
12
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';
13
import { defaultButtonStyles } from '../../../../platform/theme/browser/defaultStyles.js';
14
import { CommentCommandId } from '../common/commentCommandIds.js';
15
16
export class CommentFormActions implements IDisposable {
17
private _buttonElements: HTMLElement[] = [];
18
private readonly _toDispose = new DisposableStore();
19
private _actions: IAction[] = [];
20
21
constructor(
22
private readonly keybindingService: IKeybindingService,
23
private readonly contextKeyService: IContextKeyService,
24
private readonly contextMenuService: IContextMenuService,
25
private container: HTMLElement,
26
private actionHandler: (action: IAction) => void,
27
private readonly maxActions?: number,
28
private readonly supportDropdowns?: boolean,
29
) { }
30
31
setActions(menu: IMenu, hasOnlySecondaryActions: boolean = false) {
32
this._toDispose.clear();
33
34
this._buttonElements.forEach(b => b.remove());
35
this._buttonElements = [];
36
37
const groups = menu.getActions({ shouldForwardArgs: true });
38
let isPrimary: boolean = !hasOnlySecondaryActions;
39
for (const group of groups) {
40
const [, actions] = group;
41
42
this._actions = actions;
43
for (const current of actions) {
44
const dropDownActions = this.supportDropdowns && current instanceof SubmenuItemAction ? current.actions : [];
45
const action = dropDownActions.length ? dropDownActions[0] : current;
46
let keybinding = this.keybindingService.lookupKeybinding(action.id, this.contextKeyService)?.getLabel();
47
if (!keybinding && isPrimary) {
48
keybinding = this.keybindingService.lookupKeybinding(CommentCommandId.Submit, this.contextKeyService)?.getLabel();
49
}
50
const title = keybinding ? `${action.label} (${keybinding})` : action.label;
51
const actionHandler = this.actionHandler;
52
const button = dropDownActions.length ? new ButtonWithDropdown(this.container, {
53
contextMenuProvider: this.contextMenuService,
54
actions: dropDownActions,
55
actionRunner: this._toDispose.add(new class extends ActionRunner {
56
protected override async runAction(action: IAction, context?: unknown): Promise<void> {
57
return actionHandler(action);
58
}
59
}),
60
secondary: !isPrimary,
61
title,
62
addPrimaryActionToDropdown: false,
63
...defaultButtonStyles
64
}) : new Button(this.container, { secondary: !isPrimary, title, ...defaultButtonStyles });
65
66
isPrimary = false;
67
this._buttonElements.push(button.element);
68
69
this._toDispose.add(button);
70
this._toDispose.add(button.onDidClick(() => this.actionHandler(action)));
71
72
button.enabled = action.enabled;
73
button.label = action.label;
74
if ((this.maxActions !== undefined) && (this._buttonElements.length >= this.maxActions)) {
75
console.warn(`An extension has contributed more than the allowable number of actions to a comments menu.`);
76
return;
77
}
78
}
79
}
80
}
81
82
triggerDefaultAction() {
83
if (this._actions.length) {
84
const lastAction = this._actions[0];
85
86
if (lastAction.enabled) {
87
return this.actionHandler(lastAction);
88
}
89
}
90
}
91
92
dispose() {
93
this._toDispose.dispose();
94
}
95
}
96
97