提交 bf94f5c0 编写于 作者: A Adam Barth

Use Point, Size, and Rect in layout2.dart

R=ianh@google.com

Review URL: https://codereview.chromium.org/1156303004
上级 3971d9af
......@@ -22,12 +22,19 @@ class Rect {
..[3] = point.y + size.height;
}
Rect.fromSize(Size size) {
_value
..[2] = size.width
..[3] = size.height;
}
Rect.fromLTRB(double left, double top, double right, double bottom) {
setLTRB(left, top, right, bottom);
}
Point get upperLeft => new Point(left, top);
Point get lowerRight => new Point(right, bottom);
Point get center => new Point(left + right / 2.0, top + bottom / 2.0);
Size get size => new Size(right - left, bottom - top);
......
......@@ -11,6 +11,8 @@ class Size {
Size(this.width, this.height);
Size.infinite() : width = double.INFINITY, height = double.INFINITY;
bool operator ==(other) {
if (!(other is Size)) return false;
return width == other.width && height == other.height;
......
......@@ -256,7 +256,7 @@ class RenderSectorRing extends RenderSectorWithChildren {
RenderSector child = firstChild;
while (child != null) {
assert(child.parentData is SectorChildListParentData);
canvas.paintChild(child, 0.0, 0.0);
canvas.paintChild(child, new sky.Point(0.0, 0.0));
child = child.parentData.nextSibling;
}
}
......@@ -361,7 +361,7 @@ class RenderSectorSlice extends RenderSectorWithChildren {
RenderSector child = firstChild;
while (child != null) {
assert(child.parentData is SectorChildListParentData);
canvas.paintChild(child, 0.0, 0.0);
canvas.paintChild(child, new sky.Point(0.0, 0.0));
child = child.parentData.nextSibling;
}
}
......@@ -411,9 +411,8 @@ class RenderBoxToRenderSectorAdapter extends RenderBox {
}
void performLayout() {
BoxDimensions ourDimensions;
if (child == null) {
ourDimensions = new BoxDimensions.withConstraints(constraints, width: 0.0, height: 0.0);
size = constraints.constrain(new sky.Size(0.0, 0.0));
} else {
assert(child is RenderSector);
assert(!constraints.isInfinite);
......@@ -423,28 +422,27 @@ class RenderBoxToRenderSectorAdapter extends RenderBox {
child.parentData.theta = 0.0;
child.layout(new SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), parentUsesSize: true);
double dimension = (innerRadius + child.deltaRadius) * 2.0;
ourDimensions = new BoxDimensions.withConstraints(constraints, width: dimension, height: dimension);
size = constraints.constrain(new sky.Size(dimension, dimension));
}
width = ourDimensions.width;
height = ourDimensions.height;
}
double width;
double height;
// paint origin is 0,0 of our circle
void paint(RenderNodeDisplayList canvas) {
super.paint(canvas);
if (child != null)
canvas.paintChild(child, width/2.0, height/2.0);
if (child != null) {
sky.Rect bounds = new sky.Rect.fromSize(size);
canvas.paintChild(child, bounds.center);
}
}
bool hitTest(HitTestResult result, { double x, double y }) {
bool hitTest(HitTestResult result, { sky.Point position }) {
double x = position.x;
double y = position.y;
if (child == null)
return false;
// translate to our origin
x -= width/2.0;
y -= height/2.0;
x -= size.width/2.0;
y -= size.height/2.0;
// convert to radius/theta
double radius = math.sqrt(x*x+y*y);
double theta = (math.atan2(x, -y) - math.PI/2.0) % kTwoPi;
......
......@@ -7,24 +7,22 @@ import 'package:sky/framework/app.dart';
import 'package:sky/framework/layout2.dart';
class RenderSolidColor extends RenderDecoratedBox {
final double desiredHeight;
final double desiredWidth;
final Size desiredSize;
final int backgroundColor;
RenderSolidColor(int backgroundColor, { this.desiredHeight: double.INFINITY,
this.desiredWidth: double.INFINITY })
RenderSolidColor(int backgroundColor, { this.desiredSize })
: backgroundColor = backgroundColor,
super(new BoxDecoration(backgroundColor: backgroundColor));
super(new BoxDecoration(backgroundColor: backgroundColor)) {
}
BoxDimensions getIntrinsicDimensions(BoxConstraints constraints) {
return new BoxDimensions.withConstraints(constraints,
height: desiredHeight,
width: desiredWidth);
width: desiredSize.width,
height: desiredSize.height);
}
void performLayout() {
width = constraints.constrainWidth(desiredWidth);
height = constraints.constrainHeight(desiredHeight);
size = constraints.constrain(desiredSize);
}
void handlePointer(PointerEvent event) {
......@@ -43,7 +41,7 @@ void main() {
decoration: new BoxDecoration(backgroundColor: 0xFF000000));
void addFlexChild(RenderFlex parent, int backgroundColor, { int flex: 0 }) {
RenderNode child = new RenderSolidColor(backgroundColor);
RenderNode child = new RenderSolidColor(backgroundColor, desiredSize: new Size.infinite());
parent.add(child);
child.parentData.flex = flex;
}
......@@ -52,13 +50,13 @@ void main() {
addFlexChild(root, 0xFFFFFF00, flex: 1);
// Turquoise box
root.add(new RenderSolidColor(0x7700FFFF, desiredHeight: 100.0, desiredWidth: 100.0));
root.add(new RenderSolidColor(0x7700FFFF, desiredSize: new Size(100.0, 100.0)));
// Green and cyan render block with padding
var renderBlock = new RenderBlock(decoration: new BoxDecoration(backgroundColor: 0xFFFFFFFF));
renderBlock.add(new RenderSolidColor(0xFF00FF00, desiredHeight: 50.0, desiredWidth: 100.0));
renderBlock.add(new RenderSolidColor(0x7700FFFF, desiredHeight: 100.0, desiredWidth: 50.0));
renderBlock.add(new RenderSolidColor(0xFF00FF00, desiredSize: new Size(100.0, 50.0)));
renderBlock.add(new RenderSolidColor(0x7700FFFF, desiredSize: new Size(50.0, 100.0)));
root.add(new RenderPadding(const EdgeDims(10.0, 10.0, 10.0, 10.0), renderBlock));
......
......@@ -41,7 +41,7 @@ class AppView {
switch(event.type) {
case 'pointerdown':
result = new HitTestResult();
_renderView.hitTest(result, x: event.x, y: event.y);
_renderView.hitTest(result, position: new sky.Point(event.x, event.y));
_hitTestResultForPointer[event.pointer] = result;
break;
case 'pointerup':
......@@ -54,7 +54,7 @@ class AppView {
// In the case of mouse hover we won't already have a cached down.
if (result == null) {
result = new HitTestResult();
_renderView.hitTest(result, x: event.x, y: event.y);
_renderView.hitTest(result, position: new sky.Point(event.x, event.y));
}
break;
}
......
......@@ -5,6 +5,7 @@
import '../fn2.dart';
import '../layout2.dart';
import '../theme/typography.dart' as typography;
import 'dart:sky' as sky;
// RenderNode
class RenderScaffold extends RenderDecoratedBox {
......@@ -81,10 +82,9 @@ class RenderScaffold extends RenderDecoratedBox {
bool get sizedByParent => true;
void performResize() {
width = constraints.constrainWidth(double.INFINITY);
assert(width < double.INFINITY);
height = constraints.constrainHeight(double.INFINITY);
assert(height < double.INFINITY);
size = constraints.constrain(new sky.Size.infinite());
assert(size.width < double.INFINITY);
assert(size.height < double.INFINITY);
}
static const kToolbarHeight = 100.0;
......@@ -93,70 +93,65 @@ class RenderScaffold extends RenderDecoratedBox {
static const kButtonY = -16.0; // from bottom edge of body
void performLayout() {
double bodyHeight = height;
double bodyHeight = size.height;
double bodyPosition = 0.0;
if (toolbar != null) {
toolbar.layout(new BoxConstraints.tight(width: width, height: kToolbarHeight));
toolbar.layout(new BoxConstraints.tight(width: size.width, height: kToolbarHeight));
assert(toolbar.parentData is BoxParentData);
toolbar.parentData.x = 0.0;
toolbar.parentData.y = 0.0;
toolbar.parentData.position = new sky.Point(0.0, 0.0);
bodyPosition = kToolbarHeight;
bodyHeight -= kToolbarHeight;
}
if (statusbar != null) {
statusbar.layout(new BoxConstraints.tight(width: width, height: kStatusbarHeight));
statusbar.layout(new BoxConstraints.tight(width: size.width, height: kStatusbarHeight));
assert(statusbar.parentData is BoxParentData);
statusbar.parentData.x = 0.0;
statusbar.parentData.y = height - kStatusbarHeight;
statusbar.parentData.position = new sky.Point(0.0, size.height - kStatusbarHeight);
bodyHeight -= kStatusbarHeight;
}
if (body != null) {
body.layout(new BoxConstraints.tight(width: width, height: bodyHeight));
body.layout(new BoxConstraints.tight(width: size.width, height: bodyHeight));
assert(body.parentData is BoxParentData);
body.parentData.x = 0.0;
body.parentData.y = bodyPosition;
body.parentData.position = new sky.Point(0.0, bodyPosition);
}
if (drawer != null) {
drawer.layout(new BoxConstraints(minWidth: 0.0, maxWidth: width, minHeight: height, maxHeight: height));
drawer.layout(new BoxConstraints(minWidth: 0.0, maxWidth: size.width, minHeight: size.height, maxHeight: size.height));
assert(drawer.parentData is BoxParentData);
drawer.parentData.x = 0.0;
drawer.parentData.y = 0.0;
drawer.parentData.position = new sky.Point(0.0, 0.0);
}
if (floatingActionButton != null) {
floatingActionButton.layout(new BoxConstraints(minWidth: 0.0, maxWidth: width, minHeight: height, maxHeight: height));
floatingActionButton.layout(new BoxConstraints(minWidth: 0.0, maxWidth: size.width, minHeight: size.height, maxHeight: size.height));
assert(floatingActionButton.parentData is BoxParentData);
floatingActionButton.parentData.x = width - xButtonX;
floatingActionButton.parentData.y = bodyPosition + bodyHeight - kButtonY;
floatingActionButton.parentData.position = new sky.Point(size.width - xButtonX, bodyPosition + bodyHeight - kButtonY);
}
}
void paint(RenderNodeDisplayList canvas) {
if (body != null)
canvas.paintChild(body, (body.parentData as BoxParentData).x, (body.parentData as BoxParentData).y);
canvas.paintChild(body, (body.parentData as BoxParentData).position);
if (statusbar != null)
canvas.paintChild(statusbar, (statusbar.parentData as BoxParentData).x, (statusbar.parentData as BoxParentData).y);
canvas.paintChild(statusbar, (statusbar.parentData as BoxParentData).position);
if (toolbar != null)
canvas.paintChild(toolbar, (toolbar.parentData as BoxParentData).x, (toolbar.parentData as BoxParentData).y);
canvas.paintChild(toolbar, (toolbar.parentData as BoxParentData).position);
if (floatingActionButton != null)
canvas.paintChild(floatingActionButton, (floatingActionButton.parentData as BoxParentData).x, (floatingActionButton.parentData as BoxParentData).y);
canvas.paintChild(floatingActionButton, (floatingActionButton.parentData as BoxParentData).position);
if (drawer != null)
canvas.paintChild(drawer, (drawer.parentData as BoxParentData).x, (drawer.parentData as BoxParentData).y);
canvas.paintChild(drawer, (drawer.parentData as BoxParentData).position);
}
void hitTestChildren(HitTestResult result, { double x, double y }) {
void hitTestChildren(HitTestResult result, { sky.Point position }) {
assert(floatingActionButton == null || floatingActionButton.parentData is BoxParentData);
assert(statusbar == null || statusbar.parentData is BoxParentData);
if ((drawer != null) && (x < drawer.width)) {
drawer.hitTest(result, x: x, y: y);
} else if ((floatingActionButton != null) && (x >= floatingActionButton.parentData.x) && (x < floatingActionButton.parentData.x + floatingActionButton.width)
&& (y >= floatingActionButton.parentData.y) && (y < floatingActionButton.parentData.y + floatingActionButton.height)) {
floatingActionButton.hitTest(result, x: x-floatingActionButton.parentData.x, y: y-floatingActionButton.parentData.y);
} else if ((toolbar != null) && (y < toolbar.height)) {
toolbar.hitTest(result, x: x, y: y);
} else if ((statusbar != null) && (y > statusbar.parentData.y)) {
statusbar.hitTest(result, x: x, y: y-statusbar.parentData.y);
if ((drawer != null) && (x < drawer.size.width)) {
drawer.hitTest(result, position: position);
} else if ((floatingActionButton != null) && (position.x >= floatingActionButton.parentData.position.x) && (position.x < floatingActionButton.parentData.position.x + floatingActionButton.size.width)
&& (position.y >= floatingActionButton.parentData.position.y) && (position.y < floatingActionButton.parentData.position.y + floatingActionButton.size.height)) {
floatingActionButton.hitTest(result, position: new sky.Point(position.x - floatingActionButton.parentData.position.x, position.y - floatingActionButton.parentData.position.y));
} else if ((toolbar != null) && (position.y < toolbar.size.height)) {
toolbar.hitTest(result, position: position);
} else if ((statusbar != null) && (position.y > statusbar.parentData.position.y)) {
statusbar.hitTest(result, position: new sky.Point(position.x, position.y - statusbar.parentData.position.y));
} else if (body != null) {
body.hitTest(result, x: x, y: y-body.parentData.y);
body.hitTest(result, position: new sky.Point(position.x, position.y - body.parentData.position.y));
}
}
......
......@@ -943,24 +943,21 @@ class Text extends Component {
// for now, but only for now:
class RenderSolidColor extends RenderDecoratedBox {
final double desiredHeight;
final double desiredWidth;
final sky.Size desiredSize;
final int backgroundColor;
RenderSolidColor(int backgroundColor, { this.desiredHeight: double.INFINITY,
this.desiredWidth: double.INFINITY })
RenderSolidColor(int backgroundColor, { this.desiredSize })
: backgroundColor = backgroundColor,
super(new BoxDecoration(backgroundColor: backgroundColor));
BoxDimensions getIntrinsicDimensions(BoxConstraints constraints) {
return new BoxDimensions.withConstraints(constraints,
height: desiredHeight,
width: desiredWidth);
width: desiredSize.width,
height: desiredSize.height);
}
void performLayout() {
width = constraints.constrainWidth(desiredWidth);
height = constraints.constrainHeight(desiredHeight);
size = constraints.constrain(desiredSize);
}
void handlePointer(sky.PointerEvent event) {
......@@ -982,7 +979,7 @@ class Rectangle extends RenderNodeWrapper {
final int color;
RenderSolidColor root;
RenderSolidColor createNode() => new RenderSolidColor(color, desiredWidth: 40.0, desiredHeight: 130.0);
RenderSolidColor createNode() => new RenderSolidColor(color, desiredSize: new sky.Size(40.0, 130.0));
static final Rectangle _emptyRectangle = new Rectangle(0);
RenderNodeWrapper get emptyNode => _emptyRectangle;
......
......@@ -34,9 +34,9 @@ double clamp({double min: 0.0, double value: 0.0, double max: double.INFINITY})
class RenderNodeDisplayList extends sky.PictureRecorder {
RenderNodeDisplayList(double width, double height) : super(width, height);
void paintChild(RenderNode child, double x, double y) {
void paintChild(RenderNode child, sky.Point position) {
save();
translate(x, y);
translate(position.x, position.y);
child.paint(this);
restore();
}
......@@ -198,7 +198,7 @@ abstract class RenderNode extends AbstractNode {
// RenderNode subclasses are expected to have a method like the
// following (with the signature being whatever passes for coordinates
// for this particular class):
// bool hitTest(HitTestResult result, { double x, double y }) {
// bool hitTest(HitTestResult result, { sky.Point position }) {
// // If (x,y) is not inside this node, then return false. (You
// // can assume that the given coordinate is inside your
// // dimensions. You only need to check this if you're an
......@@ -445,9 +445,14 @@ class BoxConstraints {
return clamp(min: minHeight, max: maxHeight, value: height);
}
sky.Size constrain(sky.Size size) {
return new sky.Size(constrainWidth(size.width), constrainHeight(size.height));
}
bool get isInfinite => maxWidth >= double.INFINITY || maxHeight >= double.INFINITY;
}
// TODO(abarth): Replace with sky.Size.
class BoxDimensions {
const BoxDimensions({ this.width: 0.0, this.height: 0.0 });
......@@ -462,8 +467,7 @@ class BoxDimensions {
}
class BoxParentData extends ParentData {
double x = 0.0;
double y = 0.0;
sky.Point position = new sky.Point(0.0, 0.0);
}
abstract class RenderBox extends RenderNode {
......@@ -485,10 +489,9 @@ abstract class RenderBox extends RenderNode {
BoxConstraints get constraints => super.constraints as BoxConstraints;
void performResize() {
// default behaviour for subclasses that have sizedByParent = true
width = constraints.constrainWidth(0.0);
height = constraints.constrainHeight(0.0);
assert(height < double.INFINITY);
assert(width < double.INFINITY);
size = constraints.constrain(new sky.Size(0.0, 0.0));
assert(size.height < double.INFINITY);
assert(size.width < double.INFINITY);
}
void performLayout() {
// descendants have to either override performLayout() to set both
......@@ -497,15 +500,14 @@ abstract class RenderBox extends RenderNode {
assert(sizedByParent);
}
bool hitTest(HitTestResult result, { double x, double y }) {
hitTestChildren(result, x: x, y: y);
bool hitTest(HitTestResult result, { sky.Point position }) {
hitTestChildren(result, position: position);
result.add(this);
return true;
}
void hitTestChildren(HitTestResult result, { double x, double y }) { }
void hitTestChildren(HitTestResult result, { sky.Point position }) { }
double width;
double height;
sky.Size size = new sky.Size(0.0, 0.0);
}
class RenderPadding extends RenderBox with RenderNodeWithChildMixin<RenderBox> {
......@@ -538,29 +540,30 @@ class RenderPadding extends RenderBox with RenderNodeWithChildMixin<RenderBox> {
assert(padding != null);
BoxConstraints innerConstraints = constraints.deflate(padding);
if (child == null) {
width = innerConstraints.constrainWidth(padding.left + padding.right);
height = innerConstraints.constrainHeight(padding.top + padding.bottom);
size = innerConstraints.constrain(
new sky.Size(padding.left + padding.right, padding.top + padding.bottom));
return;
}
child.layout(innerConstraints, parentUsesSize: true);
assert(child.parentData is BoxParentData);
child.parentData.x = padding.left;
child.parentData.y = padding.top;
width = constraints.constrainWidth(padding.left + child.width + padding.right);
height = constraints.constrainHeight(padding.top + child.height + padding.bottom);
child.parentData.position = new sky.Point(padding.left, padding.top);
size = constraints.constrain(new sky.Size(padding.left + child.size.width + padding.right,
padding.top + child.size.height + padding.bottom));
}
void paint(RenderNodeDisplayList canvas) {
if (child != null)
canvas.paintChild(child, child.parentData.x, child.parentData.y);
canvas.paintChild(child, child.parentData.position);
}
void hitTestChildren(HitTestResult result, { double x, double y }) {
void hitTestChildren(HitTestResult result, { sky.Point position }) {
if (child != null) {
assert(child.parentData is BoxParentData);
if ((x >= child.parentData.x) && (x < child.parentData.x + child.width) &&
(y >= child.parentData.y) && (y < child.parentData.y + child.height))
child.hitTest(result, x: x+child.parentData.x, y: y+child.parentData.y);
sky.Rect childBounds = new sky.Rect.fromPointAndSize(child.parentData.position, child.size);
if (childBounds.contains(position)) {
child.hitTest(result, position: new sky.Point(position.x - child.parentData.position.x,
position.y - child.parentData.position.y));
}
}
}
......@@ -589,15 +592,15 @@ class RenderDecoratedBox extends RenderBox {
}
void paint(RenderNodeDisplayList canvas) {
assert(width != null);
assert(height != null);
assert(size.width != null);
assert(size.height != null);
if (_decoration == null)
return;
if (_decoration.backgroundColor != null) {
sky.Paint paint = new sky.Paint()..color = _decoration.backgroundColor;
canvas.drawRect(new sky.Rect()..setLTRB(0.0, 0.0, width, height), paint);
canvas.drawRect(new sky.Rect.fromLTRB(0.0, 0.0, size.width, size.height), paint);
}
}
......@@ -612,15 +615,15 @@ class RenderDecoratedCircle extends RenderDecoratedBox with RenderNodeWithChildM
}
void paint(RenderNodeDisplayList canvas) {
assert(width != null);
assert(height != null);
assert(size.width != null);
assert(size.height != null);
if (_decoration == null)
return;
if (_decoration.backgroundColor != null) {
sky.Paint paint = new sky.Paint()..color = _decoration.backgroundColor;
canvas.drawCircle(new sky.Rect()..setLTRB(0.0, 0.0, width, height), paint);
canvas.drawCircle(0.0, 0.0, (size.width + size.height) / 2, paint);
}
}
}
......@@ -649,10 +652,9 @@ class RenderView extends RenderNode with RenderNodeWithChildMixin<RenderBox> {
this.child = child;
}
double _width;
double get width => _width;
double _height;
double get height => _height;
sky.Size _size = new sky.Size(0.0, 0.0);
double get width => _size.width;
double get height => _size.height;
int _orientation; // 0..3
int get orientation => _orientation;
......@@ -666,16 +668,15 @@ class RenderView extends RenderNode with RenderNodeWithChildMixin<RenderBox> {
child.rotate(oldAngle: _orientation, newAngle: constraints.orientation, time: timeForRotation);
_orientation = constraints.orientation;
}
_width = constraints.width;
_height = constraints.height;
assert(height < double.INFINITY);
assert(width < double.INFINITY);
_size = new sky.Size(constraints.width, constraints.height);
assert(_size.height < double.INFINITY);
assert(_size.width < double.INFINITY);
}
void performLayout() {
if (child != null) {
child.layout(new BoxConstraints.tight(width: width, height: height));
assert(child.width == width);
assert(child.height == height);
assert(child.size.width == width);
assert(child.size.height == height);
}
}
......@@ -683,16 +684,19 @@ class RenderView extends RenderNode with RenderNodeWithChildMixin<RenderBox> {
assert(false); // nobody tells the screen to rotate, the whole rotate() dance is started from our performResize()
}
bool hitTest(HitTestResult result, { double x, double y }) {
if (child != null && x >= 0.0 && x < child.width && y >= 0.0 && y < child.height)
child.hitTest(result, x: x, y: y);
bool hitTest(HitTestResult result, { sky.Point position }) {
if (child != null) {
sky.Rect childBounds = new sky.Rect.fromSize(child.size);
if (childBounds.contains(position))
child.hitTest(result, position: position);
}
result.add(this);
return true;
}
void paint(RenderNodeDisplayList canvas) {
if (child != null)
canvas.paintChild(child, 0.0, 0.0);
canvas.paintChild(child, new sky.Point(0.0, 0.0));
}
void paintFrame() {
......@@ -708,14 +712,15 @@ class RenderView extends RenderNode with RenderNodeWithChildMixin<RenderBox> {
// DEFAULT BEHAVIORS FOR RENDERBOX CONTAINERS
abstract class RenderBoxContainerDefaultsMixin<ChildType extends RenderBox, ParentDataType extends ContainerParentDataMixin<ChildType>> implements ContainerRenderNodeMixin<ChildType, ParentDataType> {
void defaultHitTestChildren(HitTestResult result, { double x, double y }) {
void defaultHitTestChildren(HitTestResult result, { sky.Point position }) {
// the x, y parameters have the top left of the node's box as the origin
ChildType child = lastChild;
while (child != null) {
assert(child.parentData is BoxParentData);
if ((x >= child.parentData.x) && (x < child.parentData.x + child.width) &&
(y >= child.parentData.y) && (y < child.parentData.y + child.height)) {
if (child.hitTest(result, x: x-child.parentData.x, y: y-child.parentData.y))
sky.Rect childBounds = new sky.Rect.fromPointAndSize(child.parentData.position, child.size);
if (childBounds.contains(position)) {
if (child.hitTest(result, position: new sky.Point(position.x - child.parentData.position.x,
position.y - child.parentData.position.y)))
break;
}
child = child.parentData.previousSibling;
......@@ -726,7 +731,7 @@ abstract class RenderBoxContainerDefaultsMixin<ChildType extends RenderBox, Pare
RenderBox child = firstChild;
while (child != null) {
assert(child.parentData is BoxParentData);
canvas.paintChild(child, child.parentData.x, child.parentData.y);
canvas.paintChild(child, child.parentData.position);
child = child.parentData.nextSibling;
}
}
......@@ -776,24 +781,23 @@ class RenderBlock extends RenderDecoratedBox with ContainerRenderNodeMixin<Rende
void performLayout() {
assert(constraints is BoxConstraints);
width = constraints.constrainWidth(constraints.maxWidth);
assert(width < double.INFINITY);
size.width = constraints.constrainWidth(constraints.maxWidth);
assert(size.width < double.INFINITY);
double y = 0.0;
double innerWidth = width;
double innerWidth = size.width;
RenderBox child = firstChild;
while (child != null) {
child.layout(new BoxConstraints(minWidth: innerWidth, maxWidth: innerWidth), parentUsesSize: true);
assert(child.parentData is BlockParentData);
child.parentData.x = 0.0;
child.parentData.y = y;
y += child.height;
child.parentData.position = new sky.Point(0.0, y);
y += child.size.height;
child = child.parentData.nextSibling;
}
height = constraints.constrainHeight(y);
size.height = constraints.constrainHeight(y);
}
void hitTestChildren(HitTestResult result, { double x, double y }) {
defaultHitTestChildren(result, x: x, y: y);
void hitTestChildren(HitTestResult result, { sky.Point position }) {
defaultHitTestChildren(result, position: position);
}
void paint(RenderNodeDisplayList canvas) {
......@@ -841,10 +845,9 @@ class RenderFlex extends RenderDecoratedBox with ContainerRenderNodeMixin<Render
bool get sizedByParent => true;
void performResize() {
width = _constraints.constrainWidth(_constraints.maxWidth);
height = _constraints.constrainHeight(_constraints.maxHeight);
assert(height < double.INFINITY);
assert(width < double.INFINITY);
size = _constraints.constrain(new sky.Size(_constraints.maxWidth, _constraints.maxHeight));
assert(size.height < double.INFINITY);
assert(size.width < double.INFINITY);
}
int _getFlex(RenderBox child) {
......@@ -867,7 +870,7 @@ class RenderFlex extends RenderDecoratedBox with ContainerRenderNodeMixin<Render
BoxConstraints innerConstraints = new BoxConstraints(maxHeight: constraints.maxHeight,
maxWidth: constraints.maxWidth);
child.layout(innerConstraints, parentUsesSize: true);
freeSpace -= (_direction == FlexDirection.Horizontal) ? child.width : child.height;
freeSpace -= (_direction == FlexDirection.Horizontal) ? child.size.width : child.size.height;
}
child = child.parentData.nextSibling;
}
......@@ -899,22 +902,20 @@ class RenderFlex extends RenderDecoratedBox with ContainerRenderNodeMixin<Render
// For now, center the flex items in the cross direction
switch (_direction) {
case FlexDirection.Horizontal:
child.parentData.x = usedSpace;
usedSpace += child.width;
child.parentData.y = height / 2.0 - child.height / 2.0;
child.parentData.position = new sky.Point(usedSpace, size.height / 2.0 - child.size.height / 2.0);
usedSpace += child.size.width;
break;
case FlexDirection.Vertical:
child.parentData.y = usedSpace;
usedSpace += child.height;
child.parentData.x = width / 2.0 - child.width / 2.0;
child.parentData.position = new sky.Point(size.width / 2.0 - child.size.width / 2.0, usedSpace);
usedSpace += child.size.height;
break;
}
child = child.parentData.nextSibling;
}
}
void hitTestChildren(HitTestResult result, { double x, double y }) {
defaultHitTestChildren(result, x: x, y: y);
void hitTestChildren(HitTestResult result, { sky.Point position }) {
defaultHitTestChildren(result, position: position);
}
void paint(RenderNodeDisplayList canvas) {
......
......@@ -8,22 +8,18 @@ import 'dart:sky' as sky;
import 'package:sky/framework/layout2.dart';
class RenderSizedBox extends RenderBox {
final double desiredHeight;
final double desiredWidth;
final sky.Size desiredSize;
RenderSizedBox({ this.desiredHeight: double.INFINITY,
this.desiredWidth: double.INFINITY });
RenderSizedBox({ this.desiredSize });
BoxDimensions getIntrinsicDimensions(BoxConstraints constraints) {
return new BoxDimensions.withConstraints(constraints,
height: desiredHeight,
width: desiredWidth);
width: desiredSize.width,
height: desiredSize.height);
}
void layout(BoxConstraints constraints, { RenderNode relayoutSubtreeRoot }) {
width = constraints.constrainWidth(desiredWidth);
height = constraints.constrainHeight(desiredHeight);
layoutDone();
void performLayout() {
size = constraints.constrain(desiredSize);
}
}
......@@ -31,10 +27,10 @@ void main() {
initUnit();
test("should size to render view", () {
RenderSizedBox root = new RenderSizedBox();
RenderSizedBox root = new RenderSizedBox(desiredSize: new sky.Size.infinite());
RenderView renderView = new RenderView(child: root);
renderView.layout(newWidth: sky.view.width, newHeight: sky.view.height);
expect(root.width, equals(sky.view.width));
expect(root.height, equals(sky.view.height));
renderView.layout(new ViewConstraints(width: sky.view.width, height: sky.view.height));
expect(root.size.width, equals(sky.view.width));
expect(root.size.height, equals(sky.view.height));
});
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册