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

Side by Side Diff: third_party/WebKit/Source/core/layout/LayoutFlexibleBox.cpp

Issue 2170533002: [css-flexbox] Some more code refactoring (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months 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 | « third_party/WebKit/Source/core/layout/LayoutFlexibleBox.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 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google 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 are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 { 56 {
57 } 57 }
58 58
59 LayoutUnit crossAxisOffset; 59 LayoutUnit crossAxisOffset;
60 LayoutUnit crossAxisExtent; 60 LayoutUnit crossAxisExtent;
61 size_t numberOfChildren; 61 size_t numberOfChildren;
62 LayoutUnit maxAscent; 62 LayoutUnit maxAscent;
63 }; 63 };
64 64
65 struct LayoutFlexibleBox::FlexItem { 65 struct LayoutFlexibleBox::FlexItem {
66 FlexItem(LayoutBox* box, LayoutUnit flexBaseContentSize, LayoutUnit hypothet icalMainContentSize) 66 FlexItem(LayoutBox* box, LayoutUnit flexBaseContentSize, LayoutUnit hypothet icalMainContentSize, LayoutUnit mainAxisBorderAndPadding, LayoutUnit mainAxisMar gin)
67 : box(box) 67 : box(box)
68 , flexBaseContentSize(flexBaseContentSize) 68 , flexBaseContentSize(flexBaseContentSize)
69 , hypotheticalMainContentSize(hypotheticalMainContentSize) 69 , hypotheticalMainContentSize(hypotheticalMainContentSize)
70 , mainAxisBorderAndPadding(mainAxisBorderAndPadding)
71 , mainAxisMargin(mainAxisMargin)
70 , frozen(false) 72 , frozen(false)
71 { 73 {
72 } 74 }
73 75
74 // This constructor is used for out-of-flow children 76 // This constructor is used for out-of-flow children
75 explicit FlexItem(LayoutBox* box) 77 explicit FlexItem(LayoutBox* box)
76 : box(box) 78 : box(box)
77 , flexBaseContentSize() 79 , flexBaseContentSize()
78 , hypotheticalMainContentSize() 80 , hypotheticalMainContentSize()
81 , mainAxisBorderAndPadding()
79 , frozen(true) 82 , frozen(true)
80 { 83 {
81 } 84 }
85
86 LayoutUnit hypotheticalMainAxisMarginBoxSize() const
87 {
88 return hypotheticalMainContentSize + mainAxisBorderAndPadding + mainAxis Margin;
89 }
90
91 LayoutUnit flexBaseMarginBoxSize() const
92 {
93 return flexBaseContentSize + mainAxisBorderAndPadding + mainAxisMargin;
94 }
95
82 LayoutBox* box; 96 LayoutBox* box;
83 const LayoutUnit flexBaseContentSize; 97 const LayoutUnit flexBaseContentSize;
84 const LayoutUnit hypotheticalMainContentSize; 98 const LayoutUnit hypotheticalMainContentSize;
99 const LayoutUnit mainAxisBorderAndPadding;
100 const LayoutUnit mainAxisMargin;
85 LayoutUnit flexedContentSize; 101 LayoutUnit flexedContentSize;
86 bool frozen; 102 bool frozen;
87 }; 103 };
88 104
89 LayoutFlexibleBox::LayoutFlexibleBox(Element* element) 105 LayoutFlexibleBox::LayoutFlexibleBox(Element* element)
90 : LayoutBlock(element) 106 : LayoutBlock(element)
91 , m_orderIterator(this) 107 , m_orderIterator(this)
92 , m_numberOfInFlowChildrenOnFirstLine(-1) 108 , m_numberOfInFlowChildrenOnFirstLine(-1)
93 , m_hasDefiniteHeight(SizeDefiniteness::Unknown) 109 , m_hasDefiniteHeight(SizeDefiniteness::Unknown)
94 { 110 {
(...skipping 667 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 } 778 }
763 779
764 void LayoutFlexibleBox::setFlowAwareLocationForChild(LayoutBox& child, const Lay outPoint& location) 780 void LayoutFlexibleBox::setFlowAwareLocationForChild(LayoutBox& child, const Lay outPoint& location)
765 { 781 {
766 if (isHorizontalFlow()) 782 if (isHorizontalFlow())
767 child.setLocationAndUpdateOverflowControlsIfNeeded(location); 783 child.setLocationAndUpdateOverflowControlsIfNeeded(location);
768 else 784 else
769 child.setLocationAndUpdateOverflowControlsIfNeeded(location.transposedPo int()); 785 child.setLocationAndUpdateOverflowControlsIfNeeded(location.transposedPo int());
770 } 786 }
771 787
772 LayoutUnit LayoutFlexibleBox::mainAxisBorderAndPaddingExtentForChild(const Layou tBox& child) const
773 {
774 return isHorizontalFlow() ? child.borderAndPaddingWidth() : child.borderAndP addingHeight();
775 }
776
777 bool LayoutFlexibleBox::mainAxisLengthIsDefinite(const LayoutBox& child, const L ength& flexBasis) const 788 bool LayoutFlexibleBox::mainAxisLengthIsDefinite(const LayoutBox& child, const L ength& flexBasis) const
778 { 789 {
779 if (flexBasis.isAuto()) 790 if (flexBasis.isAuto())
780 return false; 791 return false;
781 if (flexBasis.hasPercent()) { 792 if (flexBasis.hasPercent()) {
782 if (!isColumnFlow() || m_hasDefiniteHeight == SizeDefiniteness::Definite ) 793 if (!isColumnFlow() || m_hasDefiniteHeight == SizeDefiniteness::Definite )
783 return true; 794 return true;
784 if (m_hasDefiniteHeight == SizeDefiniteness::Indefinite) 795 if (m_hasDefiniteHeight == SizeDefiniteness::Indefinite)
785 return false; 796 return false;
786 bool definite = child.computePercentageLogicalHeight(flexBasis) != -1; 797 bool definite = child.computePercentageLogicalHeight(flexBasis) != -1;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
830 } 841 }
831 m_intrinsicSizeAlongMainAxis.set(&child, mainSize); 842 m_intrinsicSizeAlongMainAxis.set(&child, mainSize);
832 m_relaidOutChildren.add(&child); 843 m_relaidOutChildren.add(&child);
833 } 844 }
834 845
835 void LayoutFlexibleBox::clearCachedMainSizeForChild(const LayoutBox& child) 846 void LayoutFlexibleBox::clearCachedMainSizeForChild(const LayoutBox& child)
836 { 847 {
837 m_intrinsicSizeAlongMainAxis.remove(&child); 848 m_intrinsicSizeAlongMainAxis.remove(&child);
838 } 849 }
839 850
840 LayoutUnit LayoutFlexibleBox::computeInnerFlexBaseSizeForChild(LayoutBox& child, ChildLayoutType childLayoutType) 851 LayoutUnit LayoutFlexibleBox::computeInnerFlexBaseSizeForChild(LayoutBox& child, LayoutUnit mainAxisBorderAndPadding, ChildLayoutType childLayoutType)
841 { 852 {
842 child.clearOverrideSize(); 853 child.clearOverrideSize();
843 854
844 if (child.isImage() || child.isVideo() || child.isCanvas()) 855 if (child.isImage() || child.isVideo() || child.isCanvas())
845 UseCounter::count(document(), UseCounter::AspectRatioFlexItem); 856 UseCounter::count(document(), UseCounter::AspectRatioFlexItem);
846 857
847 Length flexBasis = flexBasisForChild(child); 858 Length flexBasis = flexBasisForChild(child);
848 if (mainAxisLengthIsDefinite(child, flexBasis)) 859 if (mainAxisLengthIsDefinite(child, flexBasis))
849 return std::max(LayoutUnit(), computeMainAxisExtentForChild(child, MainO rPreferredSize, flexBasis)); 860 return std::max(LayoutUnit(), computeMainAxisExtentForChild(child, MainO rPreferredSize, flexBasis));
850 861
851 LayoutUnit mainAxisExtent; 862 LayoutUnit mainAxisExtent;
852 if (childFlexBaseSizeRequiresLayout(child)) { 863 if (childFlexBaseSizeRequiresLayout(child)) {
853 if (childLayoutType == NeverLayout) 864 if (childLayoutType == NeverLayout)
854 return LayoutUnit(); 865 return LayoutUnit();
855 866
856 if (child.needsLayout() || childLayoutType == ForceLayout || !m_intrinsi cSizeAlongMainAxis.contains(&child)) { 867 if (child.needsLayout() || childLayoutType == ForceLayout || !m_intrinsi cSizeAlongMainAxis.contains(&child)) {
857 child.forceChildLayout(); 868 child.forceChildLayout();
858 cacheChildMainSize(child); 869 cacheChildMainSize(child);
859 } 870 }
860 mainAxisExtent = m_intrinsicSizeAlongMainAxis.get(&child); 871 mainAxisExtent = m_intrinsicSizeAlongMainAxis.get(&child);
861 } else { 872 } else {
862 // We don't need to add scrollbarLogicalWidth here. For overflow: scroll , the preferred width 873 // We don't need to add scrollbarLogicalWidth here. For overflow: scroll , the preferred width
863 // already includes the scrollbar size (via scrollbarLogicalWidth()). Fo r overflow: auto, 874 // already includes the scrollbar size (via scrollbarLogicalWidth()). Fo r overflow: auto,
864 // childFlexBaseSizeRequiresLayout returns true and we handle that via t he other branch 875 // childFlexBaseSizeRequiresLayout returns true and we handle that via t he other branch
865 // of this if. 876 // of this if.
866 mainAxisExtent = child.maxPreferredLogicalWidth(); 877 mainAxisExtent = child.maxPreferredLogicalWidth();
867 } 878 }
868 ASSERT(mainAxisExtent - mainAxisBorderAndPaddingExtentForChild(child) >= 0); 879 DCHECK_GE(mainAxisExtent - mainAxisBorderAndPadding, LayoutUnit()) << mainAx isExtent << " - " << mainAxisBorderAndPadding;
869 return mainAxisExtent - mainAxisBorderAndPaddingExtentForChild(child); 880 return mainAxisExtent - mainAxisBorderAndPadding;
870 } 881 }
871 882
872 void LayoutFlexibleBox::layoutFlexItems(bool relayoutChildren, SubtreeLayoutScop e& layoutScope) 883 void LayoutFlexibleBox::layoutFlexItems(bool relayoutChildren, SubtreeLayoutScop e& layoutScope)
873 { 884 {
874 Vector<LineContext> lineContexts; 885 Vector<LineContext> lineContexts;
875 OrderedFlexItemList orderedChildren; 886 OrderedFlexItemList orderedChildren;
876 LayoutUnit sumFlexBaseSize; 887 LayoutUnit sumFlexBaseSize;
877 double totalFlexGrow; 888 double totalFlexGrow;
878 double totalFlexShrink; 889 double totalFlexShrink;
879 double totalWeightedFlexShrink; 890 double totalWeightedFlexShrink;
(...skipping 14 matching lines...) Expand all
894 // The initial free space gets calculated after freezing inflexible item s. https://drafts.csswg.org/css-flexbox/#resolve-flexible-lengths step 3 905 // The initial free space gets calculated after freezing inflexible item s. https://drafts.csswg.org/css-flexbox/#resolve-flexible-lengths step 3
895 const LayoutUnit initialFreeSpace = remainingFreeSpace; 906 const LayoutUnit initialFreeSpace = remainingFreeSpace;
896 while (!resolveFlexibleLengths(flexSign, orderedChildren, initialFreeSpa ce, remainingFreeSpace, totalFlexGrow, totalFlexShrink, totalWeightedFlexShrink) ) { 907 while (!resolveFlexibleLengths(flexSign, orderedChildren, initialFreeSpa ce, remainingFreeSpace, totalFlexGrow, totalFlexShrink, totalWeightedFlexShrink) ) {
897 ASSERT(totalFlexGrow >= 0 && totalWeightedFlexShrink >= 0); 908 ASSERT(totalFlexGrow >= 0 && totalWeightedFlexShrink >= 0);
898 } 909 }
899 910
900 // Recalculate the remaining free space. The adjustment for flex factors between 0..1 means we can't just 911 // Recalculate the remaining free space. The adjustment for flex factors between 0..1 means we can't just
901 // use remainingFreeSpace here. 912 // use remainingFreeSpace here.
902 remainingFreeSpace = containerMainInnerSize; 913 remainingFreeSpace = containerMainInnerSize;
903 for (size_t i = 0; i < orderedChildren.size(); ++i) { 914 for (size_t i = 0; i < orderedChildren.size(); ++i) {
915 FlexItem& flexItem = orderedChildren[i];
904 LayoutBox* child = orderedChildren[i].box; 916 LayoutBox* child = orderedChildren[i].box;
905 if (child->isOutOfFlowPositioned()) 917 if (child->isOutOfFlowPositioned())
906 continue; 918 continue;
907 remainingFreeSpace -= (orderedChildren[i].flexedContentSize + mainAx isBorderAndPaddingExtentForChild(*child) 919 remainingFreeSpace -= orderedChildren[i].flexedContentSize + flexIte m.mainAxisBorderAndPadding + flexItem.mainAxisMargin;
908 + (isHorizontalFlow() ? child->marginWidth() : child->marginHeig ht()));
909 } 920 }
910 layoutAndPlaceChildren(crossAxisOffset, orderedChildren, remainingFreeSp ace, relayoutChildren, layoutScope, lineContexts); 921 layoutAndPlaceChildren(crossAxisOffset, orderedChildren, remainingFreeSp ace, relayoutChildren, layoutScope, lineContexts);
911 } 922 }
912 if (hasLineIfEmpty()) { 923 if (hasLineIfEmpty()) {
913 // Even if computeNextFlexLine returns true, the flexbox might not have 924 // Even if computeNextFlexLine returns true, the flexbox might not have
914 // a line because all our children might be out of flow positioned. 925 // a line because all our children might be out of flow positioned.
915 // Instead of just checking if we have a line, make sure the flexbox 926 // Instead of just checking if we have a line, make sure the flexbox
916 // has at least a line's worth of height to cover this case. 927 // has at least a line's worth of height to cover this case.
917 LayoutUnit minHeight = minimumLogicalHeightForEmptyLine(); 928 LayoutUnit minHeight = minimumLogicalHeightForEmptyLine();
918 if (size().height() < minHeight) 929 if (size().height() < minHeight)
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
1193 } 1204 }
1194 1205
1195 if (crossAxisLengthIsDefinite(child, crossMin)) { 1206 if (crossAxisLengthIsDefinite(child, crossMin)) {
1196 LayoutUnit minValue = computeMainSizeFromAspectRatioUsing(child, crossMi n); 1207 LayoutUnit minValue = computeMainSizeFromAspectRatioUsing(child, crossMi n);
1197 childSize = std::max(minValue, childSize); 1208 childSize = std::max(minValue, childSize);
1198 } 1209 }
1199 1210
1200 return childSize; 1211 return childSize;
1201 } 1212 }
1202 1213
1214 LayoutFlexibleBox::FlexItem LayoutFlexibleBox::constructFlexItem(LayoutBox& chil d, ChildLayoutType layoutType)
1215 {
1216 LayoutUnit borderAndPadding = isHorizontalFlow() ? child.borderAndPaddingWid th() : child.borderAndPaddingHeight();
1217 LayoutUnit childInnerFlexBaseSize = computeInnerFlexBaseSizeForChild(child, borderAndPadding, layoutType);
1218 LayoutUnit childMinMaxAppliedMainAxisExtent = adjustChildSizeForMinAndMax(ch ild, childInnerFlexBaseSize);
1219 LayoutUnit margin = isHorizontalFlow() ? child.marginWidth() : child.marginH eight();
1220 return FlexItem(&child, childInnerFlexBaseSize, childMinMaxAppliedMainAxisEx tent, borderAndPadding, margin);
1221 }
1222
1203 bool LayoutFlexibleBox::computeNextFlexLine(OrderedFlexItemList& orderedChildren , LayoutUnit& sumFlexBaseSize, double& totalFlexGrow, double& totalFlexShrink, d ouble& totalWeightedFlexShrink, LayoutUnit& sumHypotheticalMainSize, bool relayo utChildren) 1223 bool LayoutFlexibleBox::computeNextFlexLine(OrderedFlexItemList& orderedChildren , LayoutUnit& sumFlexBaseSize, double& totalFlexGrow, double& totalFlexShrink, d ouble& totalWeightedFlexShrink, LayoutUnit& sumHypotheticalMainSize, bool relayo utChildren)
1204 { 1224 {
1205 orderedChildren.clear(); 1225 orderedChildren.clear();
1206 sumFlexBaseSize = LayoutUnit(); 1226 sumFlexBaseSize = LayoutUnit();
1207 totalFlexGrow = totalFlexShrink = totalWeightedFlexShrink = 0; 1227 totalFlexGrow = totalFlexShrink = totalWeightedFlexShrink = 0;
1208 sumHypotheticalMainSize = LayoutUnit(); 1228 sumHypotheticalMainSize = LayoutUnit();
1209 1229
1210 if (!m_orderIterator.currentChild()) 1230 if (!m_orderIterator.currentChild())
1211 return false; 1231 return false;
1212 1232
(...skipping 12 matching lines...) Expand all
1225 // If this condition is true, then computeMainAxisExtentForChild will ca ll child.intrinsicContentLogicalHeight() 1245 // If this condition is true, then computeMainAxisExtentForChild will ca ll child.intrinsicContentLogicalHeight()
1226 // and child.scrollbarLogicalHeight(), so if the child has intrinsic min /max/preferred size, 1246 // and child.scrollbarLogicalHeight(), so if the child has intrinsic min /max/preferred size,
1227 // run layout on it now to make sure its logical height and scroll bars are up to date. 1247 // run layout on it now to make sure its logical height and scroll bars are up to date.
1228 if (childHasIntrinsicMainAxisSize(*child) && child->needsLayout()) { 1248 if (childHasIntrinsicMainAxisSize(*child) && child->needsLayout()) {
1229 child->clearOverrideSize(); 1249 child->clearOverrideSize();
1230 child->layoutIfNeeded(); 1250 child->layoutIfNeeded();
1231 cacheChildMainSize(*child); 1251 cacheChildMainSize(*child);
1232 layoutType = LayoutIfNeeded; 1252 layoutType = LayoutIfNeeded;
1233 } 1253 }
1234 1254
1235 LayoutUnit childInnerFlexBaseSize = computeInnerFlexBaseSizeForChild(*ch ild, layoutType); 1255 FlexItem flexItem = constructFlexItem(*child, layoutType);
1236 LayoutUnit childMainAxisMarginBorderPadding = mainAxisBorderAndPaddingEx tentForChild(*child) 1256 if (isMultiline() && sumHypotheticalMainSize + flexItem.hypotheticalMain AxisMarginBoxSize() > lineBreakLength && lineHasInFlowItem)
1237 + (isHorizontalFlow() ? child->marginWidth() : child->marginHeight() );
1238 LayoutUnit childOuterFlexBaseSize = childInnerFlexBaseSize + childMainAx isMarginBorderPadding;
1239
1240 LayoutUnit childMinMaxAppliedMainAxisExtent = adjustChildSizeForMinAndMa x(*child, childInnerFlexBaseSize);
1241 LayoutUnit childHypotheticalMainSize = childMinMaxAppliedMainAxisExtent + childMainAxisMarginBorderPadding;
1242
1243 if (isMultiline() && sumHypotheticalMainSize + childHypotheticalMainSize > lineBreakLength && lineHasInFlowItem)
1244 break; 1257 break;
1245 orderedChildren.append(FlexItem(child, childInnerFlexBaseSize, childMinM axAppliedMainAxisExtent)); 1258 orderedChildren.append(flexItem);
1246 lineHasInFlowItem = true; 1259 lineHasInFlowItem = true;
1247 sumFlexBaseSize += childOuterFlexBaseSize; 1260 sumFlexBaseSize += flexItem.flexBaseMarginBoxSize();
1248 totalFlexGrow += child->style()->flexGrow(); 1261 totalFlexGrow += child->style()->flexGrow();
1249 totalFlexShrink += child->style()->flexShrink(); 1262 totalFlexShrink += child->style()->flexShrink();
1250 totalWeightedFlexShrink += child->style()->flexShrink() * childInnerFlex BaseSize; 1263 totalWeightedFlexShrink += child->style()->flexShrink() * flexItem.flexB aseContentSize;
1251 sumHypotheticalMainSize += childHypotheticalMainSize; 1264 sumHypotheticalMainSize += flexItem.hypotheticalMainAxisMarginBoxSize();
1252 } 1265 }
1253 return true; 1266 return true;
1254 } 1267 }
1255 1268
1256 void LayoutFlexibleBox::freezeViolations(Vector<FlexItem*>& violations, LayoutUn it& availableFreeSpace, double& totalFlexGrow, double& totalFlexShrink, double& totalWeightedFlexShrink) 1269 void LayoutFlexibleBox::freezeViolations(Vector<FlexItem*>& violations, LayoutUn it& availableFreeSpace, double& totalFlexGrow, double& totalFlexShrink, double& totalWeightedFlexShrink)
1257 { 1270 {
1258 for (size_t i = 0; i < violations.size(); ++i) { 1271 for (size_t i = 0; i < violations.size(); ++i) {
1259 DCHECK(!violations[i]->frozen) << i; 1272 DCHECK(!violations[i]->frozen) << i;
1260 LayoutBox* child = violations[i]->box; 1273 LayoutBox* child = violations[i]->box;
1261 LayoutUnit childSize = violations[i]->flexedContentSize; 1274 LayoutUnit childSize = violations[i]->flexedContentSize;
(...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after
1906 ASSERT(child); 1919 ASSERT(child);
1907 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisE xtent; 1920 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisE xtent;
1908 LayoutUnit originalOffset = lineContexts[lineNumber].crossAxisOffset - crossAxisStartEdge; 1921 LayoutUnit originalOffset = lineContexts[lineNumber].crossAxisOffset - crossAxisStartEdge;
1909 LayoutUnit newOffset = contentExtent - originalOffset - lineCrossAxi sExtent; 1922 LayoutUnit newOffset = contentExtent - originalOffset - lineCrossAxi sExtent;
1910 adjustAlignmentForChild(*child, newOffset - originalOffset); 1923 adjustAlignmentForChild(*child, newOffset - originalOffset);
1911 } 1924 }
1912 } 1925 }
1913 } 1926 }
1914 1927
1915 } // namespace blink 1928 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/layout/LayoutFlexibleBox.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698