Combine event and touch action regions into a single class
[WebKit-https.git] / Source / WebCore / rendering / GridTrackSizingAlgorithm.h
index 5bfc10d..75b101e 100644 (file)
@@ -25,6 +25,7 @@
 #pragma once
 
 #include "Grid.h"
+#include "GridBaselineAlignment.h"
 #include "GridTrackSize.h"
 #include "LayoutUnit.h"
 
@@ -47,12 +48,13 @@ class GridTrackSizingAlgorithmStrategy;
 
 class GridTrack {
 public:
-    GridTrack() { }
+    GridTrack() = default;
 
     const LayoutUnit& baseSize() const;
     void setBaseSize(LayoutUnit);
 
     const LayoutUnit& growthLimit() const;
+    bool growthLimitIsInfinite() const { return m_growthLimit == infinity; }
     void setGrowthLimit(LayoutUnit);
 
     bool infiniteGrowthPotential() const { return growthLimitIsInfinite() || m_infinitelyGrowable; }
@@ -68,11 +70,10 @@ public:
     bool infinitelyGrowable() const { return m_infinitelyGrowable; }
     void setInfinitelyGrowable(bool infinitelyGrowable) { m_infinitelyGrowable = infinitelyGrowable; }
 
-    void setGrowthLimitCap(std::optional<LayoutUnit>);
-    std::optional<LayoutUnit> growthLimitCap() const { return m_growthLimitCap; }
+    void setGrowthLimitCap(Optional<LayoutUnit>);
+    Optional<LayoutUnit> growthLimitCap() const { return m_growthLimitCap; }
 
 private:
-    bool growthLimitIsInfinite() const { return m_growthLimit == infinity; }
     bool isGrowthLimitBiggerThanBaseSize() const { return growthLimitIsInfinite() || m_growthLimit >= m_baseSize; }
 
     void ensureGrowthLimitIsBiggerThanBaseSize();
@@ -81,7 +82,7 @@ private:
     LayoutUnit m_growthLimit { 0 };
     LayoutUnit m_plannedSize { 0 };
     LayoutUnit m_tempSize { 0 };
-    std::optional<LayoutUnit> m_growthLimitCap;
+    Optional<LayoutUnit> m_growthLimitCap;
     bool m_infinitelyGrowable { false };
 };
 
@@ -96,35 +97,48 @@ public:
     {
     }
 
-    void setup(GridTrackSizingDirection, unsigned numTracks, SizingOperation, std::optional<LayoutUnit> availableSpace, std::optional<LayoutUnit> freeSpace);
+    void setup(GridTrackSizingDirection, unsigned numTracks, SizingOperation, Optional<LayoutUnit> availableSpace, Optional<LayoutUnit> freeSpace);
     void run();
     void reset();
 
     // Required by RenderGrid. Try to minimize the exposed surface.
     const Grid& grid() const { return m_grid; }
-    GridTrackSize gridTrackSize(GridTrackSizingDirection, unsigned translatedIndex) const;
+    // FIXME (jfernandez): We should remove any public getter for this attribute
+    // and encapsulate any access in the algorithm class.
+    Grid& mutableGrid() const { return m_grid; }
 
     LayoutUnit minContentSize() const { return m_minContentSize; };
     LayoutUnit maxContentSize() const { return m_maxContentSize; };
 
+    LayoutSize estimatedGridAreaBreadthForChild(const RenderBox&) const;
+    LayoutUnit baselineOffsetForChild(const RenderBox&, GridAxis) const;
+
+    void cacheBaselineAlignedItem(const RenderBox&, GridAxis);
+    void copyBaselineItemsCache(const GridTrackSizingAlgorithm&, GridAxis);
+    void clearBaselineItemsCache();
+
     Vector<GridTrack>& tracks(GridTrackSizingDirection direction) { return direction == ForColumns ? m_columns : m_rows; }
     const Vector<GridTrack>& tracks(GridTrackSizingDirection direction) const { return direction == ForColumns ? m_columns : m_rows; }
 
-    std::optional<LayoutUnit> freeSpace(GridTrackSizingDirection direction) const { return direction == ForColumns ? m_freeSpaceColumns : m_freeSpaceRows; }
-    void setFreeSpace(GridTrackSizingDirection, std::optional<LayoutUnit>);
+    Optional<LayoutUnit> freeSpace(GridTrackSizingDirection direction) const { return direction == ForColumns ? m_freeSpaceColumns : m_freeSpaceRows; }
+    void setFreeSpace(GridTrackSizingDirection, Optional<LayoutUnit>);
+
+    Optional<LayoutUnit> availableSpace(GridTrackSizingDirection direction) const { return direction == ForColumns ? m_availableSpaceColumns : m_availableSpaceRows; }
+    void setAvailableSpace(GridTrackSizingDirection, Optional<LayoutUnit>);
 
-    std::optional<LayoutUnit> availableSpace(GridTrackSizingDirection direction) const { return direction == ForColumns ? m_availableSpaceColumns : m_availableSpaceRows; }
-    void setAvailableSpace(GridTrackSizingDirection, std::optional<LayoutUnit>);
+    LayoutUnit computeTrackBasedSize() const;
+
+    bool hasAnyPercentSizedRowsIndefiniteHeight() const { return m_hasPercentSizedRowsIndefiniteHeight; }
 
 #ifndef NDEBUG
     bool tracksAreWiderThanMinTrackBreadth() const;
 #endif
 
 private:
-    std::optional<LayoutUnit> availableSpace() const { return availableSpace(m_direction); }
+    Optional<LayoutUnit> availableSpace() const;
+    bool isRelativeGridLengthAsAuto(const GridLength&, GridTrackSizingDirection) const;
+    GridTrackSize gridTrackSize(GridTrackSizingDirection, unsigned translatedIndex) const;
     const GridTrackSize& rawGridTrackSize(GridTrackSizingDirection, unsigned translatedIndex) const;
-    LayoutUnit assumedRowsSizeForOrthogonalChild(const RenderBox&) const;
-    LayoutUnit computeTrackBasedSize() const;
 
     // Helper methods for step 1. initializeTrackSizes().
     LayoutUnit initialBaseSize(const GridTrackSize&) const;
@@ -137,8 +151,15 @@ private:
     template <TrackSizeComputationPhase phase> void increaseSizesToAccommodateSpanningItems(const GridItemsSpanGroupRange& gridItemsWithSpan);
     LayoutUnit itemSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, RenderBox&) const;
     template <TrackSizeComputationPhase phase> void distributeSpaceToTracks(Vector<GridTrack*>& tracks, Vector<GridTrack*>* growBeyondGrowthLimitsTracks, LayoutUnit& availableLogicalSpace) const;
+    LayoutUnit estimatedGridAreaBreadthForChild(const RenderBox&, GridTrackSizingDirection) const;
     LayoutUnit gridAreaBreadthForChild(const RenderBox&, GridTrackSizingDirection) const;
 
+    void computeBaselineAlignmentContext();
+    void updateBaselineAlignmentContext(const RenderBox&, GridAxis);
+    bool canParticipateInBaselineAlignment(const RenderBox&, GridAxis) const;
+    bool participateInBaselineAlignment(const RenderBox&, GridAxis) const;
+
+    bool isIntrinsicSizedGridArea(const RenderBox&, GridAxis) const;
     void computeGridContainerIntrinsicSizes();
 
     // Helper methods for step 4. Strech flexible tracks.
@@ -152,19 +173,22 @@ private:
     // method at thise level.
     void initializeTrackSizes();
     void resolveIntrinsicTrackSizes();
-    void stretchFlexibleTracks(std::optional<LayoutUnit> freeSpace);
+    void stretchFlexibleTracks(Optional<LayoutUnit> freeSpace);
     void stretchAutoTracks();
 
     // State machine.
     void advanceNextState();
     bool isValidTransition() const;
 
+    // Data.
+    bool wasSetup() const { return !!m_strategy; }
     bool m_needsSetup { true };
-    std::optional<LayoutUnit> m_availableSpaceRows;
-    std::optional<LayoutUnit> m_availableSpaceColumns;
+    bool m_hasPercentSizedRowsIndefiniteHeight { false };
+    Optional<LayoutUnit> m_availableSpaceRows;
+    Optional<LayoutUnit> m_availableSpaceColumns;
 
-    std::optional<LayoutUnit> m_freeSpaceColumns;
-    std::optional<LayoutUnit> m_freeSpaceRows;
+    Optional<LayoutUnit> m_freeSpaceColumns;
+    Optional<LayoutUnit> m_freeSpaceRows;
 
     // We need to keep both alive in order to properly size grids with orthogonal
     // writing modes.
@@ -172,7 +196,7 @@ private:
     Vector<GridTrack> m_rows;
     Vector<unsigned> m_contentSizedTracksIndex;
     Vector<unsigned> m_flexibleSizedTracksIndex;
-    Vector<unsigned> m_autoSizedTracksIndex;
+    Vector<unsigned> m_autoSizedTracksForStretchIndex;
 
     GridTrackSizingDirection m_direction;
     SizingOperation m_sizingOperation;
@@ -198,6 +222,11 @@ private:
     };
     SizingState m_sizingState;
 
+    GridBaselineAlignment m_baselineAlignment;
+    typedef HashMap<const RenderBox*, bool> BaselineItemsCache;
+    BaselineItemsCache m_columnBaselineItemsMap;
+    BaselineItemsCache m_rowBaselineItemsMap;
+
     // This is a RAII class used to ensure that the track sizing algorithm is
     // executed as it is suppossed to be, i.e., first resolve columns and then
     // rows. Only if required a second iteration is run following the same order,
@@ -213,26 +242,30 @@ private:
 };
 
 class GridTrackSizingAlgorithmStrategy {
+    WTF_MAKE_FAST_ALLOCATED;
 public:
     LayoutUnit minContentForChild(RenderBox&) const;
     LayoutUnit maxContentForChild(RenderBox&) const;
     LayoutUnit minSizeForChild(RenderBox&) const;
 
-    virtual ~GridTrackSizingAlgorithmStrategy() { }
+    virtual ~GridTrackSizingAlgorithmStrategy() = default;
 
-    virtual void maximizeTracks(Vector<GridTrack>&, std::optional<LayoutUnit>& freeSpace) = 0;
-    virtual double findUsedFlexFraction(Vector<unsigned>& flexibleSizedTracksIndex, GridTrackSizingDirection, std::optional<LayoutUnit> initialFreeSpace) const = 0;
+    virtual void maximizeTracks(Vector<GridTrack>&, Optional<LayoutUnit>& freeSpace) = 0;
+    virtual double findUsedFlexFraction(Vector<unsigned>& flexibleSizedTracksIndex, GridTrackSizingDirection, Optional<LayoutUnit> initialFreeSpace) const = 0;
     virtual bool recomputeUsedFlexFractionIfNeeded(double& flexFraction, LayoutUnit& totalGrowth) const = 0;
+    virtual LayoutUnit freeSpaceForStretchAutoTracksStep() const = 0;
 
 protected:
     GridTrackSizingAlgorithmStrategy(GridTrackSizingAlgorithm& algorithm)
         : m_algorithm(algorithm) { }
 
-    virtual LayoutUnit minLogicalWidthForChild(RenderBox&, Length childMinSize, GridTrackSizingDirection) const = 0;
+    virtual LayoutUnit minLogicalWidthForChild(RenderBox&, Length childMinSize, LayoutUnit availableSize) const = 0;
     virtual void layoutGridItemForMinSizeComputation(RenderBox&, bool overrideSizeHasChanged) const = 0;
 
     LayoutUnit logicalHeightForChild(RenderBox&) const;
-    bool updateOverrideContainingBlockContentSizeForChild(RenderBox&, GridTrackSizingDirection) const;
+    bool updateOverrideContainingBlockContentSizeForChild(RenderBox&, GridTrackSizingDirection, Optional<LayoutUnit> = WTF::nullopt) const;
+
+    GridTrackSize gridTrackSize(GridTrackSizingDirection direction, size_t translatedIndex) const { return m_algorithm.gridTrackSize(direction, translatedIndex); }
 
     // GridTrackSizingAlgorithm accessors for subclasses.
     LayoutUnit computeTrackBasedSize() const { return m_algorithm.computeTrackBasedSize(); }
@@ -240,7 +273,7 @@ protected:
     double findFrUnitSize(const GridSpan& tracksSpan, LayoutUnit leftOverSpace) const { return m_algorithm.findFrUnitSize(tracksSpan, leftOverSpace); }
     void distributeSpaceToTracks(Vector<GridTrack*>& tracks, LayoutUnit& availableLogicalSpace) const { m_algorithm.distributeSpaceToTracks<MaximizeTracks>(tracks, nullptr, availableLogicalSpace); }
     const RenderGrid* renderGrid() const { return m_algorithm.m_renderGrid; }
-    std::optional<LayoutUnit> availableSpace() const { return m_algorithm.availableSpace(); }
+    Optional<LayoutUnit> availableSpace() const { return m_algorithm.availableSpace(); }
 
     GridTrackSizingAlgorithm& m_algorithm;
 };