From 37adeb5f901d96e57ad722bf4e1c67cbc4269396 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Thu, 7 May 2015 16:14:16 -0600 Subject: [PATCH] * Fix some handling of height, itemSpacing, etc. in VerticalMenuView * Change auto scale to be height/width dependent --- core/string_util.js | 2 +- core/vertical_menu_view.js | 42 ++++++-------------------------------- core/view.js | 16 ++++++++++----- mods/menu.json | 3 +-- 4 files changed, 19 insertions(+), 44 deletions(-) diff --git a/core/string_util.js b/core/string_util.js index e2ca4f3f..830a9ba5 100644 --- a/core/string_util.js +++ b/core/string_util.js @@ -119,7 +119,7 @@ function pad(s, len, padChar, dir, stringSGR, padSGR) { stringSGR = miscUtil.valueWithDefault(stringSGR, ''); padSGR = miscUtil.valueWithDefault(padSGR, ''); - var padlen = len - s.length; + var padlen = len >= s.length ? len - s.length : 0; switch(dir) { case 'L' : diff --git a/core/vertical_menu_view.js b/core/vertical_menu_view.js index ded1da8f..1b40ae9b 100644 --- a/core/vertical_menu_view.js +++ b/core/vertical_menu_view.js @@ -14,60 +14,32 @@ exports.VerticalMenuView = VerticalMenuView; function VerticalMenuView(options) { options.cursor = options.cursor || 'hide'; - options.justify = options.justify || 'center'; + options.justify = options.justify || 'right'; MenuView.call(this, options); var self = this; this.performAutoScale = function() { - if(this.autoScale) { + if(this.autoScale.height) { 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); + } + if(this.autoScale.width) { var l = 0; self.items.forEach(function item(i) { if(i.text.length > l) { 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.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) { - assert(self.positionCacheExpired === false); - var item = self.items[index]; if(!item) { return; @@ -161,11 +133,9 @@ VerticalMenuView.prototype.getData = function() { VerticalMenuView.prototype.setItems = function(items) { VerticalMenuView.super_.prototype.setItems.call(this, items); - this.positionCacheExpired = true; - this.cachePositions(); this.performAutoScale(); - this.maxVisibleItems = this.dimens.height / (this.itemSpacing + 1); + this.maxVisibleItems = Math.ceil(this.dimens.height / (this.itemSpacing + 1)); this.viewWindow = { top : this.focusedItemIndex, diff --git a/core/view.js b/core/view.js index 7a8221b7..8705b830 100644 --- a/core/view.js +++ b/core/view.js @@ -55,11 +55,15 @@ function View(options) { 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) { this.setDimension(options.dimens); - this.autoScale = false; // no auto scaling dimens supplied + this.autoScale = { height : false, width : false }; } else { this.dimens = { width : 0, height : 0 }; } @@ -137,15 +141,17 @@ View.prototype.setDimension = function(dimens) { assert(_.isObject(dimens) && _.isNumber(dimens.height) && _.isNumber(dimens.width)); this.dimens = dimens; - this.autoScale = false; + this.autoScale = { height : false, width : false }; }; 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) { - this.setDimension( { height : this.dimens.height, width : width } ); + this.dimens.width = width; + this.autoScale.width = false; }; /* diff --git a/mods/menu.json b/mods/menu.json index 57c4fc3d..d76fb2fc 100644 --- a/mods/menu.json +++ b/mods/menu.json @@ -233,8 +233,7 @@ "Some More Stuff", "Other" ], - "height" : 8, - "width" : 33, + "height" : 10, "itemSpacing" : 1, // :TODO: justify not working?? "focusTextStyle" : "small i"