[css-grid] Handle min-content/max-content with orthogonal flows
[WebKit-https.git] / Source / WebCore / rendering / RenderGrid.h
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  * Copyright (C) 2013, 2014 Igalia S.L.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifndef RenderGrid_h
28 #define RenderGrid_h
29
30 #if ENABLE(CSS_GRID_LAYOUT)
31
32 #include "GridPositionsResolver.h"
33 #include "OrderIterator.h"
34 #include "RenderBlock.h"
35
36 namespace WebCore {
37
38 class GridArea;
39 class GridSpan;
40 class GridTrack;
41 class GridItemWithSpan;
42
43 struct ContentAlignmentData;
44
45 enum GridAxisPosition {GridAxisStart, GridAxisEnd, GridAxisCenter};
46
47 class RenderGrid final : public RenderBlock {
48 public:
49     RenderGrid(Element&, RenderStyle&&);
50     virtual ~RenderGrid();
51
52     Element& element() const { return downcast<Element>(nodeForNonAnonymous()); }
53
54     void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
55     void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0) override;
56
57     bool avoidsFloats() const override { return true; }
58     bool canDropAnonymousBlockChild() const override { return false; }
59
60     const Vector<LayoutUnit>& columnPositions() const { return m_columnPositions; }
61     const Vector<LayoutUnit>& rowPositions() const { return m_rowPositions; }
62
63     LayoutUnit guttersSize(GridTrackSizingDirection, unsigned span) const;
64     LayoutUnit offsetBetweenTracks(GridTrackSizingDirection) const;
65
66     size_t autoRepeatCountForDirection(GridTrackSizingDirection) const;
67
68 private:
69     const char* renderName() const override;
70     bool isRenderGrid() const override { return true; }
71     void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const override;
72
73     Optional<LayoutUnit> computeIntrinsicLogicalContentHeightUsing(Length logicalHeightLength, Optional<LayoutUnit> intrinsicContentHeight, LayoutUnit borderAndPadding) const override;
74
75     class GridIterator;
76     class GridSizingData;
77     enum SizingOperation { TrackSizing, IntrinsicSizeComputation };
78     void computeUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&, LayoutUnit& baseSizesWithoutMaximization, LayoutUnit& growthLimitsWithoutMaximization) const;
79     LayoutUnit computeUsedBreadthOfMinLength(const GridLength&, LayoutUnit maxSize) const;
80     LayoutUnit computeUsedBreadthOfMaxLength(const GridLength&, LayoutUnit usedBreadth, LayoutUnit maxSize) const;
81     void resolveContentBasedTrackSizingFunctions(GridTrackSizingDirection, GridSizingData&) const;
82
83     void ensureGridSize(unsigned maximumRowSize, unsigned maximumColumnSize);
84     void insertItemIntoGrid(RenderBox&, const GridArea&);
85
86     unsigned computeAutoRepeatTracksCount(GridTrackSizingDirection) const;
87
88     void placeItemsOnGrid();
89     void populateExplicitGridAndOrderIterator();
90     std::unique_ptr<GridArea> createEmptyGridAreaAtSpecifiedPositionsOutsideGrid(const RenderBox&, GridTrackSizingDirection, const GridSpan&) const;
91     void placeSpecifiedMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
92     void placeAutoMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
93     typedef std::pair<unsigned, unsigned> AutoPlacementCursor;
94     void placeAutoMajorAxisItemOnGrid(RenderBox&, AutoPlacementCursor&);
95     GridTrackSizingDirection autoPlacementMajorAxisDirection() const;
96     GridTrackSizingDirection autoPlacementMinorAxisDirection() const;
97
98     void prepareChildForPositionedLayout(RenderBox&);
99     void layoutPositionedObject(RenderBox&, bool relayoutChildren, bool fixedPositionObjectsOnly) override;
100     void offsetAndBreadthForPositionedChild(const RenderBox&, GridTrackSizingDirection, LayoutUnit& offset, LayoutUnit& breadth);
101
102     void computeIntrinsicLogicalHeight(GridSizingData&);
103     LayoutUnit computeTrackBasedLogicalHeight(const GridSizingData&) const;
104     void computeTrackSizesForDirection(GridTrackSizingDirection, GridSizingData&, LayoutUnit freeSpace);
105
106     void repeatTracksSizingIfNeeded(GridSizingData&, LayoutUnit availableSpaceForColumns, LayoutUnit availableSpaceForRows);
107
108     void layoutGridItems(GridSizingData&);
109     void populateGridPositionsForDirection(GridSizingData&, GridTrackSizingDirection);
110     void clearGrid();
111
112     enum TrackSizeRestriction {
113         AllowInfinity,
114         ForbidInfinity,
115     };
116     enum TrackSizeComputationPhase {
117         ResolveIntrinsicMinimums,
118         ResolveContentBasedMinimums,
119         ResolveMaxContentMinimums,
120         ResolveIntrinsicMaximums,
121         ResolveMaxContentMaximums,
122         MaximizeTracks,
123     };
124     static const LayoutUnit& trackSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&, TrackSizeRestriction);
125     static bool shouldProcessTrackForTrackSizeComputationPhase(TrackSizeComputationPhase, const GridTrackSize&);
126     static bool trackShouldGrowBeyondGrowthLimitsForTrackSizeComputationPhase(TrackSizeComputationPhase, const GridTrackSize&);
127     static void markAsInfinitelyGrowableForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&);
128     static void updateTrackSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&);
129     LayoutUnit currentItemSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
130
131     typedef struct GridItemsSpanGroupRange GridItemsSpanGroupRange;
132     void resolveContentBasedTrackSizingFunctionsForNonSpanningItems(GridTrackSizingDirection, const GridSpan&, RenderBox& gridItem, GridTrack&, GridSizingData&) const;
133     template <TrackSizeComputationPhase> void resolveContentBasedTrackSizingFunctionsForItems(GridTrackSizingDirection, GridSizingData&, const GridItemsSpanGroupRange&) const;
134     template <TrackSizeComputationPhase> void distributeSpaceToTracks(Vector<GridTrack*>&, const Vector<GridTrack*>* growBeyondGrowthLimitsTracks, LayoutUnit& availableLogicalSpace) const;
135
136     typedef HashSet<unsigned, DefaultHash<unsigned>::Hash, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> TrackIndexSet;
137     double computeFlexFactorUnitSize(const Vector<GridTrack>&, GridTrackSizingDirection, SizingOperation, double flexFactorSum, LayoutUnit leftOverSpace, const Vector<unsigned, 8>& flexibleTracksIndexes, std::unique_ptr<TrackIndexSet> tracksToTreatAsInflexible = nullptr) const;
138     double findFlexFactorUnitSize(const Vector<GridTrack>&, const GridSpan&, GridTrackSizingDirection, SizingOperation, LayoutUnit spaceToFill) const;
139
140     const GridTrackSize& rawGridTrackSize(GridTrackSizingDirection, unsigned) const;
141     GridTrackSize gridTrackSize(GridTrackSizingDirection, unsigned, SizingOperation) const;
142
143     bool updateOverrideContainingBlockContentSizeForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
144     LayoutUnit logicalHeightForChild(RenderBox&) const;
145     LayoutUnit minSizeForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
146     LayoutUnit minContentForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
147     LayoutUnit maxContentForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
148     GridAxisPosition columnAxisPositionForChild(const RenderBox&) const;
149     GridAxisPosition rowAxisPositionForChild(const RenderBox&) const;
150     LayoutUnit columnAxisOffsetForChild(const RenderBox&) const;
151     LayoutUnit rowAxisOffsetForChild(const RenderBox&) const;
152     ContentAlignmentData computeContentPositionAndDistributionOffset(GridTrackSizingDirection, const LayoutUnit& availableFreeSpace, unsigned numberOfGridTracks) const;
153     LayoutPoint findChildLogicalPosition(const RenderBox&) const;
154     GridArea cachedGridArea(const RenderBox&) const;
155     GridSpan cachedGridSpan(const RenderBox&, GridTrackSizingDirection) const;
156
157
158     LayoutUnit gridAreaBreadthForChild(const RenderBox& child, GridTrackSizingDirection, const GridSizingData&) const;
159     LayoutUnit gridAreaBreadthForChildIncludingAlignmentOffsets(const RenderBox&, GridTrackSizingDirection, const GridSizingData&) const;
160     LayoutUnit assumedRowsSizeForOrthogonalChild(const RenderBox&, SizingOperation) const;
161
162     void applyStretchAlignmentToTracksIfNeeded(GridTrackSizingDirection, GridSizingData&);
163
164     void paintChildren(PaintInfo& forSelf, const LayoutPoint& paintOffset, PaintInfo& forChild, bool usePrintRect) override;
165     bool needToStretchChildLogicalHeight(const RenderBox&) const;
166     LayoutUnit marginLogicalHeightForChild(const RenderBox&) const;
167     LayoutUnit computeMarginLogicalSizeForChild(GridTrackSizingDirection, const RenderBox&) const;
168     LayoutUnit availableAlignmentSpaceForChildBeforeStretching(LayoutUnit gridAreaBreadthForChild, const RenderBox&) const;
169     void applyStretchAlignmentToChildIfNeeded(RenderBox&);
170     bool hasAutoMarginsInColumnAxis(const RenderBox&) const;
171     bool hasAutoMarginsInRowAxis(const RenderBox&) const;
172     void resetAutoMarginsAndLogicalTopInColumnAxis(RenderBox& child);
173     void updateAutoMarginsInColumnAxisIfNeeded(RenderBox&);
174     void updateAutoMarginsInRowAxisIfNeeded(RenderBox&);
175
176 #ifndef NDEBUG
177     bool tracksAreWiderThanMinTrackBreadth(GridTrackSizingDirection, GridSizingData&);
178 #endif
179
180     bool spanningItemCrossesFlexibleSizedTracks(const GridSpan&, GridTrackSizingDirection, SizingOperation) const;
181
182     unsigned gridColumnCount() const;
183     unsigned gridRowCount() const;
184
185     LayoutUnit translateRTLCoordinate(LayoutUnit) const;
186
187     bool isOrthogonalChild(const RenderBox&) const;
188     GridTrackSizingDirection flowAwareDirectionForChild(const RenderBox&, GridTrackSizingDirection) const;
189
190     Vector<Vector<Vector<RenderBox*, 1>>> m_grid;
191     Vector<LayoutUnit> m_columnPositions;
192     Vector<LayoutUnit> m_rowPositions;
193     LayoutUnit m_offsetBetweenColumns;
194     LayoutUnit m_offsetBetweenRows;
195     HashMap<const RenderBox*, GridArea> m_gridItemArea;
196     OrderIterator m_orderIterator;
197
198     Optional<LayoutUnit> m_minContentHeight;
199     Optional<LayoutUnit> m_maxContentHeight;
200
201     int m_smallestColumnStart;
202     int m_smallestRowStart;
203
204     unsigned m_autoRepeatColumns { 0 };
205     unsigned m_autoRepeatRows { 0 };
206
207     bool m_hasAnyOrthogonalChild;
208
209     bool m_gridIsDirty { true };
210 };
211
212 size_t inline RenderGrid::autoRepeatCountForDirection(GridTrackSizingDirection direction) const
213 {
214     return direction == ForColumns ? m_autoRepeatColumns : m_autoRepeatRows;
215 }
216
217 } // namespace WebCore
218
219 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderGrid, isRenderGrid())
220
221 #endif /* ENABLE(CSS_GRID_LAYOUT) */
222
223 #endif // RenderGrid_h