Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/comments/browser/commentsViewActions.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 { KeyCode, KeyMod } from '../../../../base/common/keyCodes.js';
7
import { Disposable } from '../../../../base/common/lifecycle.js';
8
import { localize } from '../../../../nls.js';
9
import { ServicesAccessor } from '../../../../platform/instantiation/common/instantiation.js';
10
import { ContextKeyExpr, IContextKey, IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
11
import { Event, Emitter } from '../../../../base/common/event.js';
12
import { CommentsViewFilterFocusContextKey, ICommentsView } from './comments.js';
13
import { MenuId, MenuRegistry, registerAction2 } from '../../../../platform/actions/common/actions.js';
14
import { ViewAction } from '../../../browser/parts/views/viewPane.js';
15
import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
16
import { COMMENTS_VIEW_ID } from './commentsTreeViewer.js';
17
import { FocusedViewContext } from '../../../common/contextkeys.js';
18
import { viewFilterSubmenu } from '../../../browser/parts/views/viewFilter.js';
19
import { Codicon } from '../../../../base/common/codicons.js';
20
21
export const enum CommentsSortOrder {
22
ResourceAscending = 'resourceAscending',
23
UpdatedAtDescending = 'updatedAtDescending',
24
}
25
26
27
const CONTEXT_KEY_SHOW_RESOLVED = new RawContextKey<boolean>('commentsView.showResolvedFilter', true);
28
const CONTEXT_KEY_SHOW_UNRESOLVED = new RawContextKey<boolean>('commentsView.showUnResolvedFilter', true);
29
const CONTEXT_KEY_SORT_BY = new RawContextKey<CommentsSortOrder>('commentsView.sortBy', CommentsSortOrder.ResourceAscending);
30
31
export interface CommentsFiltersChangeEvent {
32
showResolved?: boolean;
33
showUnresolved?: boolean;
34
sortBy?: CommentsSortOrder;
35
}
36
37
interface CommentsFiltersOptions {
38
showResolved: boolean;
39
showUnresolved: boolean;
40
sortBy: CommentsSortOrder;
41
}
42
43
export class CommentsFilters extends Disposable {
44
45
private readonly _onDidChange: Emitter<CommentsFiltersChangeEvent> = this._register(new Emitter<CommentsFiltersChangeEvent>());
46
readonly onDidChange: Event<CommentsFiltersChangeEvent> = this._onDidChange.event;
47
private readonly _showUnresolved: IContextKey<boolean>;
48
private readonly _showResolved: IContextKey<boolean>;
49
private readonly _sortBy: IContextKey<CommentsSortOrder>;
50
51
constructor(options: CommentsFiltersOptions, private readonly contextKeyService: IContextKeyService) {
52
super();
53
this._showUnresolved = CONTEXT_KEY_SHOW_UNRESOLVED.bindTo(this.contextKeyService);
54
this._showResolved = CONTEXT_KEY_SHOW_RESOLVED.bindTo(this.contextKeyService);
55
this._sortBy = CONTEXT_KEY_SORT_BY.bindTo(this.contextKeyService);
56
this._showResolved.set(options.showResolved);
57
this._showUnresolved.set(options.showUnresolved);
58
this._sortBy.set(options.sortBy);
59
}
60
61
get showUnresolved(): boolean {
62
return !!this._showUnresolved.get();
63
}
64
set showUnresolved(showUnresolved: boolean) {
65
if (this._showUnresolved.get() !== showUnresolved) {
66
this._showUnresolved.set(showUnresolved);
67
this._onDidChange.fire({ showUnresolved: true });
68
}
69
}
70
71
get showResolved(): boolean {
72
return !!this._showResolved.get();
73
}
74
set showResolved(showResolved: boolean) {
75
if (this._showResolved.get() !== showResolved) {
76
this._showResolved.set(showResolved);
77
this._onDidChange.fire({ showResolved: true });
78
}
79
}
80
81
get sortBy(): CommentsSortOrder {
82
return this._sortBy.get() ?? CommentsSortOrder.ResourceAscending;
83
}
84
set sortBy(sortBy: CommentsSortOrder) {
85
if (this._sortBy.get() !== sortBy) {
86
this._sortBy.set(sortBy);
87
this._onDidChange.fire({ sortBy });
88
}
89
}
90
}
91
92
registerAction2(class extends ViewAction<ICommentsView> {
93
constructor() {
94
super({
95
id: 'commentsFocusViewFromFilter',
96
title: localize('focusCommentsList', "Focus Comments view"),
97
keybinding: {
98
when: CommentsViewFilterFocusContextKey,
99
weight: KeybindingWeight.WorkbenchContrib,
100
primary: KeyMod.CtrlCmd | KeyCode.DownArrow
101
},
102
viewId: COMMENTS_VIEW_ID
103
});
104
}
105
async runInView(serviceAccessor: ServicesAccessor, commentsView: ICommentsView): Promise<void> {
106
commentsView.focus();
107
}
108
});
109
110
registerAction2(class extends ViewAction<ICommentsView> {
111
constructor() {
112
super({
113
id: 'commentsClearFilterText',
114
title: localize('commentsClearFilterText', "Clear filter text"),
115
keybinding: {
116
when: CommentsViewFilterFocusContextKey,
117
weight: KeybindingWeight.WorkbenchContrib,
118
primary: KeyCode.Escape
119
},
120
viewId: COMMENTS_VIEW_ID
121
});
122
}
123
async runInView(serviceAccessor: ServicesAccessor, commentsView: ICommentsView): Promise<void> {
124
commentsView.clearFilterText();
125
}
126
});
127
128
registerAction2(class extends ViewAction<ICommentsView> {
129
constructor() {
130
super({
131
id: 'commentsFocusFilter',
132
title: localize('focusCommentsFilter', "Focus comments filter"),
133
keybinding: {
134
when: FocusedViewContext.isEqualTo(COMMENTS_VIEW_ID),
135
weight: KeybindingWeight.WorkbenchContrib,
136
primary: KeyMod.CtrlCmd | KeyCode.KeyF
137
},
138
viewId: COMMENTS_VIEW_ID
139
});
140
}
141
async runInView(serviceAccessor: ServicesAccessor, commentsView: ICommentsView): Promise<void> {
142
commentsView.focusFilter();
143
}
144
});
145
146
registerAction2(class extends ViewAction<ICommentsView> {
147
constructor() {
148
super({
149
id: `workbench.actions.${COMMENTS_VIEW_ID}.toggleUnResolvedComments`,
150
title: localize('toggle unresolved', "Show Unresolved"),
151
category: localize('comments', "Comments"),
152
toggled: {
153
condition: CONTEXT_KEY_SHOW_UNRESOLVED,
154
title: localize('unresolved', "Show Unresolved"),
155
},
156
menu: {
157
id: viewFilterSubmenu,
158
group: '1_filter',
159
when: ContextKeyExpr.equals('view', COMMENTS_VIEW_ID),
160
order: 1
161
},
162
viewId: COMMENTS_VIEW_ID
163
});
164
}
165
166
async runInView(serviceAccessor: ServicesAccessor, view: ICommentsView): Promise<void> {
167
view.filters.showUnresolved = !view.filters.showUnresolved;
168
}
169
});
170
171
registerAction2(class extends ViewAction<ICommentsView> {
172
constructor() {
173
super({
174
id: `workbench.actions.${COMMENTS_VIEW_ID}.toggleResolvedComments`,
175
title: localize('toggle resolved', "Show Resolved"),
176
category: localize('comments', "Comments"),
177
toggled: {
178
condition: CONTEXT_KEY_SHOW_RESOLVED,
179
title: localize('resolved', "Show Resolved"),
180
},
181
menu: {
182
id: viewFilterSubmenu,
183
group: '1_filter',
184
when: ContextKeyExpr.equals('view', COMMENTS_VIEW_ID),
185
order: 1
186
},
187
viewId: COMMENTS_VIEW_ID
188
});
189
}
190
191
async runInView(serviceAccessor: ServicesAccessor, view: ICommentsView): Promise<void> {
192
view.filters.showResolved = !view.filters.showResolved;
193
}
194
});
195
196
const commentSortSubmenu = new MenuId('submenu.filter.commentSort');
197
MenuRegistry.appendMenuItem(viewFilterSubmenu, {
198
submenu: commentSortSubmenu,
199
title: localize('comment sorts', "Sort By"),
200
group: '2_sort',
201
icon: Codicon.history,
202
when: ContextKeyExpr.equals('view', COMMENTS_VIEW_ID),
203
});
204
205
registerAction2(class extends ViewAction<ICommentsView> {
206
constructor() {
207
super({
208
id: `workbench.actions.${COMMENTS_VIEW_ID}.toggleSortByUpdatedAt`,
209
title: localize('toggle sorting by updated at', "Updated Time"),
210
category: localize('comments', "Comments"),
211
icon: Codicon.history,
212
viewId: COMMENTS_VIEW_ID,
213
toggled: {
214
condition: ContextKeyExpr.equals(CONTEXT_KEY_SORT_BY.key, CommentsSortOrder.UpdatedAtDescending),
215
title: localize('sorting by updated at', "Updated Time"),
216
},
217
menu: {
218
id: commentSortSubmenu,
219
group: 'navigation',
220
order: 1,
221
isHiddenByDefault: false,
222
},
223
});
224
}
225
226
async runInView(serviceAccessor: ServicesAccessor, view: ICommentsView): Promise<void> {
227
view.filters.sortBy = CommentsSortOrder.UpdatedAtDescending;
228
}
229
});
230
231
registerAction2(class extends ViewAction<ICommentsView> {
232
constructor() {
233
super({
234
id: `workbench.actions.${COMMENTS_VIEW_ID}.toggleSortByResource`,
235
title: localize('toggle sorting by resource', "Position in File"),
236
category: localize('comments', "Comments"),
237
icon: Codicon.history,
238
viewId: COMMENTS_VIEW_ID,
239
toggled: {
240
condition: ContextKeyExpr.equals(CONTEXT_KEY_SORT_BY.key, CommentsSortOrder.ResourceAscending),
241
title: localize('sorting by position in file', "Position in File"),
242
},
243
menu: {
244
id: commentSortSubmenu,
245
group: 'navigation',
246
order: 0,
247
isHiddenByDefault: false,
248
},
249
});
250
}
251
252
async runInView(serviceAccessor: ServicesAccessor, view: ICommentsView): Promise<void> {
253
view.filters.sortBy = CommentsSortOrder.ResourceAscending;
254
}
255
});
256
257