diff --git a/addons/addon-webgl/src/WebglRenderer.ts b/addons/addon-webgl/src/WebglRenderer.ts index bb17baa84c..2bccc9b7c9 100644 --- a/addons/addon-webgl/src/WebglRenderer.ts +++ b/addons/addon-webgl/src/WebglRenderer.ts @@ -391,6 +391,7 @@ export class WebglRenderer extends Disposable implements IRenderer { end = clamp(end, terminal.rows - 1, 0); const cursorY = this._terminal.buffer.active.baseY + this._terminal.buffer.active.cursorY; + const viewportRelativeCursorY = cursorY - terminal.buffer.ydisp; // in case cursor.x == cols adjust visual cursor to cols - 1 const cursorX = Math.min(this._terminal.buffer.active.cursorX, terminal.cols - 1); let lastCursorX = -1; @@ -449,7 +450,7 @@ export class WebglRenderer extends Disposable implements IRenderer { if (x === cursorX) { this._model.cursor = { x: cursorX, - y: this._terminal.buffer.active.cursorY, + y: viewportRelativeCursorY, width: cell.getWidth(), style: this._coreBrowserService.isFocused ? (terminal.options.cursorStyle || 'block') : terminal.options.cursorInactiveStyle, diff --git a/test/playwright/SharedRendererTests.ts b/test/playwright/SharedRendererTests.ts index 377be16314..aa747277f7 100644 --- a/test/playwright/SharedRendererTests.ts +++ b/test/playwright/SharedRendererTests.ts @@ -1171,9 +1171,35 @@ export function injectSharedRendererTests(ctx: ISharedRendererTestContext): void await ctx.value.proxy.selectAll(); await pollFor(ctx.value.page, () => getCellColor(ctx.value, 1, 1), [0, 0, 255, 255]); }); + test('#4799: cursor should be in the correct position', async () => { + const theme: ITheme = { + cursor: '#0000FF' + }; + await ctx.value.page.evaluate(`window.term.options.theme = ${JSON.stringify(theme)};`); + for (let index = 0; index < 160; index++) { + await ctx.value.proxy.writeln(``); + } + await ctx.value.proxy.focus(); + await ctx.value.proxy.write('\x1b[A'); + await ctx.value.proxy.write('\x1b[A'); + await ctx.value.proxy.scrollLines(-2); + const rows = await ctx.value.proxy.rows; + // block cursor style + await pollFor(ctx.value.page, () => getCellColor(ctx.value, 1, rows), [0, 0, 255, 255]); + await ctx.value.proxy.blur(); + frameDetails = undefined; + // outlink cursor style + await pollFor(ctx.value.page, () => getCellColor(ctx.value, 1, rows), [0, 0, 0, 255]); + await pollFor(ctx.value.page, () => getCellColor(ctx.value, 1, rows, CellColorPosition.FIRST), [0, 0, 255, 255]); + }); }); } +enum CellColorPosition { + CENTER = 0, + FIRST = 1 +} + /** * Injects shared renderer tests where it's required to re-initialize the terminal for each test. * This is much slower than just calling `Terminal.reset` but testing some features needs this @@ -1214,11 +1240,16 @@ export function injectSharedRendererTestsStandalone(ctx: ISharedRendererTestCont * @param col The 1-based column index to get the color for. * @param row The 1-based row index to get the color for. */ -function getCellColor(ctx: ITestContext, col: number, row: number): MaybeAsync<[red: number, green: number, blue: number, alpha: number]> { +function getCellColor(ctx: ITestContext, col: number, row: number, position: CellColorPosition = CellColorPosition.CENTER): MaybeAsync<[red: number, green: number, blue: number, alpha: number]> { if (!frameDetails) { return getFrameDetails(ctx).then(frameDetails => getCellColorInner(frameDetails, col, row)); } - return getCellColorInner(frameDetails, col, row); + switch (position) { + case CellColorPosition.CENTER: + return getCellColorInner(frameDetails, col, row); + case CellColorPosition.FIRST: + return getCellColorFirstPoint(frameDetails, col, row); + } } let frameDetails: { cols: number, rows: number, decoded: IImage32 } | undefined = undefined; @@ -1244,6 +1275,17 @@ function getCellColorInner(frameDetails: { cols: number, rows: number, decoded: return Array.from(frameDetails.decoded.data.slice(i, i + 4)) as [number, number, number, number]; } +function getCellColorFirstPoint(frameDetails: { cols: number, rows: number, decoded: IImage32 }, col: number, row: number): [red: number, green: number, blue: number, alpha: number] { + const cellSize = { + width: frameDetails.decoded.width / frameDetails.cols, + height: frameDetails.decoded.height / frameDetails.rows + }; + const x = Math.floor((col - 1/* 1- to 0-based index */) * cellSize.width); + const y = Math.floor((row - 1/* 1- to 0-based index */) * cellSize.height); + const i = (y * frameDetails.decoded.width + x) * 4/* 4 channels per pixel */; + return Array.from(frameDetails.decoded.data.slice(i, i + 4)) as [number, number, number, number]; +} + const COLORS_16_TO_255 = [ '#000000', '#00005f', '#000087', '#0000af', '#0000d7', '#0000ff', '#005f00', '#005f5f', '#005f87', '#005faf', '#005fd7', '#005fff', '#008700', '#00875f', '#008787', '#0087af', '#0087d7', '#0087ff', '#00af00', '#00af5f', '#00af87', '#00afaf', '#00afd7', '#00afff', '#00d700', '#00d75f', '#00d787', '#00d7af', '#00d7d7', '#00d7ff', '#00ff00', '#00ff5f',