]> git.proxmox.com Git - mirror_xterm.js.git/blob - src/Viewport.ts
Merge remote-tracking branch 'upstream/master' into 335_CompositionHelper_ts
[mirror_xterm.js.git] / src / Viewport.ts
1 /**
2 * xterm.js: xterm, in the browser
3 * Copyright (c) 2014-2016, SourceLair Private Company (www.sourcelair.com (MIT License)
4 */
5
6 import { ITerminal } from './Interfaces';
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
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 */
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;
33
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));
37
38 this.syncScrollArea();
39 }
40
41 /**
42 * Refreshes row height, setting line-height, viewport height and scroll area height if
43 * necessary.
44 * @param charSize A character size measurement bounding rect object, if it doesn't exist it will
45 * be created.
46 */
47 private refresh(charSize?: ClientRect): void {
48 var size = charSize || this.charMeasureElement.getBoundingClientRect();
49 if (size.height > 0) {
50 var rowHeightChanged = size.height !== this.currentRowHeight;
51 if (rowHeightChanged) {
52 this.currentRowHeight = size.height;
53 this.viewportElement.style.lineHeight = size.height + 'px';
54 this.terminal.rowContainer.style.lineHeight = size.height + 'px';
55 }
56 var viewportHeightChanged = this.lastRecordedViewportHeight !== this.terminal.rows;
57 if (rowHeightChanged || viewportHeightChanged) {
58 this.lastRecordedViewportHeight = this.terminal.rows;
59 this.viewportElement.style.height = size.height * this.terminal.rows + 'px';
60 }
61 this.scrollArea.style.height = (size.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.lines.length) {
70 // If buffer height changed
71 this.lastRecordedBufferLength = this.terminal.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 var size = this.charMeasureElement.getBoundingClientRect();
79 if (size.height !== this.currentRowHeight) {
80 this.refresh(size);
81 }
82 }
83
84 // Sync scrollTop
85 var scrollTop = this.terminal.ydisp * this.currentRowHeight;
86 if (this.viewportElement.scrollTop !== scrollTop) {
87 this.viewportElement.scrollTop = scrollTop;
88 }
89 }
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) {
97 var newRow = Math.round(this.viewportElement.scrollTop / this.currentRowHeight);
98 var diff = newRow - this.terminal.ydisp;
99 this.terminal.scrollDisp(diff, true);
100 }
101
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
114 var multiplier = 1;
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 }