mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Introduce RenderProxyBox and RenderSizedBox
Also makes Point, Size, and Rect immutable. R=ianh@google.com Review URL: https://codereview.chromium.org/1162033002
This commit is contained in:
parent
bf94f5c027
commit
9a73fe989e
@ -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;
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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<RenderBox> {
|
||||
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<RenderBox> {
|
||||
|
||||
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<RenderBox> {
|
||||
@ -674,7 +718,7 @@ class RenderView extends RenderNode with RenderNodeWithChildMixin<RenderBox> {
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
@ -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));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user