1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
/*
* Copyright (C) 2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "GridMasonryLayout.h"
#include "GridLayoutFunctions.h"
#include "RenderBoxInlines.h"
#include "RenderGrid.h"
#include "RenderStyleInlines.h"
#include "StyleGridPositionsResolver.h"
#include "StyleItemTolerance.h"
#include "StylePrimitiveNumericTypes+Evaluation.h"
#include "WritingMode.h"
namespace WebCore {
void GridMasonryLayout::initializeMasonry(unsigned gridAxisTracks, Style::GridTrackSizingDirection masonryAxisDirection)
{
// Reset global variables as they may contain state from previous runs of Masonry.
m_masonryAxisDirection = masonryAxisDirection;
m_masonryAxisGridGap = m_renderGrid->gridGap(m_masonryAxisDirection);
m_gridAxisTracksCount = gridAxisTracks;
m_gridContentSize = 0;
m_renderGrid->currentGrid().setupGridForMasonryLayout();
m_renderGrid->populateExplicitGridAndOrderIterator();
resizeAndResetRunningPositions();
}
void GridMasonryLayout::performMasonryPlacement(const GridTrackSizingAlgorithm& algorithm, unsigned gridAxisTracks, Style::GridTrackSizingDirection masonryAxisDirection, GridMasonryLayout::MasonryLayoutPhase layoutPhase)
{
initializeMasonry(gridAxisTracks, masonryAxisDirection);
m_renderGrid->populateGridPositionsForDirection(algorithm, Style::GridTrackSizingDirection::Columns);
m_renderGrid->populateGridPositionsForDirection(algorithm, Style::GridTrackSizingDirection::Rows);
// 2.3 Masonry Layout Algorithm
// https://drafts.csswg.org/css-grid-3/#masonry-layout-algorithm
// the insertIntoGridAndLayoutItem() will modify the m_autoFlowNextCursor, so m_autoFlowNextCursor needs to be reset.
m_autoFlowNextCursor = 0;
placeMasonryItems(algorithm, layoutPhase);
}
void GridMasonryLayout::resizeAndResetRunningPositions()
{
m_runningPositions.fill(LayoutUnit(), m_gridAxisTracksCount);
}
void GridMasonryLayout::placeMasonryItems(const GridTrackSizingAlgorithm& algorithm, GridMasonryLayout::MasonryLayoutPhase layoutPhase)
{
if (!m_gridAxisTracksCount)
return;
auto& grid = m_renderGrid->currentGrid();
for (CheckedPtr gridItem = grid.orderIterator().first(); gridItem; gridItem = grid.orderIterator().next()) {
if (grid.orderIterator().shouldSkipChild(*gridItem))
continue;
auto gridArea = hasDefiniteGridAxisPosition(*gridItem, gridAxisDirection()) ? gridAreaForDefiniteGridAxisItem(*gridItem) : gridAreaForIndefiniteGridAxisItem(*gridItem);
insertIntoGridAndLayoutItem(algorithm, *gridItem, gridArea, layoutPhase);
}
}
GridArea GridMasonryLayout::gridAreaForDefiniteGridAxisItem(const RenderBox& gridItem) const
{
auto itemSpan = m_renderGrid->currentGrid().gridItemSpan(gridItem, gridAxisDirection());
ASSERT(!itemSpan.isIndefinite());
itemSpan.translate(m_renderGrid->currentGrid().explicitGridStart(gridAxisDirection()));
return masonryGridAreaFromGridAxisSpan(itemSpan);
}
LayoutUnit GridMasonryLayout::calculateMasonryIntrinsicLogicalWidth(RenderBox& gridItem, GridMasonryLayout::MasonryLayoutPhase layoutPhase)
{
switch (layoutPhase) {
case MasonryLayoutPhase::MinContentPhase:
return gridItem.computeIntrinsicLogicalWidthUsing(CSS::Keyword::MinContent { }, { }, gridItem.borderAndPaddingLogicalWidth());
case MasonryLayoutPhase::MaxContentPhase:
return gridItem.computeIntrinsicLogicalWidthUsing(CSS::Keyword::MaxContent { }, { }, gridItem.borderAndPaddingLogicalWidth());
case MasonryLayoutPhase::LayoutPhase:
ASSERT_NOT_REACHED();
return { };
}
return { };
}
void GridMasonryLayout::setItemGridAxisContainingBlockToGridArea(const GridTrackSizingAlgorithm& algorithm, RenderBox& gridItem)
{
if (auto direction = gridAxisDirection(); direction == Style::GridTrackSizingDirection::Columns)
gridItem.setGridAreaContentLogicalWidth(algorithm.gridAreaBreadthForGridItem(gridItem, direction));
else
gridItem.setGridAreaContentLogicalHeight(algorithm.gridAreaBreadthForGridItem(gridItem, direction));
// FIXME(249230): Try to cache masonry layout sizes
gridItem.setChildNeedsLayout(MarkOnlyThis);
}
void GridMasonryLayout::insertIntoGridAndLayoutItem(const GridTrackSizingAlgorithm& algorithm, RenderBox& gridItem, const GridArea& area, GridMasonryLayout::MasonryLayoutPhase layoutPhase)
{
auto shouldOverrideLogicalWidth = [&](RenderBox& gridItem, GridMasonryLayout::MasonryLayoutPhase layoutPhase) {
if (layoutPhase == MasonryLayoutPhase::LayoutPhase)
return false;
if (!(gridItem.style().logicalWidth().isAuto() || gridItem.style().logicalWidth().isPercent()))
return false;
ASSERT(m_renderGrid->isMasonry(Style::GridTrackSizingDirection::Columns));
if (gridItem.style().writingMode().isOrthogonal(m_renderGrid->style().writingMode()))
return false;
if (auto* renderGrid = dynamicDowncast<RenderGrid>(gridItem); renderGrid && renderGrid->isSubgridRows())
return false;
return true;
};
if (shouldOverrideLogicalWidth(gridItem, layoutPhase))
gridItem.setOverridingBorderBoxLogicalWidth(calculateMasonryIntrinsicLogicalWidth(gridItem, layoutPhase));
m_renderGrid->currentGrid().insert(gridItem, area);
setItemGridAxisContainingBlockToGridArea(algorithm, gridItem);
gridItem.layoutIfNeeded();
updateRunningPositions(gridItem, area);
m_autoFlowNextCursor = gridAxisSpanFromArea(area).endLine() % m_gridAxisTracksCount;
}
LayoutUnit GridMasonryLayout::masonryAxisMarginBoxForItem(const RenderBox& gridItem)
{
LayoutUnit marginBoxSize;
if (m_masonryAxisDirection == Style::GridTrackSizingDirection::Rows) {
if (GridLayoutFunctions::isOrthogonalGridItem(m_renderGrid, gridItem))
marginBoxSize = gridItem.isHorizontalWritingMode() ? gridItem.width() + gridItem.horizontalMarginExtent() : gridItem.height() + gridItem.verticalMarginExtent();
else
marginBoxSize = gridItem.logicalHeight() + gridItem.marginLogicalHeight();
} else {
if (GridLayoutFunctions::isOrthogonalGridItem(m_renderGrid, gridItem))
marginBoxSize = gridItem.isHorizontalWritingMode() ? gridItem.height() + gridItem.verticalMarginExtent() : gridItem.width() + gridItem.horizontalMarginExtent();
else
marginBoxSize = gridItem.logicalWidth() + gridItem.marginLogicalWidth();
}
return marginBoxSize;
}
void GridMasonryLayout::updateRunningPositions(const RenderBox& gridItem, const GridArea& area)
{
auto gridAxisSpan = gridAxisSpanFromArea(area);
ASSERT(gridAxisSpan.startLine() < m_runningPositions.size() && gridAxisSpan.endLine() <= m_runningPositions.size());
gridAxisSpan.clamp(m_runningPositions.size());
LayoutUnit previousRunningPosition;
for (auto line : gridAxisSpan)
previousRunningPosition = std::max(previousRunningPosition, m_runningPositions[line]);
auto newRunningPosition = masonryAxisMarginBoxForItem(gridItem) + previousRunningPosition + m_masonryAxisGridGap;
m_gridContentSize = std::max(m_gridContentSize, newRunningPosition - m_masonryAxisGridGap);
for (auto span : gridAxisSpan)
m_runningPositions[span] = newRunningPosition;
updateItemOffset(gridItem, previousRunningPosition);
}
void GridMasonryLayout::updateItemOffset(const RenderBox& gridItem, LayoutUnit offset)
{
// We set() and not add() to update the value if the |gridItem| is already inserted
m_itemOffsets.set(gridItem, offset);
}
LayoutUnit GridMasonryLayout::maxRunningPositionForSpan(unsigned startLine, unsigned spanLength) const
{
LayoutUnit maxPosition;
for (unsigned lineOffset = 0; lineOffset < spanLength; lineOffset++)
maxPosition = std::max(maxPosition, m_runningPositions[startLine + lineOffset]);
return maxPosition;
}
GridArea GridMasonryLayout::gridAreaForIndefiniteGridAxisItem(const RenderBox& item)
{
auto itemSpanLength = Style::GridPositionsResolver::spanSizeForAutoPlacedItem(item, gridAxisDirection());
auto gridAxisLines = m_gridAxisTracksCount + 1;
// Get item-tolerance from the masonry container's style
const auto& tolerance = m_renderGrid->style().itemTolerance();
if (tolerance.isInfinite()) {
// Infinite tolerance: place items strictly in order without considering track lengths
// Use round-robin placement starting from the cursor position
auto startingLine = m_autoFlowNextCursor;
// If the item doesn't fit at the cursor position, wrap to the beginning
if (startingLine + itemSpanLength > m_gridAxisTracksCount)
startingLine = 0;
auto gridAxisPosition = GridSpan::translatedDefiniteGridSpan(startingLine, startingLine + itemSpanLength);
return masonryGridAreaFromGridAxisSpan(gridAxisPosition);
}
// For normal and length-percentage tolerances, find positions within tolerance of the shortest track
// Calculate tolerance value
auto contentBoxSize = gridAxisDirection() == Style::GridTrackSizingDirection::Columns
? m_renderGrid->contentBoxLogicalHeight()
: m_renderGrid->contentBoxLogicalWidth();
LayoutUnit toleranceValue = tolerance.switchOn(
[&](const CSS::Keyword::Normal&) -> LayoutUnit {
// Normal resolves to 1em
return LayoutUnit { m_renderGrid->checkedStyle()->computedFontSize() };
},
[&](const typename Style::ItemTolerance::Fixed& fixed) -> LayoutUnit {
return LayoutUnit { fixed.resolveZoom(m_renderGrid->style().usedZoomForLength()) };
},
[&](const typename Style::ItemTolerance::Percentage& percentage) -> LayoutUnit {
return Style::evaluate<LayoutUnit>(percentage, contentBoxSize);
},
[&](const typename Style::ItemTolerance::Calc& calc) -> LayoutUnit {
return Style::evaluate<LayoutUnit>(calc, contentBoxSize, m_renderGrid->style().usedZoomForLength());
},
[](const CSS::Keyword::Infinite&) -> LayoutUnit {
// This case shouldn't be reached due to the outer if check
ASSERT_NOT_REACHED();
return LayoutUnit { };
}
);
// Step 1: Find the absolute shortest position across all tracks
auto maxStartingLine = gridAxisLines - itemSpanLength;
LayoutUnit absoluteShortest = LayoutUnit::max();
for (unsigned i = 0; i < maxStartingLine; i++)
absoluteShortest = std::min(absoluteShortest, maxRunningPositionForSpan(i, itemSpanLength));
// Step 2: Find first position within tolerance of shortest, starting from the cursor position.
unsigned smallestMaxPosLine = 0;
auto autoFlowNextCursorShift = (m_autoFlowNextCursor > maxStartingLine) ? 0 : m_autoFlowNextCursor;
for (unsigned i = 0; i < maxStartingLine; i++) {
auto startingLine = (autoFlowNextCursorShift + i) % maxStartingLine;
auto maxPosForCurrentStartingLine = maxRunningPositionForSpan(startingLine, itemSpanLength);
// Accept first position within tolerance of the absolute shortest
if (maxPosForCurrentStartingLine <= absoluteShortest + toleranceValue) {
smallestMaxPosLine = startingLine;
break;
}
}
auto gridAxisPosition = GridSpan::translatedDefiniteGridSpan(smallestMaxPosLine, smallestMaxPosLine + itemSpanLength);
return masonryGridAreaFromGridAxisSpan(gridAxisPosition);
}
LayoutUnit GridMasonryLayout::offsetForGridItem(const RenderBox& gridItem) const
{
const auto& offsetIter = m_itemOffsets.find(gridItem);
if (offsetIter == m_itemOffsets.end())
return 0_lu;
return offsetIter->value;
}
inline Style::GridTrackSizingDirection GridMasonryLayout::gridAxisDirection() const
{
// The masonry axis and grid axis can never be the same.
// They are always perpendicular to each other.
return orthogonalDirection(m_masonryAxisDirection);
}
bool GridMasonryLayout::hasDefiniteGridAxisPosition(const RenderBox& gridItem, Style::GridTrackSizingDirection gridAxisDirection) const
{
return !Style::GridPositionsResolver::resolveGridPositionsFromStyle(m_renderGrid, gridItem, gridAxisDirection).isIndefinite();
}
GridSpan GridMasonryLayout::gridAxisSpanFromArea(const GridArea& gridArea) const
{
return gridArea.span(gridAxisDirection());
}
GridArea GridMasonryLayout::masonryGridAreaFromGridAxisSpan(const GridSpan& gridAxisSpan) const
{
return m_masonryAxisDirection == Style::GridTrackSizingDirection::Rows
? GridArea { m_masonryAxisSpan, gridAxisSpan }
: GridArea { gridAxisSpan, m_masonryAxisSpan };
}
} // end namespace WebCore
|