Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace window usage within terminal #198360

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/vs/base/browser/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,13 @@ export function getActiveDocument(): Document {
return documents.find(doc => doc.hasFocus()) ?? document;
}

export function getDocument(element: Node | undefined | null): Document;
export function getDocument(event: UIEvent | undefined | null): Document;
export function getDocument(e: unknown): Document {
const candidateNode = e as Node | undefined | null;
return getWindow(candidateNode).document;
}

export function getActiveWindow(): CodeWindow {
const document = getActiveDocument();
return (document.defaultView?.window ?? mainWindow) as CodeWindow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { IXtermCore } from 'vs/workbench/contrib/terminal/browser/xterm-private'
import { IShellLaunchConfig } from 'vs/platform/terminal/common/terminal';
import { isLinux, isWindows } from 'vs/base/common/platform';
import { Disposable } from 'vs/base/common/lifecycle';
import { $window } from 'vs/base/browser/window';

const enum FontConstants {
MinimumFontSize = 6,
Expand Down Expand Up @@ -119,7 +118,7 @@ export class TerminalConfigHelper extends Disposable implements IBrowserTerminal
return rect;
}

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

// Bounding client rect was invalid, use last font measurement if available.
Expand All @@ -143,10 +142,10 @@ export class TerminalConfigHelper extends Disposable implements IBrowserTerminal
if (this.config.gpuAcceleration === 'off') {
this._lastFontMeasurement.charWidth = rect.width;
} else {
const deviceCharWidth = Math.floor(rect.width * $window.devicePixelRatio);
const deviceCharWidth = Math.floor(rect.width * w.devicePixelRatio);
const deviceCellWidth = deviceCharWidth + Math.round(letterSpacing);
const cssCellWidth = deviceCellWidth / $window.devicePixelRatio;
this._lastFontMeasurement.charWidth = cssCellWidth - Math.round(letterSpacing) / $window.devicePixelRatio;
const cssCellWidth = deviceCellWidth / w.devicePixelRatio;
this._lastFontMeasurement.charWidth = cssCellWidth - Math.round(letterSpacing) / w.devicePixelRatio;
}
}

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

let fontFamily = this.config.fontFamily || editorConfig.fontFamily || EDITOR_FONT_DEFAULTS.fontFamily;
Expand Down Expand Up @@ -201,13 +200,13 @@ export class TerminalConfigHelper extends Disposable implements IBrowserTerminal
letterSpacing,
lineHeight,
charHeight: cellDims.height / lineHeight,
charWidth: cellDims.width - Math.round(letterSpacing) / $window.devicePixelRatio
charWidth: cellDims.width - Math.round(letterSpacing) / w.devicePixelRatio
};
}
}

// Fall back to measuring the font ourselves
return this._measureFont(fontFamily, fontSize, letterSpacing, lineHeight);
return this._measureFont(w, fontFamily, fontSize, letterSpacing, lineHeight);
}

private _clampInt<T>(source: any, minimum: number, maximum: number, fallback: T): number | T {
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/contrib/terminal/browser/terminalGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ViewContainerLocation, IViewDescriptorService } from 'vs/workbench/comm
import { IShellLaunchConfig, ITerminalTabLayoutInfoById, TerminalLocation } from 'vs/platform/terminal/common/terminal';
import { TerminalStatus } from 'vs/workbench/contrib/terminal/browser/terminalStatusList';
import { getPartByLocation } from 'vs/workbench/browser/parts/views/viewsService';
import { getWindow } from 'vs/base/browser/dom';

const enum Constants {
/**
Expand Down Expand Up @@ -572,7 +573,7 @@ export class TerminalGroup extends Disposable implements ITerminalGroup {
}

const isHorizontal = (direction === Direction.Left || direction === Direction.Right);
const font = this._terminalService.configHelper.getFont();
const font = this._terminalService.configHelper.getFont(getWindow(this._groupElement));
// TODO: Support letter spacing and line height
const charSize = (isHorizontal ? font.charWidth : font.charHeight);
if (charSize) {
Expand Down
8 changes: 4 additions & 4 deletions src/vs/workbench/contrib/terminal/browser/terminalInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,8 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
return null;
}

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

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

private async _addScrollbar(): Promise<void> {
const charWidth = (this.xterm ? this.xterm.getFont() : this._configHelper.getFont()).charWidth;
const charWidth = (this.xterm ? this.xterm.getFont() : this._configHelper.getFont(dom.getWindow(this.domElement))).charWidth;
if (!this.xterm?.raw.element || !this._container || !charWidth || !this._fixedCols) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ import Severity from 'vs/base/common/severity';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { IEnvironmentVariableCollection, IMergedEnvironmentVariableCollection } from 'vs/platform/terminal/common/environmentVariable';
import { generateUuid } from 'vs/base/common/uuid';
import { $window } from 'vs/base/browser/window';
import { runWhenWindowIdle } from 'vs/base/browser/dom';
import { getActiveWindow, runWhenWindowIdle } from 'vs/base/browser/dom';

const enum ProcessConstants {
/**
Expand Down Expand Up @@ -395,7 +394,7 @@ export class TerminalProcessManager extends Disposable implements ITerminalProce
}

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

this._swapTimeout = $window.setTimeout(() => this.triggerSwap(), SeamlessRelaunchConstants.SwapWaitMaximumDuration);
this._swapTimeout = getActiveWindow().setTimeout(() => this.triggerSwap(), SeamlessRelaunchConstants.SwapWaitMaximumDuration);

// Pause all outgoing data events
this._dataListener?.dispose();
Expand All @@ -774,7 +773,7 @@ class SeamlessRelaunchDataFilter extends Disposable {
triggerSwap() {
// Clear the swap timeout if it exists
if (this._swapTimeout) {
$window.clearTimeout(this._swapTimeout);
getActiveWindow().clearTimeout(this._swapTimeout);
this._swapTimeout = undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { listErrorForeground, listWarningForeground } from 'vs/platform/theme/co
import { spinningLoading } from 'vs/platform/theme/common/iconRegistry';
import { ThemeIcon } from 'vs/base/common/themables';
import { ITerminalStatus } from 'vs/workbench/contrib/terminal/common/terminal';
import { $window } from 'vs/base/browser/window';
import { getActiveWindow } from 'vs/base/browser/dom';

/**
* The set of _internal_ terminal statuses, other components building on the terminal should put
Expand Down Expand Up @@ -85,11 +85,11 @@ export class TerminalStatusList extends Disposable implements ITerminalStatusLis
status = this._applyAnimationSetting(status);
const outTimeout = this._statusTimeouts.get(status.id);
if (outTimeout) {
$window.clearTimeout(outTimeout);
getActiveWindow().clearTimeout(outTimeout);
this._statusTimeouts.delete(status.id);
}
if (duration && duration > 0) {
const timeout = $window.setTimeout(() => this.remove(status), duration);
const timeout = getActiveWindow().setTimeout(() => this.remove(status), duration);
this._statusTimeouts.set(status.id, timeout);
}
const existingStatus = this._statuses.get(status.id);
Expand Down
22 changes: 11 additions & 11 deletions src/vs/workbench/contrib/terminal/browser/xterm/xtermTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import { MouseWheelClassifier } from 'vs/base/browser/ui/scrollbar/scrollableEle
import { IMouseWheelEvent, StandardWheelEvent } from 'vs/base/browser/mouseEvent';
import { AccessibleNotificationEvent, IAccessibleNotificationService } from 'vs/platform/accessibility/common/accessibility';
import { ILayoutService } from 'vs/platform/layout/browser/layoutService';
import { $window } from 'vs/base/browser/window';

const enum RenderConstants {
/**
Expand Down Expand Up @@ -214,7 +213,7 @@ export class XtermTerminal extends Disposable implements IXtermTerminal, IDetach
@ILayoutService layoutService: ILayoutService
) {
super();
const font = this._configHelper.getFont(undefined, true);
const font = this._configHelper.getFont(dom.getActiveWindow(), undefined, true);
const config = this._configHelper.config;
const editorOptions = this._configurationService.getValue<IEditorOptions>('editor');

Expand Down Expand Up @@ -533,7 +532,7 @@ export class XtermTerminal extends Disposable implements IXtermTerminal, IDetach
}

getFont(): ITerminalFont {
return this._configHelper.getFont(this._core);
return this._configHelper.getFont(dom.getWindow(this.raw.element), this._core);
}

getLongestViewportWrappedLineLength(): number {
Expand Down Expand Up @@ -651,9 +650,10 @@ export class XtermTerminal extends Disposable implements IXtermTerminal, IDetach
e.clipboardData.setData('text/html', textAsHtml);
e.preventDefault();
}
$window.document.addEventListener('copy', listener);
$window.document.execCommand('copy');
$window.document.removeEventListener('copy', listener);
const doc = dom.getDocument(this.raw.element);
doc.addEventListener('copy', listener);
doc.execCommand('copy');
doc.removeEventListener('copy', listener);
} else {
await this._clipboardService.writeText(this.raw.getSelection());
}
Expand Down Expand Up @@ -976,7 +976,7 @@ export class XtermTerminal extends Disposable implements IXtermTerminal, IDetach
}
}

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

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

const scaledHeightAvailable = height * $window.devicePixelRatio;
const scaledCharHeight = Math.ceil(font.charHeight * $window.devicePixelRatio);
const scaledHeightAvailable = height * w.devicePixelRatio;
const scaledCharHeight = Math.ceil(font.charHeight * w.devicePixelRatio);
const scaledLineHeight = Math.floor(scaledCharHeight * font.lineHeight);
const rows = Math.max(Math.floor(scaledHeightAvailable / scaledLineHeight), 1);

Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/contrib/terminal/common/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export interface ITerminalConfigHelper {
config: ITerminalConfiguration;

configFontIsMonospace(): boolean;
getFont(): ITerminalFont;
getFont(w: any): ITerminalFont;
showRecommendations(shellLaunchConfig: IShellLaunchConfig): void;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { INativeHostService } from 'vs/platform/native/common/native';
import { Disposable } from 'vs/base/common/lifecycle';
import { ITerminalService } from 'vs/workbench/contrib/terminal/browser/terminal';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { disposableWindowInterval } from 'vs/base/browser/dom';
import { $window } from 'vs/base/browser/window';
import { disposableWindowInterval, getActiveWindow } from 'vs/base/browser/dom';

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