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;
16 private lastTouchY: number;
19 * Creates a new Viewport.
20 * @param terminal The terminal this viewport belongs to.
21 * @param viewportElement The DOM element acting as the viewport.
22 * @param scrollArea The DOM element acting as the scroll area.
23 * @param charMeasure A DOM element used to measure the character size of. the terminal.
26 private terminal: ITerminal,
27 private viewportElement: HTMLElement,
28 private scrollArea: HTMLElement,
29 private charMeasure: CharMeasure
31 this.currentRowHeight = 0;
32 this.lastRecordedBufferLength = 0;
33 this.lastRecordedViewportHeight = 0;
35 this.terminal.on('scroll', this.syncScrollArea.bind(this));
36 this.terminal.on('resize', this.syncScrollArea.bind(this));
37 this.viewportElement.addEventListener('scroll', this.onScroll.bind(this));
39 // Perform this async to ensure the CharMeasure is ready.
40 setTimeout(() => this.syncScrollArea(), 0);
44 * Refreshes row height, setting line-height, viewport height and scroll area height if
47 private refresh(): void {
48 if (this.charMeasure.height > 0) {
49 const 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 const 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';
59 this.terminal.selectionContainer.style.height = this.viewportElement.style.height;
61 this.scrollArea.style.height = (this.charMeasure.height * this.lastRecordedBufferLength) + 'px';
66 * Updates dimensions and synchronizes the scroll area if necessary.
68 public syncScrollArea(): void {
69 if (this.lastRecordedBufferLength !== this.terminal.buffer.lines.length) {
70 // If buffer height changed
71 this.lastRecordedBufferLength = this.terminal.buffer.lines.length;
73 } else if (this.lastRecordedViewportHeight !== this.terminal.rows) {
74 // If viewport height changed
77 // If size has changed, refresh viewport
78 if (this.charMeasure.height !== this.currentRowHeight) {
84 const scrollTop = this.terminal.buffer.ydisp * this.currentRowHeight;
85 if (this.viewportElement.scrollTop !== scrollTop) {
86 this.viewportElement.scrollTop = scrollTop;
91 * Handles scroll events on the viewport, calculating the new viewport and requesting the
92 * terminal to scroll to it.
93 * @param ev The scroll event.
95 private onScroll(ev: Event) {
96 const newRow = Math.round(this.viewportElement.scrollTop / this.currentRowHeight);
97 const diff = newRow - this.terminal.buffer.ydisp;
98 this.terminal.scrollDisp(diff, true);
102 * Handles mouse wheel events by adjusting the viewport's scrollTop and delegating the actual
103 * scrolling to `onScroll`, this event needs to be attached manually by the consumer of
105 * @param ev The mouse wheel event.
107 public onWheel(ev: WheelEvent) {
108 if (ev.deltaY === 0) {
109 // Do nothing if it's not a vertical scroll event
112 // Fallback to WheelEvent.DOM_DELTA_PIXEL
114 if (ev.deltaMode === WheelEvent.DOM_DELTA_LINE) {
115 multiplier = this.currentRowHeight;
116 } else if (ev.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
117 multiplier = this.currentRowHeight * this.terminal.rows;
119 this.viewportElement.scrollTop += ev.deltaY * multiplier;
120 // Prevent the page from scrolling when the terminal scrolls
125 * Handles the touchstart event, recording the touch occurred.
126 * @param ev The touch event.
128 public onTouchStart(ev: TouchEvent) {
129 this.lastTouchY = ev.touches[0].pageY;
133 * Handles the touchmove event, scrolling the viewport if the position shifted.
134 * @param ev The touch event.
136 public onTouchMove(ev: TouchEvent) {
137 let deltaY = this.lastTouchY - ev.touches[0].pageY;
138 this.lastTouchY = ev.touches[0].pageY;
142 this.viewportElement.scrollTop += deltaY;