| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:math' as math; | 5 import 'dart:math' as math; |
| 6 | 6 |
| 7 import 'box.dart'; | 7 import 'box.dart'; |
| 8 import 'object.dart'; | 8 import 'object.dart'; |
| 9 | 9 |
| 10 class FlexBoxParentData extends BoxParentData with ContainerParentDataMixin<Rend
erBox> { | 10 class FlexBoxParentData extends BoxParentData with ContainerParentDataMixin<Rend
erBox> { |
| 11 int flex; | 11 int flex; |
| 12 | 12 |
| 13 void merge(FlexBoxParentData other) { | 13 void merge(FlexBoxParentData other) { |
| 14 if (other.flex != null) | 14 if (other.flex != null) |
| 15 flex = other.flex; | 15 flex = other.flex; |
| 16 super.merge(other); | 16 super.merge(other); |
| 17 } | 17 } |
| 18 | 18 |
| 19 String toString() => '${super.toString()}; flex=$flex'; | 19 String toString() => '${super.toString()}; flex=$flex'; |
| 20 } | 20 } |
| 21 | 21 |
| 22 enum FlexDirection { horizontal, vertical } | 22 enum FlexDirection { horizontal, vertical } |
| 23 | 23 |
| 24 enum FlexJustifyContent { | 24 enum FlexJustifyContent { |
| 25 flexStart, | 25 start, |
| 26 flexEnd, | 26 end, |
| 27 center, | 27 center, |
| 28 spaceBetween, | 28 spaceBetween, |
| 29 spaceAround, | 29 spaceAround, |
| 30 } | 30 } |
| 31 | 31 |
| 32 enum FlexAlignItems { | 32 enum FlexAlignItems { |
| 33 flexStart, | 33 start, |
| 34 flexEnd, | 34 end, |
| 35 center, | 35 center, |
| 36 } | 36 } |
| 37 | 37 |
| 38 typedef double _ChildSizingFunction(RenderBox child, BoxConstraints constraints)
; | 38 typedef double _ChildSizingFunction(RenderBox child, BoxConstraints constraints)
; |
| 39 | 39 |
| 40 class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
exBoxParentData>, | 40 class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
exBoxParentData>, |
| 41 RenderBoxContainerDefaultsMixin<RenderBo
x, FlexBoxParentData> { | 41 RenderBoxContainerDefaultsMixin<RenderBo
x, FlexBoxParentData> { |
| 42 // lays out RenderBox children using flexible layout | 42 // lays out RenderBox children using flexible layout |
| 43 | 43 |
| 44 RenderFlex({ | 44 RenderFlex({ |
| 45 FlexDirection direction: FlexDirection.horizontal, | 45 FlexDirection direction: FlexDirection.horizontal, |
| 46 FlexJustifyContent justifyContent: FlexJustifyContent.flexStart, | 46 FlexJustifyContent justifyContent: FlexJustifyContent.start, |
| 47 FlexAlignItems alignItems: FlexAlignItems.center | 47 FlexAlignItems alignItems: FlexAlignItems.center |
| 48 }) : _direction = direction, _justifyContent = justifyContent, _alignItems = a
lignItems; | 48 }) : _direction = direction, _justifyContent = justifyContent, _alignItems = a
lignItems; |
| 49 | 49 |
| 50 FlexDirection _direction; | 50 FlexDirection _direction; |
| 51 FlexDirection get direction => _direction; | 51 FlexDirection get direction => _direction; |
| 52 void set direction (FlexDirection value) { | 52 void set direction (FlexDirection value) { |
| 53 if (_direction != value) { | 53 if (_direction != value) { |
| 54 _direction = value; | 54 _direction = value; |
| 55 markNeedsLayout(); | 55 markNeedsLayout(); |
| 56 } | 56 } |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 assert(child.parentData is FlexBoxParentData); | 319 assert(child.parentData is FlexBoxParentData); |
| 320 child = child.parentData.nextSibling; | 320 child = child.parentData.nextSibling; |
| 321 } | 321 } |
| 322 | 322 |
| 323 // Section 8.2: Axis Alignment using the justify-content property | 323 // Section 8.2: Axis Alignment using the justify-content property |
| 324 double remainingSpace = math.max(0.0, freeSpace - usedSpace); | 324 double remainingSpace = math.max(0.0, freeSpace - usedSpace); |
| 325 double leadingSpace; | 325 double leadingSpace; |
| 326 double betweenSpace; | 326 double betweenSpace; |
| 327 child = firstChild; | 327 child = firstChild; |
| 328 switch (_justifyContent) { | 328 switch (_justifyContent) { |
| 329 case FlexJustifyContent.flexStart: | 329 case FlexJustifyContent.start: |
| 330 leadingSpace = 0.0; | 330 leadingSpace = 0.0; |
| 331 betweenSpace = 0.0; | 331 betweenSpace = 0.0; |
| 332 break; | 332 break; |
| 333 case FlexJustifyContent.flexEnd: | 333 case FlexJustifyContent.end: |
| 334 leadingSpace = remainingSpace; | 334 leadingSpace = remainingSpace; |
| 335 betweenSpace = 0.0; | 335 betweenSpace = 0.0; |
| 336 break; | 336 break; |
| 337 case FlexJustifyContent.center: | 337 case FlexJustifyContent.center: |
| 338 leadingSpace = remainingSpace / 2.0; | 338 leadingSpace = remainingSpace / 2.0; |
| 339 betweenSpace = 0.0; | 339 betweenSpace = 0.0; |
| 340 break; | 340 break; |
| 341 case FlexJustifyContent.spaceBetween: | 341 case FlexJustifyContent.spaceBetween: |
| 342 leadingSpace = 0.0; | 342 leadingSpace = 0.0; |
| 343 betweenSpace = totalChildren > 1 ? remainingSpace / (totalChildren - 1)
: 0.0; | 343 betweenSpace = totalChildren > 1 ? remainingSpace / (totalChildren - 1)
: 0.0; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 359 break; | 359 break; |
| 360 } | 360 } |
| 361 | 361 |
| 362 // Position elements. For now, center the flex items in the cross direction | 362 // Position elements. For now, center the flex items in the cross direction |
| 363 double childMainPosition = leadingSpace; | 363 double childMainPosition = leadingSpace; |
| 364 child = firstChild; | 364 child = firstChild; |
| 365 while (child != null) { | 365 while (child != null) { |
| 366 assert(child.parentData is FlexBoxParentData); | 366 assert(child.parentData is FlexBoxParentData); |
| 367 double childCrossPosition; | 367 double childCrossPosition; |
| 368 switch (_alignItems) { | 368 switch (_alignItems) { |
| 369 case FlexAlignItems.flexStart: | 369 case FlexAlignItems.start: |
| 370 childCrossPosition = 0.0; | 370 childCrossPosition = 0.0; |
| 371 break; | 371 break; |
| 372 case FlexAlignItems.flexEnd: | 372 case FlexAlignItems.end: |
| 373 childCrossPosition = crossSize - _getCrossSize(child); | 373 childCrossPosition = crossSize - _getCrossSize(child); |
| 374 break; | 374 break; |
| 375 case FlexAlignItems.center: | 375 case FlexAlignItems.center: |
| 376 childCrossPosition = crossSize / 2.0 - _getCrossSize(child) / 2.0; | 376 childCrossPosition = crossSize / 2.0 - _getCrossSize(child) / 2.0; |
| 377 break; | 377 break; |
| 378 } | 378 } |
| 379 switch (_direction) { | 379 switch (_direction) { |
| 380 case FlexDirection.horizontal: | 380 case FlexDirection.horizontal: |
| 381 child.parentData.position = new Point(childMainPosition, childCrossPos
ition); | 381 child.parentData.position = new Point(childMainPosition, childCrossPos
ition); |
| 382 break; | 382 break; |
| 383 case FlexDirection.vertical: | 383 case FlexDirection.vertical: |
| 384 child.parentData.position = new Point(childCrossPosition, childMainPos
ition); | 384 child.parentData.position = new Point(childCrossPosition, childMainPos
ition); |
| 385 break; | 385 break; |
| 386 } | 386 } |
| 387 childMainPosition += _getMainSize(child) + betweenSpace; | 387 childMainPosition += _getMainSize(child) + betweenSpace; |
| 388 child = child.parentData.nextSibling; | 388 child = child.parentData.nextSibling; |
| 389 } | 389 } |
| 390 } | 390 } |
| 391 | 391 |
| 392 void hitTestChildren(HitTestResult result, { Point position }) { | 392 void hitTestChildren(HitTestResult result, { Point position }) { |
| 393 defaultHitTestChildren(result, position: position); | 393 defaultHitTestChildren(result, position: position); |
| 394 } | 394 } |
| 395 | 395 |
| 396 void paint(PaintingCanvas canvas, Offset offset) { | 396 void paint(PaintingCanvas canvas, Offset offset) { |
| 397 defaultPaint(canvas, offset); | 397 defaultPaint(canvas, offset); |
| 398 } | 398 } |
| 399 } | 399 } |
| OLD | NEW |