Skip to content

Commit 74b5d3f

Browse files
authored
Merge branch 'master' into protected
2 parents 21105b4 + f3622df commit 74b5d3f

33 files changed

+287
-177
lines changed

.eslintrc.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,29 @@
147147
]
148148
}
149149
],
150+
"no-restricted-syntax": [
151+
"warn",
152+
{
153+
"selector": "CallExpression[callee.name='requestAnimationFrame']",
154+
"message": "The global requestAnimationFrame() should be avoided, call it on the parent window from ICoreBrowserService."
155+
},
156+
{
157+
"selector": "CallExpression[callee.name='cancelAnimationFrame']",
158+
"message": "The global cancelAnimationFrame() should be avoided, call it on the parent window from ICoreBrowserService."
159+
},
160+
{
161+
"selector": "CallExpression > MemberExpression[object.name='window'][property.name='requestAnimationFrame']",
162+
"message": "window.requestAnimationFrame() should be avoided, call it on the parent window from ICoreBrowserService."
163+
},
164+
{
165+
"selector": "CallExpression > MemberExpression[object.name='window'][property.name='cancelAnimationFrame']",
166+
"message": "window.cancelAnimationFrame() should be avoided, call it on the parent window from ICoreBrowserService."
167+
},
168+
{
169+
"selector": "MemberExpression[object.name='window'][property.name='devicePixelRatio']",
170+
"message": "window.devicePixelRatio should be avoided, get it from ICoreBrowserService."
171+
}
172+
],
150173
"no-trailing-spaces": "warn",
151174
"no-unsafe-finally": "warn",
152175
"no-var": "warn",

addons/xterm-addon-canvas/src/BaseRenderLayer.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { AttributeData } from 'common/buffer/AttributeData';
1515
import { IColorSet } from 'browser/Types';
1616
import { CellData } from 'common/buffer/CellData';
1717
import { IBufferService, IDecorationService, IOptionsService } from 'common/services/Services';
18+
import { ICoreBrowserService } from 'browser/services/Services';
1819
import { excludeFromContrastRatioDemands, throwIfFalsy } from 'browser/renderer/RendererUtils';
1920
import { channels, color, rgba } from 'common/Color';
2021
import { removeElementFromParent } from 'browser/Dom';
@@ -60,7 +61,8 @@ export abstract class BaseRenderLayer implements IRenderLayer {
6061
private _rendererId: number,
6162
protected readonly _bufferService: IBufferService,
6263
protected readonly _optionsService: IOptionsService,
63-
protected readonly _decorationService: IDecorationService
64+
protected readonly _decorationService: IDecorationService,
65+
protected readonly _coreBrowserService: ICoreBrowserService
6466
) {
6567
this._canvas = document.createElement('canvas');
6668
this._canvas.classList.add(`xterm-${id}-layer`);
@@ -125,7 +127,7 @@ export abstract class BaseRenderLayer implements IRenderLayer {
125127
if (this._scaledCharWidth <= 0 && this._scaledCharHeight <= 0) {
126128
return;
127129
}
128-
this._charAtlas = acquireCharAtlas(this._optionsService.rawOptions, this._rendererId, colorSet, this._scaledCharWidth, this._scaledCharHeight);
130+
this._charAtlas = acquireCharAtlas(this._optionsService.rawOptions, this._rendererId, colorSet, this._scaledCharWidth, this._scaledCharHeight, this._coreBrowserService.dpr);
129131
this._charAtlas.warmUp();
130132
}
131133

@@ -180,9 +182,9 @@ export abstract class BaseRenderLayer implements IRenderLayer {
180182
const cellOffset = Math.ceil(this._scaledCellHeight * 0.5);
181183
this._ctx.fillRect(
182184
x * this._scaledCellWidth,
183-
(y + 1) * this._scaledCellHeight - cellOffset - window.devicePixelRatio,
185+
(y + 1) * this._scaledCellHeight - cellOffset - this._coreBrowserService.dpr,
184186
width * this._scaledCellWidth,
185-
window.devicePixelRatio);
187+
this._coreBrowserService.dpr);
186188
}
187189

188190
/**
@@ -194,23 +196,24 @@ export abstract class BaseRenderLayer implements IRenderLayer {
194196
protected _fillBottomLineAtCells(x: number, y: number, width: number = 1, pixelOffset: number = 0): void {
195197
this._ctx.fillRect(
196198
x * this._scaledCellWidth,
197-
(y + 1) * this._scaledCellHeight + pixelOffset - window.devicePixelRatio - 1 /* Ensure it's drawn within the cell */,
199+
(y + 1) * this._scaledCellHeight + pixelOffset - this._coreBrowserService.dpr - 1 /* Ensure it's drawn within the cell */,
198200
width * this._scaledCellWidth,
199-
window.devicePixelRatio);
201+
this._coreBrowserService.dpr);
200202
}
201203

202204
protected _curlyUnderlineAtCell(x: number, y: number, width: number = 1): void {
203205
this._ctx.save();
204206
this._ctx.beginPath();
205207
this._ctx.strokeStyle = this._ctx.fillStyle;
206-
this._ctx.lineWidth = window.devicePixelRatio;
208+
const lineWidth = this._coreBrowserService.dpr;
209+
this._ctx.lineWidth = lineWidth;
207210
for (let xOffset = 0; xOffset < width; xOffset++) {
208211
const xLeft = (x + xOffset) * this._scaledCellWidth;
209212
const xMid = (x + xOffset + 0.5) * this._scaledCellWidth;
210213
const xRight = (x + xOffset + 1) * this._scaledCellWidth;
211-
const yMid = (y + 1) * this._scaledCellHeight - window.devicePixelRatio - 1;
212-
const yMidBot = yMid - window.devicePixelRatio;
213-
const yMidTop = yMid + window.devicePixelRatio;
214+
const yMid = (y + 1) * this._scaledCellHeight - lineWidth - 1;
215+
const yMidBot = yMid - lineWidth;
216+
const yMidTop = yMid + lineWidth;
214217
this._ctx.moveTo(xLeft, yMid);
215218
this._ctx.bezierCurveTo(
216219
xLeft, yMidBot,
@@ -231,10 +234,11 @@ export abstract class BaseRenderLayer implements IRenderLayer {
231234
this._ctx.save();
232235
this._ctx.beginPath();
233236
this._ctx.strokeStyle = this._ctx.fillStyle;
234-
this._ctx.lineWidth = window.devicePixelRatio;
235-
this._ctx.setLineDash([window.devicePixelRatio * 2, window.devicePixelRatio]);
237+
const lineWidth = this._coreBrowserService.dpr;
238+
this._ctx.lineWidth = lineWidth;
239+
this._ctx.setLineDash([lineWidth * 2, lineWidth]);
236240
const xLeft = x * this._scaledCellWidth;
237-
const yMid = (y + 1) * this._scaledCellHeight - window.devicePixelRatio - 1;
241+
const yMid = (y + 1) * this._scaledCellHeight - lineWidth - 1;
238242
this._ctx.moveTo(xLeft, yMid);
239243
for (let xOffset = 0; xOffset < width; xOffset++) {
240244
// const xLeft = x * this._scaledCellWidth;
@@ -250,11 +254,12 @@ export abstract class BaseRenderLayer implements IRenderLayer {
250254
this._ctx.save();
251255
this._ctx.beginPath();
252256
this._ctx.strokeStyle = this._ctx.fillStyle;
253-
this._ctx.lineWidth = window.devicePixelRatio;
254-
this._ctx.setLineDash([window.devicePixelRatio * 4, window.devicePixelRatio * 3]);
257+
const lineWidth = this._coreBrowserService.dpr;
258+
this._ctx.lineWidth = lineWidth;
259+
this._ctx.setLineDash([lineWidth * 4, lineWidth * 3]);
255260
const xLeft = x * this._scaledCellWidth;
256261
const xRight = (x + width) * this._scaledCellWidth;
257-
const yMid = (y + 1) * this._scaledCellHeight - window.devicePixelRatio - 1;
262+
const yMid = (y + 1) * this._scaledCellHeight - lineWidth - 1;
258263
this._ctx.moveTo(xLeft, yMid);
259264
this._ctx.lineTo(xRight, yMid);
260265
this._ctx.stroke();
@@ -272,7 +277,7 @@ export abstract class BaseRenderLayer implements IRenderLayer {
272277
this._ctx.fillRect(
273278
x * this._scaledCellWidth,
274279
y * this._scaledCellHeight,
275-
window.devicePixelRatio * width,
280+
this._coreBrowserService.dpr * width,
276281
this._scaledCellHeight);
277282
}
278283

@@ -283,12 +288,13 @@ export abstract class BaseRenderLayer implements IRenderLayer {
283288
* @param y The row to fill.
284289
*/
285290
protected _strokeRectAtCell(x: number, y: number, width: number, height: number): void {
286-
this._ctx.lineWidth = window.devicePixelRatio;
291+
const lineWidth = this._coreBrowserService.dpr;
292+
this._ctx.lineWidth = lineWidth;
287293
this._ctx.strokeRect(
288-
x * this._scaledCellWidth + window.devicePixelRatio / 2,
289-
y * this._scaledCellHeight + (window.devicePixelRatio / 2),
290-
width * this._scaledCellWidth - window.devicePixelRatio,
291-
(height * this._scaledCellHeight) - window.devicePixelRatio);
294+
x * this._scaledCellWidth + lineWidth / 2,
295+
y * this._scaledCellHeight + (lineWidth / 2),
296+
width * this._scaledCellWidth - lineWidth,
297+
(height * this._scaledCellHeight) - lineWidth);
292298
}
293299

294300
/**
@@ -344,7 +350,7 @@ export abstract class BaseRenderLayer implements IRenderLayer {
344350
// Draw custom characters if applicable
345351
let drawSuccess = false;
346352
if (this._optionsService.rawOptions.customGlyphs !== false) {
347-
drawSuccess = tryDrawCustomChar(this._ctx, cell.getChars(), x * this._scaledCellWidth, y * this._scaledCellHeight, this._scaledCellWidth, this._scaledCellHeight, this._optionsService.rawOptions.fontSize);
353+
drawSuccess = tryDrawCustomChar(this._ctx, cell.getChars(), x * this._scaledCellWidth, y * this._scaledCellHeight, this._scaledCellWidth, this._scaledCellHeight, this._optionsService.rawOptions.fontSize, this._coreBrowserService.dpr);
348354
}
349355

350356
// Draw the character
@@ -472,7 +478,7 @@ export abstract class BaseRenderLayer implements IRenderLayer {
472478
// Draw custom characters if applicable
473479
let drawSuccess = false;
474480
if (this._optionsService.rawOptions.customGlyphs !== false) {
475-
drawSuccess = tryDrawCustomChar(this._ctx, cell.getChars(), x * this._scaledCellWidth, y * this._scaledCellHeight, this._scaledCellWidth, this._scaledCellHeight, this._optionsService.rawOptions.fontSize);
481+
drawSuccess = tryDrawCustomChar(this._ctx, cell.getChars(), x * this._scaledCellWidth, y * this._scaledCellHeight, this._scaledCellWidth, this._scaledCellHeight, this._optionsService.rawOptions.fontSize, this._coreBrowserService.dpr);
476482
}
477483

478484
// Draw the character
@@ -509,7 +515,7 @@ export abstract class BaseRenderLayer implements IRenderLayer {
509515
const fontWeight = isBold ? this._optionsService.rawOptions.fontWeightBold : this._optionsService.rawOptions.fontWeight;
510516
const fontStyle = isItalic ? 'italic' : '';
511517

512-
return `${fontStyle} ${fontWeight} ${this._optionsService.rawOptions.fontSize * window.devicePixelRatio}px ${this._optionsService.rawOptions.fontFamily}`;
518+
return `${fontStyle} ${fontWeight} ${this._optionsService.rawOptions.fontSize * this._coreBrowserService.dpr}px ${this._optionsService.rawOptions.fontFamily}`;
513519
}
514520

515521
private _getContrastColor(cell: CellData, x: number, y: number): IColor | undefined {

addons/xterm-addon-canvas/src/CanvasRenderer.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ export class CanvasRenderer extends Disposable implements IRenderer {
3939
private readonly _optionsService: IOptionsService,
4040
characterJoinerService: ICharacterJoinerService,
4141
coreService: ICoreService,
42-
coreBrowserService: ICoreBrowserService,
42+
private readonly _coreBrowserService: ICoreBrowserService,
4343
decorationService: IDecorationService
4444
) {
4545
super();
4646
const allowTransparency = this._optionsService.rawOptions.allowTransparency;
4747
this._renderLayers = [
48-
new TextRenderLayer(this._screenElement, 0, this._colors, allowTransparency, this._id, this._bufferService, this._optionsService, characterJoinerService, decorationService),
49-
new SelectionRenderLayer(this._screenElement, 1, this._colors, this._id, this._bufferService, coreBrowserService, decorationService, this._optionsService),
50-
new LinkRenderLayer(this._screenElement, 2, this._colors, this._id, linkifier2, this._bufferService, this._optionsService, decorationService),
51-
new CursorRenderLayer(this._screenElement, 3, this._colors, this._id, this._onRequestRedraw, this._bufferService, this._optionsService, coreService, coreBrowserService, decorationService)
48+
new TextRenderLayer(this._screenElement, 0, this._colors, allowTransparency, this._id, this._bufferService, this._optionsService, characterJoinerService, decorationService, this._coreBrowserService),
49+
new SelectionRenderLayer(this._screenElement, 1, this._colors, this._id, this._bufferService, this._coreBrowserService, decorationService, this._optionsService),
50+
new LinkRenderLayer(this._screenElement, 2, this._colors, this._id, linkifier2, this._bufferService, this._optionsService, decorationService, this._coreBrowserService),
51+
new CursorRenderLayer(this._screenElement, 3, this._colors, this._id, this._onRequestRedraw, this._bufferService, this._optionsService, coreService, this._coreBrowserService, decorationService)
5252
];
5353
this.dimensions = {
5454
scaledCharWidth: 0,
@@ -64,10 +64,10 @@ export class CanvasRenderer extends Disposable implements IRenderer {
6464
actualCellWidth: 0,
6565
actualCellHeight: 0
6666
};
67-
this._devicePixelRatio = window.devicePixelRatio;
67+
this._devicePixelRatio = this._coreBrowserService.dpr;
6868
this._updateDimensions();
6969

70-
this.register(observeDevicePixelDimensions(this._renderLayers[0].canvas, (w, h) => this._setCanvasDevicePixelDimensions(w, h)));
70+
this.register(observeDevicePixelDimensions(this._renderLayers[0].canvas, this._coreBrowserService.window, (w, h) => this._setCanvasDevicePixelDimensions(w, h)));
7171

7272
this.onOptionsChanged();
7373
}
@@ -83,8 +83,8 @@ export class CanvasRenderer extends Disposable implements IRenderer {
8383
public onDevicePixelRatioChange(): void {
8484
// If the device pixel ratio changed, the char atlas needs to be regenerated
8585
// and the terminal needs to refreshed
86-
if (this._devicePixelRatio !== window.devicePixelRatio) {
87-
this._devicePixelRatio = window.devicePixelRatio;
86+
if (this._devicePixelRatio !== this._coreBrowserService.dpr) {
87+
this._devicePixelRatio = this._coreBrowserService.dpr;
8888
this.onResize(this._bufferService.cols, this._bufferService.rows);
8989
}
9090
}
@@ -175,16 +175,17 @@ export class CanvasRenderer extends Disposable implements IRenderer {
175175
}
176176

177177
// See the WebGL renderer for an explanation of this section.
178-
this.dimensions.scaledCharWidth = Math.floor(this._charSizeService.width * window.devicePixelRatio);
179-
this.dimensions.scaledCharHeight = Math.ceil(this._charSizeService.height * window.devicePixelRatio);
178+
const dpr = this._coreBrowserService.dpr;
179+
this.dimensions.scaledCharWidth = Math.floor(this._charSizeService.width * dpr);
180+
this.dimensions.scaledCharHeight = Math.ceil(this._charSizeService.height * dpr);
180181
this.dimensions.scaledCellHeight = Math.floor(this.dimensions.scaledCharHeight * this._optionsService.rawOptions.lineHeight);
181182
this.dimensions.scaledCharTop = this._optionsService.rawOptions.lineHeight === 1 ? 0 : Math.round((this.dimensions.scaledCellHeight - this.dimensions.scaledCharHeight) / 2);
182183
this.dimensions.scaledCellWidth = this.dimensions.scaledCharWidth + Math.round(this._optionsService.rawOptions.letterSpacing);
183184
this.dimensions.scaledCharLeft = Math.floor(this._optionsService.rawOptions.letterSpacing / 2);
184185
this.dimensions.scaledCanvasHeight = this._bufferService.rows * this.dimensions.scaledCellHeight;
185186
this.dimensions.scaledCanvasWidth = this._bufferService.cols * this.dimensions.scaledCellWidth;
186-
this.dimensions.canvasHeight = Math.round(this.dimensions.scaledCanvasHeight / window.devicePixelRatio);
187-
this.dimensions.canvasWidth = Math.round(this.dimensions.scaledCanvasWidth / window.devicePixelRatio);
187+
this.dimensions.canvasHeight = Math.round(this.dimensions.scaledCanvasHeight / dpr);
188+
this.dimensions.canvasWidth = Math.round(this.dimensions.scaledCanvasWidth / dpr);
188189
this.dimensions.actualCellHeight = this.dimensions.canvasHeight / this._bufferService.rows;
189190
this.dimensions.actualCellWidth = this.dimensions.canvasWidth / this._bufferService.cols;
190191
}

0 commit comments

Comments
 (0)