Fix the fit addon

This commit is contained in:
Daniel Imms
2017-09-01 20:44:41 -07:00
parent 2c9dbc6191
commit bf95333275
5 changed files with 39 additions and 38 deletions
+16 -17
View File
@@ -92,27 +92,26 @@ function createTerminal() {
term.open(terminalContainer); term.open(terminalContainer);
term.fit(); term.fit();
var initialGeometry = term.proposeGeometry(), // fit is called within a setTimeout, cols and rows need this.
cols = initialGeometry.cols, setTimeout(() => {
rows = initialGeometry.rows; colsElement.value = term.cols;
rowsElement.value = term.rows;
colsElement.value = cols; fetch('/terminals?cols=' + cols + '&rows=' + rows, {method: 'POST'}).then(function (res) {
rowsElement.value = rows;
fetch('/terminals?cols=' + cols + '&rows=' + rows, {method: 'POST'}).then(function (res) { charWidth = Math.ceil(term.element.offsetWidth / cols);
charHeight = Math.ceil(term.element.offsetHeight / rows);
charWidth = Math.ceil(term.element.offsetWidth / cols); res.text().then(function (pid) {
charHeight = Math.ceil(term.element.offsetHeight / rows); window.pid = pid;
socketURL += pid;
res.text().then(function (pid) { socket = new WebSocket(socketURL);
window.pid = pid; socket.onopen = runRealTerminal;
socketURL += pid; socket.onclose = runFakeTerminal;
socket = new WebSocket(socketURL); socket.onerror = runFakeTerminal;
socket.onopen = runRealTerminal; });
socket.onclose = runFakeTerminal;
socket.onerror = runFakeTerminal;
}); });
}); }, 0);
} }
function runRealTerminal() { function runRealTerminal() {
+13 -20
View File
@@ -45,33 +45,26 @@
availableWidth = parentElementWidth - elementPaddingHor, availableWidth = parentElementWidth - elementPaddingHor,
container = term.rowContainer, container = term.rowContainer,
subjectRow = term.rowContainer.firstElementChild, subjectRow = term.rowContainer.firstElementChild,
contentBuffer = subjectRow.innerHTML, contentBuffer = subjectRow.innerHTML;
characterHeight,
rows,
characterWidth,
cols,
geometry;
subjectRow.style.display = 'inline'; var geometry = {
subjectRow.innerHTML = 'W'; // Common character for measuring width, although on monospace cols: parseInt(availableWidth / term.charMeasure.width, 10),
characterWidth = subjectRow.getBoundingClientRect().width; rows: parseInt(availableHeight / term.charMeasure.height, 10)
subjectRow.style.display = ''; // Revert style before calculating height, since they differ. };
characterHeight = subjectRow.getBoundingClientRect().height;
subjectRow.innerHTML = contentBuffer;
rows = parseInt(availableHeight / characterHeight);
cols = parseInt(availableWidth / characterWidth);
geometry = {cols: cols, rows: rows};
return geometry; return geometry;
}; };
exports.fit = function (term) { exports.fit = function (term) {
var geometry = exports.proposeGeometry(term); // Wrap fit in a setTimeout as charMeasure needs time to get initialized
// after calling Terminal.open
setTimeout(() => {
var geometry = exports.proposeGeometry(term);
if (geometry) { if (geometry) {
term.resize(geometry.cols, geometry.rows); term.resize(geometry.cols, geometry.rows);
} }
}, 0);
}; };
Terminal.prototype.proposeGeometry = function () { Terminal.prototype.proposeGeometry = function () {
+4
View File
@@ -24,6 +24,10 @@ export class BackgroundRenderLayer extends BaseRenderLayer {
} }
public onGridChanged(terminal: ITerminal, startRow: number, endRow: number): void { public onGridChanged(terminal: ITerminal, startRow: number, endRow: number): void {
// Resize has not been called yet
if (this._state.cache.length === 0) {
return;
}
for (let y = startRow; y <= endRow; y++) { for (let y = startRow; y <= endRow; y++) {
let row = y + terminal.buffer.ydisp; let row = y + terminal.buffer.ydisp;
let line = terminal.buffer.lines.get(row); let line = terminal.buffer.lines.get(row);
+5
View File
@@ -32,6 +32,11 @@ export class ForegroundRenderLayer extends BaseRenderLayer {
// return; // return;
// } // }
// Resize has not been called yet
if (this._state.cache.length === 0) {
return;
}
for (let y = startRow; y <= endRow; y++) { for (let y = startRow; y <= endRow; y++) {
const row = y + terminal.buffer.ydisp; const row = y + terminal.buffer.ydisp;
const line = terminal.buffer.lines.get(row); const line = terminal.buffer.lines.get(row);
+1 -1
View File
@@ -18,7 +18,7 @@ export class GridCache<T> {
this.cache.length = width; this.cache.length = width;
} }
public clear() { public clear(): void {
for (let x = 0; x < this.cache.length; x++) { for (let x = 0; x < this.cache.length; x++) {
for (let y = 0; y < this.cache[x].length; y++) { for (let y = 0; y < this.cache[x].length; y++) {
this.cache[x][y] = null; this.cache[x][y] = null;