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

Offset terminal when inline chat appears to ensure cursor is visible #210239

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class TerminalChatController extends Disposable implements ITerminalContr

xtermReady(xterm: IXtermTerminal & { raw: RawXtermTerminal }): void {
this._chatWidget = new Lazy(() => {
const chatWidget = this._register(this._instantiationService.createInstance(TerminalChatWidget, this._instance.domElement!, this._instance));
const chatWidget = this._register(this._instantiationService.createInstance(TerminalChatWidget, this._instance.domElement!, this._instance, xterm));
this._register(chatWidget.focusTracker.onDidFocus(() => {
TerminalChatController.activeChatWidget = this;
if (!isDetachedTerminalInstance(this._instance)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import type { Terminal as RawXtermTerminal } from '@xterm/xterm';
import { Dimension, IFocusTracker, trackFocus } from 'vs/base/browser/dom';
import { Event } from 'vs/base/common/event';
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
import { MicrotaskDelay } from 'vs/base/common/symbols';
import 'vs/css!./media/terminalChatWidget';
import { localize } from 'vs/nls';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { ChatAgentLocation } from 'vs/workbench/contrib/chat/common/chatAgents';
import { IChatProgress } from 'vs/workbench/contrib/chat/common/chatService';
import { InlineChatWidget } from 'vs/workbench/contrib/inlineChat/browser/inlineChatWidget';
import { ITerminalInstance } from 'vs/workbench/contrib/terminal/browser/terminal';
import { ITerminalInstance, type IXtermTerminal } from 'vs/workbench/contrib/terminal/browser/terminal';
import { MENU_TERMINAL_CHAT_INPUT, MENU_TERMINAL_CHAT_WIDGET, MENU_TERMINAL_CHAT_WIDGET_FEEDBACK, MENU_TERMINAL_CHAT_WIDGET_STATUS, TerminalChatCommandId, TerminalChatContextKeys } from 'vs/workbench/contrib/terminalContrib/chat/browser/terminalChat';
import { TerminalStickyScrollContribution } from 'vs/workbench/contrib/terminalContrib/stickyScroll/browser/terminalStickyScrollContribution';

const enum Constants {
HorizontalMargin = 10
Expand All @@ -35,6 +38,7 @@ export class TerminalChatWidget extends Disposable {
constructor(
private readonly _terminalElement: HTMLElement,
private readonly _instance: ITerminalInstance,
private readonly _xterm: IXtermTerminal & { raw: RawXtermTerminal },
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@IContextKeyService private readonly _contextKeyService: IContextKeyService
) {
Expand Down Expand Up @@ -73,6 +77,7 @@ export class TerminalChatWidget extends Disposable {
this._register(Event.any(
this._inlineChatWidget.onDidChangeHeight,
this._instance.onDimensionsChanged,
Event.debounce(this._xterm.raw.onCursorMove, () => void 0, MicrotaskDelay),
)(() => this._relayout()));

const observer = new ResizeObserver(() => this._relayout());
Expand Down Expand Up @@ -102,6 +107,7 @@ export class TerminalChatWidget extends Disposable {
}
this._dimension = new Dimension(width, height);
this._inlineChatWidget.layout(this._dimension);

this._updateVerticalPosition();
}

Expand Down Expand Up @@ -134,7 +140,9 @@ export class TerminalChatWidget extends Disposable {
return;
}
if (top > terminalWrapperHeight - widgetHeight) {
this._container.style.top = '';
this._setTerminalOffset(top - (terminalWrapperHeight - widgetHeight));
} else {
this._setTerminalOffset(undefined);
}
}

Expand All @@ -154,6 +162,18 @@ export class TerminalChatWidget extends Disposable {
this._visibleContextKey.set(false);
this._inlineChatWidget.value = '';
this._instance.focus();
this._setTerminalOffset(undefined);
}
private _setTerminalOffset(offset: number | undefined) {
if (offset === undefined || this._container.classList.contains('hide')) {
this._terminalElement.style.position = '';
this._terminalElement.style.bottom = '';
TerminalStickyScrollContribution.get(this._instance)?.hideUnlock();
} else {
this._terminalElement.style.position = 'relative';
this._terminalElement.style.bottom = `${offset}px`;
TerminalStickyScrollContribution.get(this._instance)?.hideLock();
}
}
focus(): void {
this._inlineChatWidget.focus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ export class TerminalStickyScrollContribution extends Disposable implements ITer
this._refreshState();
}

hideLock() {
this._overlay.value?.lockHide();
}

hideUnlock() {
this._overlay.value?.unlockHide();
}
Comment on lines +63 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this terminology is a bit confusing ... could it just be lock and unlock? vs hideLock and hideUnlock

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lock and unlock by themselves seem like they'd be related to positioning. Also it has another hide mechanism that this overrides


private _refreshState(): void {
if (this._overlay.value) {
this._tryDisable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ export class TerminalStickyScrollOverlay extends Disposable {
});
}

lockHide() {
this._element?.classList.add('lock-hide');
}

unlockHide() {
this._element?.classList.remove('lock-hide');
}

private _setState(state: OverlayState) {
if (this._state === state) {
return;
Expand Down
Loading