559c780176bdc635a48613725bed3262b0714e2e
[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 #pragma once
28
29 #if ENABLE(CSS_GRID_LAYOUT)
30
31 #include "Grid.h"
32 #include "RenderBlock.h"
33
34 namespace WebCore {
35
36 class GridArea;
37 class GridSpan;
38 class GridTrack;
39 class GridItemWithSpan;
40
41 struct ContentAlignmentData;
42
43 enum GridAxisPosition {GridAxisStart, GridAxisEnd, GridAxisCenter};
44
45 enum TrackSizeComputationPhase {
46     ResolveIntrinsicMinimums,
47     ResolveContentBasedMinimums,
48     ResolveMaxContentMinimums,
49     ResolveIntrinsicMaximums,
50     ResolveMaxContentMaximums,
51     MaximizeTracks,
52 };
53
54 class RenderGrid final : public RenderBlock {
55 public:
56     RenderGrid(Element&, RenderStyle&&);
57     virtual ~RenderGrid();
58
59     Element& element() const { return downcast<Element>(nodeForNonAnonymous()); }
60
61     void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
62     void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0) override;
63
64     bool avoidsFloats() const override { return true; }
65     bool canDropAnonymousBlockChild() const override { return false; }
66
67     void dirtyGrid();
68     Vector<LayoutUnit> trackSizesForComputedStyle(GridTrackSizingDirection) const;
69
70     const Vector<LayoutUnit>& columnPositions() const { return m_columnPositions; }
71     const Vector<LayoutUnit>& rowPositions() const { return m_rowPositions; }
72
73     unsigned autoRepeatCountForDirection(GridTrackSizingDirection direction) const { return m_grid.autoRepeatTracks(direction); }
74
75 private:
76     const char* renderName() const override;
77     bool isRenderGrid() const override { return true; }
78     void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const override;
79
80     void addChild(RenderObject* newChild, RenderObject* beforeChild) final;
81     void removeChild(RenderObject&) final;
82
83     bool explicitGridDidResize(const RenderStyle&) const;
84     bool namedGridLinesDefinitionDidChange(const RenderStyle&) const;
85
86     std::optional<LayoutUnit> computeIntrinsicLogicalContentHeightUsing(Length logicalHeightLength, std::optional<LayoutUnit> intrinsicContentHeight, LayoutUnit borderAndPadding) const override;
87
88     class GridSizingData;
89     enum SizingOperation { TrackSizing, IntrinsicSizeComputation };
90     void computeUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&, LayoutUnit& baseSizesWithoutMaximization, LayoutUnit& growthLimitsWithoutMaximization) const;
91     void computeFlexSizedTracksGrowth(GridTrackSizingDirection, const GridSizingData&, Vector<GridTrack>&, const Vector<unsigned>& flexibleSizedTracksIndex, double flexFraction, Vector<LayoutUnit>& increments, LayoutUnit& totalGrowth) const;
92     LayoutUnit computeUsedBreadthOfMinLength(const GridTrackSize&, LayoutUnit maxSize) const;
93     LayoutUnit computeUsedBreadthOfMaxLength(const GridTrackSize&, LayoutUnit usedBreadth, LayoutUnit maxSize) const;
94     void resolveContentBasedTrackSizingFunctions(GridTrackSizingDirection, GridSizingData&) const;
95
96     unsigned computeAutoRepeatTracksCount(GridTrackSizingDirection, SizingOperation) const;
97
98     std::unique_ptr<OrderedTrackIndexSet> computeEmptyTracksForAutoRepeat(Grid&, GridTrackSizingDirection) const;
99
100     void placeItemsOnGrid(Grid&, SizingOperation) const;
101     void populateExplicitGridAndOrderIterator(Grid&) const;
102     std::unique_ptr<GridArea> createEmptyGridAreaAtSpecifiedPositionsOutsideGrid(Grid&, const RenderBox&, GridTrackSizingDirection, const GridSpan&) const;
103     void placeSpecifiedMajorAxisItemsOnGrid(Grid&, const Vector<RenderBox*>&) const;
104     void placeAutoMajorAxisItemsOnGrid(Grid&, const Vector<RenderBox*>&) const;
105     typedef std::pair<unsigned, unsigned> AutoPlacementCursor;
106     void placeAutoMajorAxisItemOnGrid(Grid&, RenderBox&, AutoPlacementCursor&) const;
107     GridTrackSizingDirection autoPlacementMajorAxisDirection() const;
108     GridTrackSizingDirection autoPlacementMinorAxisDirection() const;
109
110     bool canPerformSimplifiedLayout() const final;
111     void prepareChildForPositionedLayout(RenderBox&);
112     void layoutPositionedObject(RenderBox&, bool relayoutChildren, bool fixedPositionObjectsOnly) override;
113     void offsetAndBreadthForPositionedChild(const RenderBox&, GridTrackSizingDirection, LayoutUnit& offset, LayoutUnit& breadth);
114
115     void computeIntrinsicLogicalHeight(GridSizingData&);
116     LayoutUnit computeTrackBasedLogicalHeight(const GridSizingData&) const;
117     void computeTrackSizesForDirection(GridTrackSizingDirection, GridSizingData&, LayoutUnit freeSpace);
118
119     void repeatTracksSizingIfNeeded(GridSizingData&, LayoutUnit availableSpaceForColumns, LayoutUnit availableSpaceForRows);
120
121     void layoutGridItems(GridSizingData&);
122     void populateGridPositionsForDirection(GridSizingData&, GridTrackSizingDirection);
123
124     static bool shouldProcessTrackForTrackSizeComputationPhase(TrackSizeComputationPhase, const GridTrackSize&);
125     static bool trackShouldGrowBeyondGrowthLimitsForTrackSizeComputationPhase(TrackSizeComputationPhase, const GridTrackSize&);
126     static void markAsInfinitelyGrowableForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&);
127     static void updateTrackSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&);
128     LayoutUnit currentItemSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
129
130     typedef struct GridItemsSpanGroupRange GridItemsSpanGroupRange;
131     void resolveContentBasedTrackSizingFunctionsForNonSpanningItems(GridTrackSizingDirection, const GridSpan&, RenderBox& gridItem, GridTrack&, GridSizingData&) const;
132     template <TrackSizeComputationPhase> void resolveContentBasedTrackSizingFunctionsForItems(GridTrackSizingDirection, GridSizingData&, const GridItemsSpanGroupRange&) const;
133     template <TrackSizeComputationPhase> void distributeSpaceToTracks(Vector<GridTrack*>&, Vector<GridTrack*>* growBeyondGrowthLimitsTracks, LayoutUnit& availableLogicalSpace) const;
134
135     typedef HashSet<unsigned, DefaultHash<unsigned>::Hash, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> TrackIndexSet;
136     double computeFlexFactorUnitSize(const Vector<GridTrack>&, GridTrackSizingDirection, const GridSizingData&, double flexFactorSum, LayoutUnit leftOverSpace, const Vector<unsigned, 8>& flexibleTracksIndexes, std::unique_ptr<TrackIndexSet> tracksToTreatAsInflexible = nullptr) const;
137     double findFlexFactorUnitSize(const Vector<GridTrack>&, const GridSpan&, GridTrackSizingDirection, LayoutUnit spaceToFill, const GridSizingData&) const;
138
139     const GridTrackSize& rawGridTrackSize(GridTrackSizingDirection, unsigned, const GridSizingData&) const;
140     GridTrackSize gridTrackSize(GridTrackSizingDirection, unsigned, const GridSizingData&) const;
141
142     bool updateOverrideContainingBlockContentSizeForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
143     LayoutUnit logicalHeightForChild(RenderBox&) const;
144     LayoutUnit minSizeForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
145     LayoutUnit minContentForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
146     LayoutUnit maxContentForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&) const;
147     GridAxisPosition columnAxisPositionForChild(const RenderBox&) const;
148     GridAxisPosition rowAxisPositionForChild(const RenderBox&) const;
149     LayoutUnit columnAxisOffsetForChild(const RenderBox&, const GridSizingData&) const;
150     LayoutUnit rowAxisOffsetForChild(const RenderBox&, const GridSizingData&) const;
151     ContentAlignmentData computeContentPositionAndDistributionOffset(GridTrackSizingDirection, const LayoutUnit& availableFreeSpace, unsigned numberOfGridTracks) const;
152     LayoutPoint findChildLogicalPosition(const RenderBox&, const GridSizingData&) const;
153     GridArea cachedGridArea(const RenderBox&) const;
154     GridSpan cachedGridSpan(const RenderBox&, GridTrackSizingDirection) const;
155
156
157     LayoutUnit gridAreaBreadthForChild(const RenderBox& child, GridTrackSizingDirection, const GridSizingData&) const;
158     LayoutUnit gridAreaBreadthForChildIncludingAlignmentOffsets(const RenderBox&, GridTrackSizingDirection, const GridSizingData&) const;
159     LayoutUnit assumedRowsSizeForOrthogonalChild(const RenderBox&, const GridSizingData&) const;
160
161     void applyStretchAlignmentToTracksIfNeeded(GridTrackSizingDirection, GridSizingData&);
162
163     void paintChildren(PaintInfo& forSelf, const LayoutPoint& paintOffset, PaintInfo& forChild, bool usePrintRect) override;
164     bool needToStretchChildLogicalHeight(const RenderBox&) const;
165     LayoutUnit marginLogicalHeightForChild(const RenderBox&) const;
166     LayoutUnit computeMarginLogicalSizeForChild(GridTrackSizingDirection, const RenderBox&) const;
167     LayoutUnit availableAlignmentSpaceForChildBeforeStretching(LayoutUnit gridAreaBreadthForChild, const RenderBox&) const;
168     StyleSelfAlignmentData justifySelfForChild(const RenderBox&) const;
169     StyleSelfAlignmentData alignSelfForChild(const RenderBox&) const;
170     void applyStretchAlignmentToChildIfNeeded(RenderBox&);
171     bool hasAutoSizeInColumnAxis(const RenderBox& child) const { return isHorizontalWritingMode() ? child.style().height().isAuto() : child.style().width().isAuto(); }
172     bool hasAutoSizeInRowAxis(const RenderBox& child) const { return isHorizontalWritingMode() ? child.style().width().isAuto() : child.style().height().isAuto(); }
173     bool allowedToStretchChildAlongColumnAxis(const RenderBox& child) const { return alignSelfForChild(child).position() == ItemPositionStretch && hasAutoSizeInColumnAxis(child) && !hasAutoMarginsInColumnAxis(child); }
174     bool allowedToStretchChildAlongRowAxis(const RenderBox& child) const { return justifySelfForChild(child).position() == ItemPositionStretch && hasAutoSizeInRowAxis(child) && !hasAutoMarginsInRowAxis(child); }
175     bool hasAutoMarginsInColumnAxis(const RenderBox&) const;
176     bool hasAutoMarginsInRowAxis(const RenderBox&) const;
177     void resetAutoMarginsAndLogicalTopInColumnAxis(RenderBox& child);
178     void updateAutoMarginsInColumnAxisIfNeeded(RenderBox&);
179     void updateAutoMarginsInRowAxisIfNeeded(RenderBox&);
180
181     int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const final;
182     std::optional<int> firstLineBaseline() const final;
183     std::optional<int> inlineBlockBaseline(LineDirectionMode) const final;
184     bool isInlineBaselineAlignedChild(const RenderBox&) const;
185
186 #ifndef NDEBUG
187     bool tracksAreWiderThanMinTrackBreadth(GridTrackSizingDirection, GridSizingData&);
188 #endif
189
190     LayoutUnit gridGapForDirection(GridTrackSizingDirection) const;
191     LayoutUnit guttersSize(const Grid&, GridTrackSizingDirection, unsigned startLine, unsigned span) const;
192
193     bool spanningItemCrossesFlexibleSizedTracks(const GridSpan&, GridTrackSizingDirection, const GridSizingData&) const;
194
195     unsigned numTracks(GridTrackSizingDirection, const Grid&) const;
196
197     LayoutUnit translateRTLCoordinate(LayoutUnit) const;
198
199     bool isOrthogonalChild(const RenderBox&) const;
200     GridTrackSizingDirection flowAwareDirectionForChild(const RenderBox&, GridTrackSizingDirection) const;
201
202     Grid m_grid;
203
204     Vector<LayoutUnit> m_columnPositions;
205     Vector<LayoutUnit> m_rowPositions;
206     LayoutUnit m_offsetBetweenColumns;
207     LayoutUnit m_offsetBetweenRows;
208
209     std::optional<LayoutUnit> m_minContentHeight;
210     std::optional<LayoutUnit> m_maxContentHeight;
211 };
212
213 } // namespace WebCore
214
215 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderGrid, isRenderGrid())
216
217 #endif // ENABLE(CSS_GRID_LAYOUT)