Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 8 additions & 2 deletions src/common/TestUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ export class MockBufferService implements IBufferService {
public serviceBrand: any;
public get buffer(): IBuffer { return this.buffers.active; }
public buffers: IBufferSet = {} as any;
public onResize: Event<{ cols: number, rows: number }> = new Emitter<{ cols: number, rows: number }>().event;
public onScroll: Event<number> = new Emitter<number>().event;
private readonly _onResize = new Emitter<{ cols: number, rows: number }>();
public readonly onResize: Event<{ cols: number, rows: number }> = this._onResize.event;
private readonly _onScroll = new Emitter<number>();
public readonly onScroll: Event<number> = this._onScroll.event;
public isUserScrolling: boolean = false;
constructor(
public cols: number,
Expand All @@ -46,6 +48,10 @@ export class MockBufferService implements IBufferService {
public scrollLines(disp: number, suppressScrollEvent?: boolean): void {
throw new Error('Method not implemented.');
}
public syncScrollPosition(): void {
// Fire scroll event with current buffer position
this._onScroll.fire(this.buffer.ydisp);
}
public resize(cols: number, rows: number): void {
this.cols = cols;
this.rows = rows;
Expand Down
66 changes: 66 additions & 0 deletions src/common/buffer/BufferSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { assert } from 'chai';
import { BufferSet } from 'common/buffer/BufferSet';
import { Buffer } from 'common/buffer/Buffer';
import { MockOptionsService, MockBufferService } from 'common/TestUtils.test';
import { Emitter } from 'vs/base/common/event';

describe('BufferSet', () => {
let bufferSet: BufferSet;
Expand Down Expand Up @@ -81,4 +82,69 @@ describe('BufferSet', () => {
assert.equal(bufferSet.alt.markers.length, 0);
});
});

describe('scroll position synchronization', () => {
it('should call syncScrollPosition when switching from alt back to normal buffer', () => {
bufferSet.activateNormalBuffer();

const originalYDisp = 50;
bufferSet.normal.ydisp = originalYDisp;
bufferSet.normal.ybase = 100;
const mockBufferService = (bufferSet as any)._bufferService as MockBufferService;

let syncScrollPositionCalled = false;
const originalSyncScrollPosition = mockBufferService.syncScrollPosition.bind(mockBufferService);
mockBufferService.syncScrollPosition = () => {
syncScrollPositionCalled = true;
originalSyncScrollPosition();
};

bufferSet.activateAltBuffer();
assert.equal(bufferSet.normal.ydisp, originalYDisp, 'Normal buffer ydisp should be preserved');
syncScrollPositionCalled = false;
bufferSet.activateNormalBuffer();

assert.equal(bufferSet.normal.ydisp, originalYDisp, 'Normal buffer ydisp should be restored');
assert.equal(syncScrollPositionCalled, true, 'activateNormalBuffer should call syncScrollPosition');
assert.equal(bufferSet.active, bufferSet.normal, 'Normal buffer should be active');
});

it('should preserve normal buffer scroll position when switching back from alt buffer', () => {
bufferSet.activateNormalBuffer();
const normalScrollPos = 80;
bufferSet.normal.ydisp = normalScrollPos;
bufferSet.normal.ybase = 150;

bufferSet.activateAltBuffer();
bufferSet.alt.ydisp = 0;
bufferSet.alt.ybase = 0;
bufferSet.activateNormalBuffer();

assert.equal(bufferSet.normal.ydisp, normalScrollPos, 'Normal buffer should maintain its scroll position');
assert.equal(bufferSet.active, bufferSet.normal, 'Normal buffer should be active');
assert.notEqual(bufferSet.normal.ydisp, bufferSet.alt.ydisp, 'Normal and alt buffer should have different scroll positions');
});

it('should fire scroll event with correct position when syncScrollPosition is called', () => {
const mockBufferService = (bufferSet as any)._bufferService as MockBufferService;

bufferSet.activateNormalBuffer();
const testScrollPosition = 42;

mockBufferService.buffer.ydisp = testScrollPosition;
assert.equal(mockBufferService.buffer.ydisp, testScrollPosition, 'Active buffer ydisp should be set correctly');

let scrollEventFired = false;
let scrollEventPosition = -1;

mockBufferService.onScroll((position: number) => {
scrollEventFired = true;
scrollEventPosition = position;
});

mockBufferService.syncScrollPosition();
assert.equal(scrollEventFired, true, 'syncScrollPosition should fire scroll event');
assert.equal(scrollEventPosition, testScrollPosition, `Scroll event should contain current buffer ydisp`);
});
});
});
2 changes: 2 additions & 0 deletions src/common/buffer/BufferSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export class BufferSet extends Disposable implements IBufferSet {
activeBuffer: this._normal,
inactiveBuffer: this._alt
});
// Prevent scrollbar "teleport" to top of the terminal, from previous alt buffer.
this._bufferService.syncScrollPosition();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gist: Seems like we had the correct ydisp the whole time, but just needed to update/refresh after coming back from alt buffer.

Had to add public method syncScrollPosition() and call it from here since _onScroll was private to bufferService.

}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/common/services/BufferService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,11 @@ export class BufferService extends Disposable implements IBufferService {
this._onScroll.fire(buffer.ydisp);
}
}

/**
* Synchronize the scroll position by firing a scroll event with the current buffer's ydisp.
*/
public syncScrollPosition(): void {
this._onScroll.fire(this.buffer.ydisp);
}
}
1 change: 1 addition & 0 deletions src/common/services/Services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface IBufferService {
onScroll: Event<number>;
scroll(eraseAttr: IAttributeData, isWrapped?: boolean): void;
scrollLines(disp: number, suppressScrollEvent?: boolean): void;
syncScrollPosition(): void;
resize(cols: number, rows: number): void;
reset(): void;
}
Expand Down
Loading