* Fix some handling of height, itemSpacing, etc. in VerticalMenuView

* Change auto scale to be height/width dependent
This commit is contained in:
Bryan Ashby 2015-05-07 16:14:16 -06:00
parent 7fc4858959
commit 37adeb5f90
4 changed files with 19 additions and 44 deletions

View File

@ -119,7 +119,7 @@ function pad(s, len, padChar, dir, stringSGR, padSGR) {
stringSGR = miscUtil.valueWithDefault(stringSGR, ''); stringSGR = miscUtil.valueWithDefault(stringSGR, '');
padSGR = miscUtil.valueWithDefault(padSGR, ''); padSGR = miscUtil.valueWithDefault(padSGR, '');
var padlen = len - s.length; var padlen = len >= s.length ? len - s.length : 0;
switch(dir) { switch(dir) {
case 'L' : case 'L' :

View File

@ -14,60 +14,32 @@ exports.VerticalMenuView = VerticalMenuView;
function VerticalMenuView(options) { function VerticalMenuView(options) {
options.cursor = options.cursor || 'hide'; options.cursor = options.cursor || 'hide';
options.justify = options.justify || 'center'; options.justify = options.justify || 'right';
MenuView.call(this, options); MenuView.call(this, options);
var self = this; var self = this;
this.performAutoScale = function() { this.performAutoScale = function() {
if(this.autoScale) { if(this.autoScale.height) {
this.dimens.height = (self.items.length * (self.itemSpacing + 1)) - (self.itemSpacing); this.dimens.height = (self.items.length * (self.itemSpacing + 1)) - (self.itemSpacing);
this.dimens.height = Math.min(this.dimens.height, self.client.term.termHeight - self.position.x); this.dimens.height = Math.min(this.dimens.height, self.client.term.termHeight - self.position.x);
}
if(this.autoScale.width) {
var l = 0; var l = 0;
self.items.forEach(function item(i) { self.items.forEach(function item(i) {
if(i.text.length > l) { if(i.text.length > l) {
l = Math.min(i.text.length, self.client.term.termWidth - self.position.y); l = Math.min(i.text.length, self.client.term.termWidth - self.position.y);
} }
}); });
self.dimens.width = l; self.dimens.width = l + 1;
} }
}; };
this.performAutoScale(); this.performAutoScale();
this.cachePositions = function() {
if(self.positionCacheExpired) {
var count = this.items.length;
var x = self.position.x;
for(var i = 0; i < count; ++i) {
if(i > 0) {
x += self.itemSpacing + 1;
}
self.items[i].xPosition = x;
}
self.positionCacheExpired = false;
}
};
this.changeSelection = function(fromIndex, toIndex) {
assert(!self.positionCacheExpired);
assert(fromIndex >= 0 && fromIndex <= self.items.length);
assert(toIndex >= 0 && toIndex <= self.items.length);
self.items[fromIndex].focused = false;
self.drawItem(fromIndex);
self.items[toIndex].focused = true;
self.focusedItemIndex = toIndex;
self.drawItem(toIndex);
};
this.drawItem = function(index) { this.drawItem = function(index) {
assert(self.positionCacheExpired === false);
var item = self.items[index]; var item = self.items[index];
if(!item) { if(!item) {
return; return;
@ -161,11 +133,9 @@ VerticalMenuView.prototype.getData = function() {
VerticalMenuView.prototype.setItems = function(items) { VerticalMenuView.prototype.setItems = function(items) {
VerticalMenuView.super_.prototype.setItems.call(this, items); VerticalMenuView.super_.prototype.setItems.call(this, items);
this.positionCacheExpired = true;
this.cachePositions();
this.performAutoScale(); this.performAutoScale();
this.maxVisibleItems = this.dimens.height / (this.itemSpacing + 1); this.maxVisibleItems = Math.ceil(this.dimens.height / (this.itemSpacing + 1));
this.viewWindow = { this.viewWindow = {
top : this.focusedItemIndex, top : this.focusedItemIndex,

View File

@ -55,11 +55,15 @@ function View(options) {
this.setPosition(options.position); this.setPosition(options.position);
} }
this.autoScale = _.isBoolean(options.autoScale) ? options.autoScale : true; 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 = false; // no auto scaling dimens supplied this.autoScale = { height : false, width : false };
} else { } else {
this.dimens = { width : 0, height : 0 }; this.dimens = { width : 0, height : 0 };
} }
@ -137,15 +141,17 @@ View.prototype.setDimension = function(dimens) {
assert(_.isObject(dimens) && _.isNumber(dimens.height) && _.isNumber(dimens.width)); assert(_.isObject(dimens) && _.isNumber(dimens.height) && _.isNumber(dimens.width));
this.dimens = dimens; this.dimens = dimens;
this.autoScale = false; this.autoScale = { height : false, width : false };
}; };
View.prototype.setHeight = function(height) { View.prototype.setHeight = function(height) {
this.setDimension( { height : height, width : this.dimens.width } ); this.dimens.height = height;
this.autoScale.height = false;
}; };
View.prototype.setWidth = function(width) { View.prototype.setWidth = function(width) {
this.setDimension( { height : this.dimens.height, width : width } ); this.dimens.width = width;
this.autoScale.width = false;
}; };
/* /*

View File

@ -233,8 +233,7 @@
"Some More Stuff", "Some More Stuff",
"Other" "Other"
], ],
"height" : 8, "height" : 10,
"width" : 33,
"itemSpacing" : 1, "itemSpacing" : 1,
// :TODO: justify not working?? // :TODO: justify not working??
"focusTextStyle" : "small i" "focusTextStyle" : "small i"