]>
Commit | Line | Data |
---|---|---|
7ff03bb4 | 1 | /** |
1d300911 | 2 | * @license MIT |
7ff03bb4 DI |
3 | */ |
4 | ||
e0d98711 DI |
5 | import { ITerminal } from './Interfaces'; |
6 | ||
7ff03bb4 DI |
7 | /** |
8 | * Represents the viewport of a terminal, the visible area within the larger buffer of output. | |
9 | * Logic for the virtual scroll bar is included in this object. | |
7ff03bb4 | 10 | */ |
e0d98711 | 11 | export class Viewport { |
e0d98711 DI |
12 | private currentRowHeight: number; |
13 | private lastRecordedBufferLength: number; | |
14 | private lastRecordedViewportHeight: number; | |
6ffb8f54 | 15 | private isRefreshQueued: boolean; |
7ff03bb4 | 16 | |
81dc48ef DI |
17 | /** |
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. | |
23 | */ | |
e0d98711 DI |
24 | constructor( |
25 | private terminal: ITerminal, | |
26 | private viewportElement: HTMLElement, | |
27 | private scrollArea: HTMLElement, | |
28 | private charMeasureElement: HTMLElement | |
29 | ) { | |
30 | this.currentRowHeight = 0; | |
31 | this.lastRecordedBufferLength = 0; | |
32 | this.lastRecordedViewportHeight = 0; | |
6ffb8f54 | 33 | this.isRefreshQueued = false; |
7ff03bb4 | 34 | |
e0d98711 DI |
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)); | |
7ff03bb4 | 38 | |
e0d98711 | 39 | this.syncScrollArea(); |
6ffb8f54 DI |
40 | this.refreshLoop(); |
41 | } | |
42 | ||
43 | private refreshLoop(): void { | |
44 | if (this.isRefreshQueued) { | |
45 | this.refresh(); | |
46 | this.isRefreshQueued = false; | |
47 | } | |
48 | window.requestAnimationFrame(this.refreshLoop.bind(this)); | |
7ff03bb4 DI |
49 | } |
50 | ||
e0d98711 DI |
51 | /** |
52 | * Refreshes row height, setting line-height, viewport height and scroll area height if | |
53 | * necessary. | |
54 | * @param charSize A character size measurement bounding rect object, if it doesn't exist it will | |
55 | * be created. | |
56 | */ | |
57 | private refresh(charSize?: ClientRect): void { | |
58 | var size = charSize || this.charMeasureElement.getBoundingClientRect(); | |
59 | if (size.height > 0) { | |
60 | var rowHeightChanged = size.height !== this.currentRowHeight; | |
61 | if (rowHeightChanged) { | |
62 | this.currentRowHeight = size.height; | |
63 | this.viewportElement.style.lineHeight = size.height + 'px'; | |
64 | this.terminal.rowContainer.style.lineHeight = size.height + 'px'; | |
65 | } | |
66 | var viewportHeightChanged = this.lastRecordedViewportHeight !== this.terminal.rows; | |
67 | if (rowHeightChanged || viewportHeightChanged) { | |
68 | this.lastRecordedViewportHeight = this.terminal.rows; | |
69 | this.viewportElement.style.height = size.height * this.terminal.rows + 'px'; | |
70 | } | |
71 | this.scrollArea.style.height = (size.height * this.lastRecordedBufferLength) + 'px'; | |
72 | } | |
7ff03bb4 | 73 | } |
7ff03bb4 | 74 | |
e0d98711 DI |
75 | /** |
76 | * Updates dimensions and synchronizes the scroll area if necessary. | |
77 | */ | |
78 | public syncScrollArea(): void { | |
79 | if (this.lastRecordedBufferLength !== this.terminal.lines.length) { | |
80 | // If buffer height changed | |
81 | this.lastRecordedBufferLength = this.terminal.lines.length; | |
6ffb8f54 | 82 | this.isRefreshQueued = true; |
e0d98711 DI |
83 | } else if (this.lastRecordedViewportHeight !== this.terminal.rows) { |
84 | // If viewport height changed | |
6ffb8f54 | 85 | this.isRefreshQueued = true; |
e0d98711 DI |
86 | } else { |
87 | // If size has changed, refresh viewport | |
88 | var size = this.charMeasureElement.getBoundingClientRect(); | |
89 | if (size.height !== this.currentRowHeight) { | |
6ffb8f54 | 90 | this.isRefreshQueued = true; |
e0d98711 DI |
91 | } |
92 | } | |
7ff03bb4 | 93 | |
e0d98711 DI |
94 | // Sync scrollTop |
95 | var scrollTop = this.terminal.ydisp * this.currentRowHeight; | |
96 | if (this.viewportElement.scrollTop !== scrollTop) { | |
97 | this.viewportElement.scrollTop = scrollTop; | |
98 | } | |
7ff03bb4 | 99 | } |
e0d98711 DI |
100 | |
101 | /** | |
102 | * Handles scroll events on the viewport, calculating the new viewport and requesting the | |
103 | * terminal to scroll to it. | |
104 | * @param ev The scroll event. | |
105 | */ | |
106 | private onScroll(ev: Event) { | |
107 | var newRow = Math.round(this.viewportElement.scrollTop / this.currentRowHeight); | |
108 | var diff = newRow - this.terminal.ydisp; | |
109 | this.terminal.scrollDisp(diff, true); | |
7ff03bb4 | 110 | } |
7ff03bb4 | 111 | |
e0d98711 DI |
112 | /** |
113 | * Handles mouse wheel events by adjusting the viewport's scrollTop and delegating the actual | |
114 | * scrolling to `onScroll`, this event needs to be attached manually by the consumer of | |
115 | * `Viewport`. | |
116 | * @param ev The mouse wheel event. | |
117 | */ | |
118 | public onWheel(ev: WheelEvent) { | |
119 | if (ev.deltaY === 0) { | |
120 | // Do nothing if it's not a vertical scroll event | |
121 | return; | |
122 | } | |
123 | // Fallback to WheelEvent.DOM_DELTA_PIXEL | |
124 | var multiplier = 1; | |
125 | if (ev.deltaMode === WheelEvent.DOM_DELTA_LINE) { | |
126 | multiplier = this.currentRowHeight; | |
127 | } else if (ev.deltaMode === WheelEvent.DOM_DELTA_PAGE) { | |
128 | multiplier = this.currentRowHeight * this.terminal.rows; | |
129 | } | |
130 | this.viewportElement.scrollTop += ev.deltaY * multiplier; | |
131 | // Prevent the page from scrolling when the terminal scrolls | |
132 | ev.preventDefault(); | |
133 | }; | |
134 | } |