Skip to content

Commit

Permalink
list - list.clear only when there is a selection or focus (for #53950)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Jul 10, 2018
1 parent 6d7f814 commit 8f16f8c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/vs/platform/list/browser/listService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class ListService implements IListService {
const RawWorkbenchListFocusContextKey = new RawContextKey<boolean>('listFocus', true);
export const WorkbenchListSupportsMultiSelectContextKey = new RawContextKey<boolean>('listSupportsMultiselect', true);
export const WorkbenchListFocusContextKey = ContextKeyExpr.and(RawWorkbenchListFocusContextKey, ContextKeyExpr.not(InputFocusedContextKey));
export const WorkbenchListHasSelectionOrFocus = new RawContextKey<boolean>('listHasSelectionOrFocus', false);
export const WorkbenchListDoubleSelection = new RawContextKey<boolean>('listDoubleSelection', false);
export const WorkbenchListMultiSelection = new RawContextKey<boolean>('listMultiSelection', false);

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

readonly contextKeyService: IContextKeyService;

private listHasSelectionOrFocus: IContextKey<boolean>;
private listDoubleSelection: IContextKey<boolean>;
private listMultiSelection: IContextKey<boolean>;

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

this.contextKeyService = createScopedContextKeyService(contextKeyService, this);
this.listHasSelectionOrFocus = WorkbenchListHasSelectionOrFocus.bindTo(this.contextKeyService);
this.listDoubleSelection = WorkbenchListDoubleSelection.bindTo(this.contextKeyService);
this.listMultiSelection = WorkbenchListMultiSelection.bindTo(this.contextKeyService);

Expand All @@ -236,8 +239,17 @@ export class WorkbenchList<T> extends List<T> {
attachListStyler(this, themeService),
this.onSelectionChange(() => {
const selection = this.getSelection();
const focus = this.getFocus();

this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
this.listMultiSelection.set(selection.length > 1);
this.listDoubleSelection.set(selection.length === 2);
}),
this.onFocusChange(() => {
const selection = this.getSelection();
const focus = this.getFocus();

this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
})
]));

Expand Down Expand Up @@ -323,6 +335,7 @@ export class WorkbenchTree extends Tree {

protected disposables: IDisposable[];

private listHasSelectionOrFocus: IContextKey<boolean>;
private listDoubleSelection: IContextKey<boolean>;
private listMultiSelection: IContextKey<boolean>;

Expand Down Expand Up @@ -352,6 +365,7 @@ export class WorkbenchTree extends Tree {

this.disposables = [];
this.contextKeyService = createScopedContextKeyService(contextKeyService, this);
this.listHasSelectionOrFocus = WorkbenchListHasSelectionOrFocus.bindTo(this.contextKeyService);
this.listDoubleSelection = WorkbenchListDoubleSelection.bindTo(this.contextKeyService);
this.listMultiSelection = WorkbenchListMultiSelection.bindTo(this.contextKeyService);

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

this.disposables.push(this.onDidChangeSelection(() => {
const selection = this.getSelection();
const focus = this.getFocus();

this.listHasSelectionOrFocus.set((selection && selection.length > 0) || !!focus);
this.listDoubleSelection.set(selection && selection.length === 2);
this.listMultiSelection.set(selection && selection.length > 1);
}));

this.disposables.push(this.onDidChangeFocus(() => {
const selection = this.getSelection();
const focus = this.getFocus();

this.listHasSelectionOrFocus.set((selection && selection.length > 0) || !!focus);
}));

this.disposables.push(configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(openModeSettingKey)) {
this._openOnSingleClick = useSingleClickToOpen(configurationService);
Expand Down
25 changes: 21 additions & 4 deletions src/vs/workbench/electron-browser/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { IWindowsService, IWindowService } from 'vs/platform/windows/common/wind
import { List } from 'vs/base/browser/ui/list/listWidget';
import * as errors from 'vs/base/common/errors';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { WorkbenchListFocusContextKey, IListService, WorkbenchListSupportsMultiSelectContextKey, ListWidget } from 'vs/platform/list/browser/listService';
import { WorkbenchListFocusContextKey, IListService, WorkbenchListSupportsMultiSelectContextKey, ListWidget, WorkbenchListHasSelectionOrFocus } from 'vs/platform/list/browser/listService';
import { PagedList } from 'vs/base/browser/ui/list/listPaging';
import { range } from 'vs/base/common/arrays';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
Expand Down Expand Up @@ -471,13 +471,30 @@ export function registerCommands(): void {
KeybindingsRegistry.registerCommandAndKeybindingRule({
id: 'list.clear',
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
when: WorkbenchListFocusContextKey,
when: ContextKeyExpr.and(WorkbenchListFocusContextKey, WorkbenchListHasSelectionOrFocus),
primary: KeyCode.Escape,
handler: (accessor) => {
const focused = accessor.get(IListService).lastFocusedList;

// Tree only
if (focused && !(focused instanceof List || focused instanceof PagedList)) {
// List
if (focused instanceof List || focused instanceof PagedList) {
const list = focused;

if (list.getSelection().length > 0) {
list.setSelection([]);

return void 0;
}

if (list.getFocus().length > 0) {
list.setFocus([]);

return void 0;
}
}

// Tree
else if (focused) {
const tree = focused;

if (tree.getSelection().length) {
Expand Down

0 comments on commit 8f16f8c

Please sign in to comment.