diff --git a/lib/widget.js b/lib/widget.js index 9657add..076d8c0 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -1666,6 +1666,20 @@ Element.prototype.clearPos = function() { * Positioning */ +// Help center shrunken nested elements. +Screen.prototype.spos = +Element.prototype.spos = function() { + var pos = this.lpos; + if (!pos || !this.shrink) return this; + pos.width = pos.xl - pos.xi; + pos.height = pos.yl - pos.yi; + pos.left = pos.xi; + pos.top = pos.yi; + pos.right = this.screen.cols - pos.xl; + pos.bottom = this.screen.rows - pos.yl; + return pos; +}; + // NOTE: When coords are entered in the Element constructor, all of the coords // are *relative* to their parent, when retrieving them from `.left`, `.right`, // etc members, the coords are absolute. To see the *relative* coords again, @@ -1674,6 +1688,8 @@ Element.prototype.clearPos = function() { Element.prototype.__defineGetter__('left', function() { var left = this.position.left; + var parent = this.parent.spos(); + if (typeof left === 'string') { if (left === 'center') left = '50%'; left = +left.slice(0, -1) / 100; @@ -1687,7 +1703,7 @@ Element.prototype.__defineGetter__('left', function() { return this.screen.cols - this.width - this.right; } - return (this.parent.left || 0) + left; + return (parent.left || 0) + left; }); Element.prototype.__defineGetter__('right', function() { @@ -1700,6 +1716,8 @@ Element.prototype.__defineGetter__('right', function() { Element.prototype.__defineGetter__('top', function() { var top = this.position.top; + var parent = this.parent.spos(); + if (typeof top === 'string') { if (top === 'center') top = '50%'; top = +top.slice(0, -1) / 100; @@ -1713,7 +1731,7 @@ Element.prototype.__defineGetter__('top', function() { return this.screen.rows - this.height - this.bottom; } - return (this.parent.top || 0) + top; + return (parent.top || 0) + top; }); Element.prototype.__defineGetter__('bottom', function() { @@ -2144,15 +2162,6 @@ Box.prototype.render = function(stop) { xi = ret.xi, xl = ret.xl, yi = ret.yi, yl = ret.yl; } - // TODO: Possibly do something like this, except more complex. - // if (this.parent.padding) { - // xi += this.parent.padding; - // xl += this.parent.padding; - // yi += this.parent.padding; - // yl += this.parent.padding; - // } - // if (this.parent.border) xi++, xl++, yi++, yl++; - ret = { xi: xi, xl: xl, diff --git a/test/widget-shrink-padding.js b/test/widget-shrink-padding.js index 1bdd25e..6cd8628 100644 --- a/test/widget-shrink-padding.js +++ b/test/widget-shrink-padding.js @@ -18,8 +18,10 @@ var inner = blessed.box({ parent: outer, left: 0, top: 0, - width: 5, - height: 5, + //width: 5, + //height: 5, + shrink: true, + content: 'foobar', //padding: 1, //content: 'f', bg: 'magenta'