Skip to content

Commit 8f16f8c

Browse files
author
Benjamin Pasero
committed
list - list.clear only when there is a selection or focus (for microsoft#53950)
1 parent 6d7f814 commit 8f16f8c

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

src/vs/platform/list/browser/listService.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export class ListService implements IListService {
9090
const RawWorkbenchListFocusContextKey = new RawContextKey<boolean>('listFocus', true);
9191
export const WorkbenchListSupportsMultiSelectContextKey = new RawContextKey<boolean>('listSupportsMultiselect', true);
9292
export const WorkbenchListFocusContextKey = ContextKeyExpr.and(RawWorkbenchListFocusContextKey, ContextKeyExpr.not(InputFocusedContextKey));
93+
export const WorkbenchListHasSelectionOrFocus = new RawContextKey<boolean>('listHasSelectionOrFocus', false);
9394
export const WorkbenchListDoubleSelection = new RawContextKey<boolean>('listDoubleSelection', false);
9495
export const WorkbenchListMultiSelection = new RawContextKey<boolean>('listMultiSelection', false);
9596

@@ -199,6 +200,7 @@ export class WorkbenchList<T> extends List<T> {
199200

200201
readonly contextKeyService: IContextKeyService;
201202

203+
private listHasSelectionOrFocus: IContextKey<boolean>;
202204
private listDoubleSelection: IContextKey<boolean>;
203205
private listMultiSelection: IContextKey<boolean>;
204206

@@ -225,6 +227,7 @@ export class WorkbenchList<T> extends List<T> {
225227
);
226228

227229
this.contextKeyService = createScopedContextKeyService(contextKeyService, this);
230+
this.listHasSelectionOrFocus = WorkbenchListHasSelectionOrFocus.bindTo(this.contextKeyService);
228231
this.listDoubleSelection = WorkbenchListDoubleSelection.bindTo(this.contextKeyService);
229232
this.listMultiSelection = WorkbenchListMultiSelection.bindTo(this.contextKeyService);
230233

@@ -236,8 +239,17 @@ export class WorkbenchList<T> extends List<T> {
236239
attachListStyler(this, themeService),
237240
this.onSelectionChange(() => {
238241
const selection = this.getSelection();
242+
const focus = this.getFocus();
243+
244+
this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
239245
this.listMultiSelection.set(selection.length > 1);
240246
this.listDoubleSelection.set(selection.length === 2);
247+
}),
248+
this.onFocusChange(() => {
249+
const selection = this.getSelection();
250+
const focus = this.getFocus();
251+
252+
this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
241253
})
242254
]));
243255

@@ -323,6 +335,7 @@ export class WorkbenchTree extends Tree {
323335

324336
protected disposables: IDisposable[];
325337

338+
private listHasSelectionOrFocus: IContextKey<boolean>;
326339
private listDoubleSelection: IContextKey<boolean>;
327340
private listMultiSelection: IContextKey<boolean>;
328341

@@ -352,6 +365,7 @@ export class WorkbenchTree extends Tree {
352365

353366
this.disposables = [];
354367
this.contextKeyService = createScopedContextKeyService(contextKeyService, this);
368+
this.listHasSelectionOrFocus = WorkbenchListHasSelectionOrFocus.bindTo(this.contextKeyService);
355369
this.listDoubleSelection = WorkbenchListDoubleSelection.bindTo(this.contextKeyService);
356370
this.listMultiSelection = WorkbenchListMultiSelection.bindTo(this.contextKeyService);
357371

@@ -366,10 +380,20 @@ export class WorkbenchTree extends Tree {
366380

367381
this.disposables.push(this.onDidChangeSelection(() => {
368382
const selection = this.getSelection();
383+
const focus = this.getFocus();
384+
385+
this.listHasSelectionOrFocus.set((selection && selection.length > 0) || !!focus);
369386
this.listDoubleSelection.set(selection && selection.length === 2);
370387
this.listMultiSelection.set(selection && selection.length > 1);
371388
}));
372389

390+
this.disposables.push(this.onDidChangeFocus(() => {
391+
const selection = this.getSelection();
392+
const focus = this.getFocus();
393+
394+
this.listHasSelectionOrFocus.set((selection && selection.length > 0) || !!focus);
395+
}));
396+
373397
this.disposables.push(configurationService.onDidChangeConfiguration(e => {
374398
if (e.affectsConfiguration(openModeSettingKey)) {
375399
this._openOnSingleClick = useSingleClickToOpen(configurationService);

src/vs/workbench/electron-browser/commands.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { IWindowsService, IWindowService } from 'vs/platform/windows/common/wind
1313
import { List } from 'vs/base/browser/ui/list/listWidget';
1414
import * as errors from 'vs/base/common/errors';
1515
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
16-
import { WorkbenchListFocusContextKey, IListService, WorkbenchListSupportsMultiSelectContextKey, ListWidget } from 'vs/platform/list/browser/listService';
16+
import { WorkbenchListFocusContextKey, IListService, WorkbenchListSupportsMultiSelectContextKey, ListWidget, WorkbenchListHasSelectionOrFocus } from 'vs/platform/list/browser/listService';
1717
import { PagedList } from 'vs/base/browser/ui/list/listPaging';
1818
import { range } from 'vs/base/common/arrays';
1919
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
@@ -471,13 +471,30 @@ export function registerCommands(): void {
471471
KeybindingsRegistry.registerCommandAndKeybindingRule({
472472
id: 'list.clear',
473473
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
474-
when: WorkbenchListFocusContextKey,
474+
when: ContextKeyExpr.and(WorkbenchListFocusContextKey, WorkbenchListHasSelectionOrFocus),
475475
primary: KeyCode.Escape,
476476
handler: (accessor) => {
477477
const focused = accessor.get(IListService).lastFocusedList;
478478

479-
// Tree only
480-
if (focused && !(focused instanceof List || focused instanceof PagedList)) {
479+
// List
480+
if (focused instanceof List || focused instanceof PagedList) {
481+
const list = focused;
482+
483+
if (list.getSelection().length > 0) {
484+
list.setSelection([]);
485+
486+
return void 0;
487+
}
488+
489+
if (list.getFocus().length > 0) {
490+
list.setFocus([]);
491+
492+
return void 0;
493+
}
494+
}
495+
496+
// Tree
497+
else if (focused) {
481498
const tree = focused;
482499

483500
if (tree.getSelection().length) {

0 commit comments

Comments
 (0)