From 0e3518409e8d2ec20b2e3fbc24bef8ebb4b34990 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sat, 9 Sep 2017 19:06:30 -0700 Subject: [PATCH 01/30] Fix issues with link state - Scrolling will now clear the link renderer - Refreshing a single line will now recalculate only that line Fixes #959 Fixes #960 --- src/Linkifier.ts | 31 +++++++++++++++++++++++-------- src/Terminal.ts | 8 ++++++-- src/input/Interfaces.ts | 2 +- src/input/MouseZoneManager.ts | 25 ++++++++++++++++++++++--- 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/Linkifier.ts b/src/Linkifier.ts index 94795429..fa47271c 100644 --- a/src/Linkifier.ts +++ b/src/Linkifier.ts @@ -49,11 +49,16 @@ export class Linkifier extends EventEmitter implements ILinkifier { private _mouseZoneManager: IMouseZoneManager; private _rowsTimeoutId: number; private _nextLinkMatcherId = HYPERTEXT_LINK_MATCHER_ID; + private _rowsToLinkify: {start: number, end: number}; constructor( protected _terminal: IBufferAccessor & IElementAccessor ) { super(); + this._rowsToLinkify = { + start: null, + end: null + }; this.registerLinkMatcher(strictUrlRegex, null, { matchIndex: 1 }); } @@ -76,25 +81,35 @@ export class Linkifier extends EventEmitter implements ILinkifier { return; } - // Clear out any existing links - this._mouseZoneManager.clearAll(); + // Increase range to linkify + if (!this._rowsToLinkify.start) { + this._rowsToLinkify.start = start; + this._rowsToLinkify.end = end; + } else { + this._rowsToLinkify.start = this._rowsToLinkify.start < start ? this._rowsToLinkify.start : start; + this._rowsToLinkify.end = this._rowsToLinkify.end < end ? this._rowsToLinkify.end : end; + } + // Clear out any existing links on this row range + this._mouseZoneManager.clearAll(start, end); + + // Restart timer if (this._rowsTimeoutId) { clearTimeout(this._rowsTimeoutId); } - this._rowsTimeoutId = setTimeout(this._linkifyRows.bind(this, start, end), Linkifier.TIME_BEFORE_LINKIFY); + this._rowsTimeoutId = setTimeout(() => this._linkifyRows(), Linkifier.TIME_BEFORE_LINKIFY); } /** - * Linkifies - * @param start The row to start at. - * @param end The row to end at. + * Linkifies the rows requested. */ - private _linkifyRows(start: number, end: number): void { + private _linkifyRows(): void { this._rowsTimeoutId = null; - for (let i = start; i <= end; i++) { + for (let i = this._rowsToLinkify.start; i <= this._rowsToLinkify.end; i++) { this._linkifyRow(i); } + this._rowsToLinkify.start = null; + this._rowsToLinkify.end = null; } /** diff --git a/src/Terminal.ts b/src/Terminal.ts index e19887cb..817eed40 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -48,8 +48,11 @@ import { MouseZoneManager } from './input/MouseZoneManager'; import { initialize as initializeCharAtlas } from './renderer/CharAtlas'; import { IRenderer } from './renderer/Interfaces'; -// Declare for RequireJS in loadAddon +// Declares required for loadAddon +declare var exports: any; +declare var module: any; declare var define: any; +declare var require: any; // Let it work inside Node.js for automated testing purposes. const document = (typeof window !== 'undefined') ? window.document : null; @@ -588,6 +591,7 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT this.syncBellSound(); this._mouseZoneManager = new MouseZoneManager(this); + this.on('scroll', () => this._mouseZoneManager.clearAll()); this.linkifier.attachToDom(this._mouseZoneManager); // Create the container that will hold helpers like the textarea for @@ -1034,7 +1038,7 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT */ private queueLinkification(start: number, end: number): void { if (this.linkifier) { - this.linkifier.linkifyRows(0, this.rows); + this.linkifier.linkifyRows(start, end); } } diff --git a/src/input/Interfaces.ts b/src/input/Interfaces.ts index e3ed4eef..21514a53 100644 --- a/src/input/Interfaces.ts +++ b/src/input/Interfaces.ts @@ -5,7 +5,7 @@ export interface IMouseZoneManager { add(zone: IMouseZone): void; - clearAll(): void; + clearAll(start?: number, end?: number): void; } export interface IMouseZone { diff --git a/src/input/MouseZoneManager.ts b/src/input/MouseZoneManager.ts index d1bc8a75..f3ba90c1 100644 --- a/src/input/MouseZoneManager.ts +++ b/src/input/MouseZoneManager.ts @@ -44,9 +44,28 @@ export class MouseZoneManager implements IMouseZoneManager { } } - public clearAll(): void { - this._zones.length = 0; - this._deactivate(); + public clearAll(start?: number, end?: number): void { + // Exit if there's nothing to clear + if (this._zones.length === 0) { + return; + } + + // Iterate through zones and clear them out if they're within the range + for (let i = 0; i < this._zones.length; i++) { + const zone = this._zones[i]; + if (zone.y >= start && zone.y <= end) { + if (this._currentZone && this._currentZone === zone) { + this._currentZone.leaveCallback(); + this._currentZone = null; + } + this._zones.splice(i--, 1); + } + } + + // Deactivate the mouse zone manager if all the zones have been removed + if (this._zones.length === 0) { + this._deactivate(); + } } private _activate(): void { From 6c06d83525908d12d0c0ba245cfbfba0115f1868 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sun, 10 Sep 2017 08:01:16 -0700 Subject: [PATCH 02/30] Support theming cursor accent color Fixes #963 --- src/Interfaces.ts | 1 + src/renderer/CharAtlas.ts | 1 + src/renderer/ColorManager.ts | 3 +++ src/renderer/CursorRenderLayer.ts | 2 +- src/renderer/Interfaces.ts | 1 + typings/xterm.d.ts | 2 ++ 6 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Interfaces.ts b/src/Interfaces.ts index d022dc3e..f6c20018 100644 --- a/src/Interfaces.ts +++ b/src/Interfaces.ts @@ -323,6 +323,7 @@ export interface ITheme { foreground?: string; background?: string; cursor?: string; + cursorAccent?: string; selection?: string; black?: string; red?: string; diff --git a/src/renderer/CharAtlas.ts b/src/renderer/CharAtlas.ts index bf080cce..45562a25 100644 --- a/src/renderer/CharAtlas.ts +++ b/src/renderer/CharAtlas.ts @@ -77,6 +77,7 @@ function generateConfig(scaledCharWidth: number, scaledCharHeight: number, termi foreground: colors.foreground, background: null, cursor: null, + cursorAccent: null, selection: null, ansi: colors.ansi.slice(0, 16) }; diff --git a/src/renderer/ColorManager.ts b/src/renderer/ColorManager.ts index b9afd0e1..afd5c243 100644 --- a/src/renderer/ColorManager.ts +++ b/src/renderer/ColorManager.ts @@ -9,6 +9,7 @@ import { ITheme } from '../Interfaces'; const DEFAULT_FOREGROUND = '#ffffff'; const DEFAULT_BACKGROUND = '#000000'; const DEFAULT_CURSOR = '#ffffff'; +const DEFAULT_CURSOR_ACCENT = '#000000'; const DEFAULT_SELECTION = 'rgba(255, 255, 255, 0.3)'; export const DEFAULT_ANSI_COLORS = [ // dark: @@ -72,6 +73,7 @@ export class ColorManager { foreground: DEFAULT_FOREGROUND, background: DEFAULT_BACKGROUND, cursor: DEFAULT_CURSOR, + cursorAccent: DEFAULT_CURSOR_ACCENT, selection: DEFAULT_SELECTION, ansi: generate256Colors(DEFAULT_ANSI_COLORS) }; @@ -86,6 +88,7 @@ export class ColorManager { this.colors.foreground = theme.foreground || DEFAULT_FOREGROUND; this.colors.background = theme.background || DEFAULT_BACKGROUND; this.colors.cursor = theme.cursor || DEFAULT_CURSOR; + this.colors.cursorAccent = theme.cursorAccent || DEFAULT_CURSOR_ACCENT; this.colors.selection = theme.selection || DEFAULT_SELECTION; this.colors.ansi[0] = theme.black || DEFAULT_ANSI_COLORS[0]; this.colors.ansi[1] = theme.red || DEFAULT_ANSI_COLORS[1]; diff --git a/src/renderer/CursorRenderLayer.ts b/src/renderer/CursorRenderLayer.ts index b136a13a..bc6fbcb1 100644 --- a/src/renderer/CursorRenderLayer.ts +++ b/src/renderer/CursorRenderLayer.ts @@ -199,7 +199,7 @@ export class CursorRenderLayer extends BaseRenderLayer { this._ctx.save(); this._ctx.fillStyle = this.colors.cursor; this.fillCells(x, y, charData[CHAR_DATA_WIDTH_INDEX], 1); - this._ctx.fillStyle = this.colors.background; + this._ctx.fillStyle = this.colors.cursorAccent; this.fillCharTrueColor(terminal, charData, x, y); this._ctx.restore(); } diff --git a/src/renderer/Interfaces.ts b/src/renderer/Interfaces.ts index 05f98300..4b0028b8 100644 --- a/src/renderer/Interfaces.ts +++ b/src/renderer/Interfaces.ts @@ -72,6 +72,7 @@ export interface IColorSet { foreground: string; background: string; cursor: string; + cursorAccent: string; selection: string; ansi: string[]; } diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index 7a4d6940..de2a261b 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -88,6 +88,8 @@ interface ITheme { background?: string, /** The cursor color */ cursor?: string, + /** The accent color of the cursor (used as the foreground color for a block cursor) */ + cursorAccent?: string, /** The selection color (can be transparent) */ selection?: string, /** ANSI black (eg. `\x1b[30m`) */ From ae741348a635839ec04f7c65fc02594804f6d0c4 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sun, 10 Sep 2017 10:31:17 -0700 Subject: [PATCH 03/30] Fix several issues with line height Fixes #966 Fixes #967 --- src/Interfaces.ts | 3 +- src/Terminal.ts | 9 +++-- src/Viewport.ts | 21 +++++----- src/addons/fit/fit.js | 2 +- src/renderer/BackgroundRenderLayer.ts | 6 +-- src/renderer/BaseRenderLayer.ts | 39 +++++------------- src/renderer/CursorRenderLayer.ts | 6 +-- src/renderer/ForegroundRenderLayer.ts | 6 +-- src/renderer/Interfaces.ts | 19 +++++++-- src/renderer/LinkRenderLayer.ts | 6 +-- src/renderer/Renderer.ts | 57 ++++++++++++++++++++++++--- src/renderer/SelectionRenderLayer.ts | 6 +-- src/utils/TestUtils.test.ts | 13 +++++- 13 files changed, 125 insertions(+), 68 deletions(-) diff --git a/src/Interfaces.ts b/src/Interfaces.ts index d022dc3e..97fa3fd8 100644 --- a/src/Interfaces.ts +++ b/src/Interfaces.ts @@ -5,7 +5,7 @@ import { ILinkMatcherOptions } from './Interfaces'; import { LinkMatcherHandler, LinkMatcherValidationCallback, Charset, LineData } from './Types'; -import { IColorSet } from './renderer/Interfaces'; +import { IColorSet, IRenderer } from './renderer/Interfaces'; import { IMouseZoneManager } from './input/Interfaces'; export interface IBrowser { @@ -36,6 +36,7 @@ export interface ITerminal extends ILinkifierAccessor, IBufferAccessor, IElement selectionManager: ISelectionManager; charMeasure: ICharMeasure; textarea: HTMLTextAreaElement; + renderer: IRenderer; rows: number; cols: number; browser: IBrowser; diff --git a/src/Terminal.ts b/src/Terminal.ts index e19887cb..67026ab0 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -188,7 +188,7 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT private inputHandler: InputHandler; private parser: Parser; - private renderer: IRenderer; + public renderer: IRenderer; public selectionManager: SelectionManager; public linkifier: ILinkifier; public buffers: BufferSet; @@ -619,7 +619,6 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT this.charMeasure = new CharMeasure(document, this.helperContainer); this.viewport = new Viewport(this, this.viewportElement, this.viewportScrollArea, this.charMeasure); - this.charMeasure.on('charsizechanged', () => this.viewport.syncScrollArea()); this.renderer = new Renderer(this); this.on('cursormove', () => this.renderer.onCursorMove()); this.on('resize', () => this.renderer.onResize(this.cols, this.rows, false)); @@ -627,6 +626,7 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT this.on('focus', () => this.renderer.onFocus()); window.addEventListener('resize', () => this.renderer.onWindowResize(window.devicePixelRatio)); this.charMeasure.on('charsizechanged', () => this.renderer.onResize(this.cols, this.rows, true)); + this.renderer.on('resize', (dimensions) => this.viewport.syncScrollArea()); this.selectionManager = new SelectionManager(this, this.buffer, this.charMeasure); this.element.addEventListener('mousedown', (e: MouseEvent) => this.selectionManager.onMouseDown(e)); @@ -639,7 +639,10 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT this.textarea.focus(); this.textarea.select(); }); - this.on('scroll', () => this.selectionManager.refresh()); + this.on('scroll', () => { + this.viewport.syncScrollArea(); + this.selectionManager.refresh(); + }); this.viewportElement.addEventListener('scroll', () => this.selectionManager.refresh()); // Measure the character size diff --git a/src/Viewport.ts b/src/Viewport.ts index 1f693cfe..b7622ba7 100644 --- a/src/Viewport.ts +++ b/src/Viewport.ts @@ -34,8 +34,6 @@ export class Viewport implements IViewport { this.lastRecordedBufferLength = 0; this.lastRecordedViewportHeight = 0; - this.terminal.on('scroll', this.syncScrollArea.bind(this)); - this.terminal.on('resize', this.syncScrollArea.bind(this)); this.viewportElement.addEventListener('scroll', this.onScroll.bind(this)); // Perform this async to ensure the CharMeasure is ready. @@ -52,18 +50,20 @@ export class Viewport implements IViewport { */ private refresh(): void { if (this.charMeasure.height > 0) { - const lineHeight = Math.ceil(this.charMeasure.height * this.terminal.options.lineHeight); + const lineHeight = (this.terminal).renderer.dimensions.scaledLineHeight / window.devicePixelRatio; const rowHeightChanged = lineHeight !== this.currentRowHeight; + // TODO: Do we need lineHeight anymore?? if (rowHeightChanged) { this.currentRowHeight = lineHeight; this.viewportElement.style.lineHeight = lineHeight + 'px'; } - const viewportHeightChanged = this.lastRecordedViewportHeight !== this.terminal.rows; - if (rowHeightChanged || viewportHeightChanged) { - this.lastRecordedViewportHeight = this.terminal.rows; - this.viewportElement.style.height = lineHeight * this.terminal.rows + 'px'; + // const viewportHeightChanged = this.lastRecordedViewportHeight !== this.terminal.rows; + const viewportHeightChanged = this.lastRecordedViewportHeight !== this.terminal.renderer.dimensions.canvasHeight; + if (viewportHeightChanged) { + this.lastRecordedViewportHeight = this.terminal.renderer.dimensions.canvasHeight; + this.viewportElement.style.height = this.lastRecordedViewportHeight + 'px'; } - this.scrollArea.style.height = (lineHeight * this.lastRecordedBufferLength) + 'px'; + this.scrollArea.style.height = Math.round(lineHeight * this.lastRecordedBufferLength) + 'px'; } } @@ -75,12 +75,13 @@ export class Viewport implements IViewport { // If buffer height changed this.lastRecordedBufferLength = this.terminal.buffer.lines.length; this.refresh(); - } else if (this.lastRecordedViewportHeight !== this.terminal.rows) { + } else if (this.lastRecordedViewportHeight !== (this.terminal).renderer.dimensions.canvasHeight) { // If viewport height changed this.refresh(); } else { // If size has changed, refresh viewport - if (Math.ceil(this.charMeasure.height * this.terminal.options.lineHeight) !== this.currentRowHeight) { + console.log(this.terminal.renderer.dimensions.scaledLineHeight / window.devicePixelRatio); + if (this.terminal.renderer.dimensions.scaledLineHeight / window.devicePixelRatio !== this.currentRowHeight) { this.refresh(); } } diff --git a/src/addons/fit/fit.js b/src/addons/fit/fit.js index 59f0fd52..3e61a7ef 100644 --- a/src/addons/fit/fit.js +++ b/src/addons/fit/fit.js @@ -47,7 +47,7 @@ var availableWidth = parentElementWidth - elementPaddingHor; var geometry = { cols: Math.floor(availableWidth / term.charMeasure.width), - rows: Math.floor(availableHeight / Math.ceil(term.charMeasure.height * term.getOption('lineHeight'))) + rows: Math.floor(availableHeight / Math.floor(term.charMeasure.height * term.getOption('lineHeight'))) }; return geometry; diff --git a/src/renderer/BackgroundRenderLayer.ts b/src/renderer/BackgroundRenderLayer.ts index 0c09727d..279aee6d 100644 --- a/src/renderer/BackgroundRenderLayer.ts +++ b/src/renderer/BackgroundRenderLayer.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { IColorSet } from './Interfaces'; +import { IColorSet, IRenderDimensions } from './Interfaces'; import { IBuffer, ICharMeasure, ITerminal } from '../Interfaces'; import { CHAR_DATA_ATTR_INDEX } from '../Buffer'; import { GridCache } from './GridCache'; @@ -18,8 +18,8 @@ export class BackgroundRenderLayer extends BaseRenderLayer { this._state = new GridCache(); } - public resize(terminal: ITerminal, canvasWidth: number, canvasHeight: number, charSizeChanged: boolean): void { - super.resize(terminal, canvasWidth, canvasHeight, charSizeChanged); + public resize(terminal: ITerminal, dim: IRenderDimensions, charSizeChanged: boolean): void { + super.resize(terminal, dim, charSizeChanged); // Resizing the canvas discards the contents of the canvas so clear state this._state.clear(); this._state.resize(terminal.cols, terminal.rows); diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index cc02bcea..38457e13 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { IRenderLayer, IColorSet } from './Interfaces'; +import { IRenderLayer, IColorSet, IRenderDimensions } from './Interfaces'; import { ITerminal, ITerminalOptions } from '../Interfaces'; import { acquireCharAtlas, CHAR_ATLAS_CELL_SPACING } from './CharAtlas'; import { CharData } from '../Types'; @@ -61,34 +61,15 @@ export abstract class BaseRenderLayer implements IRenderLayer { } } - public resize(terminal: ITerminal, canvasWidth: number, canvasHeight: number, charSizeChanged: boolean): void { - // Calculate the scaled character dimensions, if devicePixelRatio is a - // floating point number then the value is ceiled to ensure there is enough - // space to draw the character to the cell - this.scaledCharWidth = Math.ceil(terminal.charMeasure.width * window.devicePixelRatio); - this.scaledCharHeight = Math.ceil(terminal.charMeasure.height * window.devicePixelRatio); - - // Calculate the scaled line height, if lineHeight is not 1 then the value - // will be floored because since lineHeight can never be lower then 1, there - // is a guarentee that the scaled line height will always be larger than - // scaled char height. - this.scaledLineHeight = Math.floor(this.scaledCharHeight * terminal.options.lineHeight); - - // Calculate the y coordinate within a cell that text should draw from in - // order to draw in the center of a cell. - this.scaledLineDrawY = terminal.options.lineHeight === 1 ? 0 : Math.round((this.scaledLineHeight - this.scaledCharHeight) / 2); - - // Recalcualte the canvas dimensions; width/height define the actual number - // of pixels in the canvas, style.width/height define the size of the canvas - // on the page. It's very important that this rounds to nearest integer and - // not ceils as browsers often set window.devicePixelRatio as something like - // 1.100000023841858, when it's actually 1.1. Ceiling causes blurriness as - // the backing canvas image is 1 pixel too large for the canvas element - // size. - this._canvas.width = Math.round(canvasWidth * window.devicePixelRatio); - this._canvas.height = Math.round(canvasHeight * window.devicePixelRatio); - this._canvas.style.width = `${canvasWidth}px`; - this._canvas.style.height = `${canvasHeight}px`; + public resize(terminal: ITerminal, dim: IRenderDimensions, charSizeChanged: boolean): void { + this.scaledCharWidth = dim.scaledCharWidth; + this.scaledCharHeight = dim.scaledCharHeight; + this.scaledLineHeight = dim.scaledLineHeight; + this.scaledLineDrawY = dim.scaledLineDrawY; + this._canvas.width = dim.scaledCanvasWidth; + this._canvas.height = dim.scaledCanvasHeight; + this._canvas.style.width = `${dim.canvasWidth}px`; + this._canvas.style.height = `${dim.canvasHeight}px`; if (charSizeChanged) { this._refreshCharAtlas(terminal, this.colors); diff --git a/src/renderer/CursorRenderLayer.ts b/src/renderer/CursorRenderLayer.ts index b136a13a..8e25d7c1 100644 --- a/src/renderer/CursorRenderLayer.ts +++ b/src/renderer/CursorRenderLayer.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { IColorSet } from './Interfaces'; +import { IColorSet, IRenderDimensions } from './Interfaces'; import { IBuffer, ICharMeasure, ITerminal, ITerminalOptions } from '../Interfaces'; import { CHAR_DATA_WIDTH_INDEX, CHAR_DATA_CODE_INDEX, CHAR_DATA_CHAR_INDEX } from '../Buffer'; import { GridCache } from './GridCache'; @@ -47,8 +47,8 @@ export class CursorRenderLayer extends BaseRenderLayer { // TODO: Consider initial options? Maybe onOptionsChanged should be called at the end of open? } - public resize(terminal: ITerminal, canvasWidth: number, canvasHeight: number, charSizeChanged: boolean): void { - super.resize(terminal, canvasWidth, canvasHeight, charSizeChanged); + public resize(terminal: ITerminal, dim: IRenderDimensions, charSizeChanged: boolean): void { + super.resize(terminal, dim, charSizeChanged); // Resizing the canvas discards the contents of the canvas so clear state this._state = { x: null, diff --git a/src/renderer/ForegroundRenderLayer.ts b/src/renderer/ForegroundRenderLayer.ts index 883be5d9..8e9e23fc 100644 --- a/src/renderer/ForegroundRenderLayer.ts +++ b/src/renderer/ForegroundRenderLayer.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { IColorSet } from './Interfaces'; +import { IColorSet, IRenderDimensions } from './Interfaces'; import { IBuffer, ICharMeasure, ITerminal } from '../Interfaces'; import { CHAR_DATA_ATTR_INDEX, CHAR_DATA_CODE_INDEX, CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX } from '../Buffer'; import { FLAGS } from './Types'; @@ -26,8 +26,8 @@ export class ForegroundRenderLayer extends BaseRenderLayer { this._state = new GridCache(); } - public resize(terminal: ITerminal, canvasWidth: number, canvasHeight: number, charSizeChanged: boolean): void { - super.resize(terminal, canvasWidth, canvasHeight, charSizeChanged); + public resize(terminal: ITerminal, dim: IRenderDimensions, charSizeChanged: boolean): void { + super.resize(terminal, dim, charSizeChanged); // Resizing the canvas discards the contents of the canvas so clear state this._state.clear(); this._state.resize(terminal.cols, terminal.rows); diff --git a/src/renderer/Interfaces.ts b/src/renderer/Interfaces.ts index 05f98300..890b01f5 100644 --- a/src/renderer/Interfaces.ts +++ b/src/renderer/Interfaces.ts @@ -3,9 +3,11 @@ * @license MIT */ -import { ITerminal, ITerminalOptions, ITheme } from '../Interfaces'; +import { ITerminal, ITerminalOptions, ITheme, IEventEmitter } from '../Interfaces'; + +export interface IRenderer extends IEventEmitter { + dimensions: IRenderDimensions; -export interface IRenderer { setTheme(theme: ITheme): IColorSet; onWindowResize(devicePixelRatio: number): void; onResize(cols: number, rows: number, didCharSizeChange: boolean): void; @@ -59,7 +61,7 @@ export interface IRenderLayer { /** * Resize the render layer. */ - resize(terminal: ITerminal, canvasWidth: number, canvasHeight: number, charSizeChanged: boolean): void; + resize(terminal: ITerminal, dim: IRenderDimensions, charSizeChanged: boolean): void; /** * Clear the state of the render layer. @@ -75,3 +77,14 @@ export interface IColorSet { selection: string; ansi: string[]; } + +export interface IRenderDimensions { + scaledCharWidth: number; + scaledCharHeight: number; + scaledLineHeight: number; + scaledLineDrawY: number; + scaledCanvasWidth: number; + scaledCanvasHeight: number; + canvasWidth: number; + canvasHeight: number; +} diff --git a/src/renderer/LinkRenderLayer.ts b/src/renderer/LinkRenderLayer.ts index 9b2cabb3..ad178933 100644 --- a/src/renderer/LinkRenderLayer.ts +++ b/src/renderer/LinkRenderLayer.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { IColorSet } from './Interfaces'; +import { IColorSet, IRenderDimensions } from './Interfaces'; import { IBuffer, ICharMeasure, ITerminal, ILinkifierAccessor } from '../Interfaces'; import { CHAR_DATA_ATTR_INDEX } from '../Buffer'; import { GridCache } from './GridCache'; @@ -20,8 +20,8 @@ export class LinkRenderLayer extends BaseRenderLayer { terminal.linkifier.on(LinkHoverEventTypes.LEAVE, (e: LinkHoverEvent) => this._onLinkLeave(e)); } - public resize(terminal: ITerminal, canvasWidth: number, canvasHeight: number, charSizeChanged: boolean): void { - super.resize(terminal, canvasWidth, canvasHeight, charSizeChanged); + public resize(terminal: ITerminal, dim: IRenderDimensions, charSizeChanged: boolean): void { + super.resize(terminal, dim, charSizeChanged); // Resizing the canvas discards the contents of the canvas so clear state this._state = null; } diff --git a/src/renderer/Renderer.ts b/src/renderer/Renderer.ts index 131a8b78..b610fd69 100644 --- a/src/renderer/Renderer.ts +++ b/src/renderer/Renderer.ts @@ -11,10 +11,11 @@ import { SelectionRenderLayer } from './SelectionRenderLayer'; import { CursorRenderLayer } from './CursorRenderLayer'; import { ColorManager } from './ColorManager'; import { BaseRenderLayer } from './BaseRenderLayer'; -import { IRenderLayer, IColorSet, IRenderer } from './Interfaces'; +import { IRenderLayer, IColorSet, IRenderer, IRenderDimensions } from './Interfaces'; import { LinkRenderLayer } from './LinkRenderLayer'; +import { EventEmitter } from '../EventEmitter'; -export class Renderer implements IRenderer { +export class Renderer extends EventEmitter implements IRenderer { /** A queue of the rows to be refreshed */ private _refreshRowsQueue: {start: number, end: number}[] = []; private _refreshAnimationFrame = null; @@ -23,8 +24,10 @@ export class Renderer implements IRenderer { private _devicePixelRatio: number; private _colorManager: ColorManager; + public dimensions: IRenderDimensions; constructor(private _terminal: ITerminal) { + super(); this._colorManager = new ColorManager(); this._renderLayers = [ new BackgroundRenderLayer(this._terminal.element, 0, this._colorManager.colors), @@ -33,6 +36,16 @@ export class Renderer implements IRenderer { new LinkRenderLayer(this._terminal.element, 3, this._colorManager.colors, this._terminal), new CursorRenderLayer(this._terminal.element, 4, this._colorManager.colors) ]; + this.dimensions = { + scaledCharWidth: null, + scaledCharHeight: null, + scaledLineHeight: null, + scaledLineDrawY: null, + scaledCanvasWidth: null, + scaledCanvasHeight: null, + canvasWidth: null, + canvasHeight: null + }; this._devicePixelRatio = window.devicePixelRatio; } @@ -63,12 +76,46 @@ export class Renderer implements IRenderer { if (!this._terminal.charMeasure.width || !this._terminal.charMeasure.height) { return; } - const width = this._terminal.charMeasure.width * cols; - const height = Math.floor(this._terminal.charMeasure.height * this._terminal.options.lineHeight) * rows; + + // Calculate the scaled character dimensions, if devicePixelRatio is a + // floating point number then the value is ceiled to ensure there is enough + // space to draw the character to the cell + this.dimensions.scaledCharWidth = Math.ceil(this._terminal.charMeasure.width * window.devicePixelRatio); + this.dimensions.scaledCharHeight = Math.ceil(this._terminal.charMeasure.height * window.devicePixelRatio); + + // Calculate the scaled line height, if lineHeight is not 1 then the value + // will be floored because since lineHeight can never be lower then 1, there + // is a guarentee that the scaled line height will always be larger than + // scaled char height. + this.dimensions.scaledLineHeight = Math.floor(this.dimensions.scaledCharHeight * this._terminal.options.lineHeight); + + // Calculate the y coordinate within a cell that text should draw from in + // order to draw in the center of a cell. + this.dimensions.scaledLineDrawY = this._terminal.options.lineHeight === 1 ? 0 : Math.round((this.dimensions.scaledLineHeight - this.dimensions.scaledCharHeight) / 2); + + // Recalculate the canvas dimensions; scaled* define the actual number of + // pixel in the canvas + this.dimensions.scaledCanvasHeight = this._terminal.rows * this.dimensions.scaledLineHeight; + this.dimensions.scaledCanvasWidth = this._terminal.cols * this.dimensions.scaledCharWidth; + + // The the size of the canvas on the page. It's very important that this + // rounds to nearest integer and not ceils as browsers often set + // window.devicePixelRatio as something like 1.100000023841858, when it's + // actually 1.1. Ceiling causes blurriness as the backing canvas image is 1 + // pixel too large for the canvas element size. + this.dimensions.canvasHeight = Math.round(this.dimensions.scaledCanvasHeight / window.devicePixelRatio); + this.dimensions.canvasWidth = Math.round(this.dimensions.scaledCanvasWidth / window.devicePixelRatio); + // Resize all render layers - this._renderLayers.forEach(l => l.resize(this._terminal, width, height, didCharSizeChange)); + this._renderLayers.forEach(l => l.resize(this._terminal, this.dimensions, didCharSizeChange)); + // Force a refresh this._terminal.refresh(0, this._terminal.rows - 1); + + this.emit('resize', { + width: this.dimensions.canvasWidth, + height: this.dimensions.canvasHeight + }); } public onCharSizeChanged(): void { diff --git a/src/renderer/SelectionRenderLayer.ts b/src/renderer/SelectionRenderLayer.ts index 39569c9b..5339c7a4 100644 --- a/src/renderer/SelectionRenderLayer.ts +++ b/src/renderer/SelectionRenderLayer.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { IColorSet } from './Interfaces'; +import { IColorSet, IRenderDimensions } from './Interfaces'; import { IBuffer, ICharMeasure, ITerminal } from '../Interfaces'; import { CHAR_DATA_ATTR_INDEX } from '../Buffer'; import { GridCache } from './GridCache'; @@ -21,8 +21,8 @@ export class SelectionRenderLayer extends BaseRenderLayer { }; } - public resize(terminal: ITerminal, canvasWidth: number, canvasHeight: number, charSizeChanged: boolean): void { - super.resize(terminal, canvasWidth, canvasHeight, charSizeChanged); + public resize(terminal: ITerminal, dim: IRenderDimensions, charSizeChanged: boolean): void { + super.resize(terminal, dim, charSizeChanged); // Resizing the canvas discards the contents of the canvas so clear state this._state = { start: null, diff --git a/src/utils/TestUtils.test.ts b/src/utils/TestUtils.test.ts index eb2f2501..45cf169a 100644 --- a/src/utils/TestUtils.test.ts +++ b/src/utils/TestUtils.test.ts @@ -7,9 +7,10 @@ import { ITerminal, IBuffer, IBufferSet, IBrowser, ICharMeasure, ISelectionManag import { LineData } from '../Types'; import { Buffer } from '../Buffer'; import * as Browser from './Browser'; -import { IColorSet, IRenderer } from '../renderer/Interfaces'; +import { IColorSet, IRenderer, IRenderDimensions } from '../renderer/Interfaces'; export class MockTerminal implements ITerminal { + renderer: IRenderer; linkifier: ILinkifier; isFocused: boolean; options: ITerminalOptions = {}; @@ -215,6 +216,16 @@ export class MockBuffer implements IBuffer { } export class MockRenderer implements IRenderer { + on(type: string, listener: IListenerType): void { + throw new Error('Method not implemented.'); + } + off(type: string, listener: IListenerType): void { + throw new Error('Method not implemented.'); + } + emit(type: string, data?: any): void { + throw new Error('Method not implemented.'); + } + dimensions: IRenderDimensions; setTheme(theme: ITheme): IColorSet { return {}; } onResize(cols: number, rows: number, didCharSizeChange: boolean): void {} onCharSizeChanged(): void {} From ac23be134b1b5926aee87654e92493cb691cd375 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sun, 10 Sep 2017 10:38:08 -0700 Subject: [PATCH 04/30] Simplify viewport --- src/Viewport.ts | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/Viewport.ts b/src/Viewport.ts index b7622ba7..064076a2 100644 --- a/src/Viewport.ts +++ b/src/Viewport.ts @@ -12,9 +12,10 @@ import { IColorSet } from './renderer/Interfaces'; * Logic for the virtual scroll bar is included in this object. */ export class Viewport implements IViewport { - private currentRowHeight: number; - private lastRecordedBufferLength: number; - private lastRecordedViewportHeight: number; + private currentRowHeight: number = 0; + private lastRecordedBufferLength: number = 0; + private lastRecordedViewportHeight: number = 0; + private lastRecordedBufferHeight: number = 0; private lastTouchY: number; /** @@ -30,10 +31,6 @@ export class Viewport implements IViewport { private scrollArea: HTMLElement, private charMeasure: CharMeasure ) { - this.currentRowHeight = 0; - this.lastRecordedBufferLength = 0; - this.lastRecordedViewportHeight = 0; - this.viewportElement.addEventListener('scroll', this.onScroll.bind(this)); // Perform this async to ensure the CharMeasure is ready. @@ -50,20 +47,18 @@ export class Viewport implements IViewport { */ private refresh(): void { if (this.charMeasure.height > 0) { - const lineHeight = (this.terminal).renderer.dimensions.scaledLineHeight / window.devicePixelRatio; - const rowHeightChanged = lineHeight !== this.currentRowHeight; - // TODO: Do we need lineHeight anymore?? - if (rowHeightChanged) { - this.currentRowHeight = lineHeight; - this.viewportElement.style.lineHeight = lineHeight + 'px'; - } - // const viewportHeightChanged = this.lastRecordedViewportHeight !== this.terminal.rows; - const viewportHeightChanged = this.lastRecordedViewportHeight !== this.terminal.renderer.dimensions.canvasHeight; - if (viewportHeightChanged) { + this.currentRowHeight = this.terminal.renderer.dimensions.scaledLineHeight / window.devicePixelRatio; + + if (this.lastRecordedViewportHeight !== this.terminal.renderer.dimensions.canvasHeight) { this.lastRecordedViewportHeight = this.terminal.renderer.dimensions.canvasHeight; this.viewportElement.style.height = this.lastRecordedViewportHeight + 'px'; } - this.scrollArea.style.height = Math.round(lineHeight * this.lastRecordedBufferLength) + 'px'; + + const newBufferHeight = Math.round(this.currentRowHeight * this.lastRecordedBufferLength); + if (this.lastRecordedBufferHeight !== newBufferHeight) { + this.lastRecordedBufferHeight = newBufferHeight; + this.scrollArea.style.height = this.lastRecordedBufferHeight + 'px'; + } } } @@ -80,7 +75,6 @@ export class Viewport implements IViewport { this.refresh(); } else { // If size has changed, refresh viewport - console.log(this.terminal.renderer.dimensions.scaledLineHeight / window.devicePixelRatio); if (this.terminal.renderer.dimensions.scaledLineHeight / window.devicePixelRatio !== this.currentRowHeight) { this.refresh(); } From 903ed1917e38c52c372d6b340d6a65ac0dcbef78 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sun, 10 Sep 2017 10:43:07 -0700 Subject: [PATCH 05/30] Remove viewport test It's not tightly linked to renderer and depends on devicePixelRatio. --- src/Viewport.test.ts | 90 -------------------------------------------- 1 file changed, 90 deletions(-) delete mode 100644 src/Viewport.test.ts diff --git a/src/Viewport.test.ts b/src/Viewport.test.ts deleted file mode 100644 index 6ce798d3..00000000 --- a/src/Viewport.test.ts +++ /dev/null @@ -1,90 +0,0 @@ -/** - * Copyright (c) 2016 The xterm.js authors. All rights reserved. - * @license MIT - */ - -import { assert } from 'chai'; -import { Viewport } from './Viewport'; -import { BufferSet } from './BufferSet'; - -describe('Viewport', () => { - let terminal; - let viewportElement; - let charMeasure; - let viewport; - let scrollAreaElement; - - const CHARACTER_HEIGHT = 10; - - beforeEach(() => { - terminal = { - rows: 0, - ydisp: 0, - on: () => {}, - rowContainer: { - style: { - lineHeight: 0 - } - }, - selectionContainer: { - style: { - height: 0 - } - }, - options: { - scrollback: 10, - lineHeight: 1 - } - }; - terminal.buffers = new BufferSet(terminal); - terminal.buffer = terminal.buffers.active; - viewportElement = { - addEventListener: () => {}, - style: { - height: 0, - lineHeight: 0 - } - }; - scrollAreaElement = { - style: { - height: 0 - } - }; - charMeasure = { - height: CHARACTER_HEIGHT - }; - viewport = new Viewport(terminal, viewportElement, scrollAreaElement, charMeasure); - }); - - describe('refresh', () => { - it('should set the height of the viewport when the line-height changed', () => { - terminal.buffer.lines.push(''); - terminal.buffer.lines.push(''); - terminal.rows = 1; - viewport.refresh(); - assert.equal(viewportElement.style.height, 1 * CHARACTER_HEIGHT + 'px'); - charMeasure.height = 2 * CHARACTER_HEIGHT; - viewport.refresh(); - assert.equal(viewportElement.style.height, 2 * CHARACTER_HEIGHT + 'px'); - }); - }); - - describe('syncScrollArea', () => { - it('should sync the scroll area', done => { - // Allow CharMeasure to be initialized - setTimeout(() => { - terminal.buffer.lines.push(''); - terminal.rows = 1; - assert.equal(scrollAreaElement.style.height, 0 * CHARACTER_HEIGHT + 'px'); - viewport.syncScrollArea(); - assert.equal(viewportElement.style.height, 1 * CHARACTER_HEIGHT + 'px'); - assert.equal(scrollAreaElement.style.height, 1 * CHARACTER_HEIGHT + 'px'); - terminal.buffer.lines.push(''); - viewport.syncScrollArea(); - assert.equal(viewportElement.style.height, 1 * CHARACTER_HEIGHT + 'px'); - assert.equal(scrollAreaElement.style.height, 2 * CHARACTER_HEIGHT + 'px'); - done(); - }, 0); - }); - }); -}); From 2114bc4837b6cf7dd8b362e5791fc4c0ed762008 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 11 Sep 2017 11:12:51 -0700 Subject: [PATCH 06/30] Ensure leave is called when leaving a MouseZone Fixes #975 --- src/input/MouseZoneManager.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/input/MouseZoneManager.ts b/src/input/MouseZoneManager.ts index d1bc8a75..d84b7218 100644 --- a/src/input/MouseZoneManager.ts +++ b/src/input/MouseZoneManager.ts @@ -83,10 +83,14 @@ export class MouseZoneManager implements IMouseZoneManager { return; } - // Fire the hover end callback if a zone was being hovered + // Fire the hover end callback and cancel any existing timer if a new zone + // is being hovered if (this._currentZone) { this._currentZone.leaveCallback(); this._currentZone = null; + if (this._tooltipTimeout) { + clearTimeout(this._tooltipTimeout); + } } // Exit if there is not zone @@ -100,14 +104,12 @@ export class MouseZoneManager implements IMouseZoneManager { zone.hoverCallback(e); } - // Restart the timeout - if (this._tooltipTimeout) { - clearTimeout(this._tooltipTimeout); - } + // Restart the tooltip timeout this._tooltipTimeout = setTimeout(() => this._onTooltip(e), HOVER_DURATION); } private _onTooltip(e: MouseEvent): void { + this._tooltipTimeout = null; const zone = this._findZoneEventAt(e); if (zone && zone.tooltipCallback) { zone.tooltipCallback(e); From 6899e01fc1e5c0b5639faefddbe6f1c63f5f1ede Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Tue, 12 Sep 2017 15:05:47 -0700 Subject: [PATCH 07/30] Improve scrolling while user has scrolled up Fixes #953 --- src/Terminal.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Terminal.ts b/src/Terminal.ts index e19887cb..f4d10dc4 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -1071,7 +1071,16 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT // Only adjust ybase and ydisp when the buffer is not trimmed if (!willBufferBeTrimmed) { this.buffer.ybase++; - this.buffer.ydisp++; + // Only scroll the ydisp with ybase if the user has not scrolled up + if (!this.userScrolling) { + this.buffer.ydisp++; + } + } else { + // When the buffer is full and the user has scrolled up, keep the text + // stable unless ydisp is right at the top + if (this.userScrolling) { + this.buffer.ydisp = Math.max(this.buffer.ydisp - 1, 0); + } } } else { // scrollTop is non-zero which means no line will be going to the From 403fd3cd4f205d74fcd51513433d9dbf8ba50921 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Tue, 12 Sep 2017 18:55:59 -0700 Subject: [PATCH 08/30] Draw text using decimal numbers Fixes #972 --- src/renderer/BaseRenderLayer.ts | 36 ++++++++++++++++++++++++++------- src/renderer/Renderer.ts | 17 +++++++++++----- src/utils/CharMeasure.ts | 2 +- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index 38457e13..3bc0a917 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -78,6 +78,16 @@ export abstract class BaseRenderLayer implements IRenderLayer { public abstract reset(terminal: ITerminal): void; + /** + * Gets the left position of a cell. Since character width is stored as a + * float in order to prevent bad letter spacing, drawing shapes in the cell + * need to be rounded. + * @param x The column of the cell. + */ + private _getCellLeft(x: number): number { + return Math.round(x * this.scaledCharWidth); + } + /** * Fills 1+ cells completely. This uses the existing fillStyle on the context. * @param x The column to start at. @@ -86,7 +96,12 @@ export abstract class BaseRenderLayer implements IRenderLayer { * @param height The number of rows to fill. */ protected fillCells(x: number, y: number, width: number, height: number): void { - this._ctx.fillRect(x * this.scaledCharWidth, y * this.scaledLineHeight, width * this.scaledCharWidth, height * this.scaledLineHeight); + const cellLeft = this._getCellLeft(x); + this._ctx.fillRect( + cellLeft, + y * this.scaledLineHeight, + this._getCellLeft(x + width) - cellLeft, + height * this.scaledLineHeight); } /** @@ -96,10 +111,11 @@ export abstract class BaseRenderLayer implements IRenderLayer { * @param y The row to fill. */ protected fillBottomLineAtCells(x: number, y: number, width: number = 1): void { + const cellLeft = this._getCellLeft(x); this._ctx.fillRect( - x * this.scaledCharWidth, + cellLeft, (y + 1) * this.scaledLineHeight - window.devicePixelRatio - 1 /* Ensure it's drawn within the cell */, - width * this.scaledCharWidth, + this._getCellLeft(x + width) - cellLeft, window.devicePixelRatio); } @@ -111,7 +127,7 @@ export abstract class BaseRenderLayer implements IRenderLayer { */ protected fillLeftLineAtCell(x: number, y: number): void { this._ctx.fillRect( - x * this.scaledCharWidth, + this._getCellLeft(x), y * this.scaledLineHeight, window.devicePixelRatio, this.scaledLineHeight); @@ -124,11 +140,12 @@ export abstract class BaseRenderLayer implements IRenderLayer { * @param y The row to fill. */ protected strokeRectAtCell(x: number, y: number, width: number, height: number): void { + const cellLeft = this._getCellLeft(x); this._ctx.lineWidth = window.devicePixelRatio; this._ctx.strokeRect( - x * this.scaledCharWidth + window.devicePixelRatio / 2, + cellLeft + window.devicePixelRatio / 2, y * this.scaledLineHeight + (window.devicePixelRatio / 2), - (width * this.scaledCharWidth) - window.devicePixelRatio, + this._getCellLeft(x + width) - cellLeft - window.devicePixelRatio, (height * this.scaledLineHeight) - window.devicePixelRatio); } @@ -147,7 +164,12 @@ export abstract class BaseRenderLayer implements IRenderLayer { * @param height The number of rows to clear. */ protected clearCells(x: number, y: number, width: number, height: number): void { - this._ctx.clearRect(x * this.scaledCharWidth, y * this.scaledLineHeight, width * this.scaledCharWidth, height * this.scaledLineHeight); + const cellLeft = this._getCellLeft(x); + this._ctx.clearRect( + cellLeft, + y * this.scaledLineHeight, + this._getCellLeft(x + width) - cellLeft, + height * this.scaledLineHeight); } /** diff --git a/src/renderer/Renderer.ts b/src/renderer/Renderer.ts index b610fd69..a2b8a7fe 100644 --- a/src/renderer/Renderer.ts +++ b/src/renderer/Renderer.ts @@ -77,10 +77,17 @@ export class Renderer extends EventEmitter implements IRenderer { return; } - // Calculate the scaled character dimensions, if devicePixelRatio is a - // floating point number then the value is ceiled to ensure there is enough - // space to draw the character to the cell - this.dimensions.scaledCharWidth = Math.ceil(this._terminal.charMeasure.width * window.devicePixelRatio); + // Calculate the scaled character width. Width is kept as a decimal to + // provide better letter spacing, otherwise the text can look odd. + // Characters drawn using this decimal number do have the potential to + // overlap, but only by a single pixel. As such, it's not a big deal when + // they do as that pixel is always cleared as necessary before drawing the + // character. + this.dimensions.scaledCharWidth = this._terminal.charMeasure.width * window.devicePixelRatio; + + // Calculate the scaled character height. Height is ceiled in case + // devicePixelRatio is a floating point number in order to ensure there is + // enough space to draw the character to the cell. this.dimensions.scaledCharHeight = Math.ceil(this._terminal.charMeasure.height * window.devicePixelRatio); // Calculate the scaled line height, if lineHeight is not 1 then the value @@ -96,7 +103,7 @@ export class Renderer extends EventEmitter implements IRenderer { // Recalculate the canvas dimensions; scaled* define the actual number of // pixel in the canvas this.dimensions.scaledCanvasHeight = this._terminal.rows * this.dimensions.scaledLineHeight; - this.dimensions.scaledCanvasWidth = this._terminal.cols * this.dimensions.scaledCharWidth; + this.dimensions.scaledCanvasWidth = Math.round(this._terminal.cols * this.dimensions.scaledCharWidth); // The the size of the canvas on the page. It's very important that this // rounds to nearest integer and not ceils as browsers often set diff --git a/src/utils/CharMeasure.ts b/src/utils/CharMeasure.ts index e8fcff86..e5b03f87 100644 --- a/src/utils/CharMeasure.ts +++ b/src/utils/CharMeasure.ts @@ -60,7 +60,7 @@ export class CharMeasure extends EventEmitter implements ICharMeasure { return; } if (this._width !== geometry.width || this._height !== geometry.height) { - this._width = Math.ceil(geometry.width); + this._width = geometry.width; this._height = Math.ceil(geometry.height); this.emit('charsizechanged'); } From cf324529175712873522b85ceb5834d5cd4d00d6 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Tue, 12 Sep 2017 19:23:26 -0700 Subject: [PATCH 09/30] Treat \u279C arrow as ambiguous width char (like emoji) Fixes #974 --- src/renderer/ForegroundRenderLayer.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/renderer/ForegroundRenderLayer.ts b/src/renderer/ForegroundRenderLayer.ts index 8e9e23fc..9334f109 100644 --- a/src/renderer/ForegroundRenderLayer.ts +++ b/src/renderer/ForegroundRenderLayer.ts @@ -159,6 +159,11 @@ export class ForegroundRenderLayer extends BaseRenderLayer { * @param char The character to search. */ private _isEmoji(char: string): boolean { + // Check special ambiguous width characters + if (char === '➜') { + return true; + } + // Check emoji unicode range return char.search(/([\uD800-\uDBFF][\uDC00-\uDFFF])/g) >= 0; } From 73d316442637c627e3cee4e54f48cdcef296f534 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Tue, 12 Sep 2017 20:00:25 -0700 Subject: [PATCH 10/30] Fix alt+selection while mouse events are enabled Fixes #970 --- src/SelectionManager.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index 29f12169..28ed1522 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -454,6 +454,11 @@ export class SelectionManager extends EventEmitter implements ISelectionManager * @param event The mousemove event. */ private _onMouseMove(event: MouseEvent): void { + // If the mousemove listener is active it means that a selection is + // currently being made, we should stop propogation to prevent mouse events + // to be sent to the pty. + event.stopImmediatePropagation(); + // Record the previous position so we know whether to redraw the selection // at the end. const previousSelectionEnd = this._model.selectionEnd ? [this._model.selectionEnd[0], this._model.selectionEnd[1]] : null; From 30cac210036278150ecf7d1a8a62e79737bdd73e Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 13 Sep 2017 10:34:58 -0700 Subject: [PATCH 11/30] Remove log --- src/CompositionHelper.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/CompositionHelper.ts b/src/CompositionHelper.ts index cec8fb37..ca5b4f4b 100644 --- a/src/CompositionHelper.ts +++ b/src/CompositionHelper.ts @@ -64,7 +64,6 @@ export class CompositionHelper { * @param {CompositionEvent} ev The event. */ public compositionupdate(ev: CompositionEvent): void { - console.log('compositionupdate'); this.compositionView.textContent = ev.data; this.updateCompositionElements(); setTimeout(() => { From ead45fe4d027eabc01e580006e4751d0b3ded091 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 13 Sep 2017 10:59:55 -0700 Subject: [PATCH 12/30] Add a TODO --- src/renderer/ForegroundRenderLayer.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/renderer/ForegroundRenderLayer.ts b/src/renderer/ForegroundRenderLayer.ts index 9334f109..19625466 100644 --- a/src/renderer/ForegroundRenderLayer.ts +++ b/src/renderer/ForegroundRenderLayer.ts @@ -159,6 +159,7 @@ export class ForegroundRenderLayer extends BaseRenderLayer { * @param char The character to search. */ private _isEmoji(char: string): boolean { + // TODO: We need a generic solution for handling characters like this // Check special ambiguous width characters if (char === '➜') { return true; From a52b9feeea7d3ba877b30f1aa1601ed41317d66c Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 13 Sep 2017 14:03:40 -0700 Subject: [PATCH 13/30] Ensure mouse coordinates are always within rows/cols Fixes #986 --- src/utils/Mouse.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/Mouse.ts b/src/utils/Mouse.ts index aef76d5b..7bc39b7f 100644 --- a/src/utils/Mouse.ts +++ b/src/utils/Mouse.ts @@ -53,8 +53,8 @@ export function getCoords(event: {pageX: number, pageY: number}, element: HTMLEl coords[1] = Math.ceil(coords[1] / Math.ceil(charMeasure.height * lineHeight)); // Ensure coordinates are within the terminal viewport. - coords[0] = Math.min(Math.max(coords[0], 1), colCount + 1); - coords[1] = Math.min(Math.max(coords[1], 1), rowCount + 1); + coords[0] = Math.min(Math.max(coords[0], 1), colCount); + coords[1] = Math.min(Math.max(coords[1], 1), rowCount); return coords; } From 5538481833fe7c0add2ded86eec757267f2bf8de Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 13 Sep 2017 15:00:36 -0700 Subject: [PATCH 14/30] Draw characters to a grid --- src/renderer/Renderer.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/renderer/Renderer.ts b/src/renderer/Renderer.ts index a2b8a7fe..63782afd 100644 --- a/src/renderer/Renderer.ts +++ b/src/renderer/Renderer.ts @@ -77,13 +77,11 @@ export class Renderer extends EventEmitter implements IRenderer { return; } - // Calculate the scaled character width. Width is kept as a decimal to - // provide better letter spacing, otherwise the text can look odd. - // Characters drawn using this decimal number do have the potential to - // overlap, but only by a single pixel. As such, it's not a big deal when - // they do as that pixel is always cleared as necessary before drawing the - // character. - this.dimensions.scaledCharWidth = this._terminal.charMeasure.width * window.devicePixelRatio; + // Calculate the scaled character width. Width is floored as it must be + // drawn to an integer grid in order for the CharAtlas "stamps" to not be + // blurry. When text is drawn to the grid not using the CharAtlas, it is + // clipped to ensure there is no overlap with the next cell. + this.dimensions.scaledCharWidth = Math.floor(this._terminal.charMeasure.width * window.devicePixelRatio); // Calculate the scaled character height. Height is ceiled in case // devicePixelRatio is a floating point number in order to ensure there is @@ -103,7 +101,7 @@ export class Renderer extends EventEmitter implements IRenderer { // Recalculate the canvas dimensions; scaled* define the actual number of // pixel in the canvas this.dimensions.scaledCanvasHeight = this._terminal.rows * this.dimensions.scaledLineHeight; - this.dimensions.scaledCanvasWidth = Math.round(this._terminal.cols * this.dimensions.scaledCharWidth); + this.dimensions.scaledCanvasWidth = this._terminal.cols * this.dimensions.scaledCharWidth; // The the size of the canvas on the page. It's very important that this // rounds to nearest integer and not ceils as browsers often set From 365661704424f9a33b3a2cb4b85ad144948a86a9 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 13 Sep 2017 15:21:40 -0700 Subject: [PATCH 15/30] Start merging fg/bg layers, draw using SP-AA --- src/renderer/BaseRenderLayer.ts | 3 +- src/renderer/CharAtlas.ts | 2 +- src/renderer/CursorRenderLayer.ts | 2 +- src/renderer/LinkRenderLayer.ts | 2 +- src/renderer/Renderer.ts | 10 +++--- src/renderer/SelectionRenderLayer.ts | 2 +- ...roundRenderLayer.ts => TextRenderLayer.ts} | 35 +++++++++++-------- 7 files changed, 30 insertions(+), 26 deletions(-) rename src/renderer/{ForegroundRenderLayer.ts => TextRenderLayer.ts} (86%) diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index 3bc0a917..22d42407 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -25,12 +25,13 @@ export abstract class BaseRenderLayer implements IRenderLayer { container: HTMLElement, id: string, zIndex: number, + alpha: boolean, protected colors: IColorSet ) { this._canvas = document.createElement('canvas'); this._canvas.id = `xterm-${id}-layer`; this._canvas.style.zIndex = zIndex.toString(); - this._ctx = this._canvas.getContext('2d'); + this._ctx = this._canvas.getContext('2d', {alpha}); this._ctx.scale(window.devicePixelRatio, window.devicePixelRatio); container.appendChild(this._canvas); } diff --git a/src/renderer/CharAtlas.ts b/src/renderer/CharAtlas.ts index bf080cce..45835a27 100644 --- a/src/renderer/CharAtlas.ts +++ b/src/renderer/CharAtlas.ts @@ -120,7 +120,7 @@ class CharAtlasGenerator { constructor(private _document: Document) { this._canvas = this._document.createElement('canvas'); - this._ctx = this._canvas.getContext('2d'); + this._ctx = this._canvas.getContext('2d', {alpha: false}); this._ctx.scale(window.devicePixelRatio, window.devicePixelRatio); } diff --git a/src/renderer/CursorRenderLayer.ts b/src/renderer/CursorRenderLayer.ts index 8e25d7c1..faed9bfe 100644 --- a/src/renderer/CursorRenderLayer.ts +++ b/src/renderer/CursorRenderLayer.ts @@ -31,7 +31,7 @@ export class CursorRenderLayer extends BaseRenderLayer { private _isFocused: boolean; constructor(container: HTMLElement, zIndex: number, colors: IColorSet) { - super(container, 'cursor', zIndex, colors); + super(container, 'cursor', zIndex, true, colors); this._state = { x: null, y: null, diff --git a/src/renderer/LinkRenderLayer.ts b/src/renderer/LinkRenderLayer.ts index ad178933..98084034 100644 --- a/src/renderer/LinkRenderLayer.ts +++ b/src/renderer/LinkRenderLayer.ts @@ -15,7 +15,7 @@ export class LinkRenderLayer extends BaseRenderLayer { private _state: LinkHoverEvent = null; constructor(container: HTMLElement, zIndex: number, colors: IColorSet, terminal: ILinkifierAccessor) { - super(container, 'link', zIndex, colors); + super(container, 'link', zIndex, true, colors); terminal.linkifier.on(LinkHoverEventTypes.HOVER, (e: LinkHoverEvent) => this._onLinkHover(e)); terminal.linkifier.on(LinkHoverEventTypes.LEAVE, (e: LinkHoverEvent) => this._onLinkLeave(e)); } diff --git a/src/renderer/Renderer.ts b/src/renderer/Renderer.ts index 63782afd..8e8934d6 100644 --- a/src/renderer/Renderer.ts +++ b/src/renderer/Renderer.ts @@ -5,8 +5,7 @@ import { ITerminal, ITheme } from '../Interfaces'; import { CHAR_DATA_WIDTH_INDEX, CHAR_DATA_CHAR_INDEX } from '../Buffer'; -import { BackgroundRenderLayer } from './BackgroundRenderLayer'; -import { ForegroundRenderLayer } from './ForegroundRenderLayer'; +import { TextRenderLayer } from './TextRenderLayer'; import { SelectionRenderLayer } from './SelectionRenderLayer'; import { CursorRenderLayer } from './CursorRenderLayer'; import { ColorManager } from './ColorManager'; @@ -30,11 +29,10 @@ export class Renderer extends EventEmitter implements IRenderer { super(); this._colorManager = new ColorManager(); this._renderLayers = [ - new BackgroundRenderLayer(this._terminal.element, 0, this._colorManager.colors), + new TextRenderLayer(this._terminal.element, 0, this._colorManager.colors), new SelectionRenderLayer(this._terminal.element, 1, this._colorManager.colors), - new ForegroundRenderLayer(this._terminal.element, 2, this._colorManager.colors), - new LinkRenderLayer(this._terminal.element, 3, this._colorManager.colors, this._terminal), - new CursorRenderLayer(this._terminal.element, 4, this._colorManager.colors) + new LinkRenderLayer(this._terminal.element, 2, this._colorManager.colors, this._terminal), + new CursorRenderLayer(this._terminal.element, 3, this._colorManager.colors) ]; this.dimensions = { scaledCharWidth: null, diff --git a/src/renderer/SelectionRenderLayer.ts b/src/renderer/SelectionRenderLayer.ts index 5339c7a4..7a0aff83 100644 --- a/src/renderer/SelectionRenderLayer.ts +++ b/src/renderer/SelectionRenderLayer.ts @@ -14,7 +14,7 @@ export class SelectionRenderLayer extends BaseRenderLayer { private _state: {start: [number, number], end: [number, number]}; constructor(container: HTMLElement, zIndex: number, colors: IColorSet) { - super(container, 'selection', zIndex, colors); + super(container, 'selection', zIndex, true, colors); this._state = { start: null, end: null diff --git a/src/renderer/ForegroundRenderLayer.ts b/src/renderer/TextRenderLayer.ts similarity index 86% rename from src/renderer/ForegroundRenderLayer.ts rename to src/renderer/TextRenderLayer.ts index 19625466..4b06ef42 100644 --- a/src/renderer/ForegroundRenderLayer.ts +++ b/src/renderer/TextRenderLayer.ts @@ -18,29 +18,34 @@ import { BaseRenderLayer, INVERTED_DEFAULT_COLOR } from './BaseRenderLayer'; */ const EMOJI_OWNED_CHAR_DATA: CharData = [null, '', 0, -1]; -export class ForegroundRenderLayer extends BaseRenderLayer { - private _state: GridCache; +export class TextRenderLayer extends BaseRenderLayer { + private _fgState: GridCache; + private _bgState: GridCache; constructor(container: HTMLElement, zIndex: number, colors: IColorSet) { - super(container, 'fg', zIndex, colors); - this._state = new GridCache(); + super(container, 'text', zIndex, false, colors); + this._fgState = new GridCache(); + this._bgState = new GridCache(); } public resize(terminal: ITerminal, dim: IRenderDimensions, charSizeChanged: boolean): void { super.resize(terminal, dim, charSizeChanged); // Resizing the canvas discards the contents of the canvas so clear state - this._state.clear(); - this._state.resize(terminal.cols, terminal.rows); + this._fgState.clear(); + this._bgState.clear(); + this._fgState.resize(terminal.cols, terminal.rows); + this._bgState.resize(terminal.cols, terminal.rows); } public reset(terminal: ITerminal): void { - this._state.clear(); + this._fgState.clear(); + this._bgState.clear(); this.clearAll(); } public onGridChanged(terminal: ITerminal, startRow: number, endRow: number): void { // Resize has not been called yet - if (this._state.cache.length === 0) { + if (this._fgState.cache.length === 0) { return; } @@ -58,7 +63,7 @@ export class ForegroundRenderLayer extends BaseRenderLayer { // The character to the left is a wide character, drawing is owned by // the char at x-1 if (width === 0) { - this._state.cache[x][y] = null; + this._fgState.cache[x][y] = null; continue; } @@ -75,10 +80,10 @@ export class ForegroundRenderLayer extends BaseRenderLayer { } // Skip rendering if the character is identical - const state = this._state.cache[x][y]; + const state = this._fgState.cache[x][y]; if (state && state[CHAR_DATA_CHAR_INDEX] === char && state[CHAR_DATA_ATTR_INDEX] === attr) { // Skip render, contents are identical - this._state.cache[x][y] = charData; + this._fgState.cache[x][y] = charData; continue; } @@ -86,7 +91,7 @@ export class ForegroundRenderLayer extends BaseRenderLayer { if (state && state[CHAR_DATA_CODE_INDEX] !== 32 /*' '*/) { this._clearChar(x, y); } - this._state.cache[x][y] = charData; + this._fgState.cache[x][y] = charData; const flags = attr >> 18; @@ -104,14 +109,14 @@ export class ForegroundRenderLayer extends BaseRenderLayer { // space is added. Without this, the first half of `b` would never // get removed, and `a` would not re-render because it thinks it's // already in the correct state. - this._state.cache[x][y] = EMOJI_OWNED_CHAR_DATA; + this._fgState.cache[x][y] = EMOJI_OWNED_CHAR_DATA; if (x < line.length && line[x + 1][CHAR_DATA_CODE_INDEX] === 32 /*' '*/) { width = 2; this._clearChar(x + 1, y); // The emoji owned char data will force a clear and render when the // emoji is no longer to the left of the character and also when the // space changes to another character. - this._state.cache[x + 1][y] = EMOJI_OWNED_CHAR_DATA; + this._fgState.cache[x + 1][y] = EMOJI_OWNED_CHAR_DATA; } } @@ -176,7 +181,7 @@ export class ForegroundRenderLayer extends BaseRenderLayer { private _clearChar(x: number, y: number): void { let colsToClear = 1; // Clear the adjacent character if it was wide - const state = this._state.cache[x][y]; + const state = this._fgState.cache[x][y]; if (state && state[CHAR_DATA_WIDTH_INDEX] === 2) { colsToClear = 2; } From 402cebc76ce45f1784a57521530c110abfeaa0ff Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 13 Sep 2017 22:50:48 -0700 Subject: [PATCH 16/30] Support bold in non cached text drawing Fixes #987 --- src/renderer/BaseRenderLayer.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index 22d42407..3f5b2bb6 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -235,7 +235,7 @@ export abstract class BaseRenderLayer implements IRenderLayer { code * charAtlasCellWidth, colorIndex * charAtlasCellHeight, this.scaledCharWidth, this.scaledCharHeight, x * this.scaledCharWidth, y * this.scaledLineHeight + this.scaledLineDrawY, this.scaledCharWidth, this.scaledCharHeight); } else { - this._drawUncachedChar(terminal, char, width, fg, x, y); + this._drawUncachedChar(terminal, char, width, fg, x, y, bold); } // This draws the atlas (for debugging purposes) // this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height); @@ -253,9 +253,12 @@ export abstract class BaseRenderLayer implements IRenderLayer { * @param x The column to draw at. * @param y The row to draw at. */ - private _drawUncachedChar(terminal: ITerminal, char: string, width: number, fg: number, x: number, y: number): void { + private _drawUncachedChar(terminal: ITerminal, char: string, width: number, fg: number, x: number, y: number, bold: boolean): void { this._ctx.save(); this._ctx.font = `${terminal.options.fontSize * window.devicePixelRatio}px ${terminal.options.fontFamily}`; + if (bold) { + this._ctx.font = `bold ${this._ctx.font}`; + } this._ctx.textBaseline = 'top'; if (fg === INVERTED_DEFAULT_COLOR) { From 872c007a295a92add19f2b9f7ed5510e8a7fffe8 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 13 Sep 2017 23:15:21 -0700 Subject: [PATCH 17/30] Finish merging bg and fg layers --- src/renderer/BackgroundRenderLayer.ts | 71 --------------------------- src/renderer/BaseRenderLayer.ts | 7 ++- src/renderer/TextRenderLayer.ts | 58 +++++++++++++--------- 3 files changed, 39 insertions(+), 97 deletions(-) delete mode 100644 src/renderer/BackgroundRenderLayer.ts diff --git a/src/renderer/BackgroundRenderLayer.ts b/src/renderer/BackgroundRenderLayer.ts deleted file mode 100644 index 279aee6d..00000000 --- a/src/renderer/BackgroundRenderLayer.ts +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright (c) 2017 The xterm.js authors. All rights reserved. - * @license MIT - */ - -import { IColorSet, IRenderDimensions } from './Interfaces'; -import { IBuffer, ICharMeasure, ITerminal } from '../Interfaces'; -import { CHAR_DATA_ATTR_INDEX } from '../Buffer'; -import { GridCache } from './GridCache'; -import { FLAGS } from './Types'; -import { BaseRenderLayer, INVERTED_DEFAULT_COLOR } from './BaseRenderLayer'; - -export class BackgroundRenderLayer extends BaseRenderLayer { - private _state: GridCache; - - constructor(container: HTMLElement, zIndex: number, colors: IColorSet) { - super(container, 'bg', zIndex, colors); - this._state = new GridCache(); - } - - public resize(terminal: ITerminal, dim: IRenderDimensions, charSizeChanged: boolean): void { - super.resize(terminal, dim, charSizeChanged); - // Resizing the canvas discards the contents of the canvas so clear state - this._state.clear(); - this._state.resize(terminal.cols, terminal.rows); - } - - public reset(terminal: ITerminal): void { - this._state.clear(); - this.clearAll(); - } - - public onGridChanged(terminal: ITerminal, startRow: number, endRow: number): void { - // Resize has not been called yet - if (this._state.cache.length === 0) { - return; - } - for (let y = startRow; y <= endRow; y++) { - let row = y + terminal.buffer.ydisp; - let line = terminal.buffer.lines.get(row); - for (let x = 0; x < terminal.cols; x++) { - const attr: number = line[x][CHAR_DATA_ATTR_INDEX]; - let bg = attr & 0x1ff; - const flags = attr >> 18; - - // If inverse flag is on, the background should become the foreground. - if (flags & FLAGS.INVERSE) { - bg = (attr >> 9) & 0x1ff; - if (bg === 257) { - bg = INVERTED_DEFAULT_COLOR; - } - } - - const cellState = this._state.cache[x][y]; - const needsRefresh = (bg < 256 && cellState !== bg) || cellState !== null; - if (needsRefresh) { - if (bg < 256) { - this._ctx.save(); - this._ctx.fillStyle = (bg === INVERTED_DEFAULT_COLOR ? this.colors.foreground : this.colors.ansi[bg]); - this.fillCells(x, y, 1, 1); - this._ctx.restore(); - this._state.cache[x][y] = bg; - } else { - this.clearCells(x, y, 1, 1); - this._state.cache[x][y] = null; - } - } - } - } - } -} diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index 3f5b2bb6..38e7b9d4 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -207,9 +207,11 @@ export abstract class BaseRenderLayer implements IRenderLayer { * @param x The column to draw at. * @param y The row to draw at. * @param fg The foreground color, in the format stored within the attributes. + * @param bg The background color, in the format stored within the attributes. + * This is used to validate whether a cached image can be used. * @param bold Whether the text is bold. */ - protected drawChar(terminal: ITerminal, char: string, code: number, width: number, x: number, y: number, fg: number, bold: boolean): void { + protected drawChar(terminal: ITerminal, char: string, code: number, width: number, x: number, y: number, fg: number, bg: number, bold: boolean): void { // Clear the cell next to this character if it's wide if (width === 2) { this.clearCells(x + 1, y, 1, 1); @@ -227,7 +229,8 @@ export abstract class BaseRenderLayer implements IRenderLayer { const isAscii = code < 256; const isBasicColor = (colorIndex > 1 && fg < 16); const isDefaultColor = fg >= 256; - if (isAscii && (isBasicColor || isDefaultColor)) { + const isDefaultBackground = bg >= 256; + if (isAscii && (isBasicColor || isDefaultColor) && isDefaultBackground) { // ImageBitmap's draw about twice as fast as from a canvas const charAtlasCellWidth = this.scaledCharWidth + CHAR_ATLAS_CELL_SPACING; const charAtlasCellHeight = this.scaledCharHeight + CHAR_ATLAS_CELL_SPACING; diff --git a/src/renderer/TextRenderLayer.ts b/src/renderer/TextRenderLayer.ts index 4b06ef42..41fcdeee 100644 --- a/src/renderer/TextRenderLayer.ts +++ b/src/renderer/TextRenderLayer.ts @@ -19,33 +19,28 @@ import { BaseRenderLayer, INVERTED_DEFAULT_COLOR } from './BaseRenderLayer'; const EMOJI_OWNED_CHAR_DATA: CharData = [null, '', 0, -1]; export class TextRenderLayer extends BaseRenderLayer { - private _fgState: GridCache; - private _bgState: GridCache; + private _state: GridCache; constructor(container: HTMLElement, zIndex: number, colors: IColorSet) { super(container, 'text', zIndex, false, colors); - this._fgState = new GridCache(); - this._bgState = new GridCache(); + this._state = new GridCache(); } public resize(terminal: ITerminal, dim: IRenderDimensions, charSizeChanged: boolean): void { super.resize(terminal, dim, charSizeChanged); // Resizing the canvas discards the contents of the canvas so clear state - this._fgState.clear(); - this._bgState.clear(); - this._fgState.resize(terminal.cols, terminal.rows); - this._bgState.resize(terminal.cols, terminal.rows); + this._state.clear(); + this._state.resize(terminal.cols, terminal.rows); } public reset(terminal: ITerminal): void { - this._fgState.clear(); - this._bgState.clear(); + this._state.clear(); this.clearAll(); } public onGridChanged(terminal: ITerminal, startRow: number, endRow: number): void { // Resize has not been called yet - if (this._fgState.cache.length === 0) { + if (this._state.cache.length === 0) { return; } @@ -63,7 +58,7 @@ export class TextRenderLayer extends BaseRenderLayer { // The character to the left is a wide character, drawing is owned by // the char at x-1 if (width === 0) { - this._fgState.cache[x][y] = null; + this._state.cache[x][y] = null; continue; } @@ -80,23 +75,26 @@ export class TextRenderLayer extends BaseRenderLayer { } // Skip rendering if the character is identical - const state = this._fgState.cache[x][y]; + const state = this._state.cache[x][y]; if (state && state[CHAR_DATA_CHAR_INDEX] === char && state[CHAR_DATA_ATTR_INDEX] === attr) { // Skip render, contents are identical - this._fgState.cache[x][y] = charData; + this._state.cache[x][y] = charData; continue; } - // Clear the old character if present - if (state && state[CHAR_DATA_CODE_INDEX] !== 32 /*' '*/) { + // Clear the old character was not a space with the default background + if (state && !(state[CHAR_DATA_CODE_INDEX] === 32 /*' '*/ && (state[CHAR_DATA_ATTR_INDEX] & 0x1ff) >= 256)) { this._clearChar(x, y); } - this._fgState.cache[x][y] = charData; + this._state.cache[x][y] = charData; const flags = attr >> 18; + let bg = attr & 0x1ff; // Skip rendering if the character is invisible - if (!code || code === 32 /*' '*/ || (flags & FLAGS.INVISIBLE)) { + const isDefaultBackground = bg >= 256; + const isInvisible = flags & FLAGS.INVISIBLE; + if (!code || (code === 32 /*' '*/ && isDefaultBackground) || isInvisible) { continue; } @@ -109,14 +107,14 @@ export class TextRenderLayer extends BaseRenderLayer { // space is added. Without this, the first half of `b` would never // get removed, and `a` would not re-render because it thinks it's // already in the correct state. - this._fgState.cache[x][y] = EMOJI_OWNED_CHAR_DATA; + this._state.cache[x][y] = EMOJI_OWNED_CHAR_DATA; if (x < line.length && line[x + 1][CHAR_DATA_CODE_INDEX] === 32 /*' '*/) { width = 2; this._clearChar(x + 1, y); // The emoji owned char data will force a clear and render when the // emoji is no longer to the left of the character and also when the // space changes to another character. - this._fgState.cache[x + 1][y] = EMOJI_OWNED_CHAR_DATA; + this._state.cache[x + 1][y] = EMOJI_OWNED_CHAR_DATA; } } @@ -124,11 +122,23 @@ export class TextRenderLayer extends BaseRenderLayer { // If inverse flag is on, the foreground should become the background. if (flags & FLAGS.INVERSE) { - fg = attr & 0x1ff; - // TODO: Is this case still needed + const temp = bg; + bg = fg; + fg = bg; if (fg === 256) { fg = INVERTED_DEFAULT_COLOR; } + if (bg === 257) { + bg = INVERTED_DEFAULT_COLOR; + } + } + + // Draw background + if (bg < 256) { + this._ctx.save(); + this._ctx.fillStyle = (bg === INVERTED_DEFAULT_COLOR ? this.colors.foreground : this.colors.ansi[bg]); + this.fillCells(x, y, width, 1); + this._ctx.restore(); } this._ctx.save(); @@ -152,7 +162,7 @@ export class TextRenderLayer extends BaseRenderLayer { this.fillBottomLineAtCells(x, y); } - this.drawChar(terminal, char, code, width, x, y, fg, !!(flags & FLAGS.BOLD)); + this.drawChar(terminal, char, code, width, x, y, fg, bg, !!(flags & FLAGS.BOLD)); this._ctx.restore(); } @@ -181,7 +191,7 @@ export class TextRenderLayer extends BaseRenderLayer { private _clearChar(x: number, y: number): void { let colsToClear = 1; // Clear the adjacent character if it was wide - const state = this._fgState.cache[x][y]; + const state = this._state.cache[x][y]; if (state && state[CHAR_DATA_WIDTH_INDEX] === 2) { colsToClear = 2; } From b225866c270a554781ac2116844fc8dbb5acfce9 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 13 Sep 2017 23:21:24 -0700 Subject: [PATCH 18/30] Clear cells properly for text layer --- src/renderer/BaseRenderLayer.ts | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index 38e7b9d4..88dfc26c 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -25,7 +25,7 @@ export abstract class BaseRenderLayer implements IRenderLayer { container: HTMLElement, id: string, zIndex: number, - alpha: boolean, + private alpha: boolean, protected colors: IColorSet ) { this._canvas = document.createElement('canvas'); @@ -154,7 +154,12 @@ export abstract class BaseRenderLayer implements IRenderLayer { * Clears the entire canvas. */ protected clearAll(): void { - this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height); + if (this.alpha) { + this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height); + } else { + this._ctx.fillStyle = this.colors.background; + this._ctx.fillRect(0, 0, this._canvas.width, this._canvas.height); + } } /** @@ -166,11 +171,20 @@ export abstract class BaseRenderLayer implements IRenderLayer { */ protected clearCells(x: number, y: number, width: number, height: number): void { const cellLeft = this._getCellLeft(x); - this._ctx.clearRect( - cellLeft, - y * this.scaledLineHeight, - this._getCellLeft(x + width) - cellLeft, - height * this.scaledLineHeight); + if (this.alpha) { + this._ctx.clearRect( + cellLeft, + y * this.scaledLineHeight, + this._getCellLeft(x + width) - cellLeft, + height * this.scaledLineHeight); + } else { + this._ctx.fillStyle = this.colors.background; + this._ctx.fillRect( + cellLeft, + y * this.scaledLineHeight, + this._getCellLeft(x + width) - cellLeft, + height * this.scaledLineHeight); + } } /** From 0a58628f74b57953d7fec4feef55031f7acb5a5a Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 13 Sep 2017 23:49:34 -0700 Subject: [PATCH 19/30] Clear text layer to the background color on creation Prevents a black flash before the first draw --- src/renderer/BaseRenderLayer.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index 88dfc26c..d06e0ee7 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -33,6 +33,10 @@ export abstract class BaseRenderLayer implements IRenderLayer { this._canvas.style.zIndex = zIndex.toString(); this._ctx = this._canvas.getContext('2d', {alpha}); this._ctx.scale(window.devicePixelRatio, window.devicePixelRatio); + // Draw the background if this is an opaque layer + if (!alpha) { + this.clearAll(); + } container.appendChild(this._canvas); } From 068c0743a1507f64f55a81dcf46e4aa3980882e4 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 13 Sep 2017 23:54:45 -0700 Subject: [PATCH 20/30] Support non-#000 background colors in char atlas --- src/renderer/CharAtlas.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/renderer/CharAtlas.ts b/src/renderer/CharAtlas.ts index 45835a27..34404e12 100644 --- a/src/renderer/CharAtlas.ts +++ b/src/renderer/CharAtlas.ts @@ -64,7 +64,7 @@ export function acquireCharAtlas(terminal: ITerminal, colors: IColorSet, scaledC } const newEntry: ICharAtlasCacheEntry = { - bitmap: generator.generate(scaledCharWidth, scaledCharHeight, terminal.options.fontSize, terminal.options.fontFamily, colors.foreground, colors.ansi), + bitmap: generator.generate(scaledCharWidth, scaledCharHeight, terminal.options.fontSize, terminal.options.fontFamily, colors.background, colors.foreground, colors.ansi), config: newConfig, ownedBy: [terminal] }; @@ -75,7 +75,7 @@ export function acquireCharAtlas(terminal: ITerminal, colors: IColorSet, scaledC function generateConfig(scaledCharWidth: number, scaledCharHeight: number, terminal: ITerminal, colors: IColorSet): ICharAtlasConfig { const clonedColors = { foreground: colors.foreground, - background: null, + background: colors.background, cursor: null, selection: null, ansi: colors.ansi.slice(0, 16) @@ -99,7 +99,8 @@ function configEquals(a: ICharAtlasConfig, b: ICharAtlasConfig): boolean { a.fontSize === b.fontSize && a.scaledCharWidth === b.scaledCharWidth && a.scaledCharHeight === b.scaledCharHeight && - a.colors.foreground === b.colors.foreground; + a.colors.foreground === b.colors.foreground && + a.colors.background === b.colors.background; } let generator: CharAtlasGenerator; @@ -124,12 +125,15 @@ class CharAtlasGenerator { this._ctx.scale(window.devicePixelRatio, window.devicePixelRatio); } - public generate(scaledCharWidth: number, scaledCharHeight: number, fontSize: number, fontFamily: string, foreground: string, ansiColors: string[]): HTMLCanvasElement | Promise { + public generate(scaledCharWidth: number, scaledCharHeight: number, fontSize: number, fontFamily: string, background: string, foreground: string, ansiColors: string[]): HTMLCanvasElement | Promise { const cellWidth = scaledCharWidth + CHAR_ATLAS_CELL_SPACING; const cellHeight = scaledCharHeight + CHAR_ATLAS_CELL_SPACING; this._canvas.width = 255 * cellWidth; this._canvas.height = (/*default+default bold*/2 + /*0-15*/16) * cellHeight; + this._ctx.fillStyle = background; + this._ctx.fillRect(0, 0, this._canvas.width, this._canvas.height); + this._ctx.save(); this._ctx.fillStyle = foreground; this._ctx.font = `${fontSize * window.devicePixelRatio}px ${fontFamily}`; From c2b765f3ffc39f11a1663abfd6c9ce67c2544d17 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 13 Sep 2017 23:56:35 -0700 Subject: [PATCH 21/30] Redraw background on text layer after a resize --- src/renderer/BaseRenderLayer.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index d06e0ee7..3f996545 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -76,6 +76,11 @@ export abstract class BaseRenderLayer implements IRenderLayer { this._canvas.style.width = `${dim.canvasWidth}px`; this._canvas.style.height = `${dim.canvasHeight}px`; + // Draw the background if this is an opaque layer + if (!this.alpha) { + this.clearAll(); + } + if (charSizeChanged) { this._refreshCharAtlas(terminal, this.colors); } From aca7ab63e87899b1e8160f047b49812756eb3574 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 14 Sep 2017 00:22:57 -0700 Subject: [PATCH 22/30] Set background color on Renderer immediately Prevents black flash on init --- src/Terminal.ts | 14 ++--- src/renderer/BaseRenderLayer.ts | 86 ++++++++++++++-------------- src/renderer/ColorManager.ts | 4 +- src/renderer/CursorRenderLayer.ts | 12 ++-- src/renderer/Interfaces.ts | 4 ++ src/renderer/LinkRenderLayer.ts | 2 +- src/renderer/Renderer.ts | 23 ++++---- src/renderer/SelectionRenderLayer.ts | 2 +- src/renderer/TextRenderLayer.ts | 8 +-- 9 files changed, 79 insertions(+), 76 deletions(-) diff --git a/src/Terminal.ts b/src/Terminal.ts index ef4b21eb..44320357 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -622,8 +622,11 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT this.charMeasure = new CharMeasure(document, this.helperContainer); + this.renderer = new Renderer(this, this.options.theme); + this.options.theme = null; this.viewport = new Viewport(this, this.viewportElement, this.viewportScrollArea, this.charMeasure); - this.renderer = new Renderer(this); + this.viewport.onThemeChanged(this.renderer.colorManager.colors); + this.on('cursormove', () => this.renderer.onCursorMove()); this.on('resize', () => this.renderer.onResize(this.cols, this.rows, false)); this.on('blur', () => this.renderer.onBlur()); @@ -652,15 +655,6 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT // Measure the character size this.charMeasure.measure(this.options); - // Set the theme if it was set via setOption/constructor before open. This - // must be run after CharMeasure.measure as it depends on char dimensions. - setTimeout(() => { - if (this.options.theme) { - this._setTheme(this.options.theme); - this.options.theme = null; - } - }, 0); - // Setup loop that draws to screen this.refresh(0, this.rows - 1); diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index 3f996545..b55b5aaa 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -14,10 +14,10 @@ export const INVERTED_DEFAULT_COLOR = -1; export abstract class BaseRenderLayer implements IRenderLayer { private _canvas: HTMLCanvasElement; protected _ctx: CanvasRenderingContext2D; - private scaledCharWidth: number; - private scaledCharHeight: number; - private scaledLineHeight: number; - private scaledLineDrawY: number; + private _scaledCharWidth: number; + private _scaledCharHeight: number; + private _scaledLineHeight: number; + private _scaledLineDrawY: number; private _charAtlas: HTMLCanvasElement | ImageBitmap; @@ -25,16 +25,17 @@ export abstract class BaseRenderLayer implements IRenderLayer { container: HTMLElement, id: string, zIndex: number, - private alpha: boolean, - protected colors: IColorSet + private _alpha: boolean, + protected _colors: IColorSet ) { this._canvas = document.createElement('canvas'); this._canvas.id = `xterm-${id}-layer`; this._canvas.style.zIndex = zIndex.toString(); - this._ctx = this._canvas.getContext('2d', {alpha}); + this._ctx = this._canvas.getContext('2d', {_alpha}); this._ctx.scale(window.devicePixelRatio, window.devicePixelRatio); // Draw the background if this is an opaque layer - if (!alpha) { + if (!_alpha) { + console.log('clearAll!'); this.clearAll(); } container.appendChild(this._canvas); @@ -58,7 +59,7 @@ export abstract class BaseRenderLayer implements IRenderLayer { */ private _refreshCharAtlas(terminal: ITerminal, colorSet: IColorSet): void { this._charAtlas = null; - const result = acquireCharAtlas(terminal, this.colors, this.scaledCharWidth, this.scaledCharHeight); + const result = acquireCharAtlas(terminal, this._colors, this._scaledCharWidth, this._scaledCharHeight); if (result instanceof HTMLCanvasElement) { this._charAtlas = result; } else { @@ -67,22 +68,22 @@ export abstract class BaseRenderLayer implements IRenderLayer { } public resize(terminal: ITerminal, dim: IRenderDimensions, charSizeChanged: boolean): void { - this.scaledCharWidth = dim.scaledCharWidth; - this.scaledCharHeight = dim.scaledCharHeight; - this.scaledLineHeight = dim.scaledLineHeight; - this.scaledLineDrawY = dim.scaledLineDrawY; + this._scaledCharWidth = dim.scaledCharWidth; + this._scaledCharHeight = dim.scaledCharHeight; + this._scaledLineHeight = dim.scaledLineHeight; + this._scaledLineDrawY = dim.scaledLineDrawY; this._canvas.width = dim.scaledCanvasWidth; this._canvas.height = dim.scaledCanvasHeight; this._canvas.style.width = `${dim.canvasWidth}px`; this._canvas.style.height = `${dim.canvasHeight}px`; // Draw the background if this is an opaque layer - if (!this.alpha) { + if (!this._alpha) { this.clearAll(); } if (charSizeChanged) { - this._refreshCharAtlas(terminal, this.colors); + this._refreshCharAtlas(terminal, this._colors); } } @@ -95,7 +96,7 @@ export abstract class BaseRenderLayer implements IRenderLayer { * @param x The column of the cell. */ private _getCellLeft(x: number): number { - return Math.round(x * this.scaledCharWidth); + return Math.round(x * this._scaledCharWidth); } /** @@ -109,9 +110,9 @@ export abstract class BaseRenderLayer implements IRenderLayer { const cellLeft = this._getCellLeft(x); this._ctx.fillRect( cellLeft, - y * this.scaledLineHeight, + y * this._scaledLineHeight, this._getCellLeft(x + width) - cellLeft, - height * this.scaledLineHeight); + height * this._scaledLineHeight); } /** @@ -124,7 +125,7 @@ export abstract class BaseRenderLayer implements IRenderLayer { const cellLeft = this._getCellLeft(x); this._ctx.fillRect( cellLeft, - (y + 1) * this.scaledLineHeight - window.devicePixelRatio - 1 /* Ensure it's drawn within the cell */, + (y + 1) * this._scaledLineHeight - window.devicePixelRatio - 1 /* Ensure it's drawn within the cell */, this._getCellLeft(x + width) - cellLeft, window.devicePixelRatio); } @@ -138,9 +139,9 @@ export abstract class BaseRenderLayer implements IRenderLayer { protected fillLeftLineAtCell(x: number, y: number): void { this._ctx.fillRect( this._getCellLeft(x), - y * this.scaledLineHeight, + y * this._scaledLineHeight, window.devicePixelRatio, - this.scaledLineHeight); + this._scaledLineHeight); } /** @@ -154,19 +155,20 @@ export abstract class BaseRenderLayer implements IRenderLayer { this._ctx.lineWidth = window.devicePixelRatio; this._ctx.strokeRect( cellLeft + window.devicePixelRatio / 2, - y * this.scaledLineHeight + (window.devicePixelRatio / 2), + y * this._scaledLineHeight + (window.devicePixelRatio / 2), this._getCellLeft(x + width) - cellLeft - window.devicePixelRatio, - (height * this.scaledLineHeight) - window.devicePixelRatio); + (height * this._scaledLineHeight) - window.devicePixelRatio); } /** * Clears the entire canvas. */ protected clearAll(): void { - if (this.alpha) { + if (this._alpha) { this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height); } else { - this._ctx.fillStyle = this.colors.background; + console.log('fill with', this._colors.background); + this._ctx.fillStyle = this._colors.background; this._ctx.fillRect(0, 0, this._canvas.width, this._canvas.height); } } @@ -180,19 +182,19 @@ export abstract class BaseRenderLayer implements IRenderLayer { */ protected clearCells(x: number, y: number, width: number, height: number): void { const cellLeft = this._getCellLeft(x); - if (this.alpha) { + if (this._alpha) { this._ctx.clearRect( cellLeft, - y * this.scaledLineHeight, + y * this._scaledLineHeight, this._getCellLeft(x + width) - cellLeft, - height * this.scaledLineHeight); + height * this._scaledLineHeight); } else { - this._ctx.fillStyle = this.colors.background; + this._ctx.fillStyle = this._colors.background; this._ctx.fillRect( cellLeft, - y * this.scaledLineHeight, + y * this._scaledLineHeight, this._getCellLeft(x + width) - cellLeft, - height * this.scaledLineHeight); + height * this._scaledLineHeight); } } @@ -215,9 +217,9 @@ export abstract class BaseRenderLayer implements IRenderLayer { // can bleed into other cells. This code will clip the following fillText, // ensuring that its contents don't go beyond the cell bounds. this._ctx.beginPath(); - this._ctx.rect(x * this.scaledCharWidth, y * this.scaledLineHeight + this.scaledLineDrawY, charData[CHAR_DATA_WIDTH_INDEX] * this.scaledCharWidth, this.scaledCharHeight); + this._ctx.rect(x * this._scaledCharWidth, y * this._scaledLineHeight + this._scaledLineDrawY, charData[CHAR_DATA_WIDTH_INDEX] * this._scaledCharWidth, this._scaledCharHeight); this._ctx.clip(); - this._ctx.fillText(charData[CHAR_DATA_CHAR_INDEX], x * this.scaledCharWidth, y * this.scaledCharHeight); + this._ctx.fillText(charData[CHAR_DATA_CHAR_INDEX], x * this._scaledCharWidth, y * this._scaledCharHeight); } /** @@ -255,11 +257,11 @@ export abstract class BaseRenderLayer implements IRenderLayer { const isDefaultBackground = bg >= 256; if (isAscii && (isBasicColor || isDefaultColor) && isDefaultBackground) { // ImageBitmap's draw about twice as fast as from a canvas - const charAtlasCellWidth = this.scaledCharWidth + CHAR_ATLAS_CELL_SPACING; - const charAtlasCellHeight = this.scaledCharHeight + CHAR_ATLAS_CELL_SPACING; + const charAtlasCellWidth = this._scaledCharWidth + CHAR_ATLAS_CELL_SPACING; + const charAtlasCellHeight = this._scaledCharHeight + CHAR_ATLAS_CELL_SPACING; this._ctx.drawImage(this._charAtlas, - code * charAtlasCellWidth, colorIndex * charAtlasCellHeight, this.scaledCharWidth, this.scaledCharHeight, - x * this.scaledCharWidth, y * this.scaledLineHeight + this.scaledLineDrawY, this.scaledCharWidth, this.scaledCharHeight); + code * charAtlasCellWidth, colorIndex * charAtlasCellHeight, this._scaledCharWidth, this._scaledCharHeight, + x * this._scaledCharWidth, y * this._scaledLineHeight + this._scaledLineDrawY, this._scaledCharWidth, this._scaledCharHeight); } else { this._drawUncachedChar(terminal, char, width, fg, x, y, bold); } @@ -288,12 +290,12 @@ export abstract class BaseRenderLayer implements IRenderLayer { this._ctx.textBaseline = 'top'; if (fg === INVERTED_DEFAULT_COLOR) { - this._ctx.fillStyle = this.colors.background; + this._ctx.fillStyle = this._colors.background; } else if (fg < 256) { // 256 color support - this._ctx.fillStyle = this.colors.ansi[fg]; + this._ctx.fillStyle = this._colors.ansi[fg]; } else { - this._ctx.fillStyle = this.colors.foreground; + this._ctx.fillStyle = this._colors.foreground; } // Since uncached characters are not coming off the char atlas with source @@ -301,11 +303,11 @@ export abstract class BaseRenderLayer implements IRenderLayer { // can bleed into other cells. This code will clip the following fillText, // ensuring that its contents don't go beyond the cell bounds. this._ctx.beginPath(); - this._ctx.rect(x * this.scaledCharWidth, y * this.scaledLineHeight + this.scaledLineDrawY, width * this.scaledCharWidth, this.scaledCharHeight); + this._ctx.rect(x * this._scaledCharWidth, y * this._scaledLineHeight + this._scaledLineDrawY, width * this._scaledCharWidth, this._scaledCharHeight); this._ctx.clip(); // Draw the character - this._ctx.fillText(char, x * this.scaledCharWidth, y * this.scaledLineHeight + this.scaledLineDrawY); + this._ctx.fillText(char, x * this._scaledCharWidth, y * this._scaledLineHeight + this._scaledLineDrawY); this._ctx.restore(); } } diff --git a/src/renderer/ColorManager.ts b/src/renderer/ColorManager.ts index b9afd0e1..746970bb 100644 --- a/src/renderer/ColorManager.ts +++ b/src/renderer/ColorManager.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { IColorSet } from './Interfaces'; +import { IColorSet, IColorManager } from './Interfaces'; import { ITheme } from '../Interfaces'; const DEFAULT_FOREGROUND = '#ffffff'; @@ -64,7 +64,7 @@ function toPaddedHex(c: number): string { /** * Manages the source of truth for a terminal's colors. */ -export class ColorManager { +export class ColorManager implements IColorManager { public colors: IColorSet; constructor() { diff --git a/src/renderer/CursorRenderLayer.ts b/src/renderer/CursorRenderLayer.ts index faed9bfe..008b5662 100644 --- a/src/renderer/CursorRenderLayer.ts +++ b/src/renderer/CursorRenderLayer.ts @@ -135,7 +135,7 @@ export class CursorRenderLayer extends BaseRenderLayer { if (!terminal.isFocused) { this._clearCursor(); this._ctx.save(); - this._ctx.fillStyle = this.colors.cursor; + this._ctx.fillStyle = this._colors.cursor; this._renderBlurCursor(terminal, terminal.buffer.x, viewportRelativeCursorY, charData); this._ctx.restore(); this._state.x = terminal.buffer.x; @@ -190,30 +190,30 @@ export class CursorRenderLayer extends BaseRenderLayer { private _renderBarCursor(terminal: ITerminal, x: number, y: number, charData: CharData): void { this._ctx.save(); - this._ctx.fillStyle = this.colors.cursor; + this._ctx.fillStyle = this._colors.cursor; this.fillLeftLineAtCell(x, y); this._ctx.restore(); } private _renderBlockCursor(terminal: ITerminal, x: number, y: number, charData: CharData): void { this._ctx.save(); - this._ctx.fillStyle = this.colors.cursor; + this._ctx.fillStyle = this._colors.cursor; this.fillCells(x, y, charData[CHAR_DATA_WIDTH_INDEX], 1); - this._ctx.fillStyle = this.colors.background; + this._ctx.fillStyle = this._colors.background; this.fillCharTrueColor(terminal, charData, x, y); this._ctx.restore(); } private _renderUnderlineCursor(terminal: ITerminal, x: number, y: number, charData: CharData): void { this._ctx.save(); - this._ctx.fillStyle = this.colors.cursor; + this._ctx.fillStyle = this._colors.cursor; this.fillBottomLineAtCells(x, y); this._ctx.restore(); } private _renderBlurCursor(terminal: ITerminal, x: number, y: number, charData: CharData): void { this._ctx.save(); - this._ctx.strokeStyle = this.colors.cursor; + this._ctx.strokeStyle = this._colors.cursor; this.strokeRectAtCell(x, y, charData[CHAR_DATA_WIDTH_INDEX], 1); this._ctx.restore(); } diff --git a/src/renderer/Interfaces.ts b/src/renderer/Interfaces.ts index 890b01f5..719ca8cf 100644 --- a/src/renderer/Interfaces.ts +++ b/src/renderer/Interfaces.ts @@ -7,6 +7,7 @@ import { ITerminal, ITerminalOptions, ITheme, IEventEmitter } from '../Interface export interface IRenderer extends IEventEmitter { dimensions: IRenderDimensions; + colorManager: IColorManager; setTheme(theme: ITheme): IColorSet; onWindowResize(devicePixelRatio: number): void; @@ -69,6 +70,9 @@ export interface IRenderLayer { reset(terminal: ITerminal): void; } +export interface IColorManager { + colors: IColorSet; +} export interface IColorSet { foreground: string; diff --git a/src/renderer/LinkRenderLayer.ts b/src/renderer/LinkRenderLayer.ts index 98084034..8f3b63a5 100644 --- a/src/renderer/LinkRenderLayer.ts +++ b/src/renderer/LinkRenderLayer.ts @@ -38,7 +38,7 @@ export class LinkRenderLayer extends BaseRenderLayer { } private _onLinkHover(e: LinkHoverEvent): void { - this._ctx.fillStyle = this.colors.foreground; + this._ctx.fillStyle = this._colors.foreground; this.fillBottomLineAtCells(e.x, e.y, e.length); this._state = e; } diff --git a/src/renderer/Renderer.ts b/src/renderer/Renderer.ts index 8e8934d6..1bdd3b4a 100644 --- a/src/renderer/Renderer.ts +++ b/src/renderer/Renderer.ts @@ -22,17 +22,20 @@ export class Renderer extends EventEmitter implements IRenderer { private _renderLayers: IRenderLayer[]; private _devicePixelRatio: number; - private _colorManager: ColorManager; + public colorManager: ColorManager; public dimensions: IRenderDimensions; - constructor(private _terminal: ITerminal) { + constructor(private _terminal: ITerminal, theme: ITheme) { super(); - this._colorManager = new ColorManager(); + this.colorManager = new ColorManager(); + if (theme) { + this.colorManager.setTheme(theme); + } this._renderLayers = [ - new TextRenderLayer(this._terminal.element, 0, this._colorManager.colors), - new SelectionRenderLayer(this._terminal.element, 1, this._colorManager.colors), - new LinkRenderLayer(this._terminal.element, 2, this._colorManager.colors, this._terminal), - new CursorRenderLayer(this._terminal.element, 3, this._colorManager.colors) + new TextRenderLayer(this._terminal.element, 0, this.colorManager.colors), + new SelectionRenderLayer(this._terminal.element, 1, this.colorManager.colors), + new LinkRenderLayer(this._terminal.element, 2, this.colorManager.colors, this._terminal), + new CursorRenderLayer(this._terminal.element, 3, this.colorManager.colors) ]; this.dimensions = { scaledCharWidth: null, @@ -57,17 +60,17 @@ export class Renderer extends EventEmitter implements IRenderer { } public setTheme(theme: ITheme): IColorSet { - this._colorManager.setTheme(theme); + this.colorManager.setTheme(theme); // Clear layers and force a full render this._renderLayers.forEach(l => { - l.onThemeChanged(this._terminal, this._colorManager.colors); + l.onThemeChanged(this._terminal, this.colorManager.colors); l.reset(this._terminal); }); this._terminal.refresh(0, this._terminal.rows - 1); - return this._colorManager.colors; + return this.colorManager.colors; } public onResize(cols: number, rows: number, didCharSizeChange: boolean): void { diff --git a/src/renderer/SelectionRenderLayer.ts b/src/renderer/SelectionRenderLayer.ts index 7a0aff83..54eb7fc4 100644 --- a/src/renderer/SelectionRenderLayer.ts +++ b/src/renderer/SelectionRenderLayer.ts @@ -68,7 +68,7 @@ export class SelectionRenderLayer extends BaseRenderLayer { // Draw first row const startCol = viewportStartRow === viewportCappedStartRow ? start[0] : 0; const startRowEndCol = viewportCappedStartRow === viewportCappedEndRow ? end[0] : terminal.cols; - this._ctx.fillStyle = this.colors.selection; + this._ctx.fillStyle = this._colors.selection; this.fillCells(startCol, viewportCappedStartRow, startRowEndCol - startCol, 1); // Draw middle rows diff --git a/src/renderer/TextRenderLayer.ts b/src/renderer/TextRenderLayer.ts index 41fcdeee..ced4ceaa 100644 --- a/src/renderer/TextRenderLayer.ts +++ b/src/renderer/TextRenderLayer.ts @@ -136,7 +136,7 @@ export class TextRenderLayer extends BaseRenderLayer { // Draw background if (bg < 256) { this._ctx.save(); - this._ctx.fillStyle = (bg === INVERTED_DEFAULT_COLOR ? this.colors.foreground : this.colors.ansi[bg]); + this._ctx.fillStyle = (bg === INVERTED_DEFAULT_COLOR ? this._colors.foreground : this._colors.ansi[bg]); this.fillCells(x, y, width, 1); this._ctx.restore(); } @@ -152,12 +152,12 @@ export class TextRenderLayer extends BaseRenderLayer { if (flags & FLAGS.UNDERLINE) { if (fg === INVERTED_DEFAULT_COLOR) { - this._ctx.fillStyle = this.colors.background; + this._ctx.fillStyle = this._colors.background; } else if (fg < 256) { // 256 color support - this._ctx.fillStyle = this.colors.ansi[fg]; + this._ctx.fillStyle = this._colors.ansi[fg]; } else { - this._ctx.fillStyle = this.colors.foreground; + this._ctx.fillStyle = this._colors.foreground; } this.fillBottomLineAtCells(x, y); } From 8bdc8fb7a22b0d16a8cfe5e54d9eee42d5216e0f Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 14 Sep 2017 00:29:38 -0700 Subject: [PATCH 23/30] Remove log --- src/renderer/BaseRenderLayer.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index b55b5aaa..8f655de5 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -35,7 +35,6 @@ export abstract class BaseRenderLayer implements IRenderLayer { this._ctx.scale(window.devicePixelRatio, window.devicePixelRatio); // Draw the background if this is an opaque layer if (!_alpha) { - console.log('clearAll!'); this.clearAll(); } container.appendChild(this._canvas); @@ -167,7 +166,6 @@ export abstract class BaseRenderLayer implements IRenderLayer { if (this._alpha) { this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height); } else { - console.log('fill with', this._colors.background); this._ctx.fillStyle = this._colors.background; this._ctx.fillRect(0, 0, this._canvas.width, this._canvas.height); } From bc824ad6a50d200009d02a882fbcc6876405a104 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 14 Sep 2017 09:03:41 -0700 Subject: [PATCH 24/30] Fix tests --- src/utils/TestUtils.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils/TestUtils.test.ts b/src/utils/TestUtils.test.ts index 45cf169a..beed0aaa 100644 --- a/src/utils/TestUtils.test.ts +++ b/src/utils/TestUtils.test.ts @@ -7,7 +7,7 @@ import { ITerminal, IBuffer, IBufferSet, IBrowser, ICharMeasure, ISelectionManag import { LineData } from '../Types'; import { Buffer } from '../Buffer'; import * as Browser from './Browser'; -import { IColorSet, IRenderer, IRenderDimensions } from '../renderer/Interfaces'; +import { IColorSet, IRenderer, IRenderDimensions, IColorManager } from '../renderer/Interfaces'; export class MockTerminal implements ITerminal { renderer: IRenderer; @@ -216,6 +216,7 @@ export class MockBuffer implements IBuffer { } export class MockRenderer implements IRenderer { + colorManager: IColorManager; on(type: string, listener: IListenerType): void { throw new Error('Method not implemented.'); } From 0cf9f1d5364d93b3bca8ed1eb5d9942b902a6de7 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 14 Sep 2017 09:05:50 -0700 Subject: [PATCH 25/30] Fix tests --- src/utils/Mouse.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/Mouse.test.ts b/src/utils/Mouse.test.ts index 4163263b..b96dd68a 100644 --- a/src/utils/Mouse.test.ts +++ b/src/utils/Mouse.test.ts @@ -58,6 +58,6 @@ describe('getCoords', () => { assert.deepEqual(coords, [1, 1]); // Event are double the cols/rows coords = getCoords({ pageX: CHAR_WIDTH * 20, pageY: CHAR_HEIGHT * 20 }, document.createElement('div'), charMeasure, 1, 10, 10); - assert.deepEqual(coords, [11, 11], 'coordinates should never come back as larger than the terminal'); + assert.deepEqual(coords, [10, 10], 'coordinates should never come back as larger than the terminal'); }); }); From 9b6c4e046f61c39470d9d7ed295df03d55dc68df Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sat, 16 Sep 2017 10:57:49 -0700 Subject: [PATCH 26/30] Only refresh char atlas when char dimensions are valid See Microsoft/vscode#34493 --- src/renderer/BaseRenderLayer.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index 8f655de5..4cc07472 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -57,6 +57,9 @@ export abstract class BaseRenderLayer implements IRenderLayer { * @param colorSet The color set to use for the char atlas. */ private _refreshCharAtlas(terminal: ITerminal, colorSet: IColorSet): void { + if (this._scaledCharWidth > 0 && this._scaledCharHeight > 0) { + return; + } this._charAtlas = null; const result = acquireCharAtlas(terminal, this._colors, this._scaledCharWidth, this._scaledCharHeight); if (result instanceof HTMLCanvasElement) { From c8153104184622a9a243a24110acf27d9be7665b Mon Sep 17 00:00:00 2001 From: Thomas Zilz Date: Sat, 16 Sep 2017 20:36:55 +0200 Subject: [PATCH 27/30] Fix inverted cells, make sure inverted cells are cleared correctly (#996) * Corretly inverse foreground and background. Be more conservative when clearing * add missing space * Re-enable optimisation with better checks * Fix insversed check --- src/renderer/TextRenderLayer.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/renderer/TextRenderLayer.ts b/src/renderer/TextRenderLayer.ts index ced4ceaa..74997866 100644 --- a/src/renderer/TextRenderLayer.ts +++ b/src/renderer/TextRenderLayer.ts @@ -83,7 +83,8 @@ export class TextRenderLayer extends BaseRenderLayer { } // Clear the old character was not a space with the default background - if (state && !(state[CHAR_DATA_CODE_INDEX] === 32 /*' '*/ && (state[CHAR_DATA_ATTR_INDEX] & 0x1ff) >= 256)) { + const wasInverted = !!(state && state[CHAR_DATA_ATTR_INDEX] && state[CHAR_DATA_ATTR_INDEX] >> 18 & FLAGS.INVERSE); + if (state && !(state[CHAR_DATA_CODE_INDEX] === 32 /*' '*/ && (state[CHAR_DATA_ATTR_INDEX] & 0x1ff) >= 256 && !wasInverted)) { this._clearChar(x, y); } this._state.cache[x][y] = charData; @@ -94,7 +95,8 @@ export class TextRenderLayer extends BaseRenderLayer { // Skip rendering if the character is invisible const isDefaultBackground = bg >= 256; const isInvisible = flags & FLAGS.INVISIBLE; - if (!code || (code === 32 /*' '*/ && isDefaultBackground) || isInvisible) { + const isInverted = flags & FLAGS.INVERSE; + if (!code || (code === 32 /*' '*/ && isDefaultBackground && !isInverted) || isInvisible) { continue; } @@ -121,10 +123,10 @@ export class TextRenderLayer extends BaseRenderLayer { let fg = (attr >> 9) & 0x1ff; // If inverse flag is on, the foreground should become the background. - if (flags & FLAGS.INVERSE) { + if (isInverted) { const temp = bg; bg = fg; - fg = bg; + fg = temp; if (fg === 256) { fg = INVERTED_DEFAULT_COLOR; } From c4da8325ace6e4bde77822377bf994d389a7c75a Mon Sep 17 00:00:00 2001 From: Thomas Zilz Date: Sun, 17 Sep 2017 10:39:09 +0200 Subject: [PATCH 28/30] Allow all overflowing characters to extend to the next cell if followed by a space (Emojis etc) (#997) * Measure characters and thread them if they are overlapping * Fix whitespace * Use charData and ctx.save/restore, code cleanup * Fix trailing whitespace --- src/renderer/TextRenderLayer.ts | 79 +++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/src/renderer/TextRenderLayer.ts b/src/renderer/TextRenderLayer.ts index 74997866..e319efe7 100644 --- a/src/renderer/TextRenderLayer.ts +++ b/src/renderer/TextRenderLayer.ts @@ -16,10 +16,13 @@ import { BaseRenderLayer, INVERTED_DEFAULT_COLOR } from './BaseRenderLayer'; * when the character changes (a regular space ' ' character may not as it's * drawn state is a cleared cell). */ -const EMOJI_OWNED_CHAR_DATA: CharData = [null, '', 0, -1]; +const OVERLAP_OWNED_CHAR_DATA: CharData = [null, '', 0, -1]; export class TextRenderLayer extends BaseRenderLayer { private _state: GridCache; + private _characterWidth: number; + private _characterFont: string; + private _characterOverlapCache: { [key: string]: boolean } = {}; constructor(container: HTMLElement, zIndex: number, colors: IColorSet) { super(container, 'text', zIndex, false, colors); @@ -28,6 +31,14 @@ export class TextRenderLayer extends BaseRenderLayer { public resize(terminal: ITerminal, dim: IRenderDimensions, charSizeChanged: boolean): void { super.resize(terminal, dim, charSizeChanged); + + // Clear the character width cache if the font or width has changed + const terminalFont = `${terminal.options.fontSize * window.devicePixelRatio}px ${terminal.options.fontFamily}`; + if (this._characterWidth !== dim.scaledCharWidth || this._characterFont !== terminalFont) { + this._characterWidth = dim.scaledCharWidth; + this._characterFont = terminalFont; + this._characterOverlapCache = {}; + } // Resizing the canvas discards the contents of the canvas so clear state this._state.clear(); this._state.resize(terminal.cols, terminal.rows); @@ -63,12 +74,12 @@ export class TextRenderLayer extends BaseRenderLayer { } // If the character is a space and the character to the left is an - // emoji, skip the character and allow the emoji char to take full - // control over this character's cell. + // overlapping character, skip the character and allow the overlapping + // char to take full control over this character's cell. if (code === 32 /*' '*/) { if (x > 0) { const previousChar: CharData = line[x - 1]; - if (this._isEmoji(previousChar[CHAR_DATA_CHAR_INDEX])) { + if (this._isOverlapping(previousChar)) { continue; } } @@ -100,23 +111,23 @@ export class TextRenderLayer extends BaseRenderLayer { continue; } - // If the character is an emoji and the character to the right is a + // If the character is an overlapping char and the character to the right is a // space, take ownership of the cell to the right. - if (this._isEmoji(char)) { - // If the character is an emoji, we want to force a re-render on every + if (width !== 0 && this._isOverlapping(charData)) { + // If the character is overlapping, we want to force a re-render on every // frame. This is specifically to work around the case where two - // emoji's `a` and `b` are adjacent, the cursor is moved to b and a + // overlaping chars `a` and `b` are adjacent, the cursor is moved to b and a // space is added. Without this, the first half of `b` would never // get removed, and `a` would not re-render because it thinks it's // already in the correct state. - this._state.cache[x][y] = EMOJI_OWNED_CHAR_DATA; + this._state.cache[x][y] = OVERLAP_OWNED_CHAR_DATA; if (x < line.length && line[x + 1][CHAR_DATA_CODE_INDEX] === 32 /*' '*/) { width = 2; this._clearChar(x + 1, y); - // The emoji owned char data will force a clear and render when the - // emoji is no longer to the left of the character and also when the - // space changes to another character. - this._state.cache[x + 1][y] = EMOJI_OWNED_CHAR_DATA; + // The overlapping char's char data will force a clear and render when the + // overlapping char is no longer to the left of the character and also when + // the space changes to another character. + this._state.cache[x + 1][y] = OVERLAP_OWNED_CHAR_DATA; } } @@ -171,18 +182,38 @@ export class TextRenderLayer extends BaseRenderLayer { } } - /** - * Whether the character is an emoji. - * @param char The character to search. - */ - private _isEmoji(char: string): boolean { - // TODO: We need a generic solution for handling characters like this - // Check special ambiguous width characters - if (char === '➜') { - return true; + /** + * Whether a character is overlapping to the + * next cell. + */ + private _isOverlapping(charData: CharData): boolean { + // We assume that any ascii character will not overlap + const code = charData[CHAR_DATA_CODE_INDEX]; + if (code < 256) { + return false; } - // Check emoji unicode range - return char.search(/([\uD800-\uDBFF][\uDC00-\uDFFF])/g) >= 0; + + // Deliver from cache if available + const char = charData[CHAR_DATA_CHAR_INDEX]; + if (this._characterOverlapCache.hasOwnProperty(char)) { + return this._characterOverlapCache[char]; + } + + // Setup the font + this._ctx.save(); + this._ctx.font = this._characterFont; + + // Measure the width of the character, but Math.floor it + // because that is what the renderer does when it calculates + // the character dimensions we are comparing against + const overlaps = Math.floor(this._ctx.measureText(char).width) > this._characterWidth; + + // Restore the original context + this._ctx.restore(); + + // Cache and return + this._characterOverlapCache[char] = overlaps; + return overlaps; } /** From 40fdd00b9e1334c94618a69e60bee6538ff7eb36 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 20 Sep 2017 09:02:21 +0900 Subject: [PATCH 29/30] Correct if statement, ensure charAtlas exists --- src/renderer/BaseRenderLayer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index 4cc07472..2bf49e68 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -57,7 +57,7 @@ export abstract class BaseRenderLayer implements IRenderLayer { * @param colorSet The color set to use for the char atlas. */ private _refreshCharAtlas(terminal: ITerminal, colorSet: IColorSet): void { - if (this._scaledCharWidth > 0 && this._scaledCharHeight > 0) { + if (this._scaledCharWidth <= 0 && this._scaledCharHeight <= 0) { return; } this._charAtlas = null; @@ -256,7 +256,7 @@ export abstract class BaseRenderLayer implements IRenderLayer { const isBasicColor = (colorIndex > 1 && fg < 16); const isDefaultColor = fg >= 256; const isDefaultBackground = bg >= 256; - if (isAscii && (isBasicColor || isDefaultColor) && isDefaultBackground) { + if (this._charAtlas && isAscii && (isBasicColor || isDefaultColor) && isDefaultBackground) { // ImageBitmap's draw about twice as fast as from a canvas const charAtlasCellWidth = this._scaledCharWidth + CHAR_ATLAS_CELL_SPACING; const charAtlasCellHeight = this._scaledCharHeight + CHAR_ATLAS_CELL_SPACING; From a2389d5c5b46d14797345ae75ba0bcc5b884f521 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sun, 24 Sep 2017 10:59:38 -0700 Subject: [PATCH 30/30] Fix validation callback to use the uri, not whole row --- src/Linkifier.test.ts | 11 +++++++++++ src/Linkifier.ts | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Linkifier.test.ts b/src/Linkifier.test.ts index df9e459c..0878aafc 100644 --- a/src/Linkifier.test.ts +++ b/src/Linkifier.test.ts @@ -156,6 +156,17 @@ describe('Linkifier', () => { linkifier.linkifyRows(); }); + it('should validate the uri, not the row', done => { + addRow('abc test abc'); + linkifier.registerLinkMatcher(/test/, () => done(), { + validationCallback: (uri, cb) => { + assert.equal(uri, 'test'); + done(); + } + }); + linkifier.linkifyRows(); + }); + it('should disable link if false', done => { addRow('test'); linkifier.registerLinkMatcher(/test/, () => assert.fail(), { diff --git a/src/Linkifier.ts b/src/Linkifier.ts index fa47271c..42d20f9c 100644 --- a/src/Linkifier.ts +++ b/src/Linkifier.ts @@ -236,7 +236,7 @@ export class Linkifier extends EventEmitter implements ILinkifier { // Ensure the link is valid before registering if (matcher.validationCallback) { - matcher.validationCallback(text, isValid => { + matcher.validationCallback(uri, isValid => { // Discard link if the line has already changed if (this._rowsTimeoutId) { return;