Rename AutoLayoutParams to AutoLayoutRect
This object represents a rect the auto-layout system.
This commit is contained in:
parent
c7d71d8ab1
commit
4408c820e1
@ -9,12 +9,12 @@ import 'package:cassowary/cassowary.dart' as al;
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
class _MyAutoLayoutDelegate extends AutoLayoutDelegate {
|
||||
AutoLayoutParams p1 = new AutoLayoutParams();
|
||||
AutoLayoutParams p2 = new AutoLayoutParams();
|
||||
AutoLayoutParams p3 = new AutoLayoutParams();
|
||||
AutoLayoutParams p4 = new AutoLayoutParams();
|
||||
AutoLayoutRect p1 = new AutoLayoutRect();
|
||||
AutoLayoutRect p2 = new AutoLayoutRect();
|
||||
AutoLayoutRect p3 = new AutoLayoutRect();
|
||||
AutoLayoutRect p4 = new AutoLayoutRect();
|
||||
|
||||
List<al.Constraint> getConstraints(AutoLayoutParams parent) {
|
||||
List<al.Constraint> getConstraints(AutoLayoutRect parent) {
|
||||
return <al.Constraint>[
|
||||
// Sum of widths of each box must be equal to that of the container
|
||||
parent.width.equals(p1.width + p2.width + p3.width),
|
||||
@ -76,10 +76,10 @@ void main() {
|
||||
AutoLayoutParentData parentData3 = c3.parentData;
|
||||
AutoLayoutParentData parentData4 = c4.parentData;
|
||||
|
||||
parentData1.params = delegate.p1;
|
||||
parentData2.params = delegate.p2;
|
||||
parentData3.params = delegate.p3;
|
||||
parentData4.params = delegate.p4;
|
||||
parentData1.rect = delegate.p1;
|
||||
parentData2.rect = delegate.p2;
|
||||
parentData3.rect = delegate.p3;
|
||||
parentData4.rect = delegate.p4;
|
||||
|
||||
new RenderingFlutterBinding(root: root);
|
||||
}
|
||||
|
@ -8,12 +8,12 @@ import 'package:cassowary/cassowary.dart' as al;
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
class _MyAutoLayoutDelegate extends AutoLayoutDelegate {
|
||||
AutoLayoutParams p1 = new AutoLayoutParams();
|
||||
AutoLayoutParams p2 = new AutoLayoutParams();
|
||||
AutoLayoutParams p3 = new AutoLayoutParams();
|
||||
AutoLayoutParams p4 = new AutoLayoutParams();
|
||||
AutoLayoutRect p1 = new AutoLayoutRect();
|
||||
AutoLayoutRect p2 = new AutoLayoutRect();
|
||||
AutoLayoutRect p3 = new AutoLayoutRect();
|
||||
AutoLayoutRect p4 = new AutoLayoutRect();
|
||||
|
||||
List<al.Constraint> getConstraints(AutoLayoutParams parent) {
|
||||
List<al.Constraint> getConstraints(AutoLayoutRect parent) {
|
||||
return <al.Constraint>[
|
||||
// Sum of widths of each box must be equal to that of the container
|
||||
parent.width.equals(p1.width + p2.width + p3.width),
|
||||
@ -58,25 +58,25 @@ class _ColoredBoxesState extends State<ColoredBoxes> {
|
||||
delegate: delegate,
|
||||
children: <Widget>[
|
||||
new AutoLayoutChild(
|
||||
params: delegate.p1,
|
||||
rect: delegate.p1,
|
||||
child: new DecoratedBox(
|
||||
decoration: new BoxDecoration(backgroundColor: const Color(0xFFFF0000))
|
||||
)
|
||||
),
|
||||
new AutoLayoutChild(
|
||||
params: delegate.p2,
|
||||
rect: delegate.p2,
|
||||
child: new DecoratedBox(
|
||||
decoration: new BoxDecoration(backgroundColor: const Color(0xFF00FF00))
|
||||
)
|
||||
),
|
||||
new AutoLayoutChild(
|
||||
params: delegate.p3,
|
||||
rect: delegate.p3,
|
||||
child: new DecoratedBox(
|
||||
decoration: new BoxDecoration(backgroundColor: const Color(0xFF0000FF))
|
||||
)
|
||||
),
|
||||
new AutoLayoutChild(
|
||||
params: delegate.p4,
|
||||
rect: delegate.p4,
|
||||
child: new DecoratedBox(
|
||||
decoration: new BoxDecoration(backgroundColor: const Color(0xFFFFFFFF))
|
||||
)
|
||||
|
@ -243,7 +243,7 @@ void main() {
|
||||
expect(c1.expression.constant, -20.0);
|
||||
expect(c1.relation, Relation.greaterThanOrEqualTo);
|
||||
|
||||
var c2 = (right - left == cm(30.0)) as Constraint;
|
||||
var c2 = (right - left).equals(cm(30.0));
|
||||
expect(c2 is Constraint, true);
|
||||
expect(c2.expression.constant, -30.0);
|
||||
expect(c2.relation, Relation.equalTo);
|
||||
@ -438,7 +438,7 @@ void main() {
|
||||
|
||||
Solver s = new Solver();
|
||||
|
||||
expect(s.addConstraint((right + left == mid * cm(2.0)) as Constraint),
|
||||
expect(s.addConstraint((right + left).equals(mid * cm(2.0))),
|
||||
Result.success);
|
||||
expect(s.addConstraint(right - left >= cm(100.0)), Result.success);
|
||||
expect(s.addConstraint(left >= cm(0.0)), Result.success);
|
||||
@ -460,7 +460,7 @@ void main() {
|
||||
var c = (left >= cm(0.0));
|
||||
|
||||
expect(s.addConstraints([
|
||||
(left + right == cm(2.0) * mid) as Constraint,
|
||||
(left + right).equals(cm(2.0) * mid),
|
||||
(right - left >= cm(100.0)),
|
||||
c
|
||||
]), Result.success);
|
||||
@ -476,7 +476,7 @@ void main() {
|
||||
|
||||
Solver s = new Solver();
|
||||
|
||||
expect(s.addConstraint((right + left == mid * cm(2.0)) as Constraint),
|
||||
expect(s.addConstraint((right + left).equals(mid * cm(2.0))),
|
||||
Result.success);
|
||||
expect(s.addConstraint(right - left >= cm(100.0)), Result.success);
|
||||
expect(s.addConstraint(left >= cm(0.0)), Result.success);
|
||||
@ -496,7 +496,7 @@ void main() {
|
||||
var right = new Param(100.0);
|
||||
var c1 = right >= left;
|
||||
var c2 = right <= left;
|
||||
var c3 = (right == left) as Constraint;
|
||||
var c3 = right.equals(left);
|
||||
|
||||
Solver s = new Solver();
|
||||
expect(s.addConstraint(c1), Result.success);
|
||||
@ -519,9 +519,9 @@ void main() {
|
||||
solver.suggestValueForVariable(container.variable, 100.0);
|
||||
|
||||
solver.addConstraint((p1 >= cm(30.0)) | Priority.strong);
|
||||
solver.addConstraint(((p1 == p3) as Constraint) | Priority.medium);
|
||||
solver.addConstraint((p2 == cm(2.0) * p1) as Constraint);
|
||||
solver.addConstraint((container == (p1 + p2 + p3)) as Constraint);
|
||||
solver.addConstraint(p1.equals(p3) | Priority.medium);
|
||||
solver.addConstraint(p2.equals(cm(2.0) * p1));
|
||||
solver.addConstraint(container.equals(p1 + p2 + p3));
|
||||
|
||||
solver.flushUpdates();
|
||||
|
||||
@ -541,7 +541,7 @@ void main() {
|
||||
|
||||
expect(s.addEditVariable(mid.variable, Priority.strong), Result.success);
|
||||
|
||||
expect(s.addConstraint((mid * cm(2.0) == left + right) as Constraint),
|
||||
expect(s.addConstraint((mid * cm(2.0)).equals(left + right)),
|
||||
Result.success);
|
||||
expect(s.addConstraint(left >= cm(0.0)), Result.success);
|
||||
|
||||
@ -565,7 +565,7 @@ void main() {
|
||||
|
||||
expect(s.addEditVariable(mid.variable, Priority.strong), Result.success);
|
||||
|
||||
expect(s.addConstraint((mid * cm(2.0) == left + right) as Constraint),
|
||||
expect(s.addConstraint((mid * cm(2.0)).equals(left + right)),
|
||||
Result.success);
|
||||
expect(s.addConstraint(left >= cm(10.0)), Result.success);
|
||||
|
||||
@ -590,7 +590,7 @@ void main() {
|
||||
Param left = new Param();
|
||||
Param right = new Param();
|
||||
|
||||
expect((left == right).runtimeType, Constraint);
|
||||
expect(left.equals(right).runtimeType, Constraint);
|
||||
});
|
||||
|
||||
test('bulk_add_edit_variables', () {
|
||||
|
@ -10,8 +10,8 @@ import 'object.dart';
|
||||
/// Hosts the edge parameters and vends useful methods to construct expressions
|
||||
/// for constraints. Also sets up and manages implicit constraints and edit
|
||||
/// variables.
|
||||
class AutoLayoutParams {
|
||||
AutoLayoutParams() {
|
||||
class AutoLayoutRect {
|
||||
AutoLayoutRect() {
|
||||
_left = new al.Param();
|
||||
_right = new al.Param();
|
||||
_top = new al.Param();
|
||||
@ -34,7 +34,7 @@ class AutoLayoutParams {
|
||||
al.Expression get horizontalCenter => (_left + _right) / al.cm(2.0);
|
||||
al.Expression get verticalCenter => (_top + _bottom) / al.cm(2.0);
|
||||
|
||||
List<al.Constraint> contains(AutoLayoutParams other) {
|
||||
List<al.Constraint> contains(AutoLayoutRect other) {
|
||||
return <al.Constraint>[
|
||||
other.left >= left,
|
||||
other.right <= right,
|
||||
@ -49,34 +49,34 @@ class AutoLayoutParentData extends ContainerBoxParentDataMixin<RenderBox> {
|
||||
|
||||
final RenderBox _renderBox;
|
||||
|
||||
AutoLayoutParams get params => _params;
|
||||
AutoLayoutParams _params;
|
||||
void set params(AutoLayoutParams value) {
|
||||
if (_params == value)
|
||||
AutoLayoutRect get rect => _rect;
|
||||
AutoLayoutRect _rect;
|
||||
void set rect(AutoLayoutRect value) {
|
||||
if (_rect == value)
|
||||
return;
|
||||
if (_params != null)
|
||||
if (_rect != null)
|
||||
_removeImplicitConstraints();
|
||||
_params = value;
|
||||
if (_params != null)
|
||||
_rect = value;
|
||||
if (_rect != null)
|
||||
_addImplicitConstraints();
|
||||
}
|
||||
|
||||
BoxConstraints get _constraintsFromSolver {
|
||||
return new BoxConstraints.tightFor(
|
||||
width: _params._right.value - _params._left.value,
|
||||
height: _params._bottom.value - _params._top.value
|
||||
width: _rect._right.value - _rect._left.value,
|
||||
height: _rect._bottom.value - _rect._top.value
|
||||
);
|
||||
}
|
||||
|
||||
Offset get _offsetFromSolver {
|
||||
return new Offset(_params._left.value, _params._top.value);
|
||||
return new Offset(_rect._left.value, _rect._top.value);
|
||||
}
|
||||
|
||||
List<al.Constraint> _implicitConstraints;
|
||||
|
||||
void _addImplicitConstraints() {
|
||||
assert(_renderBox != null);
|
||||
if (_renderBox.parent == null || _params == null)
|
||||
if (_renderBox.parent == null || _rect == null)
|
||||
return;
|
||||
final List<al.Constraint> implicit = _constructImplicitConstraints();
|
||||
assert(implicit != null && implicit.isNotEmpty);
|
||||
@ -106,8 +106,9 @@ class AutoLayoutParentData extends ContainerBoxParentDataMixin<RenderBox> {
|
||||
/// may return null.
|
||||
List<al.Constraint> _constructImplicitConstraints() {
|
||||
return <al.Constraint>[
|
||||
_params._left >= al.cm(0.0), // The left edge must be positive.
|
||||
_params._right >= _params._left, // Width must be positive.
|
||||
_rect._left >= al.cm(0.0), // The left edge must be positive.
|
||||
_rect._right >= _rect._left, // Width must be positive.
|
||||
// Why don't we need something similar for the top and the bottom?
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -115,7 +116,7 @@ class AutoLayoutParentData extends ContainerBoxParentDataMixin<RenderBox> {
|
||||
abstract class AutoLayoutDelegate {
|
||||
const AutoLayoutDelegate();
|
||||
|
||||
List<al.Constraint> getConstraints(AutoLayoutParams parent);
|
||||
List<al.Constraint> getConstraints(AutoLayoutRect parent);
|
||||
bool shouldUpdateConstraints(AutoLayoutDelegate oldDelegate);
|
||||
}
|
||||
|
||||
@ -128,10 +129,10 @@ class RenderAutoLayout extends RenderBox
|
||||
List<RenderBox> children
|
||||
}) : _delegate = delegate, _needToUpdateConstraints = (delegate != null) {
|
||||
_solver.addEditVariables(<al.Variable>[
|
||||
_params._left.variable,
|
||||
_params._right.variable,
|
||||
_params._top.variable,
|
||||
_params._bottom.variable
|
||||
_rect._left.variable,
|
||||
_rect._right.variable,
|
||||
_rect._top.variable,
|
||||
_rect._bottom.variable
|
||||
], al.Priority.required - 1);
|
||||
|
||||
addAll(children);
|
||||
@ -158,7 +159,7 @@ class RenderAutoLayout extends RenderBox
|
||||
|
||||
bool _needToUpdateConstraints;
|
||||
|
||||
final AutoLayoutParams _params = new AutoLayoutParams();
|
||||
final AutoLayoutRect _rect = new AutoLayoutRect();
|
||||
|
||||
final al.Solver _solver = new al.Solver();
|
||||
final List<al.Constraint> _explicitConstraints = new List<al.Constraint>();
|
||||
@ -212,17 +213,17 @@ class RenderAutoLayout extends RenderBox
|
||||
if (_needToUpdateConstraints) {
|
||||
_clearExplicitConstraints();
|
||||
if (_delegate != null)
|
||||
_setExplicitConstraints(_delegate.getConstraints(_params));
|
||||
_setExplicitConstraints(_delegate.getConstraints(_rect));
|
||||
_needToUpdateConstraints = false;
|
||||
needToFlushUpdates = true;
|
||||
}
|
||||
|
||||
if (size != _previousSize) {
|
||||
_solver
|
||||
..suggestValueForVariable(_params._left.variable, 0.0)
|
||||
..suggestValueForVariable(_params._top.variable, 0.0)
|
||||
..suggestValueForVariable(_params._bottom.variable, size.height)
|
||||
..suggestValueForVariable(_params._right.variable, size.width);
|
||||
..suggestValueForVariable(_rect._left.variable, 0.0)
|
||||
..suggestValueForVariable(_rect._top.variable, 0.0)
|
||||
..suggestValueForVariable(_rect._bottom.variable, size.height)
|
||||
..suggestValueForVariable(_rect._right.variable, size.width);
|
||||
_previousSize = size;
|
||||
needToFlushUpdates = true;
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import 'package:flutter/rendering.dart';
|
||||
import 'framework.dart';
|
||||
|
||||
export 'package:flutter/rendering.dart' show
|
||||
AutoLayoutParams,
|
||||
AutoLayoutRect,
|
||||
AutoLayoutDelegate;
|
||||
|
||||
class AutoLayout extends MultiChildRenderObjectWidget {
|
||||
@ -27,16 +27,16 @@ class AutoLayout extends MultiChildRenderObjectWidget {
|
||||
}
|
||||
|
||||
class AutoLayoutChild extends ParentDataWidget<AutoLayout> {
|
||||
AutoLayoutChild({ AutoLayoutParams params, Widget child })
|
||||
: params = params, super(key: new ObjectKey(params), child: child);
|
||||
AutoLayoutChild({ AutoLayoutRect rect, Widget child })
|
||||
: rect = rect, super(key: new ObjectKey(rect), child: child);
|
||||
|
||||
final AutoLayoutParams params;
|
||||
final AutoLayoutRect rect;
|
||||
|
||||
void applyParentData(RenderObject renderObject) {
|
||||
assert(renderObject.parentData is AutoLayoutParentData);
|
||||
final AutoLayoutParentData parentData = renderObject.parentData;
|
||||
// AutoLayoutParentData filters out redundant writes and marks needs layout
|
||||
// as appropriate.
|
||||
parentData.params = params;
|
||||
parentData.rect = rect;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user