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

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

Issue 2829303002: [css-flexbox] Merge align-content and justify-content handling (Closed)
Patch Set: Created 3 years, 8 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 | « no previous file | 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 1483 matching lines...) Expand 10 before | Expand all | Expand 10 after
1494 if (total_violation) 1494 if (total_violation)
1495 FreezeViolations(total_violation < 0 ? max_violations : min_violations, 1495 FreezeViolations(total_violation < 0 ? max_violations : min_violations,
1496 remaining_free_space, total_flex_grow, total_flex_shrink, 1496 remaining_free_space, total_flex_grow, total_flex_shrink,
1497 total_weighted_flex_shrink); 1497 total_weighted_flex_shrink);
1498 else 1498 else
1499 remaining_free_space -= used_free_space; 1499 remaining_free_space -= used_free_space;
1500 1500
1501 return !total_violation; 1501 return !total_violation;
1502 } 1502 }
1503 1503
1504 static LayoutUnit InitialJustifyContentOffset( 1504 static LayoutUnit InitialContentPositionOffset(
1505 LayoutUnit available_free_space, 1505 LayoutUnit available_free_space,
1506 ContentPosition justify_content, 1506 ContentPosition content_position,
1507 ContentDistributionType justify_content_distribution, 1507 ContentDistributionType content_distribution,
1508 unsigned number_of_children) { 1508 unsigned number_of_items) {
1509 if (justify_content == kContentPositionFlexEnd) 1509 if (content_position == kContentPositionFlexEnd)
1510 return available_free_space; 1510 return available_free_space;
1511 if (justify_content == kContentPositionCenter) 1511 if (content_position == kContentPositionCenter)
1512 return available_free_space / 2; 1512 return available_free_space / 2;
1513 if (justify_content_distribution == kContentDistributionSpaceAround) { 1513 if (content_distribution == kContentDistributionSpaceAround) {
1514 if (available_free_space > 0 && number_of_children) 1514 if (available_free_space > 0 && number_of_items)
1515 return available_free_space / (2 * number_of_children); 1515 return available_free_space / (2 * number_of_items);
1516 1516
1517 return available_free_space / 2; 1517 return available_free_space / 2;
1518 } 1518 }
1519 return LayoutUnit(); 1519 return LayoutUnit();
1520 } 1520 }
1521 1521
1522 static LayoutUnit JustifyContentSpaceBetweenChildren( 1522 static LayoutUnit ContentDistributionSpaceBetweenChildren(
1523 bool is_align_content,
jfernandez 2017/04/21 20:43:21 I don't like too much the bool argument to change
1523 LayoutUnit available_free_space, 1524 LayoutUnit available_free_space,
1524 ContentDistributionType justify_content_distribution, 1525 ContentDistributionType content_distribution,
1525 unsigned number_of_children) { 1526 unsigned number_of_items) {
1526 if (available_free_space > 0 && number_of_children > 1) { 1527 if (available_free_space > 0 && number_of_items > 1) {
1527 if (justify_content_distribution == kContentDistributionSpaceBetween) 1528 if (content_distribution == kContentDistributionSpaceBetween)
1528 return available_free_space / (number_of_children - 1); 1529 return available_free_space / (number_of_items - 1);
1529 if (justify_content_distribution == kContentDistributionSpaceAround) 1530 if (content_distribution == kContentDistributionSpaceAround ||
1530 return available_free_space / number_of_children; 1531 (is_align_content &&
jfernandez 2017/04/21 20:43:21 ditto.
1532 content_distribution == kContentDistributionStretch))
1533 return available_free_space / number_of_items;
1531 } 1534 }
1532 return LayoutUnit(); 1535 return LayoutUnit();
1533 } 1536 }
1534 1537
1535 static LayoutUnit AlignmentOffset(LayoutUnit available_free_space, 1538 static LayoutUnit AlignmentOffset(LayoutUnit available_free_space,
1536 ItemPosition position, 1539 ItemPosition position,
1537 LayoutUnit ascent, 1540 LayoutUnit ascent,
1538 LayoutUnit max_ascent, 1541 LayoutUnit max_ascent,
1539 bool is_wrap_reverse) { 1542 bool is_wrap_reverse) {
1540 switch (position) { 1543 switch (position) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1593 const LayoutUnit available_space = 1596 const LayoutUnit available_space =
1594 MainAxisContentExtent(ContentLogicalHeight()) - 1597 MainAxisContentExtent(ContentLogicalHeight()) -
1595 MainAxisExtentForChild(child); 1598 MainAxisExtentForChild(child);
1596 1599
1597 ContentPosition position = StyleRef().ResolvedJustifyContentPosition( 1600 ContentPosition position = StyleRef().ResolvedJustifyContentPosition(
1598 ContentAlignmentNormalBehavior()); 1601 ContentAlignmentNormalBehavior());
1599 ContentDistributionType distribution = 1602 ContentDistributionType distribution =
1600 StyleRef().ResolvedJustifyContentDistribution( 1603 StyleRef().ResolvedJustifyContentDistribution(
1601 ContentAlignmentNormalBehavior()); 1604 ContentAlignmentNormalBehavior());
1602 LayoutUnit offset = 1605 LayoutUnit offset =
1603 InitialJustifyContentOffset(available_space, position, distribution, 1); 1606 InitialContentPositionOffset(available_space, position, distribution, 1);
1604 if (StyleRef().FlexDirection() == kFlowRowReverse || 1607 if (StyleRef().FlexDirection() == kFlowRowReverse ||
1605 StyleRef().FlexDirection() == kFlowColumnReverse) 1608 StyleRef().FlexDirection() == kFlowColumnReverse)
1606 offset = available_space - offset; 1609 offset = available_space - offset;
1607 return offset; 1610 return offset;
1608 } 1611 }
1609 1612
1610 LayoutUnit LayoutFlexibleBox::StaticCrossAxisPositionForPositionedChild( 1613 LayoutUnit LayoutFlexibleBox::StaticCrossAxisPositionForPositionedChild(
1611 const LayoutBox& child) { 1614 const LayoutBox& child) {
1612 LayoutUnit available_space = 1615 LayoutUnit available_space =
1613 CrossAxisContentExtent() - CrossAxisExtentForChild(child); 1616 CrossAxisContentExtent() - CrossAxisExtentForChild(child);
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
1775 ContentPosition position = StyleRef().ResolvedJustifyContentPosition( 1778 ContentPosition position = StyleRef().ResolvedJustifyContentPosition(
1776 ContentAlignmentNormalBehavior()); 1779 ContentAlignmentNormalBehavior());
1777 ContentDistributionType distribution = 1780 ContentDistributionType distribution =
1778 StyleRef().ResolvedJustifyContentDistribution( 1781 StyleRef().ResolvedJustifyContentDistribution(
1779 ContentAlignmentNormalBehavior()); 1782 ContentAlignmentNormalBehavior());
1780 1783
1781 LayoutUnit auto_margin_offset = 1784 LayoutUnit auto_margin_offset =
1782 AutoMarginOffsetInMainAxis(children, available_free_space); 1785 AutoMarginOffsetInMainAxis(children, available_free_space);
1783 LayoutUnit main_axis_offset = 1786 LayoutUnit main_axis_offset =
1784 FlowAwareBorderStart() + FlowAwarePaddingStart(); 1787 FlowAwareBorderStart() + FlowAwarePaddingStart();
1785 main_axis_offset += InitialJustifyContentOffset( 1788 main_axis_offset += InitialContentPositionOffset(
1786 available_free_space, position, distribution, children.size()); 1789 available_free_space, position, distribution, children.size());
1787 if (Style()->FlexDirection() == kFlowRowReverse && 1790 if (Style()->FlexDirection() == kFlowRowReverse &&
1788 ShouldPlaceBlockDirectionScrollbarOnLogicalLeft()) 1791 ShouldPlaceBlockDirectionScrollbarOnLogicalLeft())
1789 main_axis_offset += IsHorizontalFlow() ? VerticalScrollbarWidth() 1792 main_axis_offset += IsHorizontalFlow() ? VerticalScrollbarWidth()
1790 : HorizontalScrollbarHeight(); 1793 : HorizontalScrollbarHeight();
1791 1794
1792 LayoutUnit total_main_extent = MainAxisExtent(); 1795 LayoutUnit total_main_extent = MainAxisExtent();
1793 if (!ShouldPlaceBlockDirectionScrollbarOnLogicalLeft()) 1796 if (!ShouldPlaceBlockDirectionScrollbarOnLogicalLeft())
1794 total_main_extent -= IsHorizontalFlow() ? VerticalScrollbarWidth() 1797 total_main_extent -= IsHorizontalFlow() ? VerticalScrollbarWidth()
1795 : HorizontalScrollbarHeight(); 1798 : HorizontalScrollbarHeight();
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1873 LayoutPoint child_location( 1876 LayoutPoint child_location(
1874 should_flip_main_axis 1877 should_flip_main_axis
1875 ? total_main_extent - main_axis_offset - child_main_extent 1878 ? total_main_extent - main_axis_offset - child_main_extent
1876 : main_axis_offset, 1879 : main_axis_offset,
1877 cross_axis_offset + FlowAwareMarginBeforeForChild(*child)); 1880 cross_axis_offset + FlowAwareMarginBeforeForChild(*child));
1878 SetFlowAwareLocationForChild(*child, child_location); 1881 SetFlowAwareLocationForChild(*child, child_location);
1879 main_axis_offset += child_main_extent + FlowAwareMarginEndForChild(*child); 1882 main_axis_offset += child_main_extent + FlowAwareMarginEndForChild(*child);
1880 1883
1881 if (i != children.size() - 1) { 1884 if (i != children.size() - 1) {
1882 // The last item does not get extra space added. 1885 // The last item does not get extra space added.
1883 main_axis_offset += JustifyContentSpaceBetweenChildren( 1886 main_axis_offset += ContentDistributionSpaceBetweenChildren(
1884 available_free_space, distribution, children.size()); 1887 false, available_free_space, distribution, children.size());
jfernandez 2017/04/21 20:43:21 Ditto.
1885 } 1888 }
1886 1889
1887 if (is_paginated) 1890 if (is_paginated)
1888 UpdateFragmentationInfoForChild(*child); 1891 UpdateFragmentationInfoForChild(*child);
1889 } 1892 }
1890 1893
1891 if (IsColumnFlow()) 1894 if (IsColumnFlow())
1892 SetLogicalHeight(std::max( 1895 SetLogicalHeight(std::max(
1893 LogicalHeight(), main_axis_offset + FlowAwareBorderEnd() + 1896 LogicalHeight(), main_axis_offset + FlowAwareBorderEnd() +
1894 FlowAwarePaddingEnd() + ScrollbarLogicalHeight())); 1897 FlowAwarePaddingEnd() + ScrollbarLogicalHeight()));
(...skipping 21 matching lines...) Expand all
1916 ContentAlignmentNormalBehavior()); 1919 ContentAlignmentNormalBehavior());
1917 ContentDistributionType distribution = 1920 ContentDistributionType distribution =
1918 StyleRef().ResolvedJustifyContentDistribution( 1921 StyleRef().ResolvedJustifyContentDistribution(
1919 ContentAlignmentNormalBehavior()); 1922 ContentAlignmentNormalBehavior());
1920 1923
1921 // This is similar to the logic in layoutAndPlaceChildren, except we place 1924 // This is similar to the logic in layoutAndPlaceChildren, except we place
1922 // the children starting from the end of the flexbox. We also don't need to 1925 // the children starting from the end of the flexbox. We also don't need to
1923 // layout anything since we're just moving the children to a new position. 1926 // layout anything since we're just moving the children to a new position.
1924 LayoutUnit main_axis_offset = 1927 LayoutUnit main_axis_offset =
1925 LogicalHeight() - FlowAwareBorderEnd() - FlowAwarePaddingEnd(); 1928 LogicalHeight() - FlowAwareBorderEnd() - FlowAwarePaddingEnd();
1926 main_axis_offset -= InitialJustifyContentOffset( 1929 main_axis_offset -= InitialContentPositionOffset(
1927 available_free_space, position, distribution, children.size()); 1930 available_free_space, position, distribution, children.size());
1928 main_axis_offset -= IsHorizontalFlow() ? VerticalScrollbarWidth() 1931 main_axis_offset -= IsHorizontalFlow() ? VerticalScrollbarWidth()
1929 : HorizontalScrollbarHeight(); 1932 : HorizontalScrollbarHeight();
1930 1933
1931 for (size_t i = 0; i < children.size(); ++i) { 1934 for (size_t i = 0; i < children.size(); ++i) {
1932 LayoutBox* child = children[i].box; 1935 LayoutBox* child = children[i].box;
1933 1936
1934 DCHECK(!child->IsOutOfFlowPositioned()); 1937 DCHECK(!child->IsOutOfFlowPositioned());
1935 1938
1936 main_axis_offset -= 1939 main_axis_offset -=
1937 MainAxisExtentForChild(*child) + FlowAwareMarginEndForChild(*child); 1940 MainAxisExtentForChild(*child) + FlowAwareMarginEndForChild(*child);
1938 1941
1939 SetFlowAwareLocationForChild( 1942 SetFlowAwareLocationForChild(
1940 *child, 1943 *child,
1941 LayoutPoint(main_axis_offset, 1944 LayoutPoint(main_axis_offset,
1942 cross_axis_offset + FlowAwareMarginBeforeForChild(*child))); 1945 cross_axis_offset + FlowAwareMarginBeforeForChild(*child)));
1943 1946
1944 main_axis_offset -= FlowAwareMarginStartForChild(*child); 1947 main_axis_offset -= FlowAwareMarginStartForChild(*child);
1945 1948
1946 main_axis_offset -= JustifyContentSpaceBetweenChildren( 1949 main_axis_offset -= ContentDistributionSpaceBetweenChildren(
1947 available_free_space, distribution, children.size()); 1950 false, available_free_space, distribution, children.size());
jfernandez 2017/04/21 20:43:21 Ditto.
1948 } 1951 }
1949 } 1952 }
1950 1953
1951 static LayoutUnit InitialAlignContentOffset(
1952 LayoutUnit available_free_space,
1953 ContentPosition align_content,
1954 ContentDistributionType align_content_distribution,
1955 unsigned number_of_lines) {
1956 if (number_of_lines <= 1)
1957 return LayoutUnit();
1958 if (align_content == kContentPositionFlexEnd)
1959 return available_free_space;
1960 if (align_content == kContentPositionCenter)
1961 return available_free_space / 2;
1962 if (align_content_distribution == kContentDistributionSpaceAround) {
1963 if (available_free_space > 0 && number_of_lines)
1964 return available_free_space / (2 * number_of_lines);
1965 if (available_free_space < 0)
1966 return available_free_space / 2;
1967 }
1968 return LayoutUnit();
1969 }
1970
1971 static LayoutUnit AlignContentSpaceBetweenChildren(
1972 LayoutUnit available_free_space,
1973 ContentDistributionType align_content_distribution,
1974 unsigned number_of_lines) {
1975 if (available_free_space > 0 && number_of_lines > 1) {
1976 if (align_content_distribution == kContentDistributionSpaceBetween)
1977 return available_free_space / (number_of_lines - 1);
1978 if (align_content_distribution == kContentDistributionSpaceAround ||
1979 align_content_distribution == kContentDistributionStretch)
1980 return available_free_space / number_of_lines;
1981 }
1982 return LayoutUnit();
1983 }
1984
1985 void LayoutFlexibleBox::AlignFlexLines(Vector<LineContext>& line_contexts) { 1954 void LayoutFlexibleBox::AlignFlexLines(Vector<LineContext>& line_contexts) {
1986 ContentPosition position = 1955 ContentPosition position =
1987 StyleRef().ResolvedAlignContentPosition(ContentAlignmentNormalBehavior()); 1956 StyleRef().ResolvedAlignContentPosition(ContentAlignmentNormalBehavior());
1988 ContentDistributionType distribution = 1957 ContentDistributionType distribution =
1989 StyleRef().ResolvedAlignContentDistribution( 1958 StyleRef().ResolvedAlignContentDistribution(
1990 ContentAlignmentNormalBehavior()); 1959 ContentAlignmentNormalBehavior());
1991 1960
1992 // If we have a single line flexbox or a multiline line flexbox with only one 1961 // If we have a single line flexbox or a multiline line flexbox with only one
1993 // flex line, the line height is all the available space. For 1962 // flex line, the line height is all the available space. For
1994 // flex-direction: row, this means we need to use the height, so we do this 1963 // flex-direction: row, this means we need to use the height, so we do this
1995 // after calling updateLogicalHeight. 1964 // after calling updateLogicalHeight.
1996 if (line_contexts.size() == 1) { 1965 if (line_contexts.size() == 1) {
1997 line_contexts[0].cross_axis_extent = CrossAxisContentExtent(); 1966 line_contexts[0].cross_axis_extent = CrossAxisContentExtent();
1998 return; 1967 return;
1999 } 1968 }
2000 1969
2001 if (position == kContentPositionFlexStart) 1970 if (position == kContentPositionFlexStart)
2002 return; 1971 return;
2003 1972
2004 LayoutUnit available_cross_axis_space = CrossAxisContentExtent(); 1973 LayoutUnit available_cross_axis_space = CrossAxisContentExtent();
2005 for (size_t i = 0; i < line_contexts.size(); ++i) 1974 for (size_t i = 0; i < line_contexts.size(); ++i)
2006 available_cross_axis_space -= line_contexts[i].cross_axis_extent; 1975 available_cross_axis_space -= line_contexts[i].cross_axis_extent;
2007 1976
2008 LayoutUnit line_offset = InitialAlignContentOffset( 1977 LayoutUnit line_offset;
2009 available_cross_axis_space, position, distribution, line_contexts.size()); 1978 if (line_contexts.size() > 1) {
1979 line_offset =
1980 InitialContentPositionOffset(available_cross_axis_space, position,
1981 distribution, line_contexts.size());
1982 }
2010 for (unsigned line_number = 0; line_number < line_contexts.size(); 1983 for (unsigned line_number = 0; line_number < line_contexts.size();
2011 ++line_number) { 1984 ++line_number) {
2012 LineContext& line_context = line_contexts[line_number]; 1985 LineContext& line_context = line_contexts[line_number];
2013 line_context.cross_axis_offset += line_offset; 1986 line_context.cross_axis_offset += line_offset;
2014 for (size_t child_number = 0; child_number < line_context.flex_items.size(); 1987 for (size_t child_number = 0; child_number < line_context.flex_items.size();
2015 ++child_number) { 1988 ++child_number) {
2016 FlexItem& flex_item = line_context.flex_items[child_number]; 1989 FlexItem& flex_item = line_context.flex_items[child_number];
2017 AdjustAlignmentForChild(*flex_item.box, line_offset); 1990 AdjustAlignmentForChild(*flex_item.box, line_offset);
2018 } 1991 }
2019 1992
2020 if (distribution == kContentDistributionStretch && 1993 if (distribution == kContentDistributionStretch &&
2021 available_cross_axis_space > 0) 1994 available_cross_axis_space > 0)
2022 line_contexts[line_number].cross_axis_extent += 1995 line_contexts[line_number].cross_axis_extent +=
2023 available_cross_axis_space / 1996 available_cross_axis_space /
2024 static_cast<unsigned>(line_contexts.size()); 1997 static_cast<unsigned>(line_contexts.size());
2025 1998
2026 line_offset += AlignContentSpaceBetweenChildren( 1999 line_offset += ContentDistributionSpaceBetweenChildren(
2027 available_cross_axis_space, distribution, line_contexts.size()); 2000 true, available_cross_axis_space, distribution, line_contexts.size());
jfernandez 2017/04/21 20:43:21 Ditto.
2028 } 2001 }
2029 } 2002 }
2030 2003
2031 void LayoutFlexibleBox::AdjustAlignmentForChild(LayoutBox& child, 2004 void LayoutFlexibleBox::AdjustAlignmentForChild(LayoutBox& child,
2032 LayoutUnit delta) { 2005 LayoutUnit delta) {
2033 DCHECK(!child.IsOutOfFlowPositioned()); 2006 DCHECK(!child.IsOutOfFlowPositioned());
2034 2007
2035 SetFlowAwareLocationForChild(child, FlowAwareLocationForChild(child) + 2008 SetFlowAwareLocationForChild(child, FlowAwareLocationForChild(child) +
2036 LayoutSize(LayoutUnit(), delta)); 2009 LayoutSize(LayoutUnit(), delta));
2037 } 2010 }
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
2202 LayoutUnit original_offset = 2175 LayoutUnit original_offset =
2203 line_contexts[line_number].cross_axis_offset - cross_axis_start_edge; 2176 line_contexts[line_number].cross_axis_offset - cross_axis_start_edge;
2204 LayoutUnit new_offset = 2177 LayoutUnit new_offset =
2205 content_extent - original_offset - line_cross_axis_extent; 2178 content_extent - original_offset - line_cross_axis_extent;
2206 AdjustAlignmentForChild(*flex_item.box, new_offset - original_offset); 2179 AdjustAlignmentForChild(*flex_item.box, new_offset - original_offset);
2207 } 2180 }
2208 } 2181 }
2209 } 2182 }
2210 2183
2211 } // namespace blink 2184 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698