Skip to content

Commit

Permalink
Merge pull request xtermjs#4638 from Tyriar/4603
Browse files Browse the repository at this point in the history
Fix reset and clear leaving viewport in a bad state
  • Loading branch information
Tyriar committed Aug 2, 2023
2 parents bb237f1 + d203195 commit 4f41070
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/browser/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,8 +1230,11 @@ export class Terminal extends CoreTerminal implements ITerminal {
for (let i = 1; i < this.rows; i++) {
this.buffer.lines.push(this.buffer.getBlankLine(DEFAULT_ATTR_DATA));
}
this.refresh(0, this.rows - 1);
// IMPORTANT: Fire scroll event before viewport is reset. This ensures embedders get the clear
// scroll event and that the viewport's state will be valid for immediate writes.
this._onScroll.fire({ position: this.buffer.ydisp, source: ScrollSource.TERMINAL });
this.viewport?.reset();
this.refresh(0, this.rows - 1);
}

/**
Expand All @@ -1255,13 +1258,13 @@ export class Terminal extends CoreTerminal implements ITerminal {
super.reset();
this._selectionService?.reset();
this._decorationService.reset();
this.viewport?.reset();

// reattach
this._customKeyEventHandler = customKeyEventHandler;

// do a full screen refresh
this.refresh(0, this.rows - 1);
this.viewport?.syncScrollArea();
}

public clearTextureAtlas(): void {
Expand Down
2 changes: 2 additions & 0 deletions src/browser/TestUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ export class MockViewport implements IViewport {
public scrollLines(disp: number): void {
this._onRequestScrollLines.fire({ amount: disp, suppressScrollEvent: false });
}
public reset(): void {
}
}

export class MockCompositionHelper implements ICompositionHelper {
Expand Down
3 changes: 2 additions & 1 deletion src/browser/Types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ export interface IPartialColorSet {
export interface IViewport extends IDisposable {
scrollBarWidth: number;
readonly onRequestScrollLines: IEvent<{ amount: number, suppressScrollEvent: boolean }>;
syncScrollArea(immediate?: boolean): void;
syncScrollArea(immediate?: boolean, force?: boolean): void;
getLinesScrolled(ev: WheelEvent): number;
getBufferElements(startLine: number, endLine?: number): { bufferElements: HTMLElement[], cursorElement?: HTMLElement };
handleWheel(ev: WheelEvent): boolean;
handleTouchStart(ev: TouchEvent): void;
handleTouchMove(ev: TouchEvent): boolean;
scrollLines(disp: number): void; // todo api name?
reset(): void;
}

export interface ILinkifierEvent {
Expand Down
14 changes: 13 additions & 1 deletion src/browser/Viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,25 @@ export class Viewport extends Disposable implements IViewport {
this.register(this._optionsService.onSpecificOptionChange('scrollback', () => this.syncScrollArea()));

// Perform this async to ensure the ICharSizeService is ready.
setTimeout(() => this.syncScrollArea(), 0);
setTimeout(() => this.syncScrollArea());
}

private _handleThemeChange(colors: ReadonlyColorSet): void {
this._viewportElement.style.backgroundColor = colors.background.css;
}

public reset(): void {
this._currentRowHeight = 0;
this._currentDeviceCellHeight = 0;
this._lastRecordedBufferLength = 0;
this._lastRecordedViewportHeight = 0;
this._lastRecordedBufferHeight = 0;
this._lastTouchY = 0;
this._lastScrollTop = 0;
// Sync on next animation frame to ensure the new terminal state is used
this._coreBrowserService.window.requestAnimationFrame(() => this.syncScrollArea());
}

/**
* Refreshes row height, setting line-height, viewport height and scroll area height if
* necessary.
Expand Down

0 comments on commit 4f41070

Please sign in to comment.