5 import { ITerminal } from './Interfaces';
6 import { CharMeasure } from './utils/CharMeasure';
9 * Represents the viewport of a terminal, the visible area within the larger buffer of output.
10 * Logic for the virtual scroll bar is included in this object.
12 export class Viewport {
13 private currentRowHeight: number;
14 private lastRecordedBufferLength: number;
15 private lastRecordedViewportHeight: number;
18 * Creates a new Viewport.
19 * @param terminal The terminal this viewport belongs to.
20 * @param viewportElement The DOM element acting as the viewport.
21 * @param scrollArea The DOM element acting as the scroll area.
22 * @param charMeasureElement A DOM element used to measure the character size of. the terminal.
25 private terminal: ITerminal,
26 private viewportElement: HTMLElement,
27 private scrollArea: HTMLElement,
28 private charMeasure: CharMeasure
30 this.currentRowHeight = 0;
31 this.lastRecordedBufferLength = 0;
32 this.lastRecordedViewportHeight = 0;
34 this.terminal.on('scroll', this.syncScrollArea.bind(this));
35 this.terminal.on('resize', this.syncScrollArea.bind(this));
36 this.viewportElement.addEventListener('scroll', this.onScroll.bind(this));
38 this.syncScrollArea();
42 * Refreshes row height, setting line-height, viewport height and scroll area height if
44 * @param charSize A character size measurement bounding rect object, if it doesn't exist it will
47 private refresh(): void {
48 if (this.charMeasure.height > 0) {
49 var rowHeightChanged = this.charMeasure.height !== this.currentRowHeight;
50 if (rowHeightChanged) {
51 this.currentRowHeight = this.charMeasure.height;
52 this.viewportElement.style.lineHeight = this.charMeasure.height + 'px';
53 this.terminal.rowContainer.style.lineHeight = this.charMeasure.height + 'px';
55 var viewportHeightChanged = this.lastRecordedViewportHeight !== this.terminal.rows;
56 if (rowHeightChanged || viewportHeightChanged) {
57 this.lastRecordedViewportHeight = this.terminal.rows;
58 this.viewportElement.style.height = this.charMeasure.height * this.terminal.rows + 'px';
60 this.scrollArea.style.height = (this.charMeasure.height * this.lastRecordedBufferLength) + 'px';
65 * Updates dimensions and synchronizes the scroll area if necessary.
67 public syncScrollArea(): void {
68 if (this.lastRecordedBufferLength !== this.terminal.lines.length) {
69 // If buffer height changed
70 this.lastRecordedBufferLength = this.terminal.lines.length;
72 } else if (this.lastRecordedViewportHeight !== this.terminal.rows) {
73 // If viewport height changed
76 // If size has changed, refresh viewport
77 if (this.charMeasure.height !== this.currentRowHeight) {
83 var scrollTop = this.terminal.ydisp * this.currentRowHeight;
84 if (this.viewportElement.scrollTop !== scrollTop) {
85 this.viewportElement.scrollTop = scrollTop;
90 * Handles scroll events on the viewport, calculating the new viewport and requesting the
91 * terminal to scroll to it.
92 * @param ev The scroll event.
94 private onScroll(ev: Event) {
95 var newRow = Math.round(this.viewportElement.scrollTop / this.currentRowHeight);
96 var diff = newRow - this.terminal.ydisp;
97 this.terminal.scrollDisp(diff, true);
101 * Handles mouse wheel events by adjusting the viewport's scrollTop and delegating the actual
102 * scrolling to `onScroll`, this event needs to be attached manually by the consumer of
104 * @param ev The mouse wheel event.
106 public onWheel(ev: WheelEvent) {
107 if (ev.deltaY === 0) {
108 // Do nothing if it's not a vertical scroll event
111 // Fallback to WheelEvent.DOM_DELTA_PIXEL
113 if (ev.deltaMode === WheelEvent.DOM_DELTA_LINE) {
114 multiplier = this.currentRowHeight;
115 } else if (ev.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
116 multiplier = this.currentRowHeight * this.terminal.rows;
118 this.viewportElement.scrollTop += ev.deltaY * multiplier;
119 // Prevent the page from scrolling when the terminal scrolls