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 5 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
31 changes: 31 additions & 0 deletions src/vs/workbench/contrib/chat/browser/chatWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ export class ChatWidget extends Disposable implements IChatWidget {

readonly viewContext: IChatWidgetViewContext;

private _activeElement: HTMLElement | undefined;
private _lastEventWasFromKeyboard: boolean = false;
meganrogge marked this conversation as resolved.
Show resolved Hide resolved

constructor(
location: ChatAgentLocation | IChatWidgetLocationOptions,
_viewContext: IChatWidgetViewContext | undefined,
Expand Down Expand Up @@ -457,6 +460,34 @@ export class ChatWidget extends Disposable implements IChatWidget {

this._register(this.instantiationService.createInstance(ChatDragAndDrop, this.container, this.inputPart, this.styles));
this._register((this.chatWidgetService as ChatWidgetService).register(this));

this._setupFocusObserver();
}

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


meganrogge marked this conversation as resolved.
Show resolved Hide resolved
private _scrollToActiveElement(element: HTMLElement) {
const containerRect = this.tree.getHTMLElement().getBoundingClientRect();
const elementRect = element.getBoundingClientRect();

const topOffset = elementRect.top - containerRect.top;

if (topOffset < 0) {
// Scroll up
this.tree.scrollTop += topOffset;
} else if (element.offsetTop > this.listContainer.clientHeight) {
// Scroll down
this.tree.scrollTop += topOffset;
}
}

getContrib<T extends IChatWidgetContrib>(id: string): T | undefined {
Expand Down
Loading