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

scroll list view on keyboard focus #232644

Merged
merged 24 commits into from
Jan 10, 2025
Merged
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/vs/base/browser/ui/list/listView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { DataTransfers, IDragAndDropData } from '../../dnd.js';
import { $, addDisposableListener, animate, Dimension, getContentHeight, getContentWidth, getDocument, getTopLeftOffset, getWindow, isAncestor, isHTMLElement, isSVGElement, scheduleAtNextAnimationFrame } from '../../dom.js';
import { $, addDisposableListener, animate, Dimension, getActiveElement, getContentHeight, getContentWidth, getDocument, getTopLeftOffset, getWindow, isAncestor, isHTMLElement, isSVGElement, scheduleAtNextAnimationFrame } from '../../dom.js';
import { DomEmitter } from '../../event.js';
import { IMouseWheelEvent } from '../../mouseEvent.js';
import { EventType as TouchEventType, Gesture, GestureEvent } from '../../touch.js';
Expand Down Expand Up @@ -324,6 +324,7 @@ export class ListView<T> implements IListView<T> {
private onDragLeaveTimeout: IDisposable = Disposable.None;
private currentSelectionDisposable: IDisposable = Disposable.None;
private currentSelectionBounds: IRange | undefined;
private activeElement: HTMLElement | undefined;

private readonly disposables: DisposableStore = new DisposableStore();

Expand Down Expand Up @@ -441,7 +442,12 @@ export class ListView<T> implements IListView<T> {

// Prevent the monaco-scrollable-element from scrolling
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
// https://github.com/microsoft/vscode/issues/44181
this.disposables.add(addDisposableListener(this.scrollableElement.getDomNode(), 'scroll', e => (e.target as HTMLElement).scrollTop = 0));
this.disposables.add(addDisposableListener(this.scrollableElement.getDomNode(), 'scroll', e => {
const element = (e.target as HTMLElement);
const scrollValue = element.scrollTop;
element.scrollTop = 0;
this.setScrollTop(this.scrollTop + scrollValue);
}));

this.disposables.add(addDisposableListener(this.domNode, 'dragover', e => this.onDragOver(this.toDragEvent(e))));
this.disposables.add(addDisposableListener(this.domNode, 'drop', e => this.onDrop(this.toDragEvent(e))));
Expand All @@ -460,6 +466,29 @@ export class ListView<T> implements IListView<T> {
this.dnd = options.dnd ?? this.disposables.add(DefaultOptions.dnd);

this.layout(options.initialSize?.height, options.initialSize?.width);
this._setupFocusObserver(container);
}

private _setupFocusObserver(container: HTMLElement): void {
container.addEventListener('focus', () => {
const element = getActiveElement() as HTMLElement | null;
if (this.activeElement !== element && element !== null) {
this.activeElement = element;
this._scrollToActiveElement(this.activeElement, container);
}
}, true);
}

private _scrollToActiveElement(element: HTMLElement, container: HTMLElement) {
const containerRect = container.getBoundingClientRect();
const elementRect = element.getBoundingClientRect();

const topOffset = elementRect.top - containerRect.top;

if (topOffset < 0) {
// Scroll up
this.setScrollTop(this.scrollTop + topOffset);
}
}

updateOptions(options: IListViewOptionsUpdate) {
Expand Down
Loading