]>
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; | |
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, | |
74483fb2 | 28 | private charMeasure: CharMeasure |
e0d98711 DI |
29 | ) { |
30 | this.currentRowHeight = 0; | |
31 | this.lastRecordedBufferLength = 0; | |
32 | this.lastRecordedViewportHeight = 0; | |
7ff03bb4 | 33 | |
e0d98711 DI |
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)); | |
7ff03bb4 | 37 | |
ee1de098 DI |
38 | // Perform this async to ensure the CharMeasure is ready. |
39 | setTimeout(() => this.syncScrollArea(), 0); | |
7ff03bb4 DI |
40 | } |
41 | ||
e0d98711 DI |
42 | /** |
43 | * Refreshes row height, setting line-height, viewport height and scroll area height if | |
44 | * necessary. | |
45 | * @param charSize A character size measurement bounding rect object, if it doesn't exist it will | |
46 | * be created. | |
47 | */ | |
74483fb2 DI |
48 | private refresh(): void { |
49 | if (this.charMeasure.height > 0) { | |
ada96a83 | 50 | const rowHeightChanged = this.charMeasure.height !== this.currentRowHeight; |
e0d98711 | 51 | if (rowHeightChanged) { |
74483fb2 DI |
52 | this.currentRowHeight = this.charMeasure.height; |
53 | this.viewportElement.style.lineHeight = this.charMeasure.height + 'px'; | |
54 | this.terminal.rowContainer.style.lineHeight = this.charMeasure.height + 'px'; | |
e0d98711 | 55 | } |
d2ef19df | 56 | const viewportHeightChanged = this.lastRecordedViewportHeight !== this.terminal.rows; |
e0d98711 DI |
57 | if (rowHeightChanged || viewportHeightChanged) { |
58 | this.lastRecordedViewportHeight = this.terminal.rows; | |
74483fb2 | 59 | this.viewportElement.style.height = this.charMeasure.height * this.terminal.rows + 'px'; |
9c459aac | 60 | this.terminal.selectionContainer.style.height = this.viewportElement.style.height; |
e0d98711 | 61 | } |
74483fb2 | 62 | this.scrollArea.style.height = (this.charMeasure.height * this.lastRecordedBufferLength) + 'px'; |
e0d98711 | 63 | } |
7ff03bb4 | 64 | } |
7ff03bb4 | 65 | |
e0d98711 DI |
66 | /** |
67 | * Updates dimensions and synchronizes the scroll area if necessary. | |
68 | */ | |
69 | public syncScrollArea(): void { | |
70 | if (this.lastRecordedBufferLength !== this.terminal.lines.length) { | |
71 | // If buffer height changed | |
72 | this.lastRecordedBufferLength = this.terminal.lines.length; | |
73 | this.refresh(); | |
74 | } else if (this.lastRecordedViewportHeight !== this.terminal.rows) { | |
75 | // If viewport height changed | |
76 | this.refresh(); | |
77 | } else { | |
78 | // If size has changed, refresh viewport | |
74483fb2 | 79 | if (this.charMeasure.height !== this.currentRowHeight) { |
9385b930 | 80 | this.refresh(); |
e0d98711 DI |
81 | } |
82 | } | |
7ff03bb4 | 83 | |
e0d98711 | 84 | // Sync scrollTop |
d2ef19df | 85 | const scrollTop = this.terminal.ydisp * this.currentRowHeight; |
e0d98711 DI |
86 | if (this.viewportElement.scrollTop !== scrollTop) { |
87 | this.viewportElement.scrollTop = scrollTop; | |
88 | } | |
7ff03bb4 | 89 | } |
e0d98711 DI |
90 | |
91 | /** | |
92 | * Handles scroll events on the viewport, calculating the new viewport and requesting the | |
93 | * terminal to scroll to it. | |
94 | * @param ev The scroll event. | |
95 | */ | |
96 | private onScroll(ev: Event) { | |
d2ef19df DI |
97 | const newRow = Math.round(this.viewportElement.scrollTop / this.currentRowHeight); |
98 | const diff = newRow - this.terminal.ydisp; | |
e0d98711 | 99 | this.terminal.scrollDisp(diff, true); |
7ff03bb4 | 100 | } |
7ff03bb4 | 101 | |
e0d98711 DI |
102 | /** |
103 | * Handles mouse wheel events by adjusting the viewport's scrollTop and delegating the actual | |
104 | * scrolling to `onScroll`, this event needs to be attached manually by the consumer of | |
105 | * `Viewport`. | |
106 | * @param ev The mouse wheel event. | |
107 | */ | |
108 | public onWheel(ev: WheelEvent) { | |
109 | if (ev.deltaY === 0) { | |
110 | // Do nothing if it's not a vertical scroll event | |
111 | return; | |
112 | } | |
113 | // Fallback to WheelEvent.DOM_DELTA_PIXEL | |
d2ef19df | 114 | let multiplier = 1; |
e0d98711 DI |
115 | if (ev.deltaMode === WheelEvent.DOM_DELTA_LINE) { |
116 | multiplier = this.currentRowHeight; | |
117 | } else if (ev.deltaMode === WheelEvent.DOM_DELTA_PAGE) { | |
118 | multiplier = this.currentRowHeight * this.terminal.rows; | |
119 | } | |
120 | this.viewportElement.scrollTop += ev.deltaY * multiplier; | |
121 | // Prevent the page from scrolling when the terminal scrolls | |
122 | ev.preventDefault(); | |
123 | }; | |
124 | } |