]>
Commit | Line | Data |
---|---|---|
7ff03bb4 | 1 | /** |
1d300911 | 2 | * @license MIT |
7ff03bb4 DI |
3 | */ |
4 | ||
e0d98711 | 5 | import { ITerminal } from './Interfaces'; |
74483fb2 | 6 | import { CharMeasure } from './utils/CharMeasure'; |
e0d98711 | 7 | |
7ff03bb4 DI |
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. | |
7ff03bb4 | 11 | */ |
e0d98711 | 12 | export class Viewport { |
e0d98711 DI |
13 | private currentRowHeight: number; |
14 | private lastRecordedBufferLength: number; | |
15 | private lastRecordedViewportHeight: number; | |
62233428 | 16 | private lastTouchY: number; |
7ff03bb4 | 17 | |
81dc48ef DI |
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. | |
de82bd00 | 23 | * @param charMeasure A DOM element used to measure the character size of. the terminal. |
81dc48ef | 24 | */ |
e0d98711 DI |
25 | constructor( |
26 | private terminal: ITerminal, | |
27 | private viewportElement: HTMLElement, | |
28 | private scrollArea: HTMLElement, | |
74483fb2 | 29 | private charMeasure: CharMeasure |
e0d98711 DI |
30 | ) { |
31 | this.currentRowHeight = 0; | |
32 | this.lastRecordedBufferLength = 0; | |
33 | this.lastRecordedViewportHeight = 0; | |
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 | |
ee1de098 DI |
39 | // Perform this async to ensure the CharMeasure is ready. |
40 | setTimeout(() => this.syncScrollArea(), 0); | |
7ff03bb4 DI |
41 | } |
42 | ||
e0d98711 DI |
43 | /** |
44 | * Refreshes row height, setting line-height, viewport height and scroll area height if | |
45 | * necessary. | |
e0d98711 | 46 | */ |
74483fb2 DI |
47 | private refresh(): void { |
48 | if (this.charMeasure.height > 0) { | |
ada96a83 | 49 | const rowHeightChanged = this.charMeasure.height !== this.currentRowHeight; |
e0d98711 | 50 | if (rowHeightChanged) { |
74483fb2 DI |
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'; | |
e0d98711 | 54 | } |
d2ef19df | 55 | const viewportHeightChanged = this.lastRecordedViewportHeight !== this.terminal.rows; |
e0d98711 DI |
56 | if (rowHeightChanged || viewportHeightChanged) { |
57 | this.lastRecordedViewportHeight = this.terminal.rows; | |
74483fb2 | 58 | this.viewportElement.style.height = this.charMeasure.height * this.terminal.rows + 'px'; |
9c459aac | 59 | this.terminal.selectionContainer.style.height = this.viewportElement.style.height; |
e0d98711 | 60 | } |
74483fb2 | 61 | this.scrollArea.style.height = (this.charMeasure.height * this.lastRecordedBufferLength) + 'px'; |
e0d98711 | 62 | } |
7ff03bb4 | 63 | } |
7ff03bb4 | 64 | |
e0d98711 DI |
65 | /** |
66 | * Updates dimensions and synchronizes the scroll area if necessary. | |
67 | */ | |
68 | public syncScrollArea(): void { | |
8ede1fc9 | 69 | if (this.lastRecordedBufferLength !== this.terminal.buffer.lines.length) { |
e0d98711 | 70 | // If buffer height changed |
8ede1fc9 | 71 | this.lastRecordedBufferLength = this.terminal.buffer.lines.length; |
e0d98711 DI |
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 | |
74483fb2 | 78 | if (this.charMeasure.height !== this.currentRowHeight) { |
9385b930 | 79 | this.refresh(); |
e0d98711 DI |
80 | } |
81 | } | |
7ff03bb4 | 82 | |
e0d98711 | 83 | // Sync scrollTop |
bbafdd3d | 84 | const scrollTop = this.terminal.buffer.ydisp * this.currentRowHeight; |
e0d98711 DI |
85 | if (this.viewportElement.scrollTop !== scrollTop) { |
86 | this.viewportElement.scrollTop = scrollTop; | |
87 | } | |
7ff03bb4 | 88 | } |
e0d98711 DI |
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) { | |
d2ef19df | 96 | const newRow = Math.round(this.viewportElement.scrollTop / this.currentRowHeight); |
bbafdd3d | 97 | const diff = newRow - this.terminal.buffer.ydisp; |
e0d98711 | 98 | this.terminal.scrollDisp(diff, true); |
7ff03bb4 | 99 | } |
7ff03bb4 | 100 | |
e0d98711 DI |
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 | |
d2ef19df | 113 | let multiplier = 1; |
e0d98711 DI |
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 | }; | |
62233428 | 123 | |
3e8ddd19 DI |
124 | /** |
125 | * Handles the touchstart event, recording the touch occurred. | |
126 | * @param ev The touch event. | |
127 | */ | |
62233428 AA |
128 | public onTouchStart(ev: TouchEvent) { |
129 | this.lastTouchY = ev.touches[0].pageY; | |
130 | }; | |
131 | ||
3e8ddd19 DI |
132 | /** |
133 | * Handles the touchmove event, scrolling the viewport if the position shifted. | |
134 | * @param ev The touch event. | |
135 | */ | |
62233428 AA |
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 | }; | |
e0d98711 | 145 | } |