Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6488bca

Browse files
committed
Merge pull request #95 from collinjackson/baseline
Fix #57 Flex needs to understand baselines
2 parents 5bbf40d + d7d625b commit 6488bca

File tree

4 files changed

+80
-8
lines changed

4 files changed

+80
-8
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2015 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:sky';
6+
7+
import 'package:sky/painting/text_style.dart';
8+
import 'package:sky/rendering/box.dart';
9+
import 'package:sky/rendering/flex.dart';
10+
import 'package:sky/rendering/object.dart';
11+
import 'package:sky/rendering/paragraph.dart';
12+
import 'package:sky/rendering/sky_binding.dart';
13+
14+
import 'solid_color_box.dart';
15+
16+
void main() {
17+
var table = new RenderFlex(direction: FlexDirection.vertical);
18+
19+
for(FlexAlignItems alignItems in FlexAlignItems.values) {
20+
TextStyle style = const TextStyle(color: const Color(0xFF000000));
21+
RenderParagraph paragraph = new RenderParagraph(new InlineStyle(style, [new InlineText("${alignItems}")]));
22+
table.add(new RenderPadding(child: paragraph, padding: new EdgeDims.only(top: 20.0)));
23+
var row = new RenderFlex(alignItems: alignItems);
24+
25+
style = new TextStyle(fontSize: 15.0, color: const Color(0xFF000000));
26+
row.add(new RenderDecoratedBox(
27+
decoration: new BoxDecoration(backgroundColor: const Color(0x7FFFCCCC)),
28+
child: new RenderParagraph(new InlineStyle(style, [new InlineText('foo foo foo')]))
29+
));
30+
style = new TextStyle(fontSize: 10.0, color: const Color(0xFF000000));
31+
row.add(new RenderDecoratedBox(
32+
decoration: new BoxDecoration(backgroundColor: const Color(0x7FCCFFCC)),
33+
child: new RenderParagraph(new InlineStyle(style, [new InlineText('foo foo foo')]))
34+
));
35+
var subrow = new RenderFlex(alignItems: alignItems);
36+
style = new TextStyle(fontSize: 25.0, color: const Color(0xFF000000));
37+
subrow.add(new RenderDecoratedBox(
38+
decoration: new BoxDecoration(backgroundColor: const Color(0x7FCCCCFF)),
39+
child: new RenderParagraph(new InlineStyle(style, [new InlineText('foo foo foo foo')]))
40+
));
41+
subrow.add(new RenderSolidColorBox(const Color(0x7FCCFFFF), desiredSize: new Size(30.0, 40.0)));
42+
row.add(subrow);
43+
table.add(row);
44+
row.parentData.flex = 1;
45+
}
46+
47+
RenderDecoratedBox root = new RenderDecoratedBox(
48+
decoration: new BoxDecoration(backgroundColor: const Color(0xFFFFFFFF)),
49+
child: new RenderPadding(child: table, padding: new EdgeDims.symmetric(vertical: 50.0))
50+
);
51+
52+
new SkyBinding(root: root);
53+
}

sky/sdk/example/stocks/lib/stock_row.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ class StockRow extends Component {
2626
if (stock.percentChange > 0) changeInPrice = "+" + changeInPrice;
2727

2828
List<Widget> children = [
29-
new Container(
30-
child: new StockArrow(percentChange: stock.percentChange),
31-
margin: const EdgeDims.only(right: 5.0)
32-
),
3329
new Flexible(
3430
child: new Text(stock.symbol),
3531
flex: 2
@@ -58,7 +54,13 @@ class StockRow extends Component {
5854
bottom: new BorderSide(color: Theme.of(this).dividerColor)
5955
)
6056
),
61-
child: new Flex(children)
57+
child: new Flex([
58+
new Container(
59+
child: new StockArrow(percentChange: stock.percentChange),
60+
margin: const EdgeDims.only(right: 5.0)
61+
),
62+
new Flex(children, alignItems: FlexAlignItems.baseline)
63+
])
6264
)
6365
);
6466
}

sky/sdk/lib/rendering/box.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,7 @@ abstract class RenderBoxContainerDefaultsMixin<ChildType extends RenderBox, Pare
16621662
assert(child.parentData is ParentDataType);
16631663
double candidate = child.getDistanceToActualBaseline(baseline);
16641664
if (candidate != null) {
1665-
candidate += child.parentData.position.x;
1665+
candidate += child.parentData.position.y;
16661666
if (result != null)
16671667
result = math.min(result, candidate);
16681668
else

sky/sdk/lib/rendering/flex.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ enum FlexAlignItems {
3434
end,
3535
center,
3636
stretch,
37+
baseline,
3738
}
3839

3940
typedef double _ChildSizingFunction(RenderBox child, BoxConstraints constraints);
@@ -315,6 +316,7 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
315316
// Steps 4-5. Distribute remaining space to flexible children.
316317
double spacePerFlex = totalFlex > 0 ? (freeSpace / totalFlex) : 0.0;
317318
double usedSpace = 0.0;
319+
double maxBaselineDistance = 0.0;
318320
child = firstChild;
319321
while (child != null) {
320322
int flex = _getFlex(child);
@@ -337,6 +339,12 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
337339
usedSpace += _getMainSize(child);
338340
crossSize = math.max(crossSize, _getCrossSize(child));
339341
}
342+
if (alignItems == FlexAlignItems.baseline) {
343+
// TODO(jackson): Support for non-alphabetic baselines
344+
double distance = child.getDistanceToBaseline(TextBaseline.alphabetic, onlyReal: true);
345+
if (distance != null)
346+
maxBaselineDistance = math.max(maxBaselineDistance, distance);
347+
}
340348
assert(child.parentData is FlexBoxParentData);
341349
child = child.parentData.nextSibling;
342350
}
@@ -345,7 +353,6 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
345353
double remainingSpace = math.max(0.0, freeSpace - usedSpace);
346354
double leadingSpace;
347355
double betweenSpace;
348-
child = firstChild;
349356
switch (_justifyContent) {
350357
case FlexJustifyContent.start:
351358
leadingSpace = 0.0;
@@ -380,7 +387,7 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
380387
break;
381388
}
382389

383-
// Position elements. For now, center the flex items in the cross direction
390+
// Position elements
384391
double childMainPosition = leadingSpace;
385392
child = firstChild;
386393
while (child != null) {
@@ -397,6 +404,16 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
397404
case FlexAlignItems.center:
398405
childCrossPosition = crossSize / 2.0 - _getCrossSize(child) / 2.0;
399406
break;
407+
case FlexAlignItems.baseline:
408+
childCrossPosition = 0.0;
409+
// TODO(jackson): Support for vertical baselines
410+
if (_direction == FlexDirection.horizontal) {
411+
// TODO(jackson): Support for non-alphabetic baselines
412+
double distance = child.getDistanceToBaseline(TextBaseline.alphabetic, onlyReal: true);
413+
if (distance != null)
414+
childCrossPosition = maxBaselineDistance - distance;
415+
}
416+
break;
400417
}
401418
switch (_direction) {
402419
case FlexDirection.horizontal:

0 commit comments

Comments
 (0)