From 9a73fe989ee4806a109ec06e9f3c33da5d8ccba4 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 28 May 2015 13:39:26 -0700 Subject: [PATCH] Introduce RenderProxyBox and RenderSizedBox Also makes Point, Size, and Rect immutable. R=ianh@google.com Review URL: https://codereview.chromium.org/1162033002 --- engine/core/painting/Point.dart | 6 +-- engine/core/painting/Rect.dart | 14 ++--- engine/core/painting/Size.dart | 8 +-- examples/raw/sector-layout.dart | 4 +- examples/raw/simple_render_tree.dart | 4 +- examples/raw/spinning_square.dart | 2 +- examples/spinning_arabic.dart | 2 +- sdk/lib/framework/components2/scaffold.dart | 6 +-- sdk/lib/framework/layout2.dart | 58 ++++++++++++++++++--- tests/raw/render_box.dart | 19 +------ 10 files changed, 74 insertions(+), 49 deletions(-) diff --git a/engine/core/painting/Point.dart b/engine/core/painting/Point.dart index 82d2875e3b8..62cd87d7b80 100644 --- a/engine/core/painting/Point.dart +++ b/engine/core/painting/Point.dart @@ -6,10 +6,10 @@ part of dart.sky; /// Holds 2 floating-point coordinates. class Point { - double x; - double y; + final double x; + final double y; - Point(this.x, this.y); + const Point(this.x, this.y); bool operator ==(other) { if (!(other is Point)) return false; diff --git a/engine/core/painting/Rect.dart b/engine/core/painting/Rect.dart index 8fdff2ef102..b0b7f7c40a0 100644 --- a/engine/core/painting/Rect.dart +++ b/engine/core/painting/Rect.dart @@ -29,7 +29,11 @@ class Rect { } Rect.fromLTRB(double left, double top, double right, double bottom) { - setLTRB(left, top, right, bottom); + _value + ..[0] = left + ..[1] = top + ..[2] = right + ..[3] = bottom; } Point get upperLeft => new Point(left, top); @@ -43,14 +47,6 @@ class Rect { bool contains(Point point) => point.x >= left && point.x < right && point.y >= top && point.y < bottom; - void setLTRB(double left, double top, double right, double bottom) { - _value - ..[0] = left - ..[1] = top - ..[2] = right - ..[3] = bottom; - } - bool operator ==(other) { if (!(other is Rect)) return false; for (var i = 0; i < 4; ++i) { diff --git a/engine/core/painting/Size.dart b/engine/core/painting/Size.dart index aa65274cd5e..f2d43a65ca1 100644 --- a/engine/core/painting/Size.dart +++ b/engine/core/painting/Size.dart @@ -6,12 +6,12 @@ part of dart.sky; /// Holds a 2D floating-point size. class Size { - double width; - double height; + final double width; + final double height; - Size(this.width, this.height); + const Size(this.width, this.height); - Size.infinite() : width = double.INFINITY, height = double.INFINITY; + const Size.infinite() : width = double.INFINITY, height = double.INFINITY; bool operator ==(other) { if (!(other is Size)) return false; diff --git a/examples/raw/sector-layout.dart b/examples/raw/sector-layout.dart index f66415be695..36ee2af84a0 100644 --- a/examples/raw/sector-layout.dart +++ b/examples/raw/sector-layout.dart @@ -126,10 +126,10 @@ class RenderDecoratedSector extends RenderSector { sky.Paint paint = new sky.Paint()..color = _decoration.backgroundColor; sky.Path path = new sky.Path(); double outerRadius = (parentData.radius + deltaRadius); - sky.Rect outerBounds = new sky.Rect()..setLTRB(-outerRadius, -outerRadius, outerRadius, outerRadius); + sky.Rect outerBounds = new sky.Rect.fromLTRB(-outerRadius, -outerRadius, outerRadius, outerRadius); path.arcTo(outerBounds, deg(parentData.theta), deg(deltaTheta), true); double innerRadius = parentData.radius; - sky.Rect innerBounds = new sky.Rect()..setLTRB(-innerRadius, -innerRadius, innerRadius, innerRadius); + sky.Rect innerBounds = new sky.Rect.fromLTRB(-innerRadius, -innerRadius, innerRadius, innerRadius); path.arcTo(innerBounds, deg(parentData.theta + deltaTheta), deg(-deltaTheta), false); path.close(); canvas.drawPath(path, paint); diff --git a/examples/raw/simple_render_tree.dart b/examples/raw/simple_render_tree.dart index 7ed385b2d6a..1131ad17a02 100644 --- a/examples/raw/simple_render_tree.dart +++ b/examples/raw/simple_render_tree.dart @@ -10,7 +10,7 @@ class RenderSolidColor extends RenderDecoratedBox { final Size desiredSize; final int backgroundColor; - RenderSolidColor(int backgroundColor, { this.desiredSize }) + RenderSolidColor(int backgroundColor, { this.desiredSize: const sky.Size.infinite() }) : backgroundColor = backgroundColor, super(new BoxDecoration(backgroundColor: backgroundColor)) { } @@ -41,7 +41,7 @@ void main() { decoration: new BoxDecoration(backgroundColor: 0xFF000000)); void addFlexChild(RenderFlex parent, int backgroundColor, { int flex: 0 }) { - RenderNode child = new RenderSolidColor(backgroundColor, desiredSize: new Size.infinite()); + RenderNode child = new RenderSolidColor(backgroundColor); parent.add(child); child.parentData.flex = flex; } diff --git a/examples/raw/spinning_square.dart b/examples/raw/spinning_square.dart index 155f5ccc685..17072330d56 100644 --- a/examples/raw/spinning_square.dart +++ b/examples/raw/spinning_square.dart @@ -13,7 +13,7 @@ void beginFrame(double timeStamp) { PictureRecorder canvas = new PictureRecorder(view.width, view.height); canvas.translate(view.width / 2.0, view.height / 2.0); canvas.rotateDegrees(delta / 10); - canvas.drawRect(new Rect()..setLTRB(-100.0, -100.0, 100.0, 100.0), + canvas.drawRect(new Rect.fromLTRB(-100.0, -100.0, 100.0, 100.0), new Paint()..setARGB(255, 0, 255, 0)); view.picture = canvas.endRecording(); view.scheduleFrame(); diff --git a/examples/spinning_arabic.dart b/examples/spinning_arabic.dart index 843c570d8eb..e42c2f4a458 100644 --- a/examples/spinning_arabic.dart +++ b/examples/spinning_arabic.dart @@ -15,7 +15,7 @@ void beginFrame(double timeStamp) { PictureRecorder canvas = new PictureRecorder(view.width, view.height); canvas.translate(view.width / 2.0, view.height / 2.0); canvas.rotateDegrees(delta / 10); - canvas.drawRect(new Rect()..setLTRB(-100.0, -100.0, 100.0, 100.0), + canvas.drawRect(new Rect.fromLTRB(-100.0, -100.0, 100.0, 100.0), new Paint()..setARGB(255, 0, 255, 0)); double sin = math.sin(delta / 200); diff --git a/sdk/lib/framework/components2/scaffold.dart b/sdk/lib/framework/components2/scaffold.dart index 8746c632845..114ea79e958 100644 --- a/sdk/lib/framework/components2/scaffold.dart +++ b/sdk/lib/framework/components2/scaffold.dart @@ -96,20 +96,20 @@ class RenderScaffold extends RenderDecoratedBox { double bodyHeight = size.height; double bodyPosition = 0.0; if (toolbar != null) { - toolbar.layout(new BoxConstraints.tight(width: size.width, height: kToolbarHeight)); + toolbar.layout(new BoxConstraints.tight(new sky.Size(size.width, kToolbarHeight))); assert(toolbar.parentData is BoxParentData); toolbar.parentData.position = new sky.Point(0.0, 0.0); bodyPosition = kToolbarHeight; bodyHeight -= kToolbarHeight; } if (statusbar != null) { - statusbar.layout(new BoxConstraints.tight(width: size.width, height: kStatusbarHeight)); + statusbar.layout(new BoxConstraints.tight(new sky.Size(size.width, kStatusbarHeight))); assert(statusbar.parentData is BoxParentData); statusbar.parentData.position = new sky.Point(0.0, size.height - kStatusbarHeight); bodyHeight -= kStatusbarHeight; } if (body != null) { - body.layout(new BoxConstraints.tight(width: size.width, height: bodyHeight)); + body.layout(new BoxConstraints.tight(new sky.Size(size.width, bodyHeight))); assert(body.parentData is BoxParentData); body.parentData.position = new sky.Point(0.0, bodyPosition); } diff --git a/sdk/lib/framework/layout2.dart b/sdk/lib/framework/layout2.dart index 37eaff8ee78..ee76408115d 100644 --- a/sdk/lib/framework/layout2.dart +++ b/sdk/lib/framework/layout2.dart @@ -416,11 +416,11 @@ class BoxConstraints { this.minHeight: 0.0, this.maxHeight: double.INFINITY}); - const BoxConstraints.tight({ double width: 0.0, double height: 0.0 }) - : minWidth = width, - maxWidth = width, - minHeight = height, - maxHeight = height; + BoxConstraints.tight(sky.Size size) + : minWidth = size.width, + maxWidth = size.width, + minHeight = size.height, + maxHeight = size.height; BoxConstraints deflate(EdgeDims edges) { assert(edges != null); @@ -510,6 +510,47 @@ abstract class RenderBox extends RenderNode { sky.Size size = new sky.Size(0.0, 0.0); } +abstract class RenderProxyBox extends RenderBox with RenderNodeWithChildMixin { + RenderProxyBox(RenderBox child) { + this.child = child; + } + + BoxDimensions getIntrinsicDimensions(BoxConstraints constraints) { + return child.getIntrinsicDimensions(constraints); + } + + void performLayout() { + child.layout(constraints, parentUsesSize: true); + size = child.size; + } + + void hitTestChildren(HitTestResult result, { sky.Point position }) { + child.hitTest(result, position: position); + } + + void paint(RenderNodeDisplayList canvas) { + child.paint(canvas); + } +} + +class RenderSizedBox extends RenderProxyBox { + final sky.Size desiredSize; + + RenderSizedBox(RenderBox child, [this.desiredSize = const sky.Size.infinite()]) + : super(child); + + BoxDimensions getIntrinsicDimensions(BoxConstraints constraints) { + return new BoxDimensions.withConstraints(constraints, + width: desiredSize.width, + height: desiredSize.height); + } + + void performLayout() { + size = constraints.constrain(desiredSize); + child.layout(new BoxConstraints.tight(size)); + } +} + class RenderPadding extends RenderBox with RenderNodeWithChildMixin { RenderPadding(EdgeDims padding, RenderBox child) { @@ -591,6 +632,10 @@ class RenderDecoratedBox extends RenderBox { markNeedsPaint(); } + void performLayout() { + size = constraints.constrain(new sky.Size.infinite()); + } + void paint(RenderNodeDisplayList canvas) { assert(size.width != null); assert(size.height != null); @@ -603,7 +648,6 @@ class RenderDecoratedBox extends RenderBox { canvas.drawRect(new sky.Rect.fromLTRB(0.0, 0.0, size.width, size.height), paint); } } - } class RenderDecoratedCircle extends RenderDecoratedBox with RenderNodeWithChildMixin { @@ -674,7 +718,7 @@ class RenderView extends RenderNode with RenderNodeWithChildMixin { } void performLayout() { if (child != null) { - child.layout(new BoxConstraints.tight(width: width, height: height)); + child.layout(new BoxConstraints.tight(_size)); assert(child.size.width == width); assert(child.size.height == height); } diff --git a/tests/raw/render_box.dart b/tests/raw/render_box.dart index bb15c805162..82a79783c9e 100644 --- a/tests/raw/render_box.dart +++ b/tests/raw/render_box.dart @@ -7,27 +7,12 @@ import '../resources/unit.dart'; import 'dart:sky' as sky; import 'package:sky/framework/layout2.dart'; -class RenderSizedBox extends RenderBox { - final sky.Size desiredSize; - - RenderSizedBox({ this.desiredSize }); - - BoxDimensions getIntrinsicDimensions(BoxConstraints constraints) { - return new BoxDimensions.withConstraints(constraints, - width: desiredSize.width, - height: desiredSize.height); - } - - void performLayout() { - size = constraints.constrain(desiredSize); - } -} - void main() { initUnit(); test("should size to render view", () { - RenderSizedBox root = new RenderSizedBox(desiredSize: new sky.Size.infinite()); + RenderSizedBox root = new RenderSizedBox( + new RenderDecoratedBox(new BoxDecoration(backgroundColor: 0xFF00FF00))); RenderView renderView = new RenderView(child: root); renderView.layout(new ViewConstraints(width: sky.view.width, height: sky.view.height)); expect(root.size.width, equals(sky.view.width));