/**
- * xterm.js: xterm, in the browser
- * Copyright (c) 2014-2016, SourceLair Private Company (www.sourcelair.com (MIT License)
+ * @license MIT
*/
import { ITerminal } from './Interfaces';
+import { CharMeasure } from './utils/CharMeasure';
/**
* Represents the viewport of a terminal, the visible area within the larger buffer of output.
* Logic for the virtual scroll bar is included in this object.
- * @param viewportElement The DOM element acting as the viewport.
- * @param scrollArea The DOM element acting as the scroll area.
- * @param charMeasureElement A DOM element used to measure the character size of. the terminal.
*/
export class Viewport {
-
private currentRowHeight: number;
private lastRecordedBufferLength: number;
private lastRecordedViewportHeight: number;
+ /**
+ * Creates a new Viewport.
+ * @param terminal The terminal this viewport belongs to.
+ * @param viewportElement The DOM element acting as the viewport.
+ * @param scrollArea The DOM element acting as the scroll area.
+ * @param charMeasureElement A DOM element used to measure the character size of. the terminal.
+ */
constructor(
private terminal: ITerminal,
private viewportElement: HTMLElement,
private scrollArea: HTMLElement,
- private charMeasureElement: HTMLElement
+ private charMeasure: CharMeasure
) {
this.currentRowHeight = 0;
this.lastRecordedBufferLength = 0;
this.terminal.on('resize', this.syncScrollArea.bind(this));
this.viewportElement.addEventListener('scroll', this.onScroll.bind(this));
- this.syncScrollArea();
+ // Perform this async to ensure the CharMeasure is ready.
+ setTimeout(() => this.syncScrollArea(), 0);
}
/**
* @param charSize A character size measurement bounding rect object, if it doesn't exist it will
* be created.
*/
- private refresh(charSize?: ClientRect): void {
- var size = charSize || this.charMeasureElement.getBoundingClientRect();
- if (size.height > 0) {
- var rowHeightChanged = size.height !== this.currentRowHeight;
+ private refresh(): void {
+ if (this.charMeasure.height > 0) {
+ const rowHeightChanged = this.charMeasure.height !== this.currentRowHeight;
if (rowHeightChanged) {
- this.currentRowHeight = size.height;
- this.viewportElement.style.lineHeight = size.height + 'px';
- this.terminal.rowContainer.style.lineHeight = size.height + 'px';
+ this.currentRowHeight = this.charMeasure.height;
+ this.viewportElement.style.lineHeight = this.charMeasure.height + 'px';
+ this.terminal.rowContainer.style.lineHeight = this.charMeasure.height + 'px';
}
- var viewportHeightChanged = this.lastRecordedViewportHeight !== this.terminal.rows;
+ const viewportHeightChanged = this.lastRecordedViewportHeight !== this.terminal.rows;
if (rowHeightChanged || viewportHeightChanged) {
this.lastRecordedViewportHeight = this.terminal.rows;
- this.viewportElement.style.height = size.height * this.terminal.rows + 'px';
+ this.viewportElement.style.height = this.charMeasure.height * this.terminal.rows + 'px';
}
- this.scrollArea.style.height = (size.height * this.lastRecordedBufferLength) + 'px';
+ this.scrollArea.style.height = (this.charMeasure.height * this.lastRecordedBufferLength) + 'px';
}
}
this.refresh();
} else {
// If size has changed, refresh viewport
- var size = this.charMeasureElement.getBoundingClientRect();
- if (size.height !== this.currentRowHeight) {
- this.refresh(size);
+ if (this.charMeasure.height !== this.currentRowHeight) {
+ this.refresh();
}
}
// Sync scrollTop
- var scrollTop = this.terminal.ydisp * this.currentRowHeight;
+ const scrollTop = this.terminal.ydisp * this.currentRowHeight;
if (this.viewportElement.scrollTop !== scrollTop) {
this.viewportElement.scrollTop = scrollTop;
}
* @param ev The scroll event.
*/
private onScroll(ev: Event) {
- var newRow = Math.round(this.viewportElement.scrollTop / this.currentRowHeight);
- var diff = newRow - this.terminal.ydisp;
+ const newRow = Math.round(this.viewportElement.scrollTop / this.currentRowHeight);
+ const diff = newRow - this.terminal.ydisp;
this.terminal.scrollDisp(diff, true);
}
return;
}
// Fallback to WheelEvent.DOM_DELTA_PIXEL
- var multiplier = 1;
+ let multiplier = 1;
if (ev.deltaMode === WheelEvent.DOM_DELTA_LINE) {
multiplier = this.currentRowHeight;
} else if (ev.deltaMode === WheelEvent.DOM_DELTA_PAGE) {