Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/comments/browser/commentThreadAdditionalActions.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 * as dom from '../../../../base/browser/dom.js';
7
8
import { IAction } from '../../../../base/common/actions.js';
9
import { IMenu, SubmenuItemAction } from '../../../../platform/actions/common/actions.js';
10
import { Disposable } from '../../../../base/common/lifecycle.js';
11
import { MarshalledId } from '../../../../base/common/marshallingIds.js';
12
import { IRange } from '../../../../editor/common/core/range.js';
13
import * as languages from '../../../../editor/common/languages.js';
14
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';
15
import { CommentFormActions } from './commentFormActions.js';
16
import { CommentMenus } from './commentMenus.js';
17
import { ICellRange } from '../../notebook/common/notebookRange.js';
18
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';
19
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.js';
20
21
export class CommentThreadAdditionalActions<T extends IRange | ICellRange> extends Disposable {
22
private _container: HTMLElement | null;
23
private _buttonBar: HTMLElement | null;
24
private _commentFormActions!: CommentFormActions;
25
26
constructor(
27
container: HTMLElement,
28
private _commentThread: languages.CommentThread<T>,
29
private _contextKeyService: IContextKeyService,
30
private _commentMenus: CommentMenus,
31
private _actionRunDelegate: (() => void) | null,
32
@IKeybindingService private _keybindingService: IKeybindingService,
33
@IContextMenuService private _contextMenuService: IContextMenuService,
34
) {
35
super();
36
37
this._container = dom.append(container, dom.$('.comment-additional-actions'));
38
dom.append(this._container, dom.$('.section-separator'));
39
40
this._buttonBar = dom.append(this._container, dom.$('.button-bar'));
41
this._createAdditionalActions(this._buttonBar);
42
}
43
44
private _showMenu() {
45
this._container?.classList.remove('hidden');
46
}
47
48
private _hideMenu() {
49
this._container?.classList.add('hidden');
50
}
51
52
private _enableDisableMenu(menu: IMenu) {
53
const groups = menu.getActions({ shouldForwardArgs: true });
54
55
// Show the menu if at least one action is enabled.
56
for (const group of groups) {
57
const [, actions] = group;
58
for (const action of actions) {
59
if (action.enabled) {
60
this._showMenu();
61
return;
62
}
63
64
for (const subAction of (action as SubmenuItemAction).actions ?? []) {
65
if (subAction.enabled) {
66
this._showMenu();
67
return;
68
}
69
}
70
}
71
}
72
73
this._hideMenu();
74
}
75
76
77
private _createAdditionalActions(container: HTMLElement) {
78
const menu = this._commentMenus.getCommentThreadAdditionalActions(this._contextKeyService);
79
this._register(menu);
80
this._register(menu.onDidChange(() => {
81
this._commentFormActions.setActions(menu, /*hasOnlySecondaryActions*/ true);
82
this._enableDisableMenu(menu);
83
}));
84
85
this._commentFormActions = new CommentFormActions(this._keybindingService, this._contextKeyService, this._contextMenuService, container, async (action: IAction) => {
86
this._actionRunDelegate?.();
87
88
action.run({
89
thread: this._commentThread,
90
$mid: MarshalledId.CommentThreadInstance
91
});
92
}, 4, true);
93
94
this._register(this._commentFormActions);
95
this._commentFormActions.setActions(menu, /*hasOnlySecondaryActions*/ true);
96
this._enableDisableMenu(menu);
97
}
98
}
99
100