Commit ec29082f authored by Hixie's avatar Hixie

Refactor layout/relayout into a single method.

This dramatically simplifies the layout model.
I haven't gone through and simplified the existing functions, we should probably go through and figure out if they can be cleaned up a bit.

R=abarth@chromium.org

Review URL: https://codereview.chromium.org/1161983004
parent e45a3f7b
...@@ -72,10 +72,17 @@ abstract class RenderSector extends RenderNode { ...@@ -72,10 +72,17 @@ abstract class RenderSector extends RenderNode {
return new SectorDimensions.withConstraints(constraints); return new SectorDimensions.withConstraints(constraints);
} }
void layout(SectorConstraints constraints, { RenderNode relayoutSubtreeRoot }) { SectorConstraints get constraints => super.constraints as SectorConstraints;
void performResize() {
// default behaviour for subclasses that have sizedByParent = true
deltaRadius = constraints.constrainDeltaRadius(0.0); deltaRadius = constraints.constrainDeltaRadius(0.0);
deltaTheta = constraints.constrainDeltaTheta(0.0); deltaTheta = constraints.constrainDeltaTheta(0.0);
layoutDone(); }
void performLayout() {
// descendants have to either override performLayout() to set both
// the dimensions and lay out children, or, set sizedByParent to
// true so that performResize()'s logic above does its thing.
assert(sizedByParent);
} }
bool hitTest(HitTestResult result, { double radius, double theta }) { bool hitTest(HitTestResult result, { double radius, double theta }) {
...@@ -210,30 +217,15 @@ class RenderSectorRing extends RenderSectorWithChildren { ...@@ -210,30 +217,15 @@ class RenderSectorRing extends RenderSectorWithChildren {
deltaTheta: innerTheta); deltaTheta: innerTheta);
} }
SectorConstraints _constraints; void performLayout() {
void layout(SectorConstraints constraints, { RenderNode relayoutSubtreeRoot }) { assert(this.parentData is SectorParentData);
if (relayoutSubtreeRoot != null)
saveRelayoutSubtreeRoot(relayoutSubtreeRoot);
relayoutSubtreeRoot = relayoutSubtreeRoot == null ? this : relayoutSubtreeRoot;
deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius); deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius);
assert(deltaRadius < double.INFINITY); assert(deltaRadius < double.INFINITY);
_constraints = constraints;
internalLayout(relayoutSubtreeRoot);
}
void relayout() {
// TODO(ianh): avoid code duplication
assert(parentData is SectorParentData);
internalLayout(this);
}
void internalLayout(RenderNode relayoutSubtreeRoot) {
assert(this.parentData is SectorParentData);
double innerDeltaRadius = deltaRadius - padding * 2.0; double innerDeltaRadius = deltaRadius - padding * 2.0;
double childRadius = this.parentData.radius + padding; double childRadius = this.parentData.radius + padding;
double paddingTheta = math.atan(padding / (this.parentData.radius + deltaRadius)); double paddingTheta = math.atan(padding / (this.parentData.radius + deltaRadius));
double innerTheta = paddingTheta; // increments with each child double innerTheta = paddingTheta; // increments with each child
double remainingDeltaTheta = _constraints.maxDeltaTheta - (innerTheta + paddingTheta); double remainingDeltaTheta = constraints.maxDeltaTheta - (innerTheta + paddingTheta);
RenderSector child = firstChild; RenderSector child = firstChild;
while (child != null) { while (child != null) {
SectorConstraints innerConstraints = new SectorConstraints( SectorConstraints innerConstraints = new SectorConstraints(
...@@ -243,7 +235,7 @@ class RenderSectorRing extends RenderSectorWithChildren { ...@@ -243,7 +235,7 @@ class RenderSectorRing extends RenderSectorWithChildren {
assert(child.parentData is SectorParentData); assert(child.parentData is SectorParentData);
child.parentData.theta = innerTheta; child.parentData.theta = innerTheta;
child.parentData.radius = childRadius; child.parentData.radius = childRadius;
child.layout(innerConstraints, relayoutSubtreeRoot: relayoutSubtreeRoot); child.layout(innerConstraints, parentUsesSize: true);
innerTheta += child.deltaTheta; innerTheta += child.deltaTheta;
remainingDeltaTheta -= child.deltaTheta; remainingDeltaTheta -= child.deltaTheta;
assert(child.parentData is SectorChildListParentData); assert(child.parentData is SectorChildListParentData);
...@@ -333,30 +325,15 @@ class RenderSectorSlice extends RenderSectorWithChildren { ...@@ -333,30 +325,15 @@ class RenderSectorSlice extends RenderSectorWithChildren {
deltaTheta: outerDeltaTheta); deltaTheta: outerDeltaTheta);
} }
SectorConstraints _constraints; void performLayout() {
void layout(SectorConstraints constraints, { RenderNode relayoutSubtreeRoot }) { assert(this.parentData is SectorParentData);
if (relayoutSubtreeRoot != null)
saveRelayoutSubtreeRoot(relayoutSubtreeRoot);
relayoutSubtreeRoot = relayoutSubtreeRoot == null ? this : relayoutSubtreeRoot;
deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta); deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta);
assert(deltaTheta <= kTwoPi); assert(deltaTheta <= kTwoPi);
_constraints = constraints;
internalLayout(relayoutSubtreeRoot);
}
void relayout() {
// TODO(ianh): avoid code duplication
assert(parentData is SectorParentData);
internalLayout(this);
}
void internalLayout(RenderNode relayoutSubtreeRoot) {
assert(this.parentData is SectorParentData);
double paddingTheta = math.atan(padding / this.parentData.radius); double paddingTheta = math.atan(padding / this.parentData.radius);
double innerTheta = this.parentData.theta + paddingTheta; double innerTheta = this.parentData.theta + paddingTheta;
double innerDeltaTheta = deltaTheta - paddingTheta * 2.0; double innerDeltaTheta = deltaTheta - paddingTheta * 2.0;
double childRadius = this.parentData.radius + padding; double childRadius = this.parentData.radius + padding;
double remainingDeltaRadius = _constraints.maxDeltaRadius - (padding * 2.0); double remainingDeltaRadius = constraints.maxDeltaRadius - (padding * 2.0);
RenderSector child = firstChild; RenderSector child = firstChild;
while (child != null) { while (child != null) {
SectorConstraints innerConstraints = new SectorConstraints( SectorConstraints innerConstraints = new SectorConstraints(
...@@ -365,7 +342,7 @@ class RenderSectorSlice extends RenderSectorWithChildren { ...@@ -365,7 +342,7 @@ class RenderSectorSlice extends RenderSectorWithChildren {
); );
child.parentData.theta = innerTheta; child.parentData.theta = innerTheta;
child.parentData.radius = childRadius; child.parentData.radius = childRadius;
child.layout(innerConstraints); child.layout(innerConstraints, parentUsesSize: true);
childRadius += child.deltaRadius; childRadius += child.deltaRadius;
remainingDeltaRadius -= child.deltaRadius; remainingDeltaRadius -= child.deltaRadius;
assert(child.parentData is SectorChildListParentData); assert(child.parentData is SectorChildListParentData);
...@@ -433,10 +410,7 @@ class RenderBoxToRenderSectorAdapter extends RenderBox { ...@@ -433,10 +410,7 @@ class RenderBoxToRenderSectorAdapter extends RenderBox {
return new BoxDimensions.withConstraints(constraints, width: dimension, height: dimension); return new BoxDimensions.withConstraints(constraints, width: dimension, height: dimension);
} }
void layout(BoxConstraints constraints, { RenderNode relayoutSubtreeRoot }) { void performLayout() {
if (relayoutSubtreeRoot != null)
saveRelayoutSubtreeRoot(relayoutSubtreeRoot);
relayoutSubtreeRoot = relayoutSubtreeRoot == null ? this : relayoutSubtreeRoot;
BoxDimensions ourDimensions; BoxDimensions ourDimensions;
if (child == null) { if (child == null) {
ourDimensions = new BoxDimensions.withConstraints(constraints, width: 0.0, height: 0.0); ourDimensions = new BoxDimensions.withConstraints(constraints, width: 0.0, height: 0.0);
...@@ -447,13 +421,12 @@ class RenderBoxToRenderSectorAdapter extends RenderBox { ...@@ -447,13 +421,12 @@ class RenderBoxToRenderSectorAdapter extends RenderBox {
assert(child.parentData is SectorParentData); assert(child.parentData is SectorParentData);
child.parentData.radius = innerRadius; child.parentData.radius = innerRadius;
child.parentData.theta = 0.0; child.parentData.theta = 0.0;
child.layout(new SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), relayoutSubtreeRoot: relayoutSubtreeRoot); child.layout(new SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), parentUsesSize: true);
double dimension = (innerRadius + child.deltaRadius) * 2.0; double dimension = (innerRadius + child.deltaRadius) * 2.0;
ourDimensions = new BoxDimensions.withConstraints(constraints, width: dimension, height: dimension); ourDimensions = new BoxDimensions.withConstraints(constraints, width: dimension, height: dimension);
} }
width = ourDimensions.width; width = ourDimensions.width;
height = ourDimensions.height; height = ourDimensions.height;
layoutDone();
} }
double width; double width;
...@@ -503,10 +476,9 @@ class RenderSolidColor extends RenderDecoratedSector { ...@@ -503,10 +476,9 @@ class RenderSolidColor extends RenderDecoratedSector {
return new SectorDimensions.withConstraints(constraints, deltaTheta: 1.0); // 1.0 radians return new SectorDimensions.withConstraints(constraints, deltaTheta: 1.0); // 1.0 radians
} }
void layout(SectorConstraints constraints, { RenderNode relayoutSubtreeRoot }) { void performLayout() {
deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius); deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius);
deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta); deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta);
layoutDone();
} }
void handlePointer(sky.PointerEvent event) { void handlePointer(sky.PointerEvent event) {
......
...@@ -22,10 +22,9 @@ class RenderSolidColor extends RenderDecoratedBox { ...@@ -22,10 +22,9 @@ class RenderSolidColor extends RenderDecoratedBox {
width: desiredWidth); width: desiredWidth);
} }
void layout(BoxConstraints constraints, { RenderNode relayoutSubtreeRoot }) { void performLayout() {
width = constraints.constrainWidth(desiredWidth); width = constraints.constrainWidth(desiredWidth);
height = constraints.constrainHeight(desiredHeight); height = constraints.constrainHeight(desiredHeight);
layoutDone();
} }
void handlePointer(PointerEvent event) { void handlePointer(PointerEvent event) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment