Path: blob/main/src/vs/workbench/contrib/comments/browser/commentsViewActions.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { KeyCode, KeyMod } from '../../../../base/common/keyCodes.js';6import { Disposable } from '../../../../base/common/lifecycle.js';7import { localize } from '../../../../nls.js';8import { ServicesAccessor } from '../../../../platform/instantiation/common/instantiation.js';9import { ContextKeyExpr, IContextKey, IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';10import { Event, Emitter } from '../../../../base/common/event.js';11import { CommentsViewFilterFocusContextKey, ICommentsView } from './comments.js';12import { MenuId, MenuRegistry, registerAction2 } from '../../../../platform/actions/common/actions.js';13import { ViewAction } from '../../../browser/parts/views/viewPane.js';14import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';15import { COMMENTS_VIEW_ID } from './commentsTreeViewer.js';16import { FocusedViewContext } from '../../../common/contextkeys.js';17import { viewFilterSubmenu } from '../../../browser/parts/views/viewFilter.js';18import { Codicon } from '../../../../base/common/codicons.js';1920export const enum CommentsSortOrder {21ResourceAscending = 'resourceAscending',22UpdatedAtDescending = 'updatedAtDescending',23}242526const CONTEXT_KEY_SHOW_RESOLVED = new RawContextKey<boolean>('commentsView.showResolvedFilter', true);27const CONTEXT_KEY_SHOW_UNRESOLVED = new RawContextKey<boolean>('commentsView.showUnResolvedFilter', true);28const CONTEXT_KEY_SORT_BY = new RawContextKey<CommentsSortOrder>('commentsView.sortBy', CommentsSortOrder.ResourceAscending);2930export interface CommentsFiltersChangeEvent {31showResolved?: boolean;32showUnresolved?: boolean;33sortBy?: CommentsSortOrder;34}3536interface CommentsFiltersOptions {37showResolved: boolean;38showUnresolved: boolean;39sortBy: CommentsSortOrder;40}4142export class CommentsFilters extends Disposable {4344private readonly _onDidChange: Emitter<CommentsFiltersChangeEvent> = this._register(new Emitter<CommentsFiltersChangeEvent>());45readonly onDidChange: Event<CommentsFiltersChangeEvent> = this._onDidChange.event;46private readonly _showUnresolved: IContextKey<boolean>;47private readonly _showResolved: IContextKey<boolean>;48private readonly _sortBy: IContextKey<CommentsSortOrder>;4950constructor(options: CommentsFiltersOptions, private readonly contextKeyService: IContextKeyService) {51super();52this._showUnresolved = CONTEXT_KEY_SHOW_UNRESOLVED.bindTo(this.contextKeyService);53this._showResolved = CONTEXT_KEY_SHOW_RESOLVED.bindTo(this.contextKeyService);54this._sortBy = CONTEXT_KEY_SORT_BY.bindTo(this.contextKeyService);55this._showResolved.set(options.showResolved);56this._showUnresolved.set(options.showUnresolved);57this._sortBy.set(options.sortBy);58}5960get showUnresolved(): boolean {61return !!this._showUnresolved.get();62}63set showUnresolved(showUnresolved: boolean) {64if (this._showUnresolved.get() !== showUnresolved) {65this._showUnresolved.set(showUnresolved);66this._onDidChange.fire({ showUnresolved: true });67}68}6970get showResolved(): boolean {71return !!this._showResolved.get();72}73set showResolved(showResolved: boolean) {74if (this._showResolved.get() !== showResolved) {75this._showResolved.set(showResolved);76this._onDidChange.fire({ showResolved: true });77}78}7980get sortBy(): CommentsSortOrder {81return this._sortBy.get() ?? CommentsSortOrder.ResourceAscending;82}83set sortBy(sortBy: CommentsSortOrder) {84if (this._sortBy.get() !== sortBy) {85this._sortBy.set(sortBy);86this._onDidChange.fire({ sortBy });87}88}89}9091registerAction2(class extends ViewAction<ICommentsView> {92constructor() {93super({94id: 'commentsFocusViewFromFilter',95title: localize('focusCommentsList', "Focus Comments view"),96keybinding: {97when: CommentsViewFilterFocusContextKey,98weight: KeybindingWeight.WorkbenchContrib,99primary: KeyMod.CtrlCmd | KeyCode.DownArrow100},101viewId: COMMENTS_VIEW_ID102});103}104async runInView(serviceAccessor: ServicesAccessor, commentsView: ICommentsView): Promise<void> {105commentsView.focus();106}107});108109registerAction2(class extends ViewAction<ICommentsView> {110constructor() {111super({112id: 'commentsClearFilterText',113title: localize('commentsClearFilterText', "Clear filter text"),114keybinding: {115when: CommentsViewFilterFocusContextKey,116weight: KeybindingWeight.WorkbenchContrib,117primary: KeyCode.Escape118},119viewId: COMMENTS_VIEW_ID120});121}122async runInView(serviceAccessor: ServicesAccessor, commentsView: ICommentsView): Promise<void> {123commentsView.clearFilterText();124}125});126127registerAction2(class extends ViewAction<ICommentsView> {128constructor() {129super({130id: 'commentsFocusFilter',131title: localize('focusCommentsFilter', "Focus comments filter"),132keybinding: {133when: FocusedViewContext.isEqualTo(COMMENTS_VIEW_ID),134weight: KeybindingWeight.WorkbenchContrib,135primary: KeyMod.CtrlCmd | KeyCode.KeyF136},137viewId: COMMENTS_VIEW_ID138});139}140async runInView(serviceAccessor: ServicesAccessor, commentsView: ICommentsView): Promise<void> {141commentsView.focusFilter();142}143});144145registerAction2(class extends ViewAction<ICommentsView> {146constructor() {147super({148id: `workbench.actions.${COMMENTS_VIEW_ID}.toggleUnResolvedComments`,149title: localize('toggle unresolved', "Show Unresolved"),150category: localize('comments', "Comments"),151toggled: {152condition: CONTEXT_KEY_SHOW_UNRESOLVED,153title: localize('unresolved', "Show Unresolved"),154},155menu: {156id: viewFilterSubmenu,157group: '1_filter',158when: ContextKeyExpr.equals('view', COMMENTS_VIEW_ID),159order: 1160},161viewId: COMMENTS_VIEW_ID162});163}164165async runInView(serviceAccessor: ServicesAccessor, view: ICommentsView): Promise<void> {166view.filters.showUnresolved = !view.filters.showUnresolved;167}168});169170registerAction2(class extends ViewAction<ICommentsView> {171constructor() {172super({173id: `workbench.actions.${COMMENTS_VIEW_ID}.toggleResolvedComments`,174title: localize('toggle resolved', "Show Resolved"),175category: localize('comments', "Comments"),176toggled: {177condition: CONTEXT_KEY_SHOW_RESOLVED,178title: localize('resolved', "Show Resolved"),179},180menu: {181id: viewFilterSubmenu,182group: '1_filter',183when: ContextKeyExpr.equals('view', COMMENTS_VIEW_ID),184order: 1185},186viewId: COMMENTS_VIEW_ID187});188}189190async runInView(serviceAccessor: ServicesAccessor, view: ICommentsView): Promise<void> {191view.filters.showResolved = !view.filters.showResolved;192}193});194195const commentSortSubmenu = new MenuId('submenu.filter.commentSort');196MenuRegistry.appendMenuItem(viewFilterSubmenu, {197submenu: commentSortSubmenu,198title: localize('comment sorts', "Sort By"),199group: '2_sort',200icon: Codicon.history,201when: ContextKeyExpr.equals('view', COMMENTS_VIEW_ID),202});203204registerAction2(class extends ViewAction<ICommentsView> {205constructor() {206super({207id: `workbench.actions.${COMMENTS_VIEW_ID}.toggleSortByUpdatedAt`,208title: localize('toggle sorting by updated at', "Updated Time"),209category: localize('comments', "Comments"),210icon: Codicon.history,211viewId: COMMENTS_VIEW_ID,212toggled: {213condition: ContextKeyExpr.equals(CONTEXT_KEY_SORT_BY.key, CommentsSortOrder.UpdatedAtDescending),214title: localize('sorting by updated at', "Updated Time"),215},216menu: {217id: commentSortSubmenu,218group: 'navigation',219order: 1,220isHiddenByDefault: false,221},222});223}224225async runInView(serviceAccessor: ServicesAccessor, view: ICommentsView): Promise<void> {226view.filters.sortBy = CommentsSortOrder.UpdatedAtDescending;227}228});229230registerAction2(class extends ViewAction<ICommentsView> {231constructor() {232super({233id: `workbench.actions.${COMMENTS_VIEW_ID}.toggleSortByResource`,234title: localize('toggle sorting by resource', "Position in File"),235category: localize('comments', "Comments"),236icon: Codicon.history,237viewId: COMMENTS_VIEW_ID,238toggled: {239condition: ContextKeyExpr.equals(CONTEXT_KEY_SORT_BY.key, CommentsSortOrder.ResourceAscending),240title: localize('sorting by position in file', "Position in File"),241},242menu: {243id: commentSortSubmenu,244group: 'navigation',245order: 0,246isHiddenByDefault: false,247},248});249}250251async runInView(serviceAccessor: ServicesAccessor, view: ICommentsView): Promise<void> {252view.filters.sortBy = CommentsSortOrder.ResourceAscending;253}254});255256257