]> git.proxmox.com Git - mirror_xterm.js.git/blob - src/Viewport.ts
Merge pull request #926 from ficristo/search-fix
[mirror_xterm.js.git] / src / Viewport.ts
1 /**
2 * @license MIT
3 */
4
5 import { ITerminal } from './Interfaces';
6 import { CharMeasure } from './utils/CharMeasure';
7
8 /**
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.
11 */
12 export class Viewport {
13 private currentRowHeight: number;
14 private lastRecordedBufferLength: number;
15 private lastRecordedViewportHeight: number;
16 private lastTouchY: number;
17
18 /**
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.
24 */
25 constructor(
26 private terminal: ITerminal,
27 private viewportElement: HTMLElement,
28 private scrollArea: HTMLElement,
29 private charMeasure: CharMeasure
30 ) {
31 this.currentRowHeight = 0;
32 this.lastRecordedBufferLength = 0;
33 this.lastRecordedViewportHeight = 0;
34
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));
38
39 // Perform this async to ensure the CharMeasure is ready.
40 setTimeout(() => this.syncScrollArea(), 0);
41 }
42
43 /**
44 * Refreshes row height, setting line-height, viewport height and scroll area height if
45 * necessary.
46 */
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';
54 }
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;
60 }
61 this.scrollArea.style.height = (this.charMeasure.height * this.lastRecordedBufferLength) + 'px';
62 }
63 }
64
65 /**
66 * Updates dimensions and synchronizes the scroll area if necessary.
67 */
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;
72 this.refresh();
73 } else if (this.lastRecordedViewportHeight !== this.terminal.rows) {
74 // If viewport height changed
75 this.refresh();
76 } else {
77 // If size has changed, refresh viewport
78 if (this.charMeasure.height !== this.currentRowHeight) {
79 this.refresh();
80 }
81 }
82
83 // Sync scrollTop
84 const scrollTop = this.terminal.buffer.ydisp * this.currentRowHeight;
85 if (this.viewportElement.scrollTop !== scrollTop) {
86 this.viewportElement.scrollTop = scrollTop;
87 }
88 }
89
90 /**
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.
94 */
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);
99 }
100
101 /**
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
104 * `Viewport`.
105 * @param ev The mouse wheel event.
106 */
107 public onWheel(ev: WheelEvent) {
108 if (ev.deltaY === 0) {
109 // Do nothing if it's not a vertical scroll event
110 return;
111 }
112 // Fallback to WheelEvent.DOM_DELTA_PIXEL
113 let multiplier = 1;
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;
118 }
119 this.viewportElement.scrollTop += ev.deltaY * multiplier;
120 // Prevent the page from scrolling when the terminal scrolls
121 ev.preventDefault();
122 };
123
124 /**
125 * Handles the touchstart event, recording the touch occurred.
126 * @param ev The touch event.
127 */
128 public onTouchStart(ev: TouchEvent) {
129 this.lastTouchY = ev.touches[0].pageY;
130 };
131
132 /**
133 * Handles the touchmove event, scrolling the viewport if the position shifted.
134 * @param ev The touch event.
135 */
136 public onTouchMove(ev: TouchEvent) {
137 let deltaY = this.lastTouchY - ev.touches[0].pageY;
138 this.lastTouchY = ev.touches[0].pageY;
139 if (deltaY === 0) {
140 return;
141 }
142 this.viewportElement.scrollTop += deltaY;
143 ev.preventDefault();
144 };
145 }