Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(332)

Side by Side Diff: Source/core/rendering/RenderGrid.cpp

Issue 706903003: [CSS Grid Layout] Partial implementation of justify-content for Grid. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Applied suggested changes. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/rendering/RenderGrid.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Apple Inc. All rights reserved. 2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 } 500 }
501 } 501 }
502 } 502 }
503 503
504 for (size_t i = 0; i < flexibleSizedTracksIndex.size(); ++i) { 504 for (size_t i = 0; i < flexibleSizedTracksIndex.size(); ++i) {
505 const size_t trackIndex = flexibleSizedTracksIndex[i]; 505 const size_t trackIndex = flexibleSizedTracksIndex[i];
506 GridTrackSize trackSize = gridTrackSize(direction, trackIndex); 506 GridTrackSize trackSize = gridTrackSize(direction, trackIndex);
507 507
508 tracks[trackIndex].m_usedBreadth = std::max<LayoutUnit>(tracks[trackInde x].m_usedBreadth, normalizedFractionBreadth * trackSize.maxTrackBreadth().flex() ); 508 tracks[trackIndex].m_usedBreadth = std::max<LayoutUnit>(tracks[trackInde x].m_usedBreadth, normalizedFractionBreadth * trackSize.maxTrackBreadth().flex() );
509 } 509 }
510
511 // Flexible tracks will exhaust the availableLogicalSpace.
Julien - ping for review 2014/11/18 16:14:36 You're making one assumption here, which is that o
jfernandez 2014/11/20 11:59:39 Acknowledged.
512 availableLogicalSpace = 0;
510 } 513 }
511 514
512 LayoutUnit RenderGrid::computeUsedBreadthOfMinLength(GridTrackSizingDirection di rection, const GridLength& gridLength) const 515 LayoutUnit RenderGrid::computeUsedBreadthOfMinLength(GridTrackSizingDirection di rection, const GridLength& gridLength) const
513 { 516 {
514 if (gridLength.isFlex()) 517 if (gridLength.isFlex())
515 return 0; 518 return 0;
516 519
517 const Length& trackLength = gridLength.length(); 520 const Length& trackLength = gridLength.length();
518 ASSERT(!trackLength.isAuto()); 521 ASSERT(!trackLength.isAuto());
519 if (trackLength.isSpecified()) 522 if (trackLength.isSpecified())
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 m_gridItemCoordinate.clear(); 1083 m_gridItemCoordinate.clear();
1081 m_gridIsDirty = true; 1084 m_gridIsDirty = true;
1082 m_gridItemsOverflowingGridArea.resize(0); 1085 m_gridItemsOverflowingGridArea.resize(0);
1083 m_gridItemsIndexesMap.clear(); 1086 m_gridItemsIndexesMap.clear();
1084 } 1087 }
1085 1088
1086 void RenderGrid::layoutGridItems() 1089 void RenderGrid::layoutGridItems()
1087 { 1090 {
1088 placeItemsOnGrid(); 1091 placeItemsOnGrid();
1089 1092
1093 LayoutUnit availableSpaceForColumns = availableLogicalWidth();
1094 LayoutUnit availableSpaceForRows = availableLogicalHeight(IncludeMarginBorde rPadding);
1090 GridSizingData sizingData(gridColumnCount(), gridRowCount()); 1095 GridSizingData sizingData(gridColumnCount(), gridRowCount());
1091 computeUsedBreadthOfGridTracks(ForColumns, sizingData); 1096 computeUsedBreadthOfGridTracks(ForColumns, sizingData, availableSpaceForColu mns);
1092 ASSERT(tracksAreWiderThanMinTrackBreadth(ForColumns, sizingData.columnTracks )); 1097 ASSERT(tracksAreWiderThanMinTrackBreadth(ForColumns, sizingData.columnTracks ));
1093 computeUsedBreadthOfGridTracks(ForRows, sizingData); 1098 computeUsedBreadthOfGridTracks(ForRows, sizingData, availableSpaceForRows);
1094 ASSERT(tracksAreWiderThanMinTrackBreadth(ForRows, sizingData.rowTracks)); 1099 ASSERT(tracksAreWiderThanMinTrackBreadth(ForRows, sizingData.rowTracks));
1095 1100
1096 populateGridPositions(sizingData); 1101 populateGridPositions(sizingData, availableSpaceForColumns, availableSpaceFo rRows);
1097 m_gridItemsOverflowingGridArea.resize(0); 1102 m_gridItemsOverflowingGridArea.resize(0);
1098 1103
1099 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBo x()) { 1104 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBo x()) {
1100 if (child->isOutOfFlowPositioned()) { 1105 if (child->isOutOfFlowPositioned()) {
1101 // FIXME: Absolute positioned grid items should have a special 1106 // FIXME: Absolute positioned grid items should have a special
1102 // behavior as described in the spec (crbug.com/273898): 1107 // behavior as described in the spec (crbug.com/273898):
1103 // http://www.w3.org/TR/css-grid-1/#abspos-items 1108 // http://www.w3.org/TR/css-grid-1/#abspos-items
1104 child->containingBlock()->insertPositionedObject(child); 1109 child->containingBlock()->insertPositionedObject(child);
1105 } 1110 }
1106 1111
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1154 LayoutUnit RenderGrid::gridAreaBreadthForChild(const RenderBox& child, GridTrack SizingDirection direction, const Vector<GridTrack>& tracks) const 1159 LayoutUnit RenderGrid::gridAreaBreadthForChild(const RenderBox& child, GridTrack SizingDirection direction, const Vector<GridTrack>& tracks) const
1155 { 1160 {
1156 const GridCoordinate& coordinate = cachedGridCoordinate(child); 1161 const GridCoordinate& coordinate = cachedGridCoordinate(child);
1157 const GridSpan& span = (direction == ForColumns) ? coordinate.columns : coor dinate.rows; 1162 const GridSpan& span = (direction == ForColumns) ? coordinate.columns : coor dinate.rows;
1158 LayoutUnit gridAreaBreadth = 0; 1163 LayoutUnit gridAreaBreadth = 0;
1159 for (GridSpan::iterator trackPosition = span.begin(); trackPosition != span. end(); ++trackPosition) 1164 for (GridSpan::iterator trackPosition = span.begin(); trackPosition != span. end(); ++trackPosition)
1160 gridAreaBreadth += tracks[trackPosition.toInt()].m_usedBreadth; 1165 gridAreaBreadth += tracks[trackPosition.toInt()].m_usedBreadth;
1161 return gridAreaBreadth; 1166 return gridAreaBreadth;
1162 } 1167 }
1163 1168
1164 void RenderGrid::populateGridPositions(const GridSizingData& sizingData) 1169 void RenderGrid::populateGridPositions(const GridSizingData& sizingData, LayoutU nit availableSpaceForColumns, LayoutUnit availableSpaceForRows)
1165 { 1170 {
1166 m_columnPositions.resize(sizingData.columnTracks.size() + 1); 1171 unsigned numberOfColumnTracks = sizingData.columnTracks.size();
1167 m_columnPositions[0] = borderAndPaddingStart(); 1172 LayoutUnit columnOffset = contentPositionAndDistributionOffset(availableSpac eForColumns, style()->justifyContent(), style()->justifyContentDistribution(), n umberOfColumnTracks);
1173
1174 m_columnPositions.resize(numberOfColumnTracks + 1);
1175 m_columnPositions[0] = borderAndPaddingStart() + columnOffset;
1168 for (size_t i = 0; i < m_columnPositions.size() - 1; ++i) 1176 for (size_t i = 0; i < m_columnPositions.size() - 1; ++i)
1169 m_columnPositions[i + 1] = m_columnPositions[i] + sizingData.columnTrack s[i].m_usedBreadth; 1177 m_columnPositions[i + 1] = m_columnPositions[i] + sizingData.columnTrack s[i].m_usedBreadth;
1170 1178
1171 m_rowPositions.resize(sizingData.rowTracks.size() + 1); 1179 m_rowPositions.resize(sizingData.rowTracks.size() + 1);
1172 m_rowPositions[0] = borderAndPaddingBefore(); 1180 m_rowPositions[0] = borderAndPaddingBefore();
1173 for (size_t i = 0; i < m_rowPositions.size() - 1; ++i) 1181 for (size_t i = 0; i < m_rowPositions.size() - 1; ++i)
1174 m_rowPositions[i + 1] = m_rowPositions[i] + sizingData.rowTracks[i].m_us edBreadth; 1182 m_rowPositions[i + 1] = m_rowPositions[i] + sizingData.rowTracks[i].m_us edBreadth;
1175 } 1183 }
1176 1184
1177 static LayoutUnit computeOverflowAlignmentOffset(OverflowAlignment overflow, Lay outUnit startOfTrack, LayoutUnit endOfTrack, LayoutUnit childBreadth) 1185 static LayoutUnit computeOverflowAlignmentOffset(OverflowAlignment overflow, Lay outUnit startOfTrack, LayoutUnit endOfTrack, LayoutUnit childBreadth)
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
1489 // FIXME: Implement the ItemPositionBaseline value. For now, we always s tart align the child. 1497 // FIXME: Implement the ItemPositionBaseline value. For now, we always s tart align the child.
1490 return startOfRowForChild(child); 1498 return startOfRowForChild(child);
1491 case ItemPositionAuto: 1499 case ItemPositionAuto:
1492 break; 1500 break;
1493 } 1501 }
1494 1502
1495 ASSERT_NOT_REACHED(); 1503 ASSERT_NOT_REACHED();
1496 return 0; 1504 return 0;
1497 } 1505 }
1498 1506
1507 ContentPosition static resolveContentDistributionFallback(ContentDistributionTyp e distribution)
1508 {
1509 switch (distribution) {
1510 case ContentDistributionSpaceBetween:
1511 return ContentPositionStart;
1512 case ContentDistributionSpaceAround:
1513 return ContentPositionCenter;
1514 case ContentDistributionSpaceEvenly:
1515 return ContentPositionCenter;
1516 case ContentDistributionStretch:
1517 return ContentPositionStart;
1518 case ContentDistributionDefault:
1519 return ContentPositionAuto;
1520 default:
Julien - ping for review 2014/11/18 16:14:36 Why do we need a default? Your reflex should be to
jfernandez 2014/11/20 11:59:39 My idea of the default option in these cases was t
1521 ASSERT_NOT_REACHED();
1522 }
1523
1524 return ContentPositionAuto;
1525 }
1526
1527 LayoutUnit static contentDistributionOffset(LayoutUnit availableFreeSpace, Conte ntPosition& fallbackPosition, ContentDistributionType distribution, unsigned num berOfGridTracks)
1528 {
1529 ASSERT(numberOfGridTracks > 0);
1530
1531 if (fallbackPosition == ContentPositionAuto)
1532 fallbackPosition = resolveContentDistributionFallback(distribution);
1533
1534 switch (distribution) {
1535 case ContentDistributionSpaceBetween:
1536 // FIXME: for the time being, spec states that it will always fallback f or Grids, but
1537 // discussion is ongoing.
1538 return -1;
1539 case ContentDistributionSpaceAround:
1540 // FIXME: for the time being, spec states that it will always fallback f or Grids, but
1541 // discussion is ongoing.
1542 return -1;
1543 case ContentDistributionSpaceEvenly:
1544 // FIXME: for the time being, spec states that it will always fallback f or Grids, but
1545 // discussion is ongoing.
1546 return -1;
1547 case ContentDistributionStretch:
1548 // FIXME: for the time being, spec states that it will always fallback f or Grids, but
1549 // discussion is ongoing.
1550 return -1;
1551 case ContentDistributionDefault:
1552 return -1;
1553 default:
1554 ASSERT_NOT_REACHED();
1555 }
1556
1557 return -1;
1558 }
Julien - ping for review 2014/11/18 16:14:36 This function is really artificial as I pointed ou
jfernandez 2014/11/20 11:59:39 Acknowledged.
1559
1560 LayoutUnit RenderGrid::contentPositionAndDistributionOffset(LayoutUnit available FreeSpace, ContentPosition position, ContentDistributionType distribution, unsig ned numberOfGridTracks) const
1561 {
1562 if (availableFreeSpace <= 0)
1563 return 0;
1564
1565 LayoutUnit offset = contentDistributionOffset(availableFreeSpace, position, distribution, numberOfGridTracks);
1566 if (offset >= 0)
1567 return offset;
1568
1569 switch (position) {
1570 case ContentPositionLeft:
1571 // If the property's axis is not parallel with the inline axis, this is equivalent to ‘start’.
1572 if (!isHorizontalWritingMode())
1573 return 0;
1574 if (style()->isLeftToRightDirection())
1575 return 0;
1576 return availableFreeSpace;
1577 case ContentPositionRight:
1578 // If the property's axis is not parallel with the inline axis, this is equivalent to ‘start’.
1579 if (!isHorizontalWritingMode())
1580 return 0;
1581 if (style()->isLeftToRightDirection())
1582 return availableFreeSpace;
1583 return 0;
1584 case ContentPositionCenter:
1585 return availableFreeSpace / 2;
1586 case ContentPositionFlexEnd:
1587 // Only used in flex layout, for other layout, it's equivalent to 'End'.
1588 case ContentPositionEnd:
1589 return availableFreeSpace;
1590 case ContentPositionFlexStart:
1591 // Only used in flex layout, for other layout, it's equivalent to 'Start '.
1592 case ContentPositionStart:
1593 return 0;
1594 case ContentPositionBaseline:
1595 case ContentPositionLastBaseline:
1596 // FIXME: Implement the previous values. For now, we always start align.
1597 return 0;
1598 case ContentPositionAuto:
1599 default:
Julien - ping for review 2014/11/18 16:14:36 I have not seen an explanation for keeping the 'de
jfernandez 2014/11/20 11:59:39 Done.
1600 ASSERT_NOT_REACHED();
1601 }
1602
1603 return 0;
1604 }
1605
1499 LayoutPoint RenderGrid::findChildLogicalPosition(const RenderBox& child) const 1606 LayoutPoint RenderGrid::findChildLogicalPosition(const RenderBox& child) const
1500 { 1607 {
1501 return LayoutPoint(columnPositionForChild(child), rowPositionForChild(child) ); 1608 return LayoutPoint(columnPositionForChild(child), rowPositionForChild(child) );
1502 } 1609 }
1503 1610
1504 void RenderGrid::paintChildren(PaintInfo& paintInfo, const LayoutPoint& paintOff set) 1611 void RenderGrid::paintChildren(PaintInfo& paintInfo, const LayoutPoint& paintOff set)
1505 { 1612 {
1506 GridPainter(*this).paintChildren(paintInfo, paintOffset); 1613 GridPainter(*this).paintChildren(paintInfo, paintOffset);
1507 } 1614 }
1508 1615
1509 const char* RenderGrid::renderName() const 1616 const char* RenderGrid::renderName() const
1510 { 1617 {
1511 if (isFloating()) 1618 if (isFloating())
1512 return "RenderGrid (floating)"; 1619 return "RenderGrid (floating)";
1513 if (isOutOfFlowPositioned()) 1620 if (isOutOfFlowPositioned())
1514 return "RenderGrid (positioned)"; 1621 return "RenderGrid (positioned)";
1515 if (isAnonymous()) 1622 if (isAnonymous())
1516 return "RenderGrid (generated)"; 1623 return "RenderGrid (generated)";
1517 if (isRelPositioned()) 1624 if (isRelPositioned())
1518 return "RenderGrid (relative positioned)"; 1625 return "RenderGrid (relative positioned)";
1519 return "RenderGrid"; 1626 return "RenderGrid";
1520 } 1627 }
1521 1628
1522 } // namespace blink 1629 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/rendering/RenderGrid.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698