Debian Codegraph @

node/@xterm/addon-webgl/src/renderLayer/LinkRenderLayer.ts

raw
1 /**
2 * Copyright (c) 2017 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { is256Color } from '../CharAtlasUtils';
7 import { INVERTED_DEFAULT_COLOR } from 'browser/renderer/shared/Constants';
8 import { IRenderDimensions } from 'browser/renderer/shared/Types';
9 import { ICoreBrowserService, IThemeService } from 'browser/services/Services';
10 import { ILinkifier2, ILinkifierEvent } from 'browser/Types';
11 import { IOptionsService } from 'common/services/Services';
12 import { Terminal } from '@xterm/xterm';
13 import { BaseRenderLayer } from './BaseRenderLayer';
14
15 export class LinkRenderLayer extends BaseRenderLayer {
16 private _state: ILinkifierEvent | undefined;
17
18 constructor(
19 container: HTMLElement,
20 zIndex: number,
21 terminal: Terminal,
22 linkifier2: ILinkifier2,
23 coreBrowserService: ICoreBrowserService,
24 optionsService: IOptionsService,
25 themeService: IThemeService
26 ) {
27 super(terminal, container, 'link', zIndex, true, coreBrowserService, optionsService, themeService);
28
29 this._register(linkifier2.onShowLinkUnderline(e => this._handleShowLinkUnderline(e)));
30 this._register(linkifier2.onHideLinkUnderline(e => this._handleHideLinkUnderline(e)));
31 }
32
33 public resize(terminal: Terminal, dim: IRenderDimensions): void {
34 super.resize(terminal, dim);
35 // Resizing the canvas discards the contents of the canvas so clear state
36 this._state = undefined;
37 }
38
39 public reset(terminal: Terminal): void {
40 this._clearCurrentLink();
41 }
42
43 private _clearCurrentLink(): void {
44 if (this._state) {
45 this._clearCells(this._state.x1, this._state.y1, this._state.cols - this._state.x1, 1);
46 const middleRowCount = this._state.y2 - this._state.y1 - 1;
47 if (middleRowCount > 0) {
48 this._clearCells(0, this._state.y1 + 1, this._state.cols, middleRowCount);
49 }
50 this._clearCells(0, this._state.y2, this._state.x2, 1);
51 this._state = undefined;
52 }
53 }
54
55 private _handleShowLinkUnderline(e: ILinkifierEvent): void {
56 if (e.fg === INVERTED_DEFAULT_COLOR) {
57 this._ctx.fillStyle = this._themeService.colors.background.css;
58 } else if (e.fg !== undefined && is256Color(e.fg)) {
59 // 256 color support
60 this._ctx.fillStyle = this._themeService.colors.ansi[e.fg!].css;
61 } else {
62 this._ctx.fillStyle = this._themeService.colors.foreground.css;
63 }
64
65 if (e.y1 === e.y2) {
66 // Single line link
67 this._fillBottomLineAtCells(e.x1, e.y1, e.x2 - e.x1);
68 } else {
69 // Multi-line link
70 this._fillBottomLineAtCells(e.x1, e.y1, e.cols - e.x1);
71 for (let y = e.y1 + 1; y < e.y2; y++) {
72 this._fillBottomLineAtCells(0, y, e.cols);
73 }
74 this._fillBottomLineAtCells(0, e.y2, e.x2);
75 }
76 this._state = e;
77 }
78
79 private _handleHideLinkUnderline(e: ILinkifierEvent): void {
80 this._clearCurrentLink();
81 }
82 }