diff --git a/examples/stocks/lib/stock_row.dart b/examples/stocks/lib/stock_row.dart index 28e54980d67..4b8f47a65d8 100644 --- a/examples/stocks/lib/stock_row.dart +++ b/examples/stocks/lib/stock_row.dart @@ -47,7 +47,7 @@ class StockRow extends StatelessComponent { onLongPress: onLongPressed, child: new InkWell( child: new Container( - padding: const EdgeDims(16.0, 16.0, 20.0, 16.0), + padding: const EdgeDims.TRBL(16.0, 16.0, 20.0, 16.0), decoration: new BoxDecoration( border: new Border( bottom: new BorderSide(color: Theme.of(context).dividerColor) diff --git a/packages/flutter/lib/src/painting/box_painter.dart b/packages/flutter/lib/src/painting/box_painter.dart index 6126fcbb1e7..4fad4379027 100644 --- a/packages/flutter/lib/src/painting/box_painter.dart +++ b/packages/flutter/lib/src/painting/box_painter.dart @@ -14,9 +14,8 @@ import 'package:sky/src/painting/shadows.dart'; /// Typically used for an offset from each of the four sides of a box. For /// example, the padding inside a box can be represented using this class. class EdgeDims { - // TODO(abarth): Remove this constructor or rename it to EdgeDims.fromTRBL. /// Constructs an EdgeDims from offsets from the top, right, bottom and left - const EdgeDims(this.top, this.right, this.bottom, this.left); + const EdgeDims.TRBL(this.top, this.right, this.bottom, this.left); /// Constructs an EdgeDims where all the offsets are value const EdgeDims.all(double value) @@ -47,32 +46,89 @@ class EdgeDims { bool get isNonNegative => top >= 0.0 && right >= 0.0 && bottom >= 0.0 && left >= 0.0; - bool operator ==(other) { - if (identical(this, other)) - return true; - return other is EdgeDims - && top == other.top - && right == other.right - && bottom == other.bottom - && left == other.left; + EdgeDims operator-(EdgeDims other) { + return new EdgeDims.TRBL( + top - other.top, + right - other.right, + bottom - other.bottom, + left - other.left + ); } EdgeDims operator+(EdgeDims other) { - return new EdgeDims(top + other.top, - right + other.right, - bottom + other.bottom, - left + other.left); + return new EdgeDims.TRBL( + top + other.top, + right + other.right, + bottom + other.bottom, + left + other.left + ); } - EdgeDims operator-(EdgeDims other) { - return new EdgeDims(top - other.top, - right - other.right, - bottom - other.bottom, - left - other.left); + EdgeDims operator*(double other) { + return new EdgeDims.TRBL( + top * other, + right * other, + bottom * other, + left * other + ); + } + + EdgeDims operator/(double other) { + return new EdgeDims.TRBL( + top / other, + right / other, + bottom / other, + left / other + ); + } + + EdgeDims operator~/(double other) { + return new EdgeDims.TRBL( + (top ~/ other).toDouble(), + (right ~/ other).toDouble(), + (bottom ~/ other).toDouble(), + (left ~/ other).toDouble() + ); + } + + EdgeDims operator%(double other) { + return new EdgeDims.TRBL( + top % other, + right % other, + bottom % other, + left % other + ); + } + + bool operator ==(other) { + return identical(this, other) || + (other is EdgeDims && + top == other.top && + right == other.right && + bottom == other.bottom && + left == other.left); + } + + /// Linearly interpolate between two EdgeDims + /// + /// If either is null, this function interpolates from [EdgeDims.zero]. + static EdgeDims lerp(EdgeDims a, EdgeDims b, double t) { + if (a == null && b == null) + return null; + if (a == null) + return b * t; + if (b == null) + return a * (1.0 - t); + return new EdgeDims.TRBL( + sky.lerpDouble(a.top, b.top, t), + sky.lerpDouble(a.right, b.right, t), + sky.lerpDouble(a.bottom, b.bottom, t), + sky.lerpDouble(a.left, b.left, t) + ); } /// An EdgeDims with zero offsets in each direction - static const EdgeDims zero = const EdgeDims(0.0, 0.0, 0.0, 0.0); + static const EdgeDims zero = const EdgeDims.TRBL(0.0, 0.0, 0.0, 0.0); int get hashCode { int value = 373; @@ -142,7 +198,7 @@ class Border { /// The widths of the sides of this border represented as an EdgeDims EdgeDims get dimensions { - return new EdgeDims(top.width, right.width, bottom.width, left.width); + return new EdgeDims.TRBL(top.width, right.width, bottom.width, left.width); } int get hashCode { diff --git a/packages/flutter/lib/src/widgets/binding.dart b/packages/flutter/lib/src/widgets/binding.dart index fd005ad0fb9..792c6a161a0 100644 --- a/packages/flutter/lib/src/widgets/binding.dart +++ b/packages/flutter/lib/src/widgets/binding.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:async'; - import 'package:sky/animation.dart'; import 'package:sky/rendering.dart'; import 'package:sky/src/widgets/framework.dart'; diff --git a/packages/flutter/lib/src/widgets/dialog.dart b/packages/flutter/lib/src/widgets/dialog.dart index b9b24ad8903..9098bebc1d4 100644 --- a/packages/flutter/lib/src/widgets/dialog.dart +++ b/packages/flutter/lib/src/widgets/dialog.dart @@ -70,7 +70,7 @@ class Dialog extends StatelessComponent { if (title != null) { EdgeDims padding = titlePadding; if (padding == null) - padding = new EdgeDims(24.0, 24.0, content == null ? 20.0 : 0.0, 24.0); + padding = new EdgeDims.TRBL(24.0, 24.0, content == null ? 20.0 : 0.0, 24.0); dialogBody.add(new Padding( padding: padding, child: new DefaultTextStyle( @@ -83,7 +83,7 @@ class Dialog extends StatelessComponent { if (content != null) { EdgeDims padding = contentPadding; if (padding == null) - padding = const EdgeDims(20.0, 24.0, 24.0, 24.0); + padding = const EdgeDims.TRBL(20.0, 24.0, 24.0, 24.0); dialogBody.add(new Padding( padding: padding, child: new DefaultTextStyle( diff --git a/packages/unit/test/painting/edge_dims_test.dart b/packages/unit/test/painting/edge_dims_test.dart new file mode 100644 index 00000000000..bfcb4583f38 --- /dev/null +++ b/packages/unit/test/painting/edge_dims_test.dart @@ -0,0 +1,14 @@ +import 'package:sky/painting.dart'; + +import 'package:test/test.dart'; + +void main() { + test("EdgeDims.lerp()", () { + EdgeDims a = new EdgeDims.all(10.0); + EdgeDims b = new EdgeDims.all(20.0); + expect(EdgeDims.lerp(a, b, 0.25), equals(a * 1.25)); + expect(EdgeDims.lerp(a, b, 0.25), equals(b * 0.625)); + expect(EdgeDims.lerp(a, b, 0.25), equals(a + const EdgeDims.all(2.5))); + expect(EdgeDims.lerp(a, b, 0.25), equals(b - const EdgeDims.all(7.5))); + }); +} diff --git a/packages/unit/test/rendering/size_test.dart b/packages/unit/test/rendering/size_test.dart new file mode 100644 index 00000000000..76e84bcda2c --- /dev/null +++ b/packages/unit/test/rendering/size_test.dart @@ -0,0 +1,18 @@ +import 'package:sky/rendering.dart'; +import 'package:test/test.dart'; + +import 'rendering_tester.dart'; + +void main() { + test('Stack can layout with top, right, bottom, left 0.0', () { + RenderBox box = new RenderConstrainedBox( + additionalConstraints: new BoxConstraints.tight(const Size(100.0, 100.0))); + + layout(box, constraints: const BoxConstraints()); + + expect(box.size.width, equals(100.0)); + expect(box.size.height, equals(100.0)); + expect(box.size, equals(const Size(100.0, 100.0))); + expect(box.size.runtimeType.toString(), equals('_DebugSize')); + }); +}