* Did add a tweak to the concept for height only: autoAdjustHeight can be used where it makes sense
* See also: #159
This commit is contained in:
parent
6d4b8abc9c
commit
611a52e946
|
@ -31,17 +31,6 @@ function HorizontalMenuView(options) {
|
||||||
return new Array(self.itemSpacing + 1).join(' ');
|
return new Array(self.itemSpacing + 1).join(' ');
|
||||||
};
|
};
|
||||||
|
|
||||||
this.performAutoScale = function() {
|
|
||||||
if(self.autoScale.width) {
|
|
||||||
var spacer = self.getSpacer();
|
|
||||||
var width = self.items.join(spacer).length + (spacer.length * 2);
|
|
||||||
assert(width <= self.client.term.termWidth - self.position.col);
|
|
||||||
self.dimens.width = width;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
this.performAutoScale();
|
|
||||||
|
|
||||||
this.cachePositions = function() {
|
this.cachePositions = function() {
|
||||||
if(this.positionCacheExpired) {
|
if(this.positionCacheExpired) {
|
||||||
var col = self.position.col;
|
var col = self.position.col;
|
||||||
|
|
|
@ -183,67 +183,23 @@ TextView.prototype.getData = function() {
|
||||||
TextView.prototype.setText = function(text, redraw) {
|
TextView.prototype.setText = function(text, redraw) {
|
||||||
redraw = _.isBoolean(redraw) ? redraw : true;
|
redraw = _.isBoolean(redraw) ? redraw : true;
|
||||||
|
|
||||||
if(!_.isString(text)) {
|
if(!_.isString(text)) { // allow |text| to be numbers/etc.
|
||||||
text = text.toString();
|
text = text.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
text = pipeToAnsi(stripAllLineFeeds(text), this.client); // expand MCI/etc.
|
this.text = pipeToAnsi(stripAllLineFeeds(text), this.client); // expand MCI/etc.
|
||||||
|
|
||||||
var widthDelta = 0;
|
|
||||||
if(this.text && this.text !== text) {
|
|
||||||
widthDelta = Math.abs(renderStringLength(this.text) - renderStringLength(text));
|
|
||||||
}
|
|
||||||
|
|
||||||
this.text = text;
|
|
||||||
|
|
||||||
if(this.maxLength > 0) {
|
if(this.maxLength > 0) {
|
||||||
this.text = renderSubstr(this.text, 0, this.maxLength);
|
this.text = renderSubstr(this.text, 0, this.maxLength);
|
||||||
//this.text = this.text.substr(0, this.maxLength);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// :TODO: it would be nice to be able to stylize strings with MCI and {special} MCI syntax, e.g. "|BN {UN!toUpper}"
|
// :TODO: it would be nice to be able to stylize strings with MCI and {special} MCI syntax, e.g. "|BN {UN!toUpper}"
|
||||||
this.text = stylizeString(this.text, this.hasFocus ? this.focusTextStyle : this.textStyle);
|
this.text = stylizeString(this.text, this.hasFocus ? this.focusTextStyle : this.textStyle);
|
||||||
|
|
||||||
if(this.autoScale.width) {
|
|
||||||
this.dimens.width = renderStringLength(this.text) + widthDelta;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(redraw) {
|
if(redraw) {
|
||||||
this.redraw();
|
this.redraw();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
TextView.prototype.setText = function(text) {
|
|
||||||
if(!_.isString(text)) {
|
|
||||||
text = text.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
var widthDelta = 0;
|
|
||||||
if(this.text && this.text !== text) {
|
|
||||||
widthDelta = Math.abs(this.text.length - text.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.text = text;
|
|
||||||
|
|
||||||
if(this.maxLength > 0) {
|
|
||||||
this.text = this.text.substr(0, this.maxLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.text = stylizeString(this.text, this.hasFocus ? this.focusTextStyle : this.textStyle);
|
|
||||||
|
|
||||||
//if(this.resizable) {
|
|
||||||
// this.dimens.width = this.text.length + widthDelta;
|
|
||||||
//}
|
|
||||||
|
|
||||||
if(this.autoScale.width) {
|
|
||||||
this.dimens.width = this.text.length + widthDelta;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.redraw();
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
|
|
||||||
TextView.prototype.clearText = function() {
|
TextView.prototype.clearText = function() {
|
||||||
this.setText('');
|
this.setText('');
|
||||||
};
|
};
|
||||||
|
|
|
@ -20,6 +20,8 @@ function VerticalMenuView(options) {
|
||||||
|
|
||||||
MenuView.call(this, options);
|
MenuView.call(this, options);
|
||||||
|
|
||||||
|
this.dimens.width = this.dimens.width || Math.min(15, this.client.term.termWidth - this.position.col);
|
||||||
|
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
// we want page up/page down by default
|
// we want page up/page down by default
|
||||||
|
@ -30,24 +32,14 @@ function VerticalMenuView(options) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.performAutoScale = function() {
|
this.autoAdjustHeightIfEnabled = function() {
|
||||||
if(this.autoScale.height) {
|
if(this.autoAdjustHeight) {
|
||||||
this.dimens.height = (self.items.length * (self.itemSpacing + 1)) - (self.itemSpacing);
|
this.dimens.height = (this.items.length * (this.itemSpacing + 1)) - (this.itemSpacing);
|
||||||
this.dimens.height = Math.min(self.dimens.height, self.client.term.termHeight - self.position.row);
|
this.dimens.height = Math.min(this.dimens.height, this.client.term.termHeight - this.position.row);
|
||||||
}
|
|
||||||
|
|
||||||
if(self.autoScale.width) {
|
|
||||||
let maxLen = 0;
|
|
||||||
self.items.forEach( item => {
|
|
||||||
if(item.text.length > maxLen) {
|
|
||||||
maxLen = Math.min(item.text.length, self.client.term.termWidth - self.position.col);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
self.dimens.width = maxLen + 1;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.performAutoScale();
|
this.autoAdjustHeightIfEnabled();
|
||||||
|
|
||||||
this.updateViewVisibleItems = function() {
|
this.updateViewVisibleItems = function() {
|
||||||
self.maxVisibleItems = Math.ceil(self.dimens.height / (self.itemSpacing + 1));
|
self.maxVisibleItems = Math.ceil(self.dimens.height / (self.itemSpacing + 1));
|
||||||
|
@ -96,7 +88,7 @@ VerticalMenuView.prototype.redraw = function() {
|
||||||
|
|
||||||
// :TODO: rename positionCacheExpired to something that makese sense; combine methods for such
|
// :TODO: rename positionCacheExpired to something that makese sense; combine methods for such
|
||||||
if(this.positionCacheExpired) {
|
if(this.positionCacheExpired) {
|
||||||
this.performAutoScale();
|
this.autoAdjustHeightIfEnabled();
|
||||||
this.updateViewVisibleItems();
|
this.updateViewVisibleItems();
|
||||||
|
|
||||||
this.positionCacheExpired = false;
|
this.positionCacheExpired = false;
|
||||||
|
@ -133,6 +125,7 @@ VerticalMenuView.prototype.setHeight = function(height) {
|
||||||
VerticalMenuView.super_.prototype.setHeight.call(this, height);
|
VerticalMenuView.super_.prototype.setHeight.call(this, height);
|
||||||
|
|
||||||
this.positionCacheExpired = true;
|
this.positionCacheExpired = true;
|
||||||
|
this.autoAdjustHeight = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
VerticalMenuView.prototype.setPosition = function(pos) {
|
VerticalMenuView.prototype.setPosition = function(pos) {
|
||||||
|
@ -158,7 +151,7 @@ VerticalMenuView.prototype.setFocusItemIndex = function(index) {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.positionCacheExpired = false; // skip standard behavior
|
this.positionCacheExpired = false; // skip standard behavior
|
||||||
this.performAutoScale();
|
this.autoAdjustHeightIfEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.redraw();
|
this.redraw();
|
||||||
|
|
22
core/view.js
22
core/view.js
|
@ -37,19 +37,14 @@ function View(options) {
|
||||||
enigAssert(_.isObject(options));
|
enigAssert(_.isObject(options));
|
||||||
enigAssert(_.isObject(options.client));
|
enigAssert(_.isObject(options.client));
|
||||||
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
this.client = options.client;
|
this.client = options.client;
|
||||||
|
|
||||||
this.cursor = options.cursor || 'show';
|
this.cursor = options.cursor || 'show';
|
||||||
this.cursorStyle = options.cursorStyle || 'default';
|
this.cursorStyle = options.cursorStyle || 'default';
|
||||||
|
|
||||||
this.acceptsFocus = options.acceptsFocus || false;
|
this.acceptsFocus = options.acceptsFocus || false;
|
||||||
this.acceptsInput = options.acceptsInput || false;
|
this.acceptsInput = options.acceptsInput || false;
|
||||||
|
this.autoAdjustHeight = _.get(options, 'dimens.height') ? false : _.get(options, 'autoAdjustHeight', true);
|
||||||
this.position = { x : 0, y : 0 };
|
this.position = { x : 0, y : 0 };
|
||||||
this.dimens = { height : 1, width : 0 };
|
|
||||||
|
|
||||||
this.textStyle = options.textStyle || 'normal';
|
this.textStyle = options.textStyle || 'normal';
|
||||||
this.focusTextStyle = options.focusTextStyle || this.textStyle;
|
this.focusTextStyle = options.focusTextStyle || this.textStyle;
|
||||||
|
|
||||||
|
@ -61,15 +56,8 @@ function View(options) {
|
||||||
this.setPosition(options.position);
|
this.setPosition(options.position);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(_.isObject(options.autoScale)) {
|
|
||||||
this.autoScale = options.autoScale;
|
|
||||||
} else {
|
|
||||||
this.autoScale = { height : true, width : true };
|
|
||||||
}
|
|
||||||
|
|
||||||
if(options.dimens) {
|
if(options.dimens) {
|
||||||
this.setDimension(options.dimens);
|
this.setDimension(options.dimens);
|
||||||
this.autoScale = { height : false, width : false };
|
|
||||||
} else {
|
} else {
|
||||||
this.dimens = {
|
this.dimens = {
|
||||||
width : options.width || 0,
|
width : options.width || 0,
|
||||||
|
@ -105,7 +93,7 @@ function View(options) {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.hideCusor = function() {
|
this.hideCusor = function() {
|
||||||
self.client.term.rawWrite(ansi.hideCursor());
|
this.client.term.rawWrite(ansi.hideCursor());
|
||||||
};
|
};
|
||||||
|
|
||||||
this.restoreCursor = function() {
|
this.restoreCursor = function() {
|
||||||
|
@ -148,9 +136,8 @@ View.prototype.setPosition = function(pos) {
|
||||||
|
|
||||||
View.prototype.setDimension = function(dimens) {
|
View.prototype.setDimension = function(dimens) {
|
||||||
enigAssert(_.isObject(dimens) && _.isNumber(dimens.height) && _.isNumber(dimens.width));
|
enigAssert(_.isObject(dimens) && _.isNumber(dimens.height) && _.isNumber(dimens.width));
|
||||||
|
|
||||||
this.dimens = dimens;
|
this.dimens = dimens;
|
||||||
this.autoScale = { height : false, width : false };
|
this.autoAdjustHeight = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
View.prototype.setHeight = function(height) {
|
View.prototype.setHeight = function(height) {
|
||||||
|
@ -158,7 +145,7 @@ View.prototype.setHeight = function(height) {
|
||||||
height = Math.min(height, this.client.term.termHeight);
|
height = Math.min(height, this.client.term.termHeight);
|
||||||
|
|
||||||
this.dimens.height = height;
|
this.dimens.height = height;
|
||||||
this.autoScale.height = false;
|
this.autoAdjustHeight = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
View.prototype.setWidth = function(width) {
|
View.prototype.setWidth = function(width) {
|
||||||
|
@ -166,7 +153,6 @@ View.prototype.setWidth = function(width) {
|
||||||
width = Math.min(width, this.client.term.termWidth);
|
width = Math.min(width, this.client.term.termWidth);
|
||||||
|
|
||||||
this.dimens.width = width;
|
this.dimens.width = width;
|
||||||
this.autoScale.width = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
View.prototype.getSGR = function() {
|
View.prototype.getSGR = function() {
|
||||||
|
|
Loading…
Reference in New Issue