Files
UXP-Fixed/dom/grid/GridLines.cpp
T
Gaming4JC 22851ce36d Issue #1233 - Part 1: Fix grid overflow and rendering issues by improving Layout CSS-Grid API
List of relevant patches applied:

1425599 part 1 - [css-grid] Change the track sizing algorithm for spanning items so that it accumulates individual item contributions to the plan by max() rather than incrementing the planned size directly.

Also, fix a bug when copying back the planned limits after updating it for the first span group. It should only copy back track sizes that were actaully spanned by those items, other content-sized tracks' limits should remain at "infinity".

1425599 part 2 - [css-grid] Factor out the min-sizing parts of the track sizing for spanned items to a templated method (idempotent change).

1425599 part 3 - [css-grid] Factor out most of the max-sizing parts of the track sizing for spanned items to a templated method (idempotent change).

1425599 part 4 - [css-grid] Factor out the starting base/limit size to a templated method (idempotent change).

1425599 part 5 - [css-grid] Make CollectGrowable a templated method so that it works with either base/limit sizes (idempotent change).

1425599 part 6 - [css-grid] Make the size distribution methods templated with the intent of merging them in a later patch (idempotent change).

This patch also introduces an eInfinitelyGrowable bit to help get rid of the 'limits' temporary track sizes in the next patch.

1425599 part 7 - [css-grid] Remove the 'limits' copy of track sizes since they are no longer needed (idempotent change).

1425599 part 8 - [css-grid] Factor out the fit-content clamping function from DistributeToTrackLimits and pass it as a param instead (idempotent change).

1425599 part 9 - [css-grid] Merge DistributeToTrackLimits/Bases (idempotent change).

1425599 part 10 - [css-grid] Make MarkExcludedTracks a static method since it doesn't use 'this' (idempotent change).

1425599 part 11 - [css-grid] Hoist the marking of excluded tracks out from GrowSelectedTracksUnlimited to a separate method (idempotent change).

1425599 part 12 - [css-grid] Merge CopyPlanToBase/Limits into a templated method instead (idempotent change).

1425599 part 13 - [css-grid] Merge Grow[Base|Limits]ForSpanningItems into a templated method instead (idempotent change).

1425599 part 14 - [css-grid] Use iterators instead of an array + start/end index for the item data (idempotent change).

1425599 part 16 - [css-grid] Make SizeContributionForPhase a template.

1425599 - [css-grid] Follow-up bustage fix for stupid compiler warnings.

1378481 - Assign 'roundingError' in the default branch too, to avoid a maybe-uninitialized compiler warning.

1423292 - [css-grid] Add a couple of ItemState bits to Dump(), and make an ItemState assertion stricter (DEBUG-only changes).

1373678 Part 1: Reduce grid line numbers by count of leading implicit lines, minimum 0.

1416350 - Part 1: Correctly account for removed 'auto-fit' tracks also when there are leading implicit tracks.

1416350 - Part 2: Correct logic for Grid API line numbering with leading implicit tracks.

1418727 part 1 - [css-grid] Introduce StateBitsForRange() that collects the union of the state bits for a range of tracks (idempotent change).

1418727 part 2 - [css-grid] Require that an item spans at least one track with an 'auto' min sizing function for Automatic Minimum Size to apply.
2019-09-28 23:47:04 -04:00

263 lines
8.6 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "GridLines.h"
#include "GridDimension.h"
#include "GridLine.h"
#include "mozilla/dom/GridBinding.h"
#include "nsGridContainerFrame.h"
namespace mozilla {
namespace dom {
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GridLines, mParent, mLines)
NS_IMPL_CYCLE_COLLECTING_ADDREF(GridLines)
NS_IMPL_CYCLE_COLLECTING_RELEASE(GridLines)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GridLines)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
GridLines::GridLines(GridDimension* aParent)
: mParent(aParent)
{
MOZ_ASSERT(aParent,
"Should never be instantiated with a null GridDimension");
}
GridLines::~GridLines()
{
}
JSObject*
GridLines::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return GridLinesBinding::Wrap(aCx, this, aGivenProto);
}
uint32_t
GridLines::Length() const
{
return mLines.Length();
}
GridLine*
GridLines::Item(uint32_t aIndex)
{
return mLines.SafeElementAt(aIndex);
}
GridLine*
GridLines::IndexedGetter(uint32_t aIndex,
bool& aFound)
{
aFound = aIndex < mLines.Length();
if (!aFound) {
return nullptr;
}
return mLines[aIndex];
}
void
GridLines::SetLineInfo(const ComputedGridTrackInfo* aTrackInfo,
const ComputedGridLineInfo* aLineInfo,
const nsTArray<RefPtr<GridArea>>& aAreas,
bool aIsRow)
{
MOZ_ASSERT(aLineInfo);
mLines.Clear();
if (!aTrackInfo) {
return;
}
uint32_t trackCount = aTrackInfo->mEndFragmentTrack -
aTrackInfo->mStartFragmentTrack;
// If there is at least one track, line count is one more
// than the number of tracks.
if (trackCount > 0) {
nscoord lastTrackEdge = 0;
nscoord startOfNextTrack;
uint32_t repeatIndex = 0;
uint32_t numRepeatTracks = aTrackInfo->mRemovedRepeatTracks.Length();
uint32_t numAddedLines = 0;
for (uint32_t i = aTrackInfo->mStartFragmentTrack;
i < aTrackInfo->mEndFragmentTrack + 1;
i++) {
// Since line indexes are 1-based, calculate a 1-based value
// for this track to simplify some calculations.
const uint32_t line1Index = i + 1;
startOfNextTrack = (i < aTrackInfo->mEndFragmentTrack) ?
aTrackInfo->mPositions[i] :
lastTrackEdge;
nsTArray<nsString> lineNames;
lineNames = aLineInfo->mNames.SafeElementAt(i, nsTArray<nsString>());
// Add in names from grid areas where this line is used as a boundary.
for (auto area : aAreas) {
bool haveNameToAdd = false;
nsAutoString nameToAdd;
area->GetName(nameToAdd);
if (aIsRow) {
if (area->RowStart() == line1Index) {
haveNameToAdd = true;
nameToAdd.AppendLiteral("-start");
} else if (area->RowEnd() == line1Index) {
haveNameToAdd = true;
nameToAdd.AppendLiteral("-end");
}
} else {
if (area->ColumnStart() == line1Index) {
haveNameToAdd = true;
nameToAdd.AppendLiteral("-start");
} else if (area->ColumnEnd() == line1Index) {
haveNameToAdd = true;
nameToAdd.AppendLiteral("-end");
}
}
if (haveNameToAdd && !lineNames.Contains(nameToAdd)) {
lineNames.AppendElement(nameToAdd);
}
}
if (i >= (aTrackInfo->mRepeatFirstTrack +
aTrackInfo->mNumLeadingImplicitTracks) &&
repeatIndex < numRepeatTracks) {
numAddedLines += AppendRemovedAutoFits(aTrackInfo,
aLineInfo,
lastTrackEdge,
repeatIndex,
numRepeatTracks,
lineNames);
}
RefPtr<GridLine> line = new GridLine(this);
mLines.AppendElement(line);
MOZ_ASSERT(line1Index > 0, "line1Index must be positive.");
bool isBeforeFirstExplicit =
(line1Index <= aTrackInfo->mNumLeadingImplicitTracks);
// Calculate an actionable line number for this line, that could be used
// in a css grid property to align a grid item or area at that line.
// For implicit lines that appear before line 1, report a number of 0.
// We can't report negative indexes, because those have a different
// meaning in the css grid spec (negative indexes are negative-1-based
// from the end of the grid decreasing towards the front).
uint32_t lineNumber = isBeforeFirstExplicit ? 0 :
(line1Index - aTrackInfo->mNumLeadingImplicitTracks + numAddedLines);
GridDeclaration lineType =
(isBeforeFirstExplicit ||
line1Index > (aTrackInfo->mNumLeadingImplicitTracks +
aTrackInfo->mNumExplicitTracks + 1))
? GridDeclaration::Implicit
: GridDeclaration::Explicit;
line->SetLineValues(
lineNames,
nsPresContext::AppUnitsToDoubleCSSPixels(lastTrackEdge),
nsPresContext::AppUnitsToDoubleCSSPixels(startOfNextTrack -
lastTrackEdge),
lineNumber,
lineType
);
if (i < aTrackInfo->mEndFragmentTrack) {
lastTrackEdge = aTrackInfo->mPositions[i] + aTrackInfo->mSizes[i];
}
}
}
}
uint32_t
GridLines::AppendRemovedAutoFits(const ComputedGridTrackInfo* aTrackInfo,
const ComputedGridLineInfo* aLineInfo,
nscoord aLastTrackEdge,
uint32_t& aRepeatIndex,
uint32_t aNumRepeatTracks,
nsTArray<nsString>& aLineNames)
{
// Check to see if lineNames contains ALL of the before line names.
bool alreadyHasBeforeLineNames = true;
for (const auto& beforeName : aLineInfo->mNamesBefore) {
if (!aLineNames.Contains(beforeName)) {
alreadyHasBeforeLineNames = false;
break;
}
}
bool extractedExplicitLineNames = false;
nsTArray<nsString> explicitLineNames;
uint32_t linesAdded = 0;
while (aRepeatIndex < aNumRepeatTracks &&
aTrackInfo->mRemovedRepeatTracks[aRepeatIndex]) {
// If this is not the very first call to this function, and if we
// haven't already added a line this call, pull all the explicit
// names to pass along to the next line that will be added after
// this function completes.
if (aRepeatIndex > 0 &&
linesAdded == 0) {
// Find the names that didn't match the before or after names,
// and extract them.
for (const auto& name : aLineNames) {
if (!aLineInfo->mNamesBefore.Contains(name) &&
!aLineInfo->mNamesAfter.Contains(name)) {
explicitLineNames.AppendElement(name);
}
}
for (const auto& extractedName : explicitLineNames) {
aLineNames.RemoveElement(extractedName);
}
extractedExplicitLineNames = true;
}
// If this is the second or later time through, or didn't already
// have before names, add them.
if (linesAdded > 0 || !alreadyHasBeforeLineNames) {
aLineNames.AppendElements(aLineInfo->mNamesBefore);
}
RefPtr<GridLine> line = new GridLine(this);
mLines.AppendElement(line);
uint32_t lineNumber = aTrackInfo->mRepeatFirstTrack +
aRepeatIndex + 1;
line->SetLineValues(
aLineNames,
nsPresContext::AppUnitsToDoubleCSSPixels(aLastTrackEdge),
nsPresContext::AppUnitsToDoubleCSSPixels(0),
lineNumber,
GridDeclaration::Explicit
);
// No matter what, the next line should have the after names associated
// with it. If we go through the loop again, the before names will also
// be added.
aLineNames = aLineInfo->mNamesAfter;
aRepeatIndex++;
linesAdded++;
}
aRepeatIndex++;
if (extractedExplicitLineNames) {
// Pass on the explicit names we saved to the next explicit line.
aLineNames.AppendElements(explicitLineNames);
}
if (alreadyHasBeforeLineNames && linesAdded > 0) {
// If we started with before names, pass them on to the next explicit
// line.
aLineNames.AppendElements(aLineInfo->mNamesBefore);
}
return linesAdded;
}
} // namespace dom
} // namespace mozilla