2 * Copyright (c) 2019 The xterm.js authors. All rights reserved.
6 import { IRenderer, IRenderDimensions, CharacterJoinerHandler } from 'browser/renderer/Types';
7 import { RenderDebouncer } from 'browser/RenderDebouncer';
8 import { EventEmitter, IEvent } from 'common/EventEmitter';
9 import { Disposable } from 'common/Lifecycle';
10 import { ScreenDprMonitor } from 'browser/ScreenDprMonitor';
11 import { addDisposableDomListener } from 'browser/Lifecycle';
12 import { IColorSet } from 'browser/Types';
13 import { IOptionsService } from 'common/services/Services';
14 import { ICharSizeService, IRenderService } from 'browser/services/Services';
16 export class RenderService extends Disposable implements IRenderService {
19 private _renderDebouncer: RenderDebouncer;
20 private _screenDprMonitor: ScreenDprMonitor;
22 private _isPaused: boolean = false;
23 private _needsFullRefresh: boolean = false;
24 private _canvasWidth: number = 0;
25 private _canvasHeight: number = 0;
27 private _onDimensionsChange = new EventEmitter<IRenderDimensions>();
28 public get onDimensionsChange(): IEvent<IRenderDimensions> { return this._onDimensionsChange.event; }
29 private _onRender = new EventEmitter<{ start: number, end: number }>();
30 public get onRender(): IEvent<{ start: number, end: number }> { return this._onRender.event; }
31 private _onRefreshRequest = new EventEmitter<{ start: number, end: number }>();
32 public get onRefreshRequest(): IEvent<{ start: number, end: number }> { return this._onRefreshRequest.event; }
34 public get dimensions(): IRenderDimensions { return this._renderer.dimensions; }
37 private _renderer: IRenderer,
38 private _rowCount: number,
39 readonly screenElement: HTMLElement,
40 @IOptionsService readonly optionsService: IOptionsService,
41 @ICharSizeService readonly charSizeService: ICharSizeService
44 this._renderDebouncer = new RenderDebouncer((start, end) => this._renderRows(start, end));
45 this.register(this._renderDebouncer);
47 this._screenDprMonitor = new ScreenDprMonitor();
48 this._screenDprMonitor.setListener(() => this.onDevicePixelRatioChange());
49 this.register(this._screenDprMonitor);
51 this.register(optionsService.onOptionChange(() => this._renderer.onOptionsChanged()));
52 this.register(charSizeService.onCharSizeChange(() => this.onCharSizeChanged()));
54 // dprchange should handle this case, we need this as well for browsers that don't support the
56 this.register(addDisposableDomListener(window, 'resize', () => this.onDevicePixelRatioChange()));
58 // Detect whether IntersectionObserver is detected and enable renderer pause
59 // and resume based on terminal visibility if so
60 if ('IntersectionObserver' in window) {
61 const observer = new IntersectionObserver(e => this._onIntersectionChange(e[e.length - 1]), { threshold: 0 });
62 observer.observe(screenElement);
63 this.register({ dispose: () => observer.disconnect() });
67 private _onIntersectionChange(entry: IntersectionObserverEntry): void {
68 this._isPaused = entry.intersectionRatio === 0;
69 if (!this._isPaused && this._needsFullRefresh) {
70 this.refreshRows(0, this._rowCount - 1);
71 this._needsFullRefresh = false;
75 public refreshRows(start: number, end: number): void {
77 this._needsFullRefresh = true;
80 this._renderDebouncer.refresh(start, end, this._rowCount);
83 private _renderRows(start: number, end: number): void {
84 this._renderer.renderRows(start, end);
85 this._onRender.fire({ start, end });
88 public resize(cols: number, rows: number): void {
89 this._rowCount = rows;
90 this._fireOnCanvasResize();
93 public changeOptions(): void {
94 this._renderer.onOptionsChanged();
95 this._fireOnCanvasResize();
98 private _fireOnCanvasResize(): void {
99 // Don't fire the event if the dimensions haven't changed
100 if (this._renderer.dimensions.canvasWidth === this._canvasWidth && this._renderer.dimensions.canvasHeight === this._canvasHeight) {
103 this._onDimensionsChange.fire(this._renderer.dimensions);
106 public dispose(): void {
107 this._renderer.dispose();
110 public setRenderer(renderer: IRenderer): void {
111 // TODO: RenderService should be the only one to dispose the renderer
112 this._renderer.dispose();
113 this._renderer = renderer;
114 this.refreshRows(0, this._rowCount - 1);
117 private _fullRefresh(): void {
118 if (this._isPaused) {
119 this._needsFullRefresh = true;
121 this.refreshRows(0, this._rowCount - 1);
125 public setColors(colors: IColorSet): void {
126 this._renderer.setColors(colors);
130 public onDevicePixelRatioChange(): void {
131 this._renderer.onDevicePixelRatioChange();
132 this.refreshRows(0, this._rowCount - 1);
135 public onResize(cols: number, rows: number): void {
136 this._renderer.onResize(cols, rows);
140 // TODO: Is this useful when we have onResize?
141 public onCharSizeChanged(): void {
142 this._renderer.onCharSizeChanged();
145 public onBlur(): void {
146 this._renderer.onBlur();
149 public onFocus(): void {
150 this._renderer.onFocus();
153 public onSelectionChanged(start: [number, number], end: [number, number], columnSelectMode: boolean): void {
154 this._renderer.onSelectionChanged(start, end, columnSelectMode);
157 public onCursorMove(): void {
158 this._renderer.onCursorMove();
161 public clear(): void {
162 this._renderer.clear();
165 public registerCharacterJoiner(handler: CharacterJoinerHandler): number {
166 return this._renderer.registerCharacterJoiner(handler);
169 public deregisterCharacterJoiner(joinerId: number): boolean {
170 return this._renderer.deregisterCharacterJoiner(joinerId);