Skip to content

Commit 49a83ee

Browse files
committed
Replace window usage within terminal
Fixes #195804
1 parent 8943ea4 commit 49a83ee

File tree

12 files changed

+57
-50
lines changed

12 files changed

+57
-50
lines changed

src/vs/base/browser/dom.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,13 @@ export function getActiveDocument(): Document {
850850
return documents.find(doc => doc.hasFocus()) ?? document;
851851
}
852852

853+
export function getDocument(element: Node | undefined | null): Document;
854+
export function getDocument(event: UIEvent | undefined | null): Document;
855+
export function getDocument(e: unknown): Document {
856+
const candidateNode = e as Node | undefined | null;
857+
return getWindow(candidateNode).document;
858+
}
859+
853860
export function getActiveWindow(): CodeWindow {
854861
const document = getActiveDocument();
855862
return (document.defaultView?.window ?? mainWindow) as CodeWindow;

src/vs/workbench/contrib/terminal/browser/terminalConfigHelper.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { IXtermCore } from 'vs/workbench/contrib/terminal/browser/xterm-private'
2020
import { IShellLaunchConfig } from 'vs/platform/terminal/common/terminal';
2121
import { isLinux, isWindows } from 'vs/base/common/platform';
2222
import { Disposable } from 'vs/base/common/lifecycle';
23-
import { $window } from 'vs/base/browser/window';
2423

2524
const enum FontConstants {
2625
MinimumFontSize = 6,
@@ -119,7 +118,7 @@ export class TerminalConfigHelper extends Disposable implements IBrowserTerminal
119118
return rect;
120119
}
121120

122-
private _measureFont(fontFamily: string, fontSize: number, letterSpacing: number, lineHeight: number): ITerminalFont {
121+
private _measureFont(w: Window, fontFamily: string, fontSize: number, letterSpacing: number, lineHeight: number): ITerminalFont {
123122
const rect = this._getBoundingRectFor('X', fontFamily, fontSize);
124123

125124
// Bounding client rect was invalid, use last font measurement if available.
@@ -143,10 +142,10 @@ export class TerminalConfigHelper extends Disposable implements IBrowserTerminal
143142
if (this.config.gpuAcceleration === 'off') {
144143
this._lastFontMeasurement.charWidth = rect.width;
145144
} else {
146-
const deviceCharWidth = Math.floor(rect.width * $window.devicePixelRatio);
145+
const deviceCharWidth = Math.floor(rect.width * w.devicePixelRatio);
147146
const deviceCellWidth = deviceCharWidth + Math.round(letterSpacing);
148-
const cssCellWidth = deviceCellWidth / $window.devicePixelRatio;
149-
this._lastFontMeasurement.charWidth = cssCellWidth - Math.round(letterSpacing) / $window.devicePixelRatio;
147+
const cssCellWidth = deviceCellWidth / w.devicePixelRatio;
148+
this._lastFontMeasurement.charWidth = cssCellWidth - Math.round(letterSpacing) / w.devicePixelRatio;
150149
}
151150
}
152151

@@ -157,7 +156,7 @@ export class TerminalConfigHelper extends Disposable implements IBrowserTerminal
157156
* Gets the font information based on the terminal.integrated.fontFamily
158157
* terminal.integrated.fontSize, terminal.integrated.lineHeight configuration properties
159158
*/
160-
getFont(xtermCore?: IXtermCore, excludeDimensions?: boolean): ITerminalFont {
159+
getFont(w: Window, xtermCore?: IXtermCore, excludeDimensions?: boolean): ITerminalFont {
161160
const editorConfig = this._configurationService.getValue<IEditorOptions>('editor');
162161

163162
let fontFamily = this.config.fontFamily || editorConfig.fontFamily || EDITOR_FONT_DEFAULTS.fontFamily;
@@ -201,13 +200,13 @@ export class TerminalConfigHelper extends Disposable implements IBrowserTerminal
201200
letterSpacing,
202201
lineHeight,
203202
charHeight: cellDims.height / lineHeight,
204-
charWidth: cellDims.width - Math.round(letterSpacing) / $window.devicePixelRatio
203+
charWidth: cellDims.width - Math.round(letterSpacing) / w.devicePixelRatio
205204
};
206205
}
207206
}
208207

209208
// Fall back to measuring the font ourselves
210-
return this._measureFont(fontFamily, fontSize, letterSpacing, lineHeight);
209+
return this._measureFont(w, fontFamily, fontSize, letterSpacing, lineHeight);
211210
}
212211

213212
private _clampInt<T>(source: any, minimum: number, maximum: number, fallback: T): number | T {

src/vs/workbench/contrib/terminal/browser/terminalGroup.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ViewContainerLocation, IViewDescriptorService } from 'vs/workbench/comm
1414
import { IShellLaunchConfig, ITerminalTabLayoutInfoById, TerminalLocation } from 'vs/platform/terminal/common/terminal';
1515
import { TerminalStatus } from 'vs/workbench/contrib/terminal/browser/terminalStatusList';
1616
import { getPartByLocation } from 'vs/workbench/browser/parts/views/viewsService';
17+
import { getWindow } from 'vs/base/browser/dom';
1718

1819
const enum Constants {
1920
/**
@@ -572,7 +573,7 @@ export class TerminalGroup extends Disposable implements ITerminalGroup {
572573
}
573574

574575
const isHorizontal = (direction === Direction.Left || direction === Direction.Right);
575-
const font = this._terminalService.configHelper.getFont();
576+
const font = this._terminalService.configHelper.getFont(getWindow(this._groupElement));
576577
// TODO: Support letter spacing and line height
577578
const charSize = (isHorizontal ? font.charWidth : font.charHeight);
578579
if (charSize) {

src/vs/workbench/contrib/terminal/browser/terminalInstance.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,8 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
637637
return null;
638638
}
639639

640-
const font = this.xterm ? this.xterm.getFont() : this._configHelper.getFont();
641-
const newRC = getXtermScaledDimensions(font, dimension.width, dimension.height);
640+
const font = this.xterm ? this.xterm.getFont() : this._configHelper.getFont(dom.getWindow(this.domElement));
641+
const newRC = getXtermScaledDimensions(dom.getWindow(this.domElement), font, dimension.width, dimension.height);
642642
if (!newRC) {
643643
this._setLastKnownColsAndRows();
644644
return null;
@@ -667,7 +667,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
667667

668668
private _getDimension(width: number, height: number): ICanvasDimensions | undefined {
669669
// The font needs to have been initialized
670-
const font = this.xterm ? this.xterm.getFont() : this._configHelper.getFont();
670+
const font = this.xterm ? this.xterm.getFont() : this._configHelper.getFont(dom.getWindow(this.domElement));
671671
if (!font || !font.charWidth || !font.charHeight) {
672672
return undefined;
673673
}
@@ -2031,7 +2031,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
20312031
}
20322032

20332033
private async _addScrollbar(): Promise<void> {
2034-
const charWidth = (this.xterm ? this.xterm.getFont() : this._configHelper.getFont()).charWidth;
2034+
const charWidth = (this.xterm ? this.xterm.getFont() : this._configHelper.getFont(dom.getWindow(this.domElement))).charWidth;
20352035
if (!this.xterm?.raw.element || !this._container || !charWidth || !this._fixedCols) {
20362036
return;
20372037
}

src/vs/workbench/contrib/terminal/browser/terminalProcessManager.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ import Severity from 'vs/base/common/severity';
3838
import { INotificationService } from 'vs/platform/notification/common/notification';
3939
import { IEnvironmentVariableCollection, IMergedEnvironmentVariableCollection } from 'vs/platform/terminal/common/environmentVariable';
4040
import { generateUuid } from 'vs/base/common/uuid';
41-
import { $window } from 'vs/base/browser/window';
42-
import { runWhenWindowIdle } from 'vs/base/browser/dom';
41+
import { getActiveWindow, runWhenWindowIdle } from 'vs/base/browser/dom';
4342

4443
const enum ProcessConstants {
4544
/**
@@ -395,7 +394,7 @@ export class TerminalProcessManager extends Disposable implements ITerminalProce
395394
}
396395

397396
// Report the latency to the pty host when idle
398-
runWhenWindowIdle($window, () => {
397+
runWhenWindowIdle(getActiveWindow(), () => {
399398
this.backend?.getLatency().then(measurements => {
400399
this._logService.info(`Latency measurements for ${this.remoteAuthority ?? 'local'} backend\n${measurements.map(e => `${e.label}: ${e.latency.toFixed(2)}ms`).join('\n')}`);
401400
});
@@ -749,7 +748,7 @@ class SeamlessRelaunchDataFilter extends Disposable {
749748
this.triggerSwap();
750749
}
751750

752-
this._swapTimeout = $window.setTimeout(() => this.triggerSwap(), SeamlessRelaunchConstants.SwapWaitMaximumDuration);
751+
this._swapTimeout = getActiveWindow().setTimeout(() => this.triggerSwap(), SeamlessRelaunchConstants.SwapWaitMaximumDuration);
753752

754753
// Pause all outgoing data events
755754
this._dataListener?.dispose();
@@ -774,7 +773,7 @@ class SeamlessRelaunchDataFilter extends Disposable {
774773
triggerSwap() {
775774
// Clear the swap timeout if it exists
776775
if (this._swapTimeout) {
777-
$window.clearTimeout(this._swapTimeout);
776+
getActiveWindow().clearTimeout(this._swapTimeout);
778777
this._swapTimeout = undefined;
779778
}
780779

src/vs/workbench/contrib/terminal/browser/terminalStatusList.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { listErrorForeground, listWarningForeground } from 'vs/platform/theme/co
1313
import { spinningLoading } from 'vs/platform/theme/common/iconRegistry';
1414
import { ThemeIcon } from 'vs/base/common/themables';
1515
import { ITerminalStatus } from 'vs/workbench/contrib/terminal/common/terminal';
16-
import { $window } from 'vs/base/browser/window';
16+
import { getActiveWindow } from 'vs/base/browser/dom';
1717

1818
/**
1919
* The set of _internal_ terminal statuses, other components building on the terminal should put
@@ -85,11 +85,11 @@ export class TerminalStatusList extends Disposable implements ITerminalStatusLis
8585
status = this._applyAnimationSetting(status);
8686
const outTimeout = this._statusTimeouts.get(status.id);
8787
if (outTimeout) {
88-
$window.clearTimeout(outTimeout);
88+
getActiveWindow().clearTimeout(outTimeout);
8989
this._statusTimeouts.delete(status.id);
9090
}
9191
if (duration && duration > 0) {
92-
const timeout = $window.setTimeout(() => this.remove(status), duration);
92+
const timeout = getActiveWindow().setTimeout(() => this.remove(status), duration);
9393
this._statusTimeouts.set(status.id, timeout);
9494
}
9595
const existingStatus = this._statuses.get(status.id);

src/vs/workbench/contrib/terminal/browser/xterm/xtermTerminal.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import { MouseWheelClassifier } from 'vs/base/browser/ui/scrollbar/scrollableEle
4545
import { IMouseWheelEvent, StandardWheelEvent } from 'vs/base/browser/mouseEvent';
4646
import { AccessibleNotificationEvent, IAccessibleNotificationService } from 'vs/platform/accessibility/common/accessibility';
4747
import { ILayoutService } from 'vs/platform/layout/browser/layoutService';
48-
import { $window } from 'vs/base/browser/window';
4948

5049
const enum RenderConstants {
5150
/**
@@ -214,7 +213,7 @@ export class XtermTerminal extends Disposable implements IXtermTerminal, IDetach
214213
@ILayoutService layoutService: ILayoutService
215214
) {
216215
super();
217-
const font = this._configHelper.getFont(undefined, true);
216+
const font = this._configHelper.getFont(dom.getActiveWindow(), undefined, true);
218217
const config = this._configHelper.config;
219218
const editorOptions = this._configurationService.getValue<IEditorOptions>('editor');
220219

@@ -533,7 +532,7 @@ export class XtermTerminal extends Disposable implements IXtermTerminal, IDetach
533532
}
534533

535534
getFont(): ITerminalFont {
536-
return this._configHelper.getFont(this._core);
535+
return this._configHelper.getFont(dom.getWindow(this.raw.element), this._core);
537536
}
538537

539538
getLongestViewportWrappedLineLength(): number {
@@ -651,9 +650,10 @@ export class XtermTerminal extends Disposable implements IXtermTerminal, IDetach
651650
e.clipboardData.setData('text/html', textAsHtml);
652651
e.preventDefault();
653652
}
654-
$window.document.addEventListener('copy', listener);
655-
$window.document.execCommand('copy');
656-
$window.document.removeEventListener('copy', listener);
653+
const doc = dom.getDocument(this.raw.element);
654+
doc.addEventListener('copy', listener);
655+
doc.execCommand('copy');
656+
doc.removeEventListener('copy', listener);
657657
} else {
658658
await this._clipboardService.writeText(this.raw.getSelection());
659659
}
@@ -976,7 +976,7 @@ export class XtermTerminal extends Disposable implements IXtermTerminal, IDetach
976976
}
977977
}
978978

979-
export function getXtermScaledDimensions(font: ITerminalFont, width: number, height: number) {
979+
export function getXtermScaledDimensions(w: Window, font: ITerminalFont, width: number, height: number) {
980980
if (!font.charWidth || !font.charHeight) {
981981
return null;
982982
}
@@ -985,13 +985,13 @@ export function getXtermScaledDimensions(font: ITerminalFont, width: number, hei
985985
// the use of canvas, window.devicePixelRatio needs to be used here in
986986
// order to be precise. font.charWidth/charHeight alone as insufficient
987987
// when window.devicePixelRatio changes.
988-
const scaledWidthAvailable = width * $window.devicePixelRatio;
988+
const scaledWidthAvailable = width * w.devicePixelRatio;
989989

990-
const scaledCharWidth = font.charWidth * $window.devicePixelRatio + font.letterSpacing;
990+
const scaledCharWidth = font.charWidth * w.devicePixelRatio + font.letterSpacing;
991991
const cols = Math.max(Math.floor(scaledWidthAvailable / scaledCharWidth), 1);
992992

993-
const scaledHeightAvailable = height * $window.devicePixelRatio;
994-
const scaledCharHeight = Math.ceil(font.charHeight * $window.devicePixelRatio);
993+
const scaledHeightAvailable = height * w.devicePixelRatio;
994+
const scaledCharHeight = Math.ceil(font.charHeight * w.devicePixelRatio);
995995
const scaledLineHeight = Math.floor(scaledCharHeight * font.lineHeight);
996996
const rows = Math.max(Math.floor(scaledHeightAvailable / scaledLineHeight), 1);
997997

src/vs/workbench/contrib/terminal/common/terminal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export interface ITerminalConfigHelper {
213213
config: ITerminalConfiguration;
214214

215215
configFontIsMonospace(): boolean;
216-
getFont(): ITerminalFont;
216+
getFont(w: Window): ITerminalFont;
217217
showRecommendations(shellLaunchConfig: IShellLaunchConfig): void;
218218
}
219219

src/vs/workbench/contrib/terminal/electron-sandbox/terminalNativeContribution.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import { INativeHostService } from 'vs/platform/native/common/native';
1313
import { Disposable } from 'vs/base/common/lifecycle';
1414
import { ITerminalService } from 'vs/workbench/contrib/terminal/browser/terminal';
1515
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
16-
import { disposableWindowInterval } from 'vs/base/browser/dom';
17-
import { $window } from 'vs/base/browser/window';
16+
import { disposableWindowInterval, getActiveWindow } from 'vs/base/browser/dom';
1817

1918
export class TerminalNativeContribution extends Disposable implements IWorkbenchContribution {
2019
declare _serviceBrand: undefined;
@@ -63,7 +62,7 @@ export class TerminalNativeContribution extends Disposable implements IWorkbench
6362
// Complete when wait marker file is deleted
6463
return new Promise<void>(resolve => {
6564
let running = false;
66-
const interval = disposableWindowInterval($window, async () => {
65+
const interval = disposableWindowInterval(getActiveWindow(), async () => {
6766
if (!running) {
6867
running = true;
6968
const exists = await this._fileService.exists(path);

0 commit comments

Comments
 (0)