Skip to content

Commit

Permalink
Create initial XtermTerminal class
Browse files Browse the repository at this point in the history
Part of #136055
  • Loading branch information
Tyriar committed Oct 29, 2021
1 parent b70ee4d commit 220d404
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 121 deletions.
143 changes: 143 additions & 0 deletions src/vs/workbench/contrib/terminal/browser/addons/xtermTerminal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import type { ITheme, RendererType, Terminal as IXtermTerminal } from 'xterm';
// import { IXtermCore } from 'vs/workbench/contrib/terminal/browser/xterm-private';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { TerminalConfigHelper } from 'vs/workbench/contrib/terminal/browser/terminalConfigHelper';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
import { TerminalLocation, TerminalSettingId } from 'vs/platform/terminal/common/terminal';
import { IColorTheme, IThemeService } from 'vs/platform/theme/common/themeService';
import { IViewDescriptorService, ViewContainerLocation } from 'vs/workbench/common/views';
import { editorBackground } from 'vs/platform/theme/common/colorRegistry';
import { ansiColorIdentifiers, TERMINAL_BACKGROUND_COLOR, TERMINAL_CURSOR_BACKGROUND_COLOR, TERMINAL_CURSOR_FOREGROUND_COLOR, TERMINAL_FOREGROUND_COLOR, TERMINAL_SELECTION_BACKGROUND_COLOR } from 'vs/workbench/contrib/terminal/common/terminalColorRegistry';
import { TERMINAL_VIEW_ID } from 'vs/workbench/contrib/terminal/common/terminal';
import { PANEL_BACKGROUND, SIDE_BAR_BACKGROUND } from 'vs/workbench/common/theme';
import { Color } from 'vs/base/common/color';

export const enum XtermTerminalConstants {
DefaultCols = 80,
DefaultRows = 30,
MaxSupportedCols = 5000,
MaxCanvasWidth = 8000
}

/**
* Wraps the xterm object with additional functionality. Interaction with the backing process is out
* of the scope of this class.
*/
export class XtermTerminal extends DisposableStore {
/** The raw xterm.js instance */
readonly raw: IXtermTerminal;
target?: TerminalLocation;

// private _core: IXtermCore;
private static _suggestedRendererType: 'canvas' | 'dom' | undefined = undefined;
private _cols: number = 0;
private _rows: number = 0;

/**
* @param xtermCtor The xterm.js constructor, this is passed in so it can be fetched lazily
* outside of this class such that {@link raw} is not nullable.
*/
constructor(
xtermCtor: typeof IXtermTerminal,
private readonly _configHelper: TerminalConfigHelper,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@IThemeService private readonly _themeService: IThemeService,
@IViewDescriptorService private readonly _viewDescriptorService: IViewDescriptorService,
) {
super();

const font = this._configHelper.getFont(undefined, true);
const config = this._configHelper.config;
const editorOptions = this._configurationService.getValue<IEditorOptions>('editor');

this.raw = this.add(new xtermCtor({
cols: this._cols || XtermTerminalConstants.DefaultCols,
rows: this._rows || XtermTerminalConstants.DefaultRows,
altClickMovesCursor: config.altClickMovesCursor && editorOptions.multiCursorModifier === 'alt',
scrollback: config.scrollback,
theme: this._getXtermTheme(),
drawBoldTextInBrightColors: config.drawBoldTextInBrightColors,
fontFamily: font.fontFamily,
fontWeight: config.fontWeight,
fontWeightBold: config.fontWeightBold,
fontSize: font.fontSize,
letterSpacing: font.letterSpacing,
lineHeight: font.lineHeight,
minimumContrastRatio: config.minimumContrastRatio,
cursorBlink: config.cursorBlinking,
cursorStyle: config.cursorStyle === 'line' ? 'bar' : config.cursorStyle,
cursorWidth: config.cursorWidth,
bellStyle: 'none',
macOptionIsMeta: config.macOptionIsMeta,
macOptionClickForcesSelection: config.macOptionClickForcesSelection,
rightClickSelectsWord: config.rightClickBehavior === 'selectWord',
fastScrollModifier: 'alt',
fastScrollSensitivity: editorOptions.fastScrollSensitivity,
scrollSensitivity: editorOptions.mouseWheelScrollSensitivity,
rendererType: this._getBuiltInXtermRenderer(config.gpuAcceleration, XtermTerminal._suggestedRendererType),
wordSeparator: config.wordSeparators
}));

this.add(this._configurationService.onDidChangeConfiguration(async e => {
if (e.affectsConfiguration(TerminalSettingId.GpuAcceleration)) {
XtermTerminal._suggestedRendererType = undefined;
}
}));
}

private _getBuiltInXtermRenderer(gpuAcceleration: string, suggestedRendererType?: string): RendererType {
let rendererType: RendererType = 'canvas';
if (gpuAcceleration === 'off' || (gpuAcceleration === 'auto' && suggestedRendererType === 'dom')) {
rendererType = 'dom';
}
return rendererType;
}

private _getXtermTheme(theme?: IColorTheme): ITheme {
if (!theme) {
theme = this._themeService.getColorTheme();
}

const location = this._viewDescriptorService.getViewLocationById(TERMINAL_VIEW_ID)!;
const foregroundColor = theme.getColor(TERMINAL_FOREGROUND_COLOR);
let backgroundColor: Color | undefined;
if (this.target === TerminalLocation.Editor) {
backgroundColor = theme.getColor(TERMINAL_BACKGROUND_COLOR) || theme.getColor(editorBackground);
} else {
backgroundColor = theme.getColor(TERMINAL_BACKGROUND_COLOR) || (location === ViewContainerLocation.Panel ? theme.getColor(PANEL_BACKGROUND) : theme.getColor(SIDE_BAR_BACKGROUND));
}
const cursorColor = theme.getColor(TERMINAL_CURSOR_FOREGROUND_COLOR) || foregroundColor;
const cursorAccentColor = theme.getColor(TERMINAL_CURSOR_BACKGROUND_COLOR) || backgroundColor;
const selectionColor = theme.getColor(TERMINAL_SELECTION_BACKGROUND_COLOR);

return {
background: backgroundColor ? backgroundColor.toString() : undefined,
foreground: foregroundColor ? foregroundColor.toString() : undefined,
cursor: cursorColor ? cursorColor.toString() : undefined,
cursorAccent: cursorAccentColor ? cursorAccentColor.toString() : undefined,
selection: selectionColor ? selectionColor.toString() : undefined,
black: theme.getColor(ansiColorIdentifiers[0])!.toString(),
red: theme.getColor(ansiColorIdentifiers[1])!.toString(),
green: theme.getColor(ansiColorIdentifiers[2])!.toString(),
yellow: theme.getColor(ansiColorIdentifiers[3])!.toString(),
blue: theme.getColor(ansiColorIdentifiers[4])!.toString(),
magenta: theme.getColor(ansiColorIdentifiers[5])!.toString(),
cyan: theme.getColor(ansiColorIdentifiers[6])!.toString(),
white: theme.getColor(ansiColorIdentifiers[7])!.toString(),
brightBlack: theme.getColor(ansiColorIdentifiers[8])!.toString(),
brightRed: theme.getColor(ansiColorIdentifiers[9])!.toString(),
brightGreen: theme.getColor(ansiColorIdentifiers[10])!.toString(),
brightYellow: theme.getColor(ansiColorIdentifiers[11])!.toString(),
brightBlue: theme.getColor(ansiColorIdentifiers[12])!.toString(),
brightMagenta: theme.getColor(ansiColorIdentifiers[13])!.toString(),
brightCyan: theme.getColor(ansiColorIdentifiers[14])!.toString(),
brightWhite: theme.getColor(ansiColorIdentifiers[15])!.toString()
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { TerminalProtocolLinkProvider } from 'vs/workbench/contrib/terminal/brow
import { TerminalValidatedLocalLinkProvider, lineAndColumnClause, unixLocalLinkClause, winLocalLinkClause, winDrivePrefix, winLineAndColumnMatchIndex, unixLineAndColumnMatchIndex, lineAndColumnClauseGroupCount } from 'vs/workbench/contrib/terminal/browser/links/terminalValidatedLocalLinkProvider';
import { TerminalWordLinkProvider } from 'vs/workbench/contrib/terminal/browser/links/terminalWordLinkProvider';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { XTermCore } from 'vs/workbench/contrib/terminal/browser/xterm-private';
import { IXtermCore } from 'vs/workbench/contrib/terminal/browser/xterm-private';
import { TerminalHover, ILinkHoverTargetOptions } from 'vs/workbench/contrib/terminal/browser/widgets/terminalHoverWidget';
import { TerminalLink } from 'vs/workbench/contrib/terminal/browser/links/terminalLink';
import { TerminalExternalLinkProviderAdapter } from 'vs/workbench/contrib/terminal/browser/links/terminalExternalLinkProviderAdapter';
Expand Down Expand Up @@ -94,7 +94,7 @@ export class TerminalLinkManager extends DisposableStore {
return;
}

const core = (this._xterm as any)._core as XTermCore;
const core = (this._xterm as any)._core as IXtermCore;
const cellDimensions = {
width: core._renderService.dimensions.actualCellWidth,
height: core._renderService.dimensions.actualCellHeight
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { InstallRecommendedExtensionAction } from 'vs/workbench/contrib/extensions/browser/extensionsActions';
import { IProductService } from 'vs/platform/product/common/productService';
import { XTermCore } from 'vs/workbench/contrib/terminal/browser/xterm-private';
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';

Expand Down Expand Up @@ -154,7 +154,7 @@ export class TerminalConfigHelper implements IBrowserTerminalConfigHelper {
* Gets the font information based on the terminal.integrated.fontFamily
* terminal.integrated.fontSize, terminal.integrated.lineHeight configuration properties
*/
getFont(xtermCore?: XTermCore, excludeDimensions?: boolean): ITerminalFont {
getFont(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
Loading

0 comments on commit 220d404

Please sign in to comment.