Commit 48f600f8 authored by Adam Barth's avatar Adam Barth

Merge pull request #1071 from abarth/center_expand

Center and Align should expand by default
parents 18e512d3 5af85d90
...@@ -136,6 +136,13 @@ class RenderPadding extends RenderShiftedBox { ...@@ -136,6 +136,13 @@ class RenderPadding extends RenderShiftedBox {
String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}padding: ${padding}\n'; String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}padding: ${padding}\n';
} }
enum ShrinkWrap {
width,
height,
both,
none
}
class RenderPositionedBox extends RenderShiftedBox { class RenderPositionedBox extends RenderShiftedBox {
// This box aligns a child box within itself. It's only useful for // This box aligns a child box within itself. It's only useful for
...@@ -147,12 +154,15 @@ class RenderPositionedBox extends RenderShiftedBox { ...@@ -147,12 +154,15 @@ class RenderPositionedBox extends RenderShiftedBox {
RenderPositionedBox({ RenderPositionedBox({
RenderBox child, RenderBox child,
double horizontal: 0.5, double horizontal: 0.5,
double vertical: 0.5 double vertical: 0.5,
ShrinkWrap shrinkWrap: ShrinkWrap.none
}) : _horizontal = horizontal, }) : _horizontal = horizontal,
_vertical = vertical, _vertical = vertical,
_shrinkWrap = shrinkWrap,
super(child) { super(child) {
assert(horizontal != null); assert(horizontal != null);
assert(vertical != null); assert(vertical != null);
assert(shrinkWrap != null);
} }
double _horizontal; double _horizontal;
...@@ -175,15 +185,30 @@ class RenderPositionedBox extends RenderShiftedBox { ...@@ -175,15 +185,30 @@ class RenderPositionedBox extends RenderShiftedBox {
markNeedsLayout(); markNeedsLayout();
} }
ShrinkWrap _shrinkWrap;
ShrinkWrap get shrinkWrap => _shrinkWrap;
void set shrinkWrap (ShrinkWrap value) {
assert(value != null);
if (_shrinkWrap == value)
return;
_shrinkWrap = value;
markNeedsLayout();
}
bool get _shinkWrapWidth => _shrinkWrap == ShrinkWrap.width || _shrinkWrap == ShrinkWrap.both;
bool get _shinkWrapHeight => _shrinkWrap == ShrinkWrap.height || _shrinkWrap == ShrinkWrap.both;
void performLayout() { void performLayout() {
if (child != null) { if (child != null) {
child.layout(constraints.loosen(), parentUsesSize: true); child.layout(constraints.loosen(), parentUsesSize: true);
size = constraints.constrain(child.size); size = constraints.constrain(new Size(_shinkWrapWidth ? child.size.width : double.INFINITY,
_shinkWrapHeight ? child.size.height : double.INFINITY));
assert(child.parentData is BoxParentData); assert(child.parentData is BoxParentData);
Offset delta = size - child.size; Offset delta = size - child.size;
child.parentData.position = (delta.scale(horizontal, vertical)).toPoint(); child.parentData.position = (delta.scale(horizontal, vertical)).toPoint();
} else { } else {
performResize(); size = constraints.constrain(new Size(_shinkWrapWidth ? 0.0 : double.INFINITY,
_shinkWrapHeight ? 0.0 : double.INFINITY));
} }
} }
......
...@@ -32,6 +32,7 @@ export 'package:sky/src/rendering/box.dart' show BoxConstraints; ...@@ -32,6 +32,7 @@ export 'package:sky/src/rendering/box.dart' show BoxConstraints;
export 'package:sky/src/rendering/flex.dart' show FlexJustifyContent, FlexAlignItems, FlexDirection; export 'package:sky/src/rendering/flex.dart' show FlexJustifyContent, FlexAlignItems, FlexDirection;
export 'package:sky/src/rendering/object.dart' show Point, Offset, Size, Rect, Color, Paint, Path; export 'package:sky/src/rendering/object.dart' show Point, Offset, Size, Rect, Color, Paint, Path;
export 'package:sky/src/rendering/proxy_box.dart' show BackgroundImage, BoxDecoration, BoxDecorationPosition, BoxShadow, Border, BorderSide, EdgeDims, Shape; export 'package:sky/src/rendering/proxy_box.dart' show BackgroundImage, BoxDecoration, BoxDecorationPosition, BoxShadow, Border, BorderSide, EdgeDims, Shape;
export 'package:sky/src/rendering/shifted_box.dart' show ShrinkWrap;
export 'package:sky/src/rendering/toggleable.dart' show ValueChanged; export 'package:sky/src/rendering/toggleable.dart' show ValueChanged;
export 'package:sky/src/rendering/viewport.dart' show ScrollDirection; export 'package:sky/src/rendering/viewport.dart' show ScrollDirection;
...@@ -200,28 +201,36 @@ class Padding extends OneChildRenderObjectWrapper { ...@@ -200,28 +201,36 @@ class Padding extends OneChildRenderObjectWrapper {
} }
class Align extends OneChildRenderObjectWrapper { class Align extends OneChildRenderObjectWrapper {
Align({ Key key, this.horizontal: 0.5, this.vertical: 0.5, Widget child }) Align({
: super(key: key, child: child) { Key key,
this.horizontal: 0.5,
this.vertical: 0.5,
this.shrinkWrap: ShrinkWrap.none,
Widget child
}) : super(key: key, child: child) {
assert(horizontal != null); assert(horizontal != null);
assert(vertical != null); assert(vertical != null);
assert(shrinkWrap != null);
} }
final double horizontal; final double horizontal;
final double vertical; final double vertical;
final ShrinkWrap shrinkWrap;
RenderPositionedBox createNode() => new RenderPositionedBox(horizontal: horizontal, vertical: vertical); RenderPositionedBox createNode() => new RenderPositionedBox(horizontal: horizontal, vertical: vertical, shrinkWrap: shrinkWrap);
RenderPositionedBox get renderObject => super.renderObject; RenderPositionedBox get renderObject => super.renderObject;
void syncRenderObject(Align old) { void syncRenderObject(Align old) {
super.syncRenderObject(old); super.syncRenderObject(old);
renderObject.horizontal = horizontal; renderObject.horizontal = horizontal;
renderObject.vertical = vertical; renderObject.vertical = vertical;
renderObject.shrinkWrap = shrinkWrap;
} }
} }
class Center extends Align { class Center extends Align {
Center({ Key key, Widget child }) Center({ Key key, ShrinkWrap shrinkWrap: ShrinkWrap.none, Widget child })
: super(key: key, child: child); : super(key: key, shrinkWrap: shrinkWrap, child: child);
} }
class SizedBox extends OneChildRenderObjectWrapper { class SizedBox extends OneChildRenderObjectWrapper {
......
...@@ -98,7 +98,13 @@ class DatePicker extends StatefulComponent { ...@@ -98,7 +98,13 @@ class DatePicker extends StatefulComponent {
); );
break; break;
} }
return new BlockBody([header, new Container(height: _calendarHeight, child: picker)]); return new Column([
header,
new Container(
height: _calendarHeight,
child: picker
)
], alignItems: FlexAlignItems.stretch);
} }
} }
...@@ -143,28 +149,22 @@ class DatePickerHeader extends Component { ...@@ -143,28 +149,22 @@ class DatePickerHeader extends Component {
TextStyle yearStyle = headerTheme.headline.copyWith(color: yearColor, height: 1.0); TextStyle yearStyle = headerTheme.headline.copyWith(color: yearColor, height: 1.0);
return new Container( return new Container(
child: new BlockBody([ padding: new EdgeDims.all(10.0),
new Center( decoration: new BoxDecoration(backgroundColor: theme.primaryColor),
child: new GestureDetector( child: new Column([
child: new Text(new DateFormat("MMM").format(selectedDate).toUpperCase(), style: monthStyle), new GestureDetector(
onTap: () => _handleChangeMode(DatePickerMode.day) onTap: () => _handleChangeMode(DatePickerMode.day),
) child: new Text(new DateFormat("MMM").format(selectedDate).toUpperCase(), style: monthStyle)
), ),
new Center( new GestureDetector(
child: new GestureDetector( onTap: () => _handleChangeMode(DatePickerMode.day),
child: new Text(new DateFormat("d").format(selectedDate), style: dayStyle), child: new Text(new DateFormat("d").format(selectedDate), style: dayStyle)
onTap: () => _handleChangeMode(DatePickerMode.day)
)
), ),
new Center( new GestureDetector(
child: new GestureDetector( onTap: () => _handleChangeMode(DatePickerMode.year),
child: new Text(new DateFormat("yyyy").format(selectedDate), style: yearStyle), child: new Text(new DateFormat("yyyy").format(selectedDate), style: yearStyle)
onTap: () => _handleChangeMode(DatePickerMode.year)
)
) )
]), ])
padding: new EdgeDims.all(10.0),
decoration: new BoxDecoration(backgroundColor: theme.primaryColor)
); );
} }
} }
...@@ -267,11 +267,8 @@ class DayPicker extends Component { ...@@ -267,11 +267,8 @@ class DayPicker extends Component {
} }
for (int w = 0; w < weeksShown; w++) { for (int w = 0; w < weeksShown; w++) {
int startIndex = w * days.length; int startIndex = w * days.length;
rows.add(new Container( rows.add(new Row(
child: new Flex( labels.sublist(startIndex, startIndex + days.length)
labels.sublist(startIndex, startIndex + days.length),
justifyContent: FlexJustifyContent.spaceAround
)
)); ));
} }
......
...@@ -95,21 +95,22 @@ class Dialog extends Component { ...@@ -95,21 +95,22 @@ class Dialog extends Component {
)); ));
} }
if (actions != null) if (actions != null) {
dialogBody.add(new Container( dialogBody.add(new Container(
child: new Row(actions, child: new Row(actions,
justifyContent: FlexJustifyContent.end justifyContent: FlexJustifyContent.end
) )
)); ));
}
return new Stack([ return new Stack([
new GestureDetector( new GestureDetector(
onTap: onDismiss,
child: new Container( child: new Container(
decoration: const BoxDecoration( decoration: const BoxDecoration(
backgroundColor: const Color(0x7F000000) backgroundColor: const Color(0x7F000000)
) )
), )
onTap: onDismiss
), ),
new Center( new Center(
child: new Container( child: new Container(
......
...@@ -36,7 +36,10 @@ abstract class MaterialButton extends ButtonBase { ...@@ -36,7 +36,10 @@ abstract class MaterialButton extends ButtonBase {
Widget buildContent() { Widget buildContent() {
Widget contents = new Container( Widget contents = new Container(
padding: new EdgeDims.symmetric(horizontal: 8.0), padding: new EdgeDims.symmetric(horizontal: 8.0),
child: new Center(child: child) // TODO(ianh): figure out a way to compell the child to have gray text when disabled... child: new Center(
shrinkWrap: ShrinkWrap.width,
child: child // TODO(ianh): figure out a way to compell the child to have gray text when disabled...
)
); );
return new GestureDetector( return new GestureDetector(
onTap: enabled ? onPressed : null, onTap: enabled ? onPressed : null,
......
import 'package:sky/rendering.dart';
import 'package:test/test.dart';
import 'rendering_tester.dart';
void main() {
test('RenderPositionedBox expands', () {
RenderConstrainedBox sizer = new RenderConstrainedBox(
additionalConstraints: new BoxConstraints.tight(new Size(100.0, 100.0)),
child: new RenderDecoratedBox(decoration: new BoxDecoration())
);
RenderPositionedBox positioner = new RenderPositionedBox(child: sizer);
layout(positioner, constraints: new BoxConstraints.loose(new Size(200.0, 200.0)));
expect(positioner.size.width, equals(200.0), reason: "positioner width");
expect(positioner.size.height, equals(200.0), reason: "positioner height");
});
test('RenderPositionedBox shrink wraps', () {
RenderConstrainedBox sizer = new RenderConstrainedBox(
additionalConstraints: new BoxConstraints.tight(new Size(100.0, 100.0)),
child: new RenderDecoratedBox(decoration: new BoxDecoration())
);
RenderPositionedBox positioner = new RenderPositionedBox(child: sizer, shrinkWrap: ShrinkWrap.width);
RenderingTester tester = layout(positioner, constraints: new BoxConstraints.loose(new Size(200.0, 200.0)));
expect(positioner.size.width, equals(100.0), reason: "positioner width");
expect(positioner.size.height, equals(200.0), reason: "positioner height");
positioner.shrinkWrap = ShrinkWrap.height;
tester.pumpFrame(phase: EnginePhase.layout);
expect(positioner.size.width, equals(200.0), reason: "positioner width");
expect(positioner.size.height, equals(100.0), reason: "positioner height");
positioner.shrinkWrap = ShrinkWrap.both;
tester.pumpFrame(phase: EnginePhase.layout);
expect(positioner.size.width, equals(100.0), reason: "positioner width");
expect(positioner.size.height, equals(100.0), reason: "positioner height");
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment