refactor shrink.

This commit is contained in:
Christopher Jeffrey 2013-07-17 03:52:21 -05:00
parent 8776ec2c2d
commit 566b800a04
2 changed files with 129 additions and 107 deletions

View File

@ -1941,7 +1941,6 @@ Box.prototype.__proto__ = Element.prototype;
Box.prototype.type = 'box'; Box.prototype.type = 'box';
// TODO: Optimize. Move elsewhere.
Box.prototype._getShrinkSize = function(content) { Box.prototype._getShrinkSize = function(content) {
return { return {
height: this._clines.length, height: this._clines.length,
@ -1954,6 +1953,132 @@ Box.prototype._getShrinkSize = function(content) {
}; };
}; };
Box.prototype._getShrinkBox = function(xi_, xl, yi_, yl) {
if (!this.children.length) {
return { xi: xi_, xl: xi_, yi: yi_, yl: yi_ };
//return { xi: Infinity, xl: 0, yi: Infinity, yl: 0 };
}
var i, el, mxi = 0, mxl = 0, myi = 0, myl = 0;
for (i = 0; i < this.children.length; i++) {
el = this.children[i];
var ret = el.render(true);
if (ret.xi < mxi) mxi = ret.xi;
if (ret.xl > mxl) mxl = ret.xl;
if (ret.yi < myi) myi = ret.yi;
if (ret.yl > myl) myl = ret.yl;
}
var xll = xl, yll = yl;
if (this.options.left == null && this.options.right != null) {
if (this.options.width == null) xi_ = xl - (mxl - mxi);
} else {
if (this.options.width == null) xl = mxl;
}
if (this.options.top == null && this.options.bottom != null) {
if (this.options.height == null) yi_ = yl - (myl - myi);
} else {
if (this.options.height == null) yl = myl;
}
// Recenter shrunken elements.
if (xl < xll && this.options.left === 'center') {
xll = (xll - xl) / 2 | 0;
xi_ += xll;
xl += xll;
}
if (yl < yll && this.options.top === 'center') {
yll = (yll - yl) / 2 | 0;
yi_ += yll;
yl += yll;
}
return { xi: xi_, xl: xl, yi: yi_, yl: yl };
};
Box.prototype._getShrinkContent = function(xi_, xl, yi_, yl, content) {
var hw = this._getShrinkSize(content)
, h = hw.height
, w = hw.width
, xll = xl
, yll = yl;
if (this.options.width == null
&& (this.options.left == null
|| this.options.right == null)) {
if (this.options.left == null && this.options.right != null) {
xi_ = xl - w - (this.border ? 2 : 0) - this.padding * 2;
} else {
xl = xi_ + w + (this.border ? 2 : 0) + this.padding * 2;
}
}
if (this.options.height == null
&& (this.options.top == null
|| this.options.bottom == null)
&& this.childBase == null) {
if (this.options.top == null && this.options.bottom != null) {
yi_ = yl - h - (this.border ? 2 : 0) - this.padding * 2;
} else {
yl = yi_ + h + (this.border ? 2 : 0) + this.padding * 2;
}
}
// Recenter shrunken elements.
if (xl < xll && this.options.left === 'center') {
xll = (xll - xl) / 2 | 0;
xi_ += xll;
xl += xll;
}
if (yl < yll && this.options.top === 'center') {
yll = (yll - yl) / 2 | 0;
yi_ += yll;
yl += yll;
}
return { xi: xi_, xl: xl, yi: yi_, yl: yl };
};
Box.prototype._getShrink = function(xi_, xl, yi_, yl, content) {
var shrinkBox = this._getShrinkBox(xi_, xl, yi_, yl)
, shrinkContent = this._getShrinkContent(xi_, xl, yi_, yl, content);
if (this.options.left == null && this.options.right != null) {
xi_ = Math.min(shrinkBox.xi, shrinkContent.xi);
} else {
xl = Math.max(shrinkBox.xl, shrinkContent.xl);
}
if (this.options.top == null && this.options.bottom != null) {
yi_ = Math.min(shrinkBox.yi, shrinkContent.yi);
} else {
yl = Math.max(shrinkBox.yl, shrinkContent.yl);
}
// Add padding if we're shrinking.
if (this.padding) {
if (this.options.left == null && this.options.right != null) {
if (this.options.width == null) xi_ -= this.padding * 2;
} else {
if (this.options.width == null) xl += this.padding * 2;
}
if (this.options.top == null && this.options.bottom != null) {
if (this.options.height == null) yi_ -= this.padding * 2;
} else {
if (this.options.height == null) yl += this.padding * 2;
}
}
return { xi: xi_, xl: xl, yi: yi_, yl: yl };
};
// Here be dragons. // Here be dragons.
// TODO: Potentially move all calculations performed on // TODO: Potentially move all calculations performed on
// xi/xl/yi/yl here to Element offset and size getters. // xi/xl/yi/yl here to Element offset and size getters.
@ -1981,11 +2106,7 @@ Box.prototype.render = function(stop) {
, c , c
, rtop , rtop
, visible , visible
, hw
, h , h
, w
, xll
, yll
, ret , ret
, cci; , cci;
@ -2016,110 +2137,11 @@ Box.prototype.render = function(stop) {
// TODO: Possibly do both shrinkBox and shrink // TODO: Possibly do both shrinkBox and shrink
// and use whichever values are higher. // and use whichever values are higher.
if (this.options.shrinkBox && this.children.length) {
var i, el, mxi = 0, mxl = 0, myi = 0, myl = 0;
for (i = 0; i < this.children.length; i++) {
el = this.children[i];
// Recurse - shouldn't be necessary.
// //if (!el.options.shrink
// // && typeof el.options.width !== 'number'
// // && typeof el.options.height !== 'number') {
// el.options._shrinkBox = !!el.options.shrinkBox;
// el.options.shrinkBox = true;
ret = el.render(true);
// Reset
// if (el.options._shrinkBox != null) {
// el.options.shrinkBox = el.options._shrinkBox;
// delete el.options._shrinkBox;
// }
if (ret.xi < mxi) mxi = ret.xi;
if (ret.xl > mxl) mxl = ret.xl;
if (ret.yi < myi) myi = ret.yi;
if (ret.yl > myl) myl = ret.yl;
}
xll = xl, yll = yl;
if (this.options.top == null && this.options.bottom != null) {
if (this.options.width == null) xi_ = xl - (mxl - mxi);
if (this.options.height == null) yi_ = yl - (myl - myi);
} else {
if (this.options.width == null) xl = mxl;
if (this.options.height == null) yl = myl;
}
// Recenter shrunken elements.
if (xl < xll && this.options.left === 'center') {
xll = (xll - xl) / 2 | 0;
xi_ += xll;
xl += xll;
}
if (yl < yll && this.options.top === 'center') {
yll = (yll - yl) / 2 | 0;
yi_ += yll;
yl += yll;
}
}
// TODO: Check for 'center', recalculate yi, and xi. Better // TODO: Check for 'center', recalculate yi, and xi. Better
// yet, simply move this check into this.left/width/etc. // yet, simply move this check into this.left/width/etc.
if (this.shrink) { if (this.shrink) {
hw = this._getShrinkSize(content); ret = this._getShrink(xi_, xl, yi_, yl, content);
h = hw.height; xi_ = ret.xi, xl = ret.xl, yi_ = ret.yi, yl = ret.yl;
w = hw.width;
xll = xl;
yll = yl;
if (this.options.width == null
&& (this.options.left == null
|| this.options.right == null)) {
if (this.options.left == null && this.options.right != null) {
xi_ = xl - w - (this.border ? 2 : 0) - this.padding * 2;
} else {
xl = xi_ + w + (this.border ? 2 : 0) + this.padding * 2;
}
}
if (this.options.height == null
&& (this.options.top == null
|| this.options.bottom == null)
&& this.childBase == null) {
if (this.options.top == null && this.options.bottom != null) {
yi_ = yl - h - (this.border ? 2 : 0) - this.padding * 2;
} else {
yl = yi_ + h + (this.border ? 2 : 0) + this.padding * 2;
}
}
// Recenter shrunken elements.
if (xl < xll && this.options.left === 'center') {
xll = (xll - xl) / 2 | 0;
xi_ += xll;
xl += xll;
}
if (yl < yll && this.options.top === 'center') {
yll = (yll - yl) / 2 | 0;
yi_ += yll;
yl += yll;
}
}
// Add padding if we're shrinking.
if ((this.options.shrinkBox || this.shrink) && this.padding) {
if (this.options.top == null && this.options.bottom != null) {
if (this.options.width == null) xi_ -= this.padding * 2;
if (this.options.height == null) yi_ -= this.padding * 2;
} else {
if (this.options.width == null) xl += this.padding * 2;
if (this.options.height == null) yl += this.padding * 2;
}
} }
// TODO: // TODO:

View File

@ -23,7 +23,7 @@ var set = blessed.radioset({
parent: form, parent: form,
left: 0, left: 0,
top: 0, top: 0,
shrinkBox: true, shrink: true,
height: 1, height: 1,
bg: 'magenta' bg: 'magenta'
}); });