Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds basic keybinding for focusing on an element in outline pane #91799

Merged
merged 9 commits into from
Jun 12, 2020
74 changes: 43 additions & 31 deletions src/vs/workbench/browser/actions/listCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,40 @@ function listFocusLast(accessor: ServicesAccessor, options?: { fromFocused: bool
}
}


function focusElement(accessor: ServicesAccessor, retainCurrentFocus: boolean): void {
const focused = accessor.get(IListService).lastFocusedList;
const fakeKeyboardEvent = getSelectionKeyboardEvent('keydown', retainCurrentFocus);
// List
if (focused instanceof List || focused instanceof PagedList) {
const list = focused;
list.setSelection(list.getFocus(), fakeKeyboardEvent);
list.open(list.getFocus(), fakeKeyboardEvent);
}

// Trees
else if (focused instanceof ObjectTree || focused instanceof DataTree || focused instanceof AsyncDataTree) {
const list = focused;
const focus = list.getFocus();

if (focus.length > 0) {
let toggleCollapsed = true;

if (list.expandOnlyOnTwistieClick === true) {
toggleCollapsed = false;
} else if (typeof list.expandOnlyOnTwistieClick !== 'boolean' && list.expandOnlyOnTwistieClick(focus[0])) {
toggleCollapsed = false;
}

if (toggleCollapsed) {
list.toggleCollapsed(focus[0]);
}
}
list.setSelection(focus, fakeKeyboardEvent);
list.open(focus, fakeKeyboardEvent);
}
}

KeybindingsRegistry.registerCommandAndKeybindingRule({
id: 'list.select',
weight: KeybindingWeight.WorkbenchContrib,
Expand All @@ -504,38 +538,16 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
secondary: [KeyMod.CtrlCmd | KeyCode.DownArrow]
},
handler: (accessor) => {
const focused = accessor.get(IListService).lastFocusedList;
const fakeKeyboardEvent = getSelectionKeyboardEvent('keydown', false);

// List
if (focused instanceof List || focused instanceof PagedList) {
const list = focused;
list.setSelection(list.getFocus(), fakeKeyboardEvent);
list.open(list.getFocus(), fakeKeyboardEvent);
}

// Tree
else if (focused instanceof ObjectTree || focused instanceof DataTree || focused instanceof AsyncDataTree) {
const list = focused;
const focus = list.getFocus();

if (focus.length > 0) {
let toggleCollapsed = true;

if (list.expandOnlyOnTwistieClick === true) {
toggleCollapsed = false;
} else if (typeof list.expandOnlyOnTwistieClick !== 'boolean' && list.expandOnlyOnTwistieClick(focus[0])) {
toggleCollapsed = false;
}

if (toggleCollapsed) {
list.toggleCollapsed(focus[0]);
}
}
focusElement(accessor, false);
}
});

list.setSelection(focus, fakeKeyboardEvent);
list.open(focus, fakeKeyboardEvent);
}
KeybindingsRegistry.registerCommandAndKeybindingRule({
id: 'list.focus',
weight: KeybindingWeight.WorkbenchContrib,
when: WorkbenchListFocusContextKey,
handler: accessor => {
focusElement(accessor, true);
}
});

Expand Down
5 changes: 3 additions & 2 deletions src/vs/workbench/contrib/outline/browser/outlinePane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { IContextMenuService } from 'vs/platform/contextview/browser/contextView
import { TextEditorSelectionRevealType } from 'vs/platform/editor/common/editor';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { WorkbenchDataTree } from 'vs/platform/list/browser/listService';
import { WorkbenchDataTree, SelectionKeyboardEvent } from 'vs/platform/list/browser/listService';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { attachProgressBarStyler } from 'vs/platform/theme/common/styler';
import { IThemeService } from 'vs/platform/theme/common/themeService';
Expand Down Expand Up @@ -571,7 +571,8 @@ export class OutlinePane extends ViewPane {
// todo@Joh
if (e.browserEvent) {
if (e.browserEvent.type === 'keydown') {
focus = true;
const selectionEvent = e.browserEvent as SelectionKeyboardEvent;
focus = typeof selectionEvent.preserveFocus === 'boolean' ? selectionEvent.preserveFocus : true;
lambainsaan marked this conversation as resolved.
Show resolved Hide resolved
} else if (e.browserEvent.type === 'click') {
const event = new StandardMouseEvent(e.browserEvent as MouseEvent);
focus = e.browserEvent.detail === 2;
Expand Down