layout/generic/nsFlexContainerFrame.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3
michael@0 4 /* This Source Code is subject to the terms of the Mozilla Public License
michael@0 5 * version 2.0 (the "License"). You can obtain a copy of the License at
michael@0 6 * http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 /* rendering object for CSS "display: flex" */
michael@0 9
michael@0 10 #include "nsFlexContainerFrame.h"
michael@0 11 #include "nsContentUtils.h"
michael@0 12 #include "nsCSSAnonBoxes.h"
michael@0 13 #include "nsDisplayList.h"
michael@0 14 #include "nsIFrameInlines.h"
michael@0 15 #include "nsLayoutUtils.h"
michael@0 16 #include "nsPlaceholderFrame.h"
michael@0 17 #include "nsPresContext.h"
michael@0 18 #include "nsStyleContext.h"
michael@0 19 #include "prlog.h"
michael@0 20 #include <algorithm>
michael@0 21 #include "mozilla/LinkedList.h"
michael@0 22
michael@0 23 using namespace mozilla;
michael@0 24 using namespace mozilla::css;
michael@0 25 using namespace mozilla::layout;
michael@0 26
michael@0 27 // Convenience typedefs for helper classes that we forward-declare in .h file
michael@0 28 // (so that nsFlexContainerFrame methods can use them as parameters):
michael@0 29 typedef nsFlexContainerFrame::FlexItem FlexItem;
michael@0 30 typedef nsFlexContainerFrame::FlexLine FlexLine;
michael@0 31 typedef nsFlexContainerFrame::FlexboxAxisTracker FlexboxAxisTracker;
michael@0 32 typedef nsFlexContainerFrame::StrutInfo StrutInfo;
michael@0 33
michael@0 34 #ifdef PR_LOGGING
michael@0 35 static PRLogModuleInfo*
michael@0 36 GetFlexContainerLog()
michael@0 37 {
michael@0 38 static PRLogModuleInfo *sLog;
michael@0 39 if (!sLog)
michael@0 40 sLog = PR_NewLogModule("nsFlexContainerFrame");
michael@0 41 return sLog;
michael@0 42 }
michael@0 43 #endif /* PR_LOGGING */
michael@0 44
michael@0 45 // XXXdholbert Some of this helper-stuff should be separated out into a general
michael@0 46 // "LogicalAxisUtils.h" helper. Should that be a class, or a namespace (under
michael@0 47 // what super-namespace?), or what?
michael@0 48
michael@0 49 // Helper enums
michael@0 50 // ============
michael@0 51
michael@0 52 // Represents a physical orientation for an axis.
michael@0 53 // The directional suffix indicates the direction in which the axis *grows*.
michael@0 54 // So e.g. eAxis_LR means a horizontal left-to-right axis, whereas eAxis_BT
michael@0 55 // means a vertical bottom-to-top axis.
michael@0 56 // NOTE: The order here is important -- these values are used as indices into
michael@0 57 // the static array 'kAxisOrientationToSidesMap', defined below.
michael@0 58 enum AxisOrientationType {
michael@0 59 eAxis_LR,
michael@0 60 eAxis_RL,
michael@0 61 eAxis_TB,
michael@0 62 eAxis_BT,
michael@0 63 eNumAxisOrientationTypes // For sizing arrays that use these values as indices
michael@0 64 };
michael@0 65
michael@0 66 // Represents one or the other extreme of an axis (e.g. for the main axis, the
michael@0 67 // main-start vs. main-end edge.
michael@0 68 // NOTE: The order here is important -- these values are used as indices into
michael@0 69 // the sub-arrays in 'kAxisOrientationToSidesMap', defined below.
michael@0 70 enum AxisEdgeType {
michael@0 71 eAxisEdge_Start,
michael@0 72 eAxisEdge_End,
michael@0 73 eNumAxisEdges // For sizing arrays that use these values as indices
michael@0 74 };
michael@0 75
michael@0 76 // This array maps each axis orientation to a pair of corresponding
michael@0 77 // [start, end] physical mozilla::css::Side values.
michael@0 78 static const Side
michael@0 79 kAxisOrientationToSidesMap[eNumAxisOrientationTypes][eNumAxisEdges] = {
michael@0 80 { eSideLeft, eSideRight }, // eAxis_LR
michael@0 81 { eSideRight, eSideLeft }, // eAxis_RL
michael@0 82 { eSideTop, eSideBottom }, // eAxis_TB
michael@0 83 { eSideBottom, eSideTop } // eAxis_BT
michael@0 84 };
michael@0 85
michael@0 86 // Helper structs / classes / methods
michael@0 87 // ==================================
michael@0 88
michael@0 89 // Indicates whether advancing along the given axis is equivalent to
michael@0 90 // increasing our X or Y position (as opposed to decreasing it).
michael@0 91 static inline bool
michael@0 92 AxisGrowsInPositiveDirection(AxisOrientationType aAxis)
michael@0 93 {
michael@0 94 return eAxis_LR == aAxis || eAxis_TB == aAxis;
michael@0 95 }
michael@0 96
michael@0 97 // Indicates whether the given axis is horizontal.
michael@0 98 static inline bool
michael@0 99 IsAxisHorizontal(AxisOrientationType aAxis)
michael@0 100 {
michael@0 101 return eAxis_LR == aAxis || eAxis_RL == aAxis;
michael@0 102 }
michael@0 103
michael@0 104 // Given an AxisOrientationType, returns the "reverse" AxisOrientationType
michael@0 105 // (in the same dimension, but the opposite direction)
michael@0 106 static inline AxisOrientationType
michael@0 107 GetReverseAxis(AxisOrientationType aAxis)
michael@0 108 {
michael@0 109 AxisOrientationType reversedAxis;
michael@0 110
michael@0 111 if (aAxis % 2 == 0) {
michael@0 112 // even enum value. Add 1 to reverse.
michael@0 113 reversedAxis = AxisOrientationType(aAxis + 1);
michael@0 114 } else {
michael@0 115 // odd enum value. Subtract 1 to reverse.
michael@0 116 reversedAxis = AxisOrientationType(aAxis - 1);
michael@0 117 }
michael@0 118
michael@0 119 // Check that we're still in the enum's valid range
michael@0 120 MOZ_ASSERT(reversedAxis >= eAxis_LR &&
michael@0 121 reversedAxis <= eAxis_BT);
michael@0 122
michael@0 123 return reversedAxis;
michael@0 124 }
michael@0 125
michael@0 126 // Returns aFrame's computed value for 'height' or 'width' -- whichever is in
michael@0 127 // the same dimension as aAxis.
michael@0 128 static inline const nsStyleCoord&
michael@0 129 GetSizePropertyForAxis(const nsIFrame* aFrame, AxisOrientationType aAxis)
michael@0 130 {
michael@0 131 const nsStylePosition* stylePos = aFrame->StylePosition();
michael@0 132
michael@0 133 return IsAxisHorizontal(aAxis) ?
michael@0 134 stylePos->mWidth :
michael@0 135 stylePos->mHeight;
michael@0 136 }
michael@0 137
michael@0 138 /**
michael@0 139 * Converts a logical position in a given axis into a position in the
michael@0 140 * corresponding physical (x or y) axis. If the logical axis already maps
michael@0 141 * directly onto one of our physical axes (i.e. LTR or TTB), then the logical
michael@0 142 * and physical positions are equal; otherwise, we subtract the logical
michael@0 143 * position from the container-size in that axis, to flip the polarity.
michael@0 144 * (so e.g. a logical position of 2px in a RTL 20px-wide container
michael@0 145 * would correspond to a physical position of 18px.)
michael@0 146 */
michael@0 147 static nscoord
michael@0 148 PhysicalPosFromLogicalPos(nscoord aLogicalPosn,
michael@0 149 nscoord aLogicalContainerSize,
michael@0 150 AxisOrientationType aAxis) {
michael@0 151 if (AxisGrowsInPositiveDirection(aAxis)) {
michael@0 152 return aLogicalPosn;
michael@0 153 }
michael@0 154 return aLogicalContainerSize - aLogicalPosn;
michael@0 155 }
michael@0 156
michael@0 157 static nscoord
michael@0 158 MarginComponentForSide(const nsMargin& aMargin, Side aSide)
michael@0 159 {
michael@0 160 switch (aSide) {
michael@0 161 case eSideLeft:
michael@0 162 return aMargin.left;
michael@0 163 case eSideRight:
michael@0 164 return aMargin.right;
michael@0 165 case eSideTop:
michael@0 166 return aMargin.top;
michael@0 167 case eSideBottom:
michael@0 168 return aMargin.bottom;
michael@0 169 }
michael@0 170
michael@0 171 NS_NOTREACHED("unexpected Side enum");
michael@0 172 return aMargin.left; // have to return something
michael@0 173 // (but something's busted if we got here)
michael@0 174 }
michael@0 175
michael@0 176 static nscoord&
michael@0 177 MarginComponentForSide(nsMargin& aMargin, Side aSide)
michael@0 178 {
michael@0 179 switch (aSide) {
michael@0 180 case eSideLeft:
michael@0 181 return aMargin.left;
michael@0 182 case eSideRight:
michael@0 183 return aMargin.right;
michael@0 184 case eSideTop:
michael@0 185 return aMargin.top;
michael@0 186 case eSideBottom:
michael@0 187 return aMargin.bottom;
michael@0 188 }
michael@0 189
michael@0 190 NS_NOTREACHED("unexpected Side enum");
michael@0 191 return aMargin.left; // have to return something
michael@0 192 // (but something's busted if we got here)
michael@0 193 }
michael@0 194
michael@0 195 // Helper-macro to let us pick one of two expressions to evaluate
michael@0 196 // (a width expression vs. a height expression), to get a main-axis or
michael@0 197 // cross-axis component.
michael@0 198 // For code that has e.g. a nsSize object, FlexboxAxisTracker::GetMainComponent
michael@0 199 // and GetCrossComponent are cleaner; but in cases where we simply have
michael@0 200 // two separate expressions for width and height (which may be expensive to
michael@0 201 // evaluate), these macros will ensure that only the expression for the correct
michael@0 202 // axis gets evaluated.
michael@0 203 #define GET_MAIN_COMPONENT(axisTracker_, width_, height_) \
michael@0 204 IsAxisHorizontal((axisTracker_).GetMainAxis()) ? (width_) : (height_)
michael@0 205
michael@0 206 #define GET_CROSS_COMPONENT(axisTracker_, width_, height_) \
michael@0 207 IsAxisHorizontal((axisTracker_).GetCrossAxis()) ? (width_) : (height_)
michael@0 208
michael@0 209 // Encapsulates our flex container's main & cross axes.
michael@0 210 class MOZ_STACK_CLASS nsFlexContainerFrame::FlexboxAxisTracker {
michael@0 211 public:
michael@0 212 FlexboxAxisTracker(nsFlexContainerFrame* aFlexContainerFrame);
michael@0 213
michael@0 214 // Accessors:
michael@0 215 AxisOrientationType GetMainAxis() const { return mMainAxis; }
michael@0 216 AxisOrientationType GetCrossAxis() const { return mCrossAxis; }
michael@0 217
michael@0 218 nscoord GetMainComponent(const nsSize& aSize) const {
michael@0 219 return GET_MAIN_COMPONENT(*this, aSize.width, aSize.height);
michael@0 220 }
michael@0 221 int32_t GetMainComponent(const nsIntSize& aIntSize) const {
michael@0 222 return GET_MAIN_COMPONENT(*this, aIntSize.width, aIntSize.height);
michael@0 223 }
michael@0 224
michael@0 225 nscoord GetCrossComponent(const nsSize& aSize) const {
michael@0 226 return GET_CROSS_COMPONENT(*this, aSize.width, aSize.height);
michael@0 227 }
michael@0 228 int32_t GetCrossComponent(const nsIntSize& aIntSize) const {
michael@0 229 return GET_CROSS_COMPONENT(*this, aIntSize.width, aIntSize.height);
michael@0 230 }
michael@0 231
michael@0 232 nscoord GetMarginSizeInMainAxis(const nsMargin& aMargin) const {
michael@0 233 return IsAxisHorizontal(mMainAxis) ?
michael@0 234 aMargin.LeftRight() :
michael@0 235 aMargin.TopBottom();
michael@0 236 }
michael@0 237 nscoord GetMarginSizeInCrossAxis(const nsMargin& aMargin) const {
michael@0 238 return IsAxisHorizontal(mCrossAxis) ?
michael@0 239 aMargin.LeftRight() :
michael@0 240 aMargin.TopBottom();
michael@0 241 }
michael@0 242
michael@0 243 /**
michael@0 244 * Converts a logical point into a "physical" x,y point.
michael@0 245 *
michael@0 246 * In the simplest case where the main-axis is left-to-right and the
michael@0 247 * cross-axis is top-to-bottom, this just returns
michael@0 248 * nsPoint(aMainPosn, aCrossPosn).
michael@0 249 *
michael@0 250 * @arg aMainPosn The main-axis position -- i.e an offset from the
michael@0 251 * main-start edge of the container's content box.
michael@0 252 * @arg aCrossPosn The cross-axis position -- i.e an offset from the
michael@0 253 * cross-start edge of the container's content box.
michael@0 254 * @return A nsPoint representing the same position (in coordinates
michael@0 255 * relative to the container's content box).
michael@0 256 */
michael@0 257 nsPoint PhysicalPointFromLogicalPoint(nscoord aMainPosn,
michael@0 258 nscoord aCrossPosn,
michael@0 259 nscoord aContainerMainSize,
michael@0 260 nscoord aContainerCrossSize) const {
michael@0 261 nscoord physicalPosnInMainAxis =
michael@0 262 PhysicalPosFromLogicalPos(aMainPosn, aContainerMainSize, mMainAxis);
michael@0 263 nscoord physicalPosnInCrossAxis =
michael@0 264 PhysicalPosFromLogicalPos(aCrossPosn, aContainerCrossSize, mCrossAxis);
michael@0 265
michael@0 266 return IsAxisHorizontal(mMainAxis) ?
michael@0 267 nsPoint(physicalPosnInMainAxis, physicalPosnInCrossAxis) :
michael@0 268 nsPoint(physicalPosnInCrossAxis, physicalPosnInMainAxis);
michael@0 269 }
michael@0 270 nsSize PhysicalSizeFromLogicalSizes(nscoord aMainSize,
michael@0 271 nscoord aCrossSize) const {
michael@0 272 return IsAxisHorizontal(mMainAxis) ?
michael@0 273 nsSize(aMainSize, aCrossSize) :
michael@0 274 nsSize(aCrossSize, aMainSize);
michael@0 275 }
michael@0 276
michael@0 277 // Are my axes reversed with respect to what the author asked for?
michael@0 278 // (We may reverse the axes in the FlexboxAxisTracker constructor and set
michael@0 279 // this flag, to avoid reflowing our children in bottom-to-top order.)
michael@0 280 bool AreAxesInternallyReversed() const
michael@0 281 {
michael@0 282 return mAreAxesInternallyReversed;
michael@0 283 }
michael@0 284
michael@0 285 private:
michael@0 286 AxisOrientationType mMainAxis;
michael@0 287 AxisOrientationType mCrossAxis;
michael@0 288 bool mAreAxesInternallyReversed;
michael@0 289 };
michael@0 290
michael@0 291 /**
michael@0 292 * Represents a flex item.
michael@0 293 * Includes the various pieces of input that the Flexbox Layout Algorithm uses
michael@0 294 * to resolve a flexible width.
michael@0 295 */
michael@0 296 class nsFlexContainerFrame::FlexItem : public LinkedListElement<FlexItem>
michael@0 297 {
michael@0 298 public:
michael@0 299 // Normal constructor:
michael@0 300 FlexItem(nsIFrame* aChildFrame,
michael@0 301 float aFlexGrow, float aFlexShrink, nscoord aMainBaseSize,
michael@0 302 nscoord aMainMinSize, nscoord aMainMaxSize,
michael@0 303 nscoord aCrossMinSize, nscoord aCrossMaxSize,
michael@0 304 nsMargin aMargin, nsMargin aBorderPadding,
michael@0 305 const FlexboxAxisTracker& aAxisTracker);
michael@0 306
michael@0 307 // Simplified constructor, to be used only for generating "struts":
michael@0 308 FlexItem(nsIFrame* aChildFrame, nscoord aCrossSize);
michael@0 309
michael@0 310 // Accessors
michael@0 311 nsIFrame* Frame() const { return mFrame; }
michael@0 312 nscoord GetFlexBaseSize() const { return mFlexBaseSize; }
michael@0 313
michael@0 314 nscoord GetMainMinSize() const { return mMainMinSize; }
michael@0 315 nscoord GetMainMaxSize() const { return mMainMaxSize; }
michael@0 316
michael@0 317 // Note: These return the main-axis position and size of our *content box*.
michael@0 318 nscoord GetMainSize() const { return mMainSize; }
michael@0 319 nscoord GetMainPosition() const { return mMainPosn; }
michael@0 320
michael@0 321 nscoord GetCrossMinSize() const { return mCrossMinSize; }
michael@0 322 nscoord GetCrossMaxSize() const { return mCrossMaxSize; }
michael@0 323
michael@0 324 // Note: These return the cross-axis position and size of our *content box*.
michael@0 325 nscoord GetCrossSize() const { return mCrossSize; }
michael@0 326 nscoord GetCrossPosition() const { return mCrossPosn; }
michael@0 327
michael@0 328 // Convenience methods to compute the main & cross size of our *margin-box*.
michael@0 329 // The caller is responsible for telling us the right axis, so that we can
michael@0 330 // pull out the appropriate components of our margin/border/padding structs.
michael@0 331 nscoord GetOuterMainSize(AxisOrientationType aMainAxis) const
michael@0 332 {
michael@0 333 return mMainSize + GetMarginBorderPaddingSizeInAxis(aMainAxis);
michael@0 334 }
michael@0 335
michael@0 336 nscoord GetOuterCrossSize(AxisOrientationType aCrossAxis) const
michael@0 337 {
michael@0 338 return mCrossSize + GetMarginBorderPaddingSizeInAxis(aCrossAxis);
michael@0 339 }
michael@0 340
michael@0 341 // Returns the distance between this FlexItem's baseline and the cross-start
michael@0 342 // edge of its margin-box. Used in baseline alignment.
michael@0 343 // (This function needs to be told what cross axis is & which edge we're
michael@0 344 // measuring the baseline from, so that it can look up the appropriate
michael@0 345 // components from mMargin.)
michael@0 346 nscoord GetBaselineOffsetFromOuterCrossEdge(AxisOrientationType aCrossAxis,
michael@0 347 AxisEdgeType aEdge) const;
michael@0 348
michael@0 349 float GetShareOfFlexWeightSoFar() const { return mShareOfFlexWeightSoFar; }
michael@0 350
michael@0 351 bool IsFrozen() const { return mIsFrozen; }
michael@0 352
michael@0 353 bool HadMinViolation() const { return mHadMinViolation; }
michael@0 354 bool HadMaxViolation() const { return mHadMaxViolation; }
michael@0 355
michael@0 356 // Indicates whether this item received a preliminary "measuring" reflow
michael@0 357 // before its actual reflow.
michael@0 358 bool HadMeasuringReflow() const { return mHadMeasuringReflow; }
michael@0 359
michael@0 360 // Indicates whether this item's cross-size has been stretched (from having
michael@0 361 // "align-self: stretch" with an auto cross-size and no auto margins in the
michael@0 362 // cross axis).
michael@0 363 bool IsStretched() const { return mIsStretched; }
michael@0 364
michael@0 365 // Indicates whether this item is a "strut" left behind by an element with
michael@0 366 // visibility:collapse.
michael@0 367 bool IsStrut() const { return mIsStrut; }
michael@0 368
michael@0 369 uint8_t GetAlignSelf() const { return mAlignSelf; }
michael@0 370
michael@0 371 // Returns the flex weight that we should use in the "resolving flexible
michael@0 372 // lengths" algorithm. If we're using flex grow, we just return that;
michael@0 373 // otherwise, we use the "scaled flex shrink weight" (scaled by our flex
michael@0 374 // base size, so that when both large and small items are shrinking,
michael@0 375 // the large items shrink more).
michael@0 376 float GetFlexWeightToUse(bool aIsUsingFlexGrow)
michael@0 377 {
michael@0 378 if (IsFrozen()) {
michael@0 379 return 0.0f;
michael@0 380 }
michael@0 381
michael@0 382 if (aIsUsingFlexGrow) {
michael@0 383 return mFlexGrow;
michael@0 384 }
michael@0 385
michael@0 386 // We're using flex-shrink --> return mFlexShrink * mFlexBaseSize
michael@0 387 if (mFlexBaseSize == 0) {
michael@0 388 // Special-case for mFlexBaseSize == 0 -- we have no room to shrink, so
michael@0 389 // regardless of mFlexShrink, we should just return 0.
michael@0 390 // (This is really a special-case for when mFlexShrink is infinity, to
michael@0 391 // avoid performing mFlexShrink * mFlexBaseSize = inf * 0 = undefined.)
michael@0 392 return 0.0f;
michael@0 393 }
michael@0 394 return mFlexShrink * mFlexBaseSize;
michael@0 395 }
michael@0 396
michael@0 397 // Getters for margin:
michael@0 398 // ===================
michael@0 399 const nsMargin& GetMargin() const { return mMargin; }
michael@0 400
michael@0 401 // Returns the margin component for a given mozilla::css::Side
michael@0 402 nscoord GetMarginComponentForSide(Side aSide) const
michael@0 403 { return MarginComponentForSide(mMargin, aSide); }
michael@0 404
michael@0 405 // Returns the total space occupied by this item's margins in the given axis
michael@0 406 nscoord GetMarginSizeInAxis(AxisOrientationType aAxis) const
michael@0 407 {
michael@0 408 Side startSide = kAxisOrientationToSidesMap[aAxis][eAxisEdge_Start];
michael@0 409 Side endSide = kAxisOrientationToSidesMap[aAxis][eAxisEdge_End];
michael@0 410 return GetMarginComponentForSide(startSide) +
michael@0 411 GetMarginComponentForSide(endSide);
michael@0 412 }
michael@0 413
michael@0 414 // Getters for border/padding
michael@0 415 // ==========================
michael@0 416 const nsMargin& GetBorderPadding() const { return mBorderPadding; }
michael@0 417
michael@0 418 // Returns the border+padding component for a given mozilla::css::Side
michael@0 419 nscoord GetBorderPaddingComponentForSide(Side aSide) const
michael@0 420 { return MarginComponentForSide(mBorderPadding, aSide); }
michael@0 421
michael@0 422 // Returns the total space occupied by this item's borders and padding in
michael@0 423 // the given axis
michael@0 424 nscoord GetBorderPaddingSizeInAxis(AxisOrientationType aAxis) const
michael@0 425 {
michael@0 426 Side startSide = kAxisOrientationToSidesMap[aAxis][eAxisEdge_Start];
michael@0 427 Side endSide = kAxisOrientationToSidesMap[aAxis][eAxisEdge_End];
michael@0 428 return GetBorderPaddingComponentForSide(startSide) +
michael@0 429 GetBorderPaddingComponentForSide(endSide);
michael@0 430 }
michael@0 431
michael@0 432 // Getter for combined margin/border/padding
michael@0 433 // =========================================
michael@0 434 // Returns the total space occupied by this item's margins, borders and
michael@0 435 // padding in the given axis
michael@0 436 nscoord GetMarginBorderPaddingSizeInAxis(AxisOrientationType aAxis) const
michael@0 437 {
michael@0 438 return GetMarginSizeInAxis(aAxis) + GetBorderPaddingSizeInAxis(aAxis);
michael@0 439 }
michael@0 440
michael@0 441 // Setters
michael@0 442 // =======
michael@0 443
michael@0 444 // This sets our flex base size, and then updates the main size to the
michael@0 445 // base size clamped to our main-axis [min,max] constraints.
michael@0 446 void SetFlexBaseSizeAndMainSize(nscoord aNewFlexBaseSize)
michael@0 447 {
michael@0 448 MOZ_ASSERT(!mIsFrozen || mFlexBaseSize == NS_INTRINSICSIZE,
michael@0 449 "flex base size shouldn't change after we're frozen "
michael@0 450 "(unless we're just resolving an intrinsic size)");
michael@0 451 mFlexBaseSize = aNewFlexBaseSize;
michael@0 452
michael@0 453 // Before we've resolved flexible lengths, we keep mMainSize set to
michael@0 454 // the 'hypothetical main size', which is the flex base size, clamped
michael@0 455 // to the [min,max] range:
michael@0 456 mMainSize = NS_CSS_MINMAX(mFlexBaseSize, mMainMinSize, mMainMaxSize);
michael@0 457 }
michael@0 458
michael@0 459 // Setters used while we're resolving flexible lengths
michael@0 460 // ---------------------------------------------------
michael@0 461
michael@0 462 // Sets the main-size of our flex item's content-box.
michael@0 463 void SetMainSize(nscoord aNewMainSize)
michael@0 464 {
michael@0 465 MOZ_ASSERT(!mIsFrozen, "main size shouldn't change after we're frozen");
michael@0 466 mMainSize = aNewMainSize;
michael@0 467 }
michael@0 468
michael@0 469 void SetShareOfFlexWeightSoFar(float aNewShare)
michael@0 470 {
michael@0 471 MOZ_ASSERT(!mIsFrozen || aNewShare == 0.0f,
michael@0 472 "shouldn't be giving this item any share of the weight "
michael@0 473 "after it's frozen");
michael@0 474 mShareOfFlexWeightSoFar = aNewShare;
michael@0 475 }
michael@0 476
michael@0 477 void Freeze() { mIsFrozen = true; }
michael@0 478
michael@0 479 void SetHadMinViolation()
michael@0 480 {
michael@0 481 MOZ_ASSERT(!mIsFrozen,
michael@0 482 "shouldn't be changing main size & having violations "
michael@0 483 "after we're frozen");
michael@0 484 mHadMinViolation = true;
michael@0 485 }
michael@0 486 void SetHadMaxViolation()
michael@0 487 {
michael@0 488 MOZ_ASSERT(!mIsFrozen,
michael@0 489 "shouldn't be changing main size & having violations "
michael@0 490 "after we're frozen");
michael@0 491 mHadMaxViolation = true;
michael@0 492 }
michael@0 493 void ClearViolationFlags()
michael@0 494 { mHadMinViolation = mHadMaxViolation = false; }
michael@0 495
michael@0 496 // Setters for values that are determined after we've resolved our main size
michael@0 497 // -------------------------------------------------------------------------
michael@0 498
michael@0 499 // Sets the main-axis position of our flex item's content-box.
michael@0 500 // (This is the distance between the main-start edge of the flex container
michael@0 501 // and the main-start edge of the flex item's content-box.)
michael@0 502 void SetMainPosition(nscoord aPosn) {
michael@0 503 MOZ_ASSERT(mIsFrozen, "main size should be resolved before this");
michael@0 504 mMainPosn = aPosn;
michael@0 505 }
michael@0 506
michael@0 507 // Sets the cross-size of our flex item's content-box.
michael@0 508 void SetCrossSize(nscoord aCrossSize) {
michael@0 509 MOZ_ASSERT(!mIsStretched,
michael@0 510 "Cross size shouldn't be modified after it's been stretched");
michael@0 511 mCrossSize = aCrossSize;
michael@0 512 }
michael@0 513
michael@0 514 // Sets the cross-axis position of our flex item's content-box.
michael@0 515 // (This is the distance between the cross-start edge of the flex container
michael@0 516 // and the cross-start edge of the flex item.)
michael@0 517 void SetCrossPosition(nscoord aPosn) {
michael@0 518 MOZ_ASSERT(mIsFrozen, "main size should be resolved before this");
michael@0 519 mCrossPosn = aPosn;
michael@0 520 }
michael@0 521
michael@0 522 void SetAscent(nscoord aAscent) {
michael@0 523 mAscent = aAscent;
michael@0 524 }
michael@0 525
michael@0 526 void SetHadMeasuringReflow() {
michael@0 527 mHadMeasuringReflow = true;
michael@0 528 }
michael@0 529
michael@0 530 void SetIsStretched() {
michael@0 531 MOZ_ASSERT(mIsFrozen, "main size should be resolved before this");
michael@0 532 mIsStretched = true;
michael@0 533 }
michael@0 534
michael@0 535 // Setter for margin components (for resolving "auto" margins)
michael@0 536 void SetMarginComponentForSide(Side aSide, nscoord aLength)
michael@0 537 {
michael@0 538 MOZ_ASSERT(mIsFrozen, "main size should be resolved before this");
michael@0 539 MarginComponentForSide(mMargin, aSide) = aLength;
michael@0 540 }
michael@0 541
michael@0 542 void ResolveStretchedCrossSize(nscoord aLineCrossSize,
michael@0 543 const FlexboxAxisTracker& aAxisTracker);
michael@0 544
michael@0 545 uint32_t GetNumAutoMarginsInAxis(AxisOrientationType aAxis) const;
michael@0 546
michael@0 547 protected:
michael@0 548 // Our frame:
michael@0 549 nsIFrame* const mFrame;
michael@0 550
michael@0 551 // Values that we already know in constructor: (and are hence mostly 'const')
michael@0 552 const float mFlexGrow;
michael@0 553 const float mFlexShrink;
michael@0 554
michael@0 555 const nsMargin mBorderPadding;
michael@0 556 nsMargin mMargin; // non-const because we need to resolve auto margins
michael@0 557
michael@0 558 nscoord mFlexBaseSize;
michael@0 559
michael@0 560 const nscoord mMainMinSize;
michael@0 561 const nscoord mMainMaxSize;
michael@0 562 const nscoord mCrossMinSize;
michael@0 563 const nscoord mCrossMaxSize;
michael@0 564
michael@0 565 // Values that we compute after constructor:
michael@0 566 nscoord mMainSize;
michael@0 567 nscoord mMainPosn;
michael@0 568 nscoord mCrossSize;
michael@0 569 nscoord mCrossPosn;
michael@0 570 nscoord mAscent;
michael@0 571
michael@0 572 // Temporary state, while we're resolving flexible widths (for our main size)
michael@0 573 // XXXdholbert To save space, we could use a union to make these variables
michael@0 574 // overlay the same memory as some other member vars that aren't touched
michael@0 575 // until after main-size has been resolved. In particular, these could share
michael@0 576 // memory with mMainPosn through mAscent, and mIsStretched.
michael@0 577 float mShareOfFlexWeightSoFar;
michael@0 578 bool mIsFrozen;
michael@0 579 bool mHadMinViolation;
michael@0 580 bool mHadMaxViolation;
michael@0 581
michael@0 582 // Misc:
michael@0 583 bool mHadMeasuringReflow; // Did this item get a preliminary reflow,
michael@0 584 // to measure its desired height?
michael@0 585 bool mIsStretched; // See IsStretched() documentation
michael@0 586 bool mIsStrut; // Is this item a "strut" left behind by an element
michael@0 587 // with visibility:collapse?
michael@0 588 uint8_t mAlignSelf; // My "align-self" computed value (with "auto"
michael@0 589 // swapped out for parent"s "align-items" value,
michael@0 590 // in our constructor).
michael@0 591 };
michael@0 592
michael@0 593 /**
michael@0 594 * Represents a single flex line in a flex container.
michael@0 595 * Manages a linked list of the FlexItems that are in the line.
michael@0 596 */
michael@0 597 class nsFlexContainerFrame::FlexLine : public LinkedListElement<FlexLine>
michael@0 598 {
michael@0 599 public:
michael@0 600 FlexLine()
michael@0 601 : mNumItems(0),
michael@0 602 mTotalInnerHypotheticalMainSize(0),
michael@0 603 mTotalOuterHypotheticalMainSize(0),
michael@0 604 mLineCrossSize(0),
michael@0 605 mBaselineOffset(nscoord_MIN)
michael@0 606 {}
michael@0 607
michael@0 608 // Returns the sum of our FlexItems' outer hypothetical main sizes.
michael@0 609 // ("outer" = margin-box, and "hypothetical" = before flexing)
michael@0 610 nscoord GetTotalOuterHypotheticalMainSize() const {
michael@0 611 return mTotalOuterHypotheticalMainSize;
michael@0 612 }
michael@0 613
michael@0 614 // Accessors for our FlexItems & information about them:
michael@0 615 FlexItem* GetFirstItem()
michael@0 616 {
michael@0 617 MOZ_ASSERT(mItems.isEmpty() == (mNumItems == 0),
michael@0 618 "mNumItems bookkeeping is off");
michael@0 619 return mItems.getFirst();
michael@0 620 }
michael@0 621
michael@0 622 const FlexItem* GetFirstItem() const
michael@0 623 {
michael@0 624 MOZ_ASSERT(mItems.isEmpty() == (mNumItems == 0),
michael@0 625 "mNumItems bookkeeping is off");
michael@0 626 return mItems.getFirst();
michael@0 627 }
michael@0 628
michael@0 629 bool IsEmpty() const
michael@0 630 {
michael@0 631 MOZ_ASSERT(mItems.isEmpty() == (mNumItems == 0),
michael@0 632 "mNumItems bookkeeping is off");
michael@0 633 return mItems.isEmpty();
michael@0 634 }
michael@0 635
michael@0 636 uint32_t NumItems() const
michael@0 637 {
michael@0 638 MOZ_ASSERT(mItems.isEmpty() == (mNumItems == 0),
michael@0 639 "mNumItems bookkeeping is off");
michael@0 640 return mNumItems;
michael@0 641 }
michael@0 642
michael@0 643 // Adds the given FlexItem to our list of items (at the front or back
michael@0 644 // depending on aShouldInsertAtFront), and adds its hypothetical
michael@0 645 // outer & inner main sizes to our totals. Use this method instead of
michael@0 646 // directly modifying the item list, so that our bookkeeping remains correct.
michael@0 647 void AddItem(FlexItem* aItem,
michael@0 648 bool aShouldInsertAtFront,
michael@0 649 nscoord aItemInnerHypotheticalMainSize,
michael@0 650 nscoord aItemOuterHypotheticalMainSize)
michael@0 651 {
michael@0 652 if (aShouldInsertAtFront) {
michael@0 653 mItems.insertFront(aItem);
michael@0 654 } else {
michael@0 655 mItems.insertBack(aItem);
michael@0 656 }
michael@0 657 mNumItems++;
michael@0 658 mTotalInnerHypotheticalMainSize += aItemInnerHypotheticalMainSize;
michael@0 659 mTotalOuterHypotheticalMainSize += aItemOuterHypotheticalMainSize;
michael@0 660 }
michael@0 661
michael@0 662 // Computes the cross-size and baseline position of this FlexLine, based on
michael@0 663 // its FlexItems.
michael@0 664 void ComputeCrossSizeAndBaseline(const FlexboxAxisTracker& aAxisTracker);
michael@0 665
michael@0 666 // Returns the cross-size of this line.
michael@0 667 nscoord GetLineCrossSize() const { return mLineCrossSize; }
michael@0 668
michael@0 669 // Setter for line cross-size -- needed for cases where the flex container
michael@0 670 // imposes a cross-size on the line. (e.g. for single-line flexbox, or for
michael@0 671 // multi-line flexbox with 'align-content: stretch')
michael@0 672 void SetLineCrossSize(nscoord aLineCrossSize) {
michael@0 673 mLineCrossSize = aLineCrossSize;
michael@0 674 }
michael@0 675
michael@0 676 /**
michael@0 677 * Returns the offset within this line where any baseline-aligned FlexItems
michael@0 678 * should place their baseline. Usually, this represents a distance from the
michael@0 679 * line's cross-start edge, but if we're internally reversing the axes (see
michael@0 680 * AreAxesInternallyReversed()), this instead represents the distance from
michael@0 681 * its cross-end edge.
michael@0 682 *
michael@0 683 * If there are no baseline-aligned FlexItems, returns nscoord_MIN.
michael@0 684 */
michael@0 685 nscoord GetBaselineOffset() const {
michael@0 686 return mBaselineOffset;
michael@0 687 }
michael@0 688
michael@0 689 // Runs the "Resolving Flexible Lengths" algorithm from section 9.7 of the
michael@0 690 // CSS flexbox spec to distribute aFlexContainerMainSize among our flex items.
michael@0 691 void ResolveFlexibleLengths(nscoord aFlexContainerMainSize);
michael@0 692
michael@0 693 void PositionItemsInMainAxis(uint8_t aJustifyContent,
michael@0 694 nscoord aContentBoxMainSize,
michael@0 695 const FlexboxAxisTracker& aAxisTracker);
michael@0 696
michael@0 697 void PositionItemsInCrossAxis(nscoord aLineStartPosition,
michael@0 698 const FlexboxAxisTracker& aAxisTracker);
michael@0 699
michael@0 700 friend class AutoFlexLineListClearer; // (needs access to mItems)
michael@0 701
michael@0 702 private:
michael@0 703 // Helper for ResolveFlexibleLengths():
michael@0 704 void FreezeOrRestoreEachFlexibleSize(const nscoord aTotalViolation,
michael@0 705 bool aIsFinalIteration);
michael@0 706
michael@0 707 LinkedList<FlexItem> mItems; // Linked list of this line's flex items.
michael@0 708
michael@0 709 uint32_t mNumItems; // Number of FlexItems in this line (in |mItems|).
michael@0 710 // (Shouldn't change after GenerateFlexLines finishes
michael@0 711 // with this line -- at least, not until we add support
michael@0 712 // for splitting lines across continuations. Then we can
michael@0 713 // update this count carefully.)
michael@0 714
michael@0 715 nscoord mTotalInnerHypotheticalMainSize;
michael@0 716 nscoord mTotalOuterHypotheticalMainSize;
michael@0 717 nscoord mLineCrossSize;
michael@0 718 nscoord mBaselineOffset;
michael@0 719 };
michael@0 720
michael@0 721 // Information about a strut left behind by a FlexItem that's been collapsed
michael@0 722 // using "visibility:collapse".
michael@0 723 struct nsFlexContainerFrame::StrutInfo {
michael@0 724 StrutInfo(uint32_t aItemIdx, nscoord aStrutCrossSize)
michael@0 725 : mItemIdx(aItemIdx),
michael@0 726 mStrutCrossSize(aStrutCrossSize)
michael@0 727 {
michael@0 728 }
michael@0 729
michael@0 730 uint32_t mItemIdx; // Index in the child list.
michael@0 731 nscoord mStrutCrossSize; // The cross-size of this strut.
michael@0 732 };
michael@0 733
michael@0 734 static void
michael@0 735 BuildStrutInfoFromCollapsedItems(const FlexLine* aFirstLine,
michael@0 736 nsTArray<StrutInfo>& aStruts)
michael@0 737 {
michael@0 738 MOZ_ASSERT(aFirstLine, "null first line pointer");
michael@0 739 MOZ_ASSERT(aStruts.IsEmpty(),
michael@0 740 "We should only build up StrutInfo once per reflow, so "
michael@0 741 "aStruts should be empty when this is called");
michael@0 742
michael@0 743 uint32_t itemIdxInContainer = 0;
michael@0 744 for (const FlexLine* line = aFirstLine; line; line = line->getNext()) {
michael@0 745 for (const FlexItem* item = line->GetFirstItem(); item;
michael@0 746 item = item->getNext()) {
michael@0 747 if (NS_STYLE_VISIBILITY_COLLAPSE ==
michael@0 748 item->Frame()->StyleVisibility()->mVisible) {
michael@0 749 // Note the cross size of the line as the item's strut size.
michael@0 750 aStruts.AppendElement(StrutInfo(itemIdxInContainer,
michael@0 751 line->GetLineCrossSize()));
michael@0 752 }
michael@0 753 itemIdxInContainer++;
michael@0 754 }
michael@0 755 }
michael@0 756 }
michael@0 757
michael@0 758 // Helper-function to find the first non-anonymous-box descendent of aFrame.
michael@0 759 static nsIFrame*
michael@0 760 GetFirstNonAnonBoxDescendant(nsIFrame* aFrame)
michael@0 761 {
michael@0 762 while (aFrame) {
michael@0 763 nsIAtom* pseudoTag = aFrame->StyleContext()->GetPseudo();
michael@0 764
michael@0 765 // If aFrame isn't an anonymous container, then it'll do.
michael@0 766 if (!pseudoTag || // No pseudotag.
michael@0 767 !nsCSSAnonBoxes::IsAnonBox(pseudoTag) || // Pseudotag isn't anon.
michael@0 768 pseudoTag == nsCSSAnonBoxes::mozNonElement) { // Text, not a container.
michael@0 769 break;
michael@0 770 }
michael@0 771
michael@0 772 // Otherwise, descend to its first child and repeat.
michael@0 773
michael@0 774 // SPECIAL CASE: if we're dealing with an anonymous table, then it might
michael@0 775 // be wrapping something non-anonymous in its caption or col-group lists
michael@0 776 // (instead of its principal child list), so we have to look there.
michael@0 777 // (Note: For anonymous tables that have a non-anon cell *and* a non-anon
michael@0 778 // column, we'll always return the column. This is fine; we're really just
michael@0 779 // looking for a handle to *anything* with a meaningful content node inside
michael@0 780 // the table, for use in DOM comparisons to things outside of the table.)
michael@0 781 if (MOZ_UNLIKELY(aFrame->GetType() == nsGkAtoms::tableOuterFrame)) {
michael@0 782 nsIFrame* captionDescendant =
michael@0 783 GetFirstNonAnonBoxDescendant(aFrame->GetFirstChild(kCaptionList));
michael@0 784 if (captionDescendant) {
michael@0 785 return captionDescendant;
michael@0 786 }
michael@0 787 } else if (MOZ_UNLIKELY(aFrame->GetType() == nsGkAtoms::tableFrame)) {
michael@0 788 nsIFrame* colgroupDescendant =
michael@0 789 GetFirstNonAnonBoxDescendant(aFrame->GetFirstChild(kColGroupList));
michael@0 790 if (colgroupDescendant) {
michael@0 791 return colgroupDescendant;
michael@0 792 }
michael@0 793 }
michael@0 794
michael@0 795 // USUAL CASE: Descend to the first child in principal list.
michael@0 796 aFrame = aFrame->GetFirstPrincipalChild();
michael@0 797 }
michael@0 798 return aFrame;
michael@0 799 }
michael@0 800
michael@0 801 /**
michael@0 802 * Sorting helper-function that compares two frames' "order" property-values,
michael@0 803 * and if they're equal, compares the DOM positions of their corresponding
michael@0 804 * content nodes. Returns true if aFrame1 is "less than or equal to" aFrame2
michael@0 805 * according to this comparison.
michael@0 806 *
michael@0 807 * Note: This can't be a static function, because we need to pass it as a
michael@0 808 * template argument. (Only functions with external linkage can be passed as
michael@0 809 * template arguments.)
michael@0 810 *
michael@0 811 * @return true if the computed "order" property of aFrame1 is less than that
michael@0 812 * of aFrame2, or if the computed "order" values are equal and aFrame1's
michael@0 813 * corresponding DOM node is earlier than aFrame2's in the DOM tree.
michael@0 814 * Otherwise, returns false.
michael@0 815 */
michael@0 816 bool
michael@0 817 IsOrderLEQWithDOMFallback(nsIFrame* aFrame1,
michael@0 818 nsIFrame* aFrame2)
michael@0 819 {
michael@0 820 MOZ_ASSERT(aFrame1->IsFlexItem() && aFrame2->IsFlexItem(),
michael@0 821 "this method only intended for comparing flex items");
michael@0 822
michael@0 823 if (aFrame1 == aFrame2) {
michael@0 824 // Anything is trivially LEQ itself, so we return "true" here... but it's
michael@0 825 // probably bad if we end up actually needing this, so let's assert.
michael@0 826 NS_ERROR("Why are we checking if a frame is LEQ itself?");
michael@0 827 return true;
michael@0 828 }
michael@0 829
michael@0 830 // If we've got a placeholder frame, use its out-of-flow frame's 'order' val.
michael@0 831 {
michael@0 832 nsIFrame* aRealFrame1 = nsPlaceholderFrame::GetRealFrameFor(aFrame1);
michael@0 833 nsIFrame* aRealFrame2 = nsPlaceholderFrame::GetRealFrameFor(aFrame2);
michael@0 834
michael@0 835 int32_t order1 = aRealFrame1->StylePosition()->mOrder;
michael@0 836 int32_t order2 = aRealFrame2->StylePosition()->mOrder;
michael@0 837
michael@0 838 if (order1 != order2) {
michael@0 839 return order1 < order2;
michael@0 840 }
michael@0 841 }
michael@0 842
michael@0 843 // The "order" values are equal, so we need to fall back on DOM comparison.
michael@0 844 // For that, we need to dig through any anonymous box wrapper frames to find
michael@0 845 // the actual frame that corresponds to our child content.
michael@0 846 aFrame1 = GetFirstNonAnonBoxDescendant(aFrame1);
michael@0 847 aFrame2 = GetFirstNonAnonBoxDescendant(aFrame2);
michael@0 848 MOZ_ASSERT(aFrame1 && aFrame2,
michael@0 849 "why do we have an anonymous box without any "
michael@0 850 "non-anonymous descendants?");
michael@0 851
michael@0 852
michael@0 853 // Special case:
michael@0 854 // If either frame is for generated content from ::before or ::after, then
michael@0 855 // we can't use nsContentUtils::PositionIsBefore(), since that method won't
michael@0 856 // recognize generated content as being an actual sibling of other nodes.
michael@0 857 // We know where ::before and ::after nodes *effectively* insert in the DOM
michael@0 858 // tree, though (at the beginning & end), so we can just special-case them.
michael@0 859 nsIAtom* pseudo1 = aFrame1->StyleContext()->GetPseudo();
michael@0 860 nsIAtom* pseudo2 = aFrame2->StyleContext()->GetPseudo();
michael@0 861 if (pseudo1 == nsCSSPseudoElements::before ||
michael@0 862 pseudo2 == nsCSSPseudoElements::after) {
michael@0 863 // frame1 is ::before and/or frame2 is ::after => frame1 is LEQ frame2.
michael@0 864 return true;
michael@0 865 }
michael@0 866 if (pseudo1 == nsCSSPseudoElements::after ||
michael@0 867 pseudo2 == nsCSSPseudoElements::before) {
michael@0 868 // frame1 is ::after and/or frame2 is ::before => frame1 is not LEQ frame2.
michael@0 869 return false;
michael@0 870 }
michael@0 871
michael@0 872 // Usual case: Compare DOM position.
michael@0 873 nsIContent* content1 = aFrame1->GetContent();
michael@0 874 nsIContent* content2 = aFrame2->GetContent();
michael@0 875 MOZ_ASSERT(content1 != content2,
michael@0 876 "Two different flex items are using the same nsIContent node for "
michael@0 877 "comparison, so we may be sorting them in an arbitrary order");
michael@0 878
michael@0 879 return nsContentUtils::PositionIsBefore(content1, content2);
michael@0 880 }
michael@0 881
michael@0 882 /**
michael@0 883 * Sorting helper-function that compares two frames' "order" property-values.
michael@0 884 * Returns true if aFrame1 is "less than or equal to" aFrame2 according to this
michael@0 885 * comparison.
michael@0 886 *
michael@0 887 * Note: This can't be a static function, because we need to pass it as a
michael@0 888 * template argument. (Only functions with external linkage can be passed as
michael@0 889 * template arguments.)
michael@0 890 *
michael@0 891 * @return true if the computed "order" property of aFrame1 is less than or
michael@0 892 * equal to that of aFrame2. Otherwise, returns false.
michael@0 893 */
michael@0 894 bool
michael@0 895 IsOrderLEQ(nsIFrame* aFrame1,
michael@0 896 nsIFrame* aFrame2)
michael@0 897 {
michael@0 898 MOZ_ASSERT(aFrame1->IsFlexItem() && aFrame2->IsFlexItem(),
michael@0 899 "this method only intended for comparing flex items");
michael@0 900
michael@0 901 // If we've got a placeholder frame, use its out-of-flow frame's 'order' val.
michael@0 902 nsIFrame* aRealFrame1 = nsPlaceholderFrame::GetRealFrameFor(aFrame1);
michael@0 903 nsIFrame* aRealFrame2 = nsPlaceholderFrame::GetRealFrameFor(aFrame2);
michael@0 904
michael@0 905 int32_t order1 = aRealFrame1->StylePosition()->mOrder;
michael@0 906 int32_t order2 = aRealFrame2->StylePosition()->mOrder;
michael@0 907
michael@0 908 return order1 <= order2;
michael@0 909 }
michael@0 910
michael@0 911 bool
michael@0 912 nsFlexContainerFrame::IsHorizontal()
michael@0 913 {
michael@0 914 const FlexboxAxisTracker axisTracker(this);
michael@0 915 return IsAxisHorizontal(axisTracker.GetMainAxis());
michael@0 916 }
michael@0 917
michael@0 918 FlexItem*
michael@0 919 nsFlexContainerFrame::GenerateFlexItemForChild(
michael@0 920 nsPresContext* aPresContext,
michael@0 921 nsIFrame* aChildFrame,
michael@0 922 const nsHTMLReflowState& aParentReflowState,
michael@0 923 const FlexboxAxisTracker& aAxisTracker)
michael@0 924 {
michael@0 925 // Create temporary reflow state just for sizing -- to get hypothetical
michael@0 926 // main-size and the computed values of min / max main-size property.
michael@0 927 // (This reflow state will _not_ be used for reflow.)
michael@0 928 nsHTMLReflowState childRS(aPresContext, aParentReflowState, aChildFrame,
michael@0 929 nsSize(aParentReflowState.ComputedWidth(),
michael@0 930 aParentReflowState.ComputedHeight()));
michael@0 931
michael@0 932 // FLEX GROW & SHRINK WEIGHTS
michael@0 933 // --------------------------
michael@0 934 const nsStylePosition* stylePos = aChildFrame->StylePosition();
michael@0 935 float flexGrow = stylePos->mFlexGrow;
michael@0 936 float flexShrink = stylePos->mFlexShrink;
michael@0 937
michael@0 938 // MAIN SIZES (flex base size, min/max size)
michael@0 939 // -----------------------------------------
michael@0 940 nscoord flexBaseSize = GET_MAIN_COMPONENT(aAxisTracker,
michael@0 941 childRS.ComputedWidth(),
michael@0 942 childRS.ComputedHeight());
michael@0 943 nscoord mainMinSize = GET_MAIN_COMPONENT(aAxisTracker,
michael@0 944 childRS.ComputedMinWidth(),
michael@0 945 childRS.ComputedMinHeight());
michael@0 946 nscoord mainMaxSize = GET_MAIN_COMPONENT(aAxisTracker,
michael@0 947 childRS.ComputedMaxWidth(),
michael@0 948 childRS.ComputedMaxHeight());
michael@0 949 // This is enforced by the nsHTMLReflowState where these values come from:
michael@0 950 MOZ_ASSERT(mainMinSize <= mainMaxSize, "min size is larger than max size");
michael@0 951
michael@0 952 // CROSS MIN/MAX SIZE
michael@0 953 // ------------------
michael@0 954
michael@0 955 nscoord crossMinSize = GET_CROSS_COMPONENT(aAxisTracker,
michael@0 956 childRS.ComputedMinWidth(),
michael@0 957 childRS.ComputedMinHeight());
michael@0 958 nscoord crossMaxSize = GET_CROSS_COMPONENT(aAxisTracker,
michael@0 959 childRS.ComputedMaxWidth(),
michael@0 960 childRS.ComputedMaxHeight());
michael@0 961
michael@0 962 // SPECIAL-CASE FOR WIDGET-IMPOSED SIZES
michael@0 963 // Check if we're a themed widget, in which case we might have a minimum
michael@0 964 // main & cross size imposed by our widget (which we can't go below), or
michael@0 965 // (more severe) our widget might have only a single valid size.
michael@0 966 bool isFixedSizeWidget = false;
michael@0 967 const nsStyleDisplay* disp = aChildFrame->StyleDisplay();
michael@0 968 if (aChildFrame->IsThemed(disp)) {
michael@0 969 nsIntSize widgetMinSize(0, 0);
michael@0 970 bool canOverride = true;
michael@0 971 aPresContext->GetTheme()->
michael@0 972 GetMinimumWidgetSize(childRS.rendContext, aChildFrame,
michael@0 973 disp->mAppearance,
michael@0 974 &widgetMinSize, &canOverride);
michael@0 975
michael@0 976 nscoord widgetMainMinSize =
michael@0 977 aPresContext->DevPixelsToAppUnits(
michael@0 978 aAxisTracker.GetMainComponent(widgetMinSize));
michael@0 979 nscoord widgetCrossMinSize =
michael@0 980 aPresContext->DevPixelsToAppUnits(
michael@0 981 aAxisTracker.GetCrossComponent(widgetMinSize));
michael@0 982
michael@0 983 // GMWS() returns border-box. We need content-box, so subtract
michael@0 984 // borderPadding (but don't let that push our min sizes below 0).
michael@0 985 nsMargin& bp = childRS.ComputedPhysicalBorderPadding();
michael@0 986 widgetMainMinSize = std::max(widgetMainMinSize -
michael@0 987 aAxisTracker.GetMarginSizeInMainAxis(bp), 0);
michael@0 988 widgetCrossMinSize = std::max(widgetCrossMinSize -
michael@0 989 aAxisTracker.GetMarginSizeInCrossAxis(bp), 0);
michael@0 990
michael@0 991 if (!canOverride) {
michael@0 992 // Fixed-size widget: freeze our main-size at the widget's mandated size.
michael@0 993 // (Set min and max main-sizes to that size, too, to keep us from
michael@0 994 // clamping to any other size later on.)
michael@0 995 flexBaseSize = mainMinSize = mainMaxSize = widgetMainMinSize;
michael@0 996 crossMinSize = crossMaxSize = widgetCrossMinSize;
michael@0 997 isFixedSizeWidget = true;
michael@0 998 } else {
michael@0 999 // Variable-size widget: ensure our min/max sizes are at least as large
michael@0 1000 // as the widget's mandated minimum size, so we don't flex below that.
michael@0 1001 mainMinSize = std::max(mainMinSize, widgetMainMinSize);
michael@0 1002 mainMaxSize = std::max(mainMaxSize, widgetMainMinSize);
michael@0 1003
michael@0 1004 crossMinSize = std::max(crossMinSize, widgetCrossMinSize);
michael@0 1005 crossMaxSize = std::max(crossMaxSize, widgetCrossMinSize);
michael@0 1006 }
michael@0 1007 }
michael@0 1008
michael@0 1009 // Construct the flex item!
michael@0 1010 FlexItem* item = new FlexItem(aChildFrame,
michael@0 1011 flexGrow, flexShrink, flexBaseSize,
michael@0 1012 mainMinSize, mainMaxSize,
michael@0 1013 crossMinSize, crossMaxSize,
michael@0 1014 childRS.ComputedPhysicalMargin(),
michael@0 1015 childRS.ComputedPhysicalBorderPadding(),
michael@0 1016 aAxisTracker);
michael@0 1017
michael@0 1018 // If we're inflexible, we can just freeze to our hypothetical main-size
michael@0 1019 // up-front. Similarly, if we're a fixed-size widget, we only have one
michael@0 1020 // valid size, so we freeze to keep ourselves from flexing.
michael@0 1021 if (isFixedSizeWidget || (flexGrow == 0.0f && flexShrink == 0.0f)) {
michael@0 1022 item->Freeze();
michael@0 1023 }
michael@0 1024
michael@0 1025 return item;
michael@0 1026 }
michael@0 1027
michael@0 1028 nsresult
michael@0 1029 nsFlexContainerFrame::
michael@0 1030 ResolveFlexItemMaxContentSizing(nsPresContext* aPresContext,
michael@0 1031 FlexItem& aFlexItem,
michael@0 1032 const nsHTMLReflowState& aParentReflowState,
michael@0 1033 const FlexboxAxisTracker& aAxisTracker)
michael@0 1034 {
michael@0 1035 if (IsAxisHorizontal(aAxisTracker.GetMainAxis())) {
michael@0 1036 // Nothing to do -- this function is only for measuring flex items
michael@0 1037 // in a vertical flex container.
michael@0 1038 return NS_OK;
michael@0 1039 }
michael@0 1040
michael@0 1041 if (NS_AUTOHEIGHT != aFlexItem.GetFlexBaseSize()) {
michael@0 1042 // Nothing to do; this function's only relevant for flex items
michael@0 1043 // with a base size of "auto" (or equivalent).
michael@0 1044 // XXXdholbert If & when we handle "min-height: min-content" for flex items,
michael@0 1045 // we'll want to resolve that in this function, too.
michael@0 1046 return NS_OK;
michael@0 1047 }
michael@0 1048
michael@0 1049 // If we get here, we're vertical and our main size ended up being
michael@0 1050 // unconstrained. We need to use our "max-content" height, which is what we
michael@0 1051 // get from reflowing into our available width.
michael@0 1052 // Note: This has to come *after* we construct the FlexItem, since we
michael@0 1053 // invoke at least one convenience method (ResolveStretchedCrossSize) which
michael@0 1054 // requires a FlexItem.
michael@0 1055
michael@0 1056 // Give the item a special reflow with "mIsFlexContainerMeasuringHeight"
michael@0 1057 // set. This tells it to behave as if it had "height: auto", regardless
michael@0 1058 // of what the "height" property is actually set to.
michael@0 1059 nsHTMLReflowState
michael@0 1060 childRSForMeasuringHeight(aPresContext, aParentReflowState,
michael@0 1061 aFlexItem.Frame(),
michael@0 1062 nsSize(aParentReflowState.ComputedWidth(),
michael@0 1063 NS_UNCONSTRAINEDSIZE),
michael@0 1064 -1, -1, nsHTMLReflowState::CALLER_WILL_INIT);
michael@0 1065 childRSForMeasuringHeight.mFlags.mIsFlexContainerMeasuringHeight = true;
michael@0 1066 childRSForMeasuringHeight.Init(aPresContext);
michael@0 1067
michael@0 1068 aFlexItem.ResolveStretchedCrossSize(aParentReflowState.ComputedWidth(),
michael@0 1069 aAxisTracker);
michael@0 1070 if (aFlexItem.IsStretched()) {
michael@0 1071 childRSForMeasuringHeight.SetComputedWidth(aFlexItem.GetCrossSize());
michael@0 1072 childRSForMeasuringHeight.mFlags.mHResize = true;
michael@0 1073 }
michael@0 1074
michael@0 1075 // If this item is flexible (vertically), then we assume that the
michael@0 1076 // computed-height we're reflowing with now could be different
michael@0 1077 // from the one we'll use for this flex item's "actual" reflow later on.
michael@0 1078 // In that case, we need to be sure the flex item treats this as a
michael@0 1079 // vertical resize, even though none of its ancestors are necessarily
michael@0 1080 // being vertically resized.
michael@0 1081 // (Note: We don't have to do this for width, because InitResizeFlags
michael@0 1082 // will always turn on mHResize on when it sees that the computed width
michael@0 1083 // is different from current width, and that's all we need.)
michael@0 1084 if (!aFlexItem.IsFrozen()) { // Are we flexible?
michael@0 1085 childRSForMeasuringHeight.mFlags.mVResize = true;
michael@0 1086 }
michael@0 1087
michael@0 1088 nsHTMLReflowMetrics childDesiredSize(childRSForMeasuringHeight);
michael@0 1089 nsReflowStatus childReflowStatus;
michael@0 1090 const uint32_t flags = NS_FRAME_NO_MOVE_FRAME;
michael@0 1091 nsresult rv = ReflowChild(aFlexItem.Frame(), aPresContext,
michael@0 1092 childDesiredSize, childRSForMeasuringHeight,
michael@0 1093 0, 0, flags, childReflowStatus);
michael@0 1094 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1095
michael@0 1096 MOZ_ASSERT(NS_FRAME_IS_COMPLETE(childReflowStatus),
michael@0 1097 "We gave flex item unconstrained available height, so it "
michael@0 1098 "should be complete");
michael@0 1099
michael@0 1100 rv = FinishReflowChild(aFlexItem.Frame(), aPresContext,
michael@0 1101 childDesiredSize, &childRSForMeasuringHeight,
michael@0 1102 0, 0, flags);
michael@0 1103 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1104
michael@0 1105 // Subtract border/padding in vertical axis, to get _just_
michael@0 1106 // the effective computed value of the "height" property.
michael@0 1107 nscoord childDesiredHeight = childDesiredSize.Height() -
michael@0 1108 childRSForMeasuringHeight.ComputedPhysicalBorderPadding().TopBottom();
michael@0 1109 childDesiredHeight = std::max(0, childDesiredHeight);
michael@0 1110
michael@0 1111 aFlexItem.SetFlexBaseSizeAndMainSize(childDesiredHeight);
michael@0 1112 aFlexItem.SetHadMeasuringReflow();
michael@0 1113
michael@0 1114 return NS_OK;
michael@0 1115 }
michael@0 1116
michael@0 1117 FlexItem::FlexItem(nsIFrame* aChildFrame,
michael@0 1118 float aFlexGrow, float aFlexShrink, nscoord aFlexBaseSize,
michael@0 1119 nscoord aMainMinSize, nscoord aMainMaxSize,
michael@0 1120 nscoord aCrossMinSize, nscoord aCrossMaxSize,
michael@0 1121 nsMargin aMargin, nsMargin aBorderPadding,
michael@0 1122 const FlexboxAxisTracker& aAxisTracker)
michael@0 1123 : mFrame(aChildFrame),
michael@0 1124 mFlexGrow(aFlexGrow),
michael@0 1125 mFlexShrink(aFlexShrink),
michael@0 1126 mBorderPadding(aBorderPadding),
michael@0 1127 mMargin(aMargin),
michael@0 1128 mMainMinSize(aMainMinSize),
michael@0 1129 mMainMaxSize(aMainMaxSize),
michael@0 1130 mCrossMinSize(aCrossMinSize),
michael@0 1131 mCrossMaxSize(aCrossMaxSize),
michael@0 1132 mMainPosn(0),
michael@0 1133 mCrossSize(0),
michael@0 1134 mCrossPosn(0),
michael@0 1135 mAscent(0),
michael@0 1136 mShareOfFlexWeightSoFar(0.0f),
michael@0 1137 mIsFrozen(false),
michael@0 1138 mHadMinViolation(false),
michael@0 1139 mHadMaxViolation(false),
michael@0 1140 mHadMeasuringReflow(false),
michael@0 1141 mIsStretched(false),
michael@0 1142 mIsStrut(false),
michael@0 1143 mAlignSelf(aChildFrame->StylePosition()->mAlignSelf)
michael@0 1144 {
michael@0 1145 MOZ_ASSERT(mFrame, "expecting a non-null child frame");
michael@0 1146 MOZ_ASSERT(mFrame->GetType() != nsGkAtoms::placeholderFrame,
michael@0 1147 "placeholder frames should not be treated as flex items");
michael@0 1148 MOZ_ASSERT(!(mFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW),
michael@0 1149 "out-of-flow frames should not be treated as flex items");
michael@0 1150
michael@0 1151 SetFlexBaseSizeAndMainSize(aFlexBaseSize);
michael@0 1152
michael@0 1153 // Assert that any "auto" margin components are set to 0.
michael@0 1154 // (We'll resolve them later; until then, we want to treat them as 0-sized.)
michael@0 1155 #ifdef DEBUG
michael@0 1156 {
michael@0 1157 const nsStyleSides& styleMargin = mFrame->StyleMargin()->mMargin;
michael@0 1158 NS_FOR_CSS_SIDES(side) {
michael@0 1159 if (styleMargin.GetUnit(side) == eStyleUnit_Auto) {
michael@0 1160 MOZ_ASSERT(GetMarginComponentForSide(side) == 0,
michael@0 1161 "Someone else tried to resolve our auto margin");
michael@0 1162 }
michael@0 1163 }
michael@0 1164 }
michael@0 1165 #endif // DEBUG
michael@0 1166
michael@0 1167 // Resolve "align-self: auto" to parent's "align-items" value.
michael@0 1168 if (mAlignSelf == NS_STYLE_ALIGN_SELF_AUTO) {
michael@0 1169 mAlignSelf =
michael@0 1170 mFrame->StyleContext()->GetParent()->StylePosition()->mAlignItems;
michael@0 1171 }
michael@0 1172
michael@0 1173 // If the flex item's inline axis is the same as the cross axis, then
michael@0 1174 // 'align-self:baseline' is identical to 'flex-start'. If that's the case, we
michael@0 1175 // just directly convert our align-self value here, so that we don't have to
michael@0 1176 // handle this with special cases elsewhere.
michael@0 1177 // Moreover: for the time being (until we support writing-modes),
michael@0 1178 // all inline axes are horizontal -- so we can just check if the cross axis
michael@0 1179 // is horizontal.
michael@0 1180 // FIXME: Once we support writing-mode (vertical text), this IsAxisHorizontal
michael@0 1181 // check won't be sufficient anymore -- we'll actually need to compare our
michael@0 1182 // inline axis vs. the cross axis.
michael@0 1183 if (mAlignSelf == NS_STYLE_ALIGN_ITEMS_BASELINE &&
michael@0 1184 IsAxisHorizontal(aAxisTracker.GetCrossAxis())) {
michael@0 1185 mAlignSelf = NS_STYLE_ALIGN_ITEMS_FLEX_START;
michael@0 1186 }
michael@0 1187 }
michael@0 1188
michael@0 1189 // Simplified constructor for creating a special "strut" FlexItem, for a child
michael@0 1190 // with visibility:collapse. The strut has 0 main-size, and it only exists to
michael@0 1191 // impose a minimum cross size on whichever FlexLine it ends up in.
michael@0 1192 FlexItem::FlexItem(nsIFrame* aChildFrame, nscoord aCrossSize)
michael@0 1193 : mFrame(aChildFrame),
michael@0 1194 mFlexGrow(0.0f),
michael@0 1195 mFlexShrink(0.0f),
michael@0 1196 // mBorderPadding uses default constructor,
michael@0 1197 // mMargin uses default constructor,
michael@0 1198 mFlexBaseSize(0),
michael@0 1199 mMainMinSize(0),
michael@0 1200 mMainMaxSize(0),
michael@0 1201 mCrossMinSize(0),
michael@0 1202 mCrossMaxSize(0),
michael@0 1203 mMainSize(0),
michael@0 1204 mMainPosn(0),
michael@0 1205 mCrossSize(aCrossSize),
michael@0 1206 mCrossPosn(0),
michael@0 1207 mAscent(0),
michael@0 1208 mShareOfFlexWeightSoFar(0.0f),
michael@0 1209 mIsFrozen(true),
michael@0 1210 mHadMinViolation(false),
michael@0 1211 mHadMaxViolation(false),
michael@0 1212 mHadMeasuringReflow(false),
michael@0 1213 mIsStretched(false),
michael@0 1214 mIsStrut(true), // (this is the constructor for making struts, after all)
michael@0 1215 mAlignSelf(NS_STYLE_ALIGN_ITEMS_FLEX_START)
michael@0 1216 {
michael@0 1217 MOZ_ASSERT(mFrame, "expecting a non-null child frame");
michael@0 1218 MOZ_ASSERT(NS_STYLE_VISIBILITY_COLLAPSE ==
michael@0 1219 mFrame->StyleVisibility()->mVisible,
michael@0 1220 "Should only make struts for children with 'visibility:collapse'");
michael@0 1221 MOZ_ASSERT(mFrame->GetType() != nsGkAtoms::placeholderFrame,
michael@0 1222 "placeholder frames should not be treated as flex items");
michael@0 1223 MOZ_ASSERT(!(mFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW),
michael@0 1224 "out-of-flow frames should not be treated as flex items");
michael@0 1225 }
michael@0 1226
michael@0 1227 nscoord
michael@0 1228 FlexItem::GetBaselineOffsetFromOuterCrossEdge(AxisOrientationType aCrossAxis,
michael@0 1229 AxisEdgeType aEdge) const
michael@0 1230 {
michael@0 1231 // NOTE: Currently, 'mAscent' (taken from reflow) is an inherently vertical
michael@0 1232 // measurement -- it's the distance from the border-top edge of this FlexItem
michael@0 1233 // to its baseline. So, we can really only do baseline alignment when the
michael@0 1234 // cross axis is vertical. (The FlexItem constructor enforces this when
michael@0 1235 // resolving the item's "mAlignSelf" value).
michael@0 1236 MOZ_ASSERT(!IsAxisHorizontal(aCrossAxis),
michael@0 1237 "Only expecting to be doing baseline computations when the "
michael@0 1238 "cross axis is vertical");
michael@0 1239
michael@0 1240 Side sideToMeasureFrom = kAxisOrientationToSidesMap[aCrossAxis][aEdge];
michael@0 1241
michael@0 1242 nscoord marginTopToBaseline = mAscent + mMargin.top;
michael@0 1243
michael@0 1244 if (sideToMeasureFrom == eSideTop) {
michael@0 1245 // Measuring from top (normal case): the distance from the margin-box top
michael@0 1246 // edge to the baseline is just ascent + margin-top.
michael@0 1247 return marginTopToBaseline;
michael@0 1248 }
michael@0 1249
michael@0 1250 MOZ_ASSERT(sideToMeasureFrom == eSideBottom,
michael@0 1251 "We already checked that we're dealing with a vertical axis, and "
michael@0 1252 "we're not using the top side, so that only leaves the bottom...");
michael@0 1253
michael@0 1254 // Measuring from bottom: The distance from the margin-box bottom edge to the
michael@0 1255 // baseline is just the margin-box cross size (i.e. outer cross size), minus
michael@0 1256 // the already-computed distance from margin-top to baseline.
michael@0 1257 return GetOuterCrossSize(aCrossAxis) - marginTopToBaseline;
michael@0 1258 }
michael@0 1259
michael@0 1260 uint32_t
michael@0 1261 FlexItem::GetNumAutoMarginsInAxis(AxisOrientationType aAxis) const
michael@0 1262 {
michael@0 1263 uint32_t numAutoMargins = 0;
michael@0 1264 const nsStyleSides& styleMargin = mFrame->StyleMargin()->mMargin;
michael@0 1265 for (uint32_t i = 0; i < eNumAxisEdges; i++) {
michael@0 1266 Side side = kAxisOrientationToSidesMap[aAxis][i];
michael@0 1267 if (styleMargin.GetUnit(side) == eStyleUnit_Auto) {
michael@0 1268 numAutoMargins++;
michael@0 1269 }
michael@0 1270 }
michael@0 1271
michael@0 1272 // Mostly for clarity:
michael@0 1273 MOZ_ASSERT(numAutoMargins <= 2,
michael@0 1274 "We're just looking at one item along one dimension, so we "
michael@0 1275 "should only have examined 2 margins");
michael@0 1276
michael@0 1277 return numAutoMargins;
michael@0 1278 }
michael@0 1279
michael@0 1280 // Keeps track of our position along a particular axis (where a '0' position
michael@0 1281 // corresponds to the 'start' edge of that axis).
michael@0 1282 // This class shouldn't be instantiated directly -- rather, it should only be
michael@0 1283 // instantiated via its subclasses defined below.
michael@0 1284 class MOZ_STACK_CLASS PositionTracker {
michael@0 1285 public:
michael@0 1286 // Accessor for the current value of the position that we're tracking.
michael@0 1287 inline nscoord GetPosition() const { return mPosition; }
michael@0 1288 inline AxisOrientationType GetAxis() const { return mAxis; }
michael@0 1289
michael@0 1290 // Advances our position across the start edge of the given margin, in the
michael@0 1291 // axis we're tracking.
michael@0 1292 void EnterMargin(const nsMargin& aMargin)
michael@0 1293 {
michael@0 1294 Side side = kAxisOrientationToSidesMap[mAxis][eAxisEdge_Start];
michael@0 1295 mPosition += MarginComponentForSide(aMargin, side);
michael@0 1296 }
michael@0 1297
michael@0 1298 // Advances our position across the end edge of the given margin, in the axis
michael@0 1299 // we're tracking.
michael@0 1300 void ExitMargin(const nsMargin& aMargin)
michael@0 1301 {
michael@0 1302 Side side = kAxisOrientationToSidesMap[mAxis][eAxisEdge_End];
michael@0 1303 mPosition += MarginComponentForSide(aMargin, side);
michael@0 1304 }
michael@0 1305
michael@0 1306 // Advances our current position from the start side of a child frame's
michael@0 1307 // border-box to the frame's upper or left edge (depending on our axis).
michael@0 1308 // (Note that this is a no-op if our axis grows in positive direction.)
michael@0 1309 void EnterChildFrame(nscoord aChildFrameSize)
michael@0 1310 {
michael@0 1311 if (!AxisGrowsInPositiveDirection(mAxis))
michael@0 1312 mPosition += aChildFrameSize;
michael@0 1313 }
michael@0 1314
michael@0 1315 // Advances our current position from a frame's upper or left border-box edge
michael@0 1316 // (whichever is in the axis we're tracking) to the 'end' side of the frame
michael@0 1317 // in the axis that we're tracking. (Note that this is a no-op if our axis
michael@0 1318 // grows in the negative direction.)
michael@0 1319 void ExitChildFrame(nscoord aChildFrameSize)
michael@0 1320 {
michael@0 1321 if (AxisGrowsInPositiveDirection(mAxis))
michael@0 1322 mPosition += aChildFrameSize;
michael@0 1323 }
michael@0 1324
michael@0 1325 protected:
michael@0 1326 // Protected constructor, to be sure we're only instantiated via a subclass.
michael@0 1327 PositionTracker(AxisOrientationType aAxis)
michael@0 1328 : mPosition(0),
michael@0 1329 mAxis(aAxis)
michael@0 1330 {}
michael@0 1331
michael@0 1332 private:
michael@0 1333 // Private copy-constructor, since we don't want any instances of our
michael@0 1334 // subclasses to be accidentally copied.
michael@0 1335 PositionTracker(const PositionTracker& aOther)
michael@0 1336 : mPosition(aOther.mPosition),
michael@0 1337 mAxis(aOther.mAxis)
michael@0 1338 {}
michael@0 1339
michael@0 1340 protected:
michael@0 1341 // Member data:
michael@0 1342 nscoord mPosition; // The position we're tracking
michael@0 1343 const AxisOrientationType mAxis; // The axis along which we're moving
michael@0 1344 };
michael@0 1345
michael@0 1346 // Tracks our position in the main axis, when we're laying out flex items.
michael@0 1347 // The "0" position represents the main-start edge of the flex container's
michael@0 1348 // content-box.
michael@0 1349 class MOZ_STACK_CLASS MainAxisPositionTracker : public PositionTracker {
michael@0 1350 public:
michael@0 1351 MainAxisPositionTracker(const FlexboxAxisTracker& aAxisTracker,
michael@0 1352 const FlexLine* aLine,
michael@0 1353 uint8_t aJustifyContent,
michael@0 1354 nscoord aContentBoxMainSize);
michael@0 1355
michael@0 1356 ~MainAxisPositionTracker() {
michael@0 1357 MOZ_ASSERT(mNumPackingSpacesRemaining == 0,
michael@0 1358 "miscounted the number of packing spaces");
michael@0 1359 MOZ_ASSERT(mNumAutoMarginsInMainAxis == 0,
michael@0 1360 "miscounted the number of auto margins");
michael@0 1361 }
michael@0 1362
michael@0 1363 // Advances past the packing space (if any) between two flex items
michael@0 1364 void TraversePackingSpace();
michael@0 1365
michael@0 1366 // If aItem has any 'auto' margins in the main axis, this method updates the
michael@0 1367 // corresponding values in its margin.
michael@0 1368 void ResolveAutoMarginsInMainAxis(FlexItem& aItem);
michael@0 1369
michael@0 1370 private:
michael@0 1371 nscoord mPackingSpaceRemaining;
michael@0 1372 uint32_t mNumAutoMarginsInMainAxis;
michael@0 1373 uint32_t mNumPackingSpacesRemaining;
michael@0 1374 uint8_t mJustifyContent;
michael@0 1375 };
michael@0 1376
michael@0 1377 // Utility class for managing our position along the cross axis along
michael@0 1378 // the whole flex container (at a higher level than a single line).
michael@0 1379 // The "0" position represents the cross-start edge of the flex container's
michael@0 1380 // content-box.
michael@0 1381 class MOZ_STACK_CLASS CrossAxisPositionTracker : public PositionTracker {
michael@0 1382 public:
michael@0 1383 CrossAxisPositionTracker(FlexLine* aFirstLine,
michael@0 1384 uint8_t aAlignContent,
michael@0 1385 nscoord aContentBoxCrossSize,
michael@0 1386 bool aIsCrossSizeDefinite,
michael@0 1387 const FlexboxAxisTracker& aAxisTracker);
michael@0 1388
michael@0 1389 // Advances past the packing space (if any) between two flex lines
michael@0 1390 void TraversePackingSpace();
michael@0 1391
michael@0 1392 // Advances past the given FlexLine
michael@0 1393 void TraverseLine(FlexLine& aLine) { mPosition += aLine.GetLineCrossSize(); }
michael@0 1394
michael@0 1395 private:
michael@0 1396 // Redeclare the frame-related methods from PositionTracker as private with
michael@0 1397 // MOZ_DELETE, to be sure (at compile time) that no client code can invoke
michael@0 1398 // them. (Unlike the other PositionTracker derived classes, this class here
michael@0 1399 // deals with FlexLines, not with individual FlexItems or frames.)
michael@0 1400 void EnterMargin(const nsMargin& aMargin) MOZ_DELETE;
michael@0 1401 void ExitMargin(const nsMargin& aMargin) MOZ_DELETE;
michael@0 1402 void EnterChildFrame(nscoord aChildFrameSize) MOZ_DELETE;
michael@0 1403 void ExitChildFrame(nscoord aChildFrameSize) MOZ_DELETE;
michael@0 1404
michael@0 1405 nscoord mPackingSpaceRemaining;
michael@0 1406 uint32_t mNumPackingSpacesRemaining;
michael@0 1407 uint8_t mAlignContent;
michael@0 1408 };
michael@0 1409
michael@0 1410 // Utility class for managing our position along the cross axis, *within* a
michael@0 1411 // single flex line.
michael@0 1412 class MOZ_STACK_CLASS SingleLineCrossAxisPositionTracker : public PositionTracker {
michael@0 1413 public:
michael@0 1414 SingleLineCrossAxisPositionTracker(const FlexboxAxisTracker& aAxisTracker);
michael@0 1415
michael@0 1416 void ResolveAutoMarginsInCrossAxis(const FlexLine& aLine,
michael@0 1417 FlexItem& aItem);
michael@0 1418
michael@0 1419 void EnterAlignPackingSpace(const FlexLine& aLine,
michael@0 1420 const FlexItem& aItem,
michael@0 1421 const FlexboxAxisTracker& aAxisTracker);
michael@0 1422
michael@0 1423 // Resets our position to the cross-start edge of this line.
michael@0 1424 inline void ResetPosition() { mPosition = 0; }
michael@0 1425 };
michael@0 1426
michael@0 1427 //----------------------------------------------------------------------
michael@0 1428
michael@0 1429 // Frame class boilerplate
michael@0 1430 // =======================
michael@0 1431
michael@0 1432 NS_QUERYFRAME_HEAD(nsFlexContainerFrame)
michael@0 1433 NS_QUERYFRAME_ENTRY(nsFlexContainerFrame)
michael@0 1434 NS_QUERYFRAME_TAIL_INHERITING(nsFlexContainerFrameSuper)
michael@0 1435
michael@0 1436 NS_IMPL_FRAMEARENA_HELPERS(nsFlexContainerFrame)
michael@0 1437
michael@0 1438 nsIFrame*
michael@0 1439 NS_NewFlexContainerFrame(nsIPresShell* aPresShell,
michael@0 1440 nsStyleContext* aContext)
michael@0 1441 {
michael@0 1442 return new (aPresShell) nsFlexContainerFrame(aContext);
michael@0 1443 }
michael@0 1444
michael@0 1445 //----------------------------------------------------------------------
michael@0 1446
michael@0 1447 // nsFlexContainerFrame Method Implementations
michael@0 1448 // ===========================================
michael@0 1449
michael@0 1450 /* virtual */
michael@0 1451 nsFlexContainerFrame::~nsFlexContainerFrame()
michael@0 1452 {
michael@0 1453 }
michael@0 1454
michael@0 1455 template<bool IsLessThanOrEqual(nsIFrame*, nsIFrame*)>
michael@0 1456 /* static */ bool
michael@0 1457 nsFlexContainerFrame::SortChildrenIfNeeded()
michael@0 1458 {
michael@0 1459 if (nsIFrame::IsFrameListSorted<IsLessThanOrEqual>(mFrames)) {
michael@0 1460 return false;
michael@0 1461 }
michael@0 1462
michael@0 1463 nsIFrame::SortFrameList<IsLessThanOrEqual>(mFrames);
michael@0 1464 return true;
michael@0 1465 }
michael@0 1466
michael@0 1467 /* virtual */
michael@0 1468 nsIAtom*
michael@0 1469 nsFlexContainerFrame::GetType() const
michael@0 1470 {
michael@0 1471 return nsGkAtoms::flexContainerFrame;
michael@0 1472 }
michael@0 1473
michael@0 1474 #ifdef DEBUG_FRAME_DUMP
michael@0 1475 nsresult
michael@0 1476 nsFlexContainerFrame::GetFrameName(nsAString& aResult) const
michael@0 1477 {
michael@0 1478 return MakeFrameName(NS_LITERAL_STRING("FlexContainer"), aResult);
michael@0 1479 }
michael@0 1480 #endif
michael@0 1481
michael@0 1482 // Helper for BuildDisplayList, to implement this special-case for flex items
michael@0 1483 // from the spec:
michael@0 1484 // Flex items paint exactly the same as block-level elements in the
michael@0 1485 // normal flow, except that 'z-index' values other than 'auto' create
michael@0 1486 // a stacking context even if 'position' is 'static'.
michael@0 1487 // http://www.w3.org/TR/2012/CR-css3-flexbox-20120918/#painting
michael@0 1488 uint32_t
michael@0 1489 GetDisplayFlagsForFlexItem(nsIFrame* aFrame)
michael@0 1490 {
michael@0 1491 MOZ_ASSERT(aFrame->IsFlexItem(), "Should only be called on flex items");
michael@0 1492
michael@0 1493 const nsStylePosition* pos = aFrame->StylePosition();
michael@0 1494 if (pos->mZIndex.GetUnit() == eStyleUnit_Integer) {
michael@0 1495 return nsIFrame::DISPLAY_CHILD_FORCE_STACKING_CONTEXT;
michael@0 1496 }
michael@0 1497 return nsIFrame::DISPLAY_CHILD_FORCE_PSEUDO_STACKING_CONTEXT;
michael@0 1498 }
michael@0 1499
michael@0 1500 void
michael@0 1501 nsFlexContainerFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
michael@0 1502 const nsRect& aDirtyRect,
michael@0 1503 const nsDisplayListSet& aLists)
michael@0 1504 {
michael@0 1505 NS_ASSERTION(
michael@0 1506 nsIFrame::IsFrameListSorted<IsOrderLEQWithDOMFallback>(mFrames),
michael@0 1507 "Child frames aren't sorted correctly");
michael@0 1508
michael@0 1509 DisplayBorderBackgroundOutline(aBuilder, aLists);
michael@0 1510
michael@0 1511 // Our children are all block-level, so their borders/backgrounds all go on
michael@0 1512 // the BlockBorderBackgrounds list.
michael@0 1513 nsDisplayListSet childLists(aLists, aLists.BlockBorderBackgrounds());
michael@0 1514 for (nsFrameList::Enumerator e(mFrames); !e.AtEnd(); e.Next()) {
michael@0 1515 BuildDisplayListForChild(aBuilder, e.get(), aDirtyRect, childLists,
michael@0 1516 GetDisplayFlagsForFlexItem(e.get()));
michael@0 1517 }
michael@0 1518 }
michael@0 1519
michael@0 1520 #ifdef DEBUG
michael@0 1521 // helper for the debugging method below
michael@0 1522 bool
michael@0 1523 FrameWantsToBeInAnonymousFlexItem(nsIFrame* aFrame)
michael@0 1524 {
michael@0 1525 // Note: This needs to match the logic in
michael@0 1526 // nsCSSFrameConstructor::FrameConstructionItem::NeedsAnonFlexItem()
michael@0 1527 return (aFrame->IsFrameOfType(nsIFrame::eLineParticipant) ||
michael@0 1528 nsGkAtoms::placeholderFrame == aFrame->GetType());
michael@0 1529 }
michael@0 1530
michael@0 1531 // Debugging method, to let us assert that our anonymous flex items are
michael@0 1532 // set up correctly -- in particular, we assert:
michael@0 1533 // (1) we don't have any inline non-replaced children
michael@0 1534 // (2) we don't have any consecutive anonymous flex items
michael@0 1535 // (3) we don't have any empty anonymous flex items
michael@0 1536 //
michael@0 1537 // XXXdholbert This matches what nsCSSFrameConstructor currently does, and what
michael@0 1538 // the spec used to say. However, the spec has now changed regarding what
michael@0 1539 // types of content get wrapped in an anonymous flexbox item. The patch that
michael@0 1540 // implements those changes (in nsCSSFrameConstructor) will need to change
michael@0 1541 // this method as well.
michael@0 1542 void
michael@0 1543 nsFlexContainerFrame::SanityCheckAnonymousFlexItems() const
michael@0 1544 {
michael@0 1545 bool prevChildWasAnonFlexItem = false;
michael@0 1546 for (nsIFrame* child = mFrames.FirstChild(); child;
michael@0 1547 child = child->GetNextSibling()) {
michael@0 1548 MOZ_ASSERT(!FrameWantsToBeInAnonymousFlexItem(child),
michael@0 1549 "frame wants to be inside an anonymous flex item, "
michael@0 1550 "but it isn't");
michael@0 1551 if (child->StyleContext()->GetPseudo() ==
michael@0 1552 nsCSSAnonBoxes::anonymousFlexItem) {
michael@0 1553 MOZ_ASSERT(!prevChildWasAnonFlexItem ||
michael@0 1554 HasAnyStateBits(NS_STATE_FLEX_CHILDREN_REORDERED),
michael@0 1555 "two anon flex items in a row (shouldn't happen, unless our "
michael@0 1556 "children have been reordered with the 'order' property)");
michael@0 1557
michael@0 1558 nsIFrame* firstWrappedChild = child->GetFirstPrincipalChild();
michael@0 1559 MOZ_ASSERT(firstWrappedChild,
michael@0 1560 "anonymous flex item is empty (shouldn't happen)");
michael@0 1561 prevChildWasAnonFlexItem = true;
michael@0 1562 } else {
michael@0 1563 prevChildWasAnonFlexItem = false;
michael@0 1564 }
michael@0 1565 }
michael@0 1566 }
michael@0 1567 #endif // DEBUG
michael@0 1568
michael@0 1569 // Based on the sign of aTotalViolation, this function freezes a subset of our
michael@0 1570 // flexible sizes, and restores the remaining ones to their initial pref sizes.
michael@0 1571 void
michael@0 1572 FlexLine::FreezeOrRestoreEachFlexibleSize(const nscoord aTotalViolation,
michael@0 1573 bool aIsFinalIteration)
michael@0 1574 {
michael@0 1575 enum FreezeType {
michael@0 1576 eFreezeEverything,
michael@0 1577 eFreezeMinViolations,
michael@0 1578 eFreezeMaxViolations
michael@0 1579 };
michael@0 1580
michael@0 1581 FreezeType freezeType;
michael@0 1582 if (aTotalViolation == 0) {
michael@0 1583 freezeType = eFreezeEverything;
michael@0 1584 } else if (aTotalViolation > 0) {
michael@0 1585 freezeType = eFreezeMinViolations;
michael@0 1586 } else { // aTotalViolation < 0
michael@0 1587 freezeType = eFreezeMaxViolations;
michael@0 1588 }
michael@0 1589
michael@0 1590 for (FlexItem* item = mItems.getFirst(); item; item = item->getNext()) {
michael@0 1591 MOZ_ASSERT(!item->HadMinViolation() || !item->HadMaxViolation(),
michael@0 1592 "Can have either min or max violation, but not both");
michael@0 1593
michael@0 1594 if (!item->IsFrozen()) {
michael@0 1595 if (eFreezeEverything == freezeType ||
michael@0 1596 (eFreezeMinViolations == freezeType && item->HadMinViolation()) ||
michael@0 1597 (eFreezeMaxViolations == freezeType && item->HadMaxViolation())) {
michael@0 1598
michael@0 1599 MOZ_ASSERT(item->GetMainSize() >= item->GetMainMinSize(),
michael@0 1600 "Freezing item at a size below its minimum");
michael@0 1601 MOZ_ASSERT(item->GetMainSize() <= item->GetMainMaxSize(),
michael@0 1602 "Freezing item at a size above its maximum");
michael@0 1603
michael@0 1604 item->Freeze();
michael@0 1605 } else if (MOZ_UNLIKELY(aIsFinalIteration)) {
michael@0 1606 // XXXdholbert If & when bug 765861 is fixed, we should upgrade this
michael@0 1607 // assertion to be fatal except in documents with enormous lengths.
michael@0 1608 NS_ERROR("Final iteration still has unfrozen items, this shouldn't"
michael@0 1609 " happen unless there was nscoord under/overflow.");
michael@0 1610 item->Freeze();
michael@0 1611 } // else, we'll reset this item's main size to its flex base size on the
michael@0 1612 // next iteration of this algorithm.
michael@0 1613
michael@0 1614 // Clear this item's violation(s), now that we've dealt with them
michael@0 1615 item->ClearViolationFlags();
michael@0 1616 }
michael@0 1617 }
michael@0 1618 }
michael@0 1619
michael@0 1620 void
michael@0 1621 FlexLine::ResolveFlexibleLengths(nscoord aFlexContainerMainSize)
michael@0 1622 {
michael@0 1623 PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG, ("ResolveFlexibleLengths\n"));
michael@0 1624 if (IsEmpty()) {
michael@0 1625 return;
michael@0 1626 }
michael@0 1627
michael@0 1628 // Subtract space occupied by our items' margins/borders/padding, so we can
michael@0 1629 // just be dealing with the space available for our flex items' content
michael@0 1630 // boxes.
michael@0 1631 nscoord spaceReservedForMarginBorderPadding =
michael@0 1632 mTotalOuterHypotheticalMainSize - mTotalInnerHypotheticalMainSize;
michael@0 1633
michael@0 1634 nscoord spaceAvailableForFlexItemsContentBoxes =
michael@0 1635 aFlexContainerMainSize - spaceReservedForMarginBorderPadding;
michael@0 1636
michael@0 1637 // Determine whether we're going to be growing or shrinking items.
michael@0 1638 const bool isUsingFlexGrow =
michael@0 1639 (mTotalOuterHypotheticalMainSize < aFlexContainerMainSize);
michael@0 1640
michael@0 1641 // NOTE: I claim that this chunk of the algorithm (the looping part) needs to
michael@0 1642 // run the loop at MOST mNumItems times. This claim should hold up
michael@0 1643 // because we'll freeze at least one item on each loop iteration, and once
michael@0 1644 // we've run out of items to freeze, there's nothing left to do. However,
michael@0 1645 // in most cases, we'll break out of this loop long before we hit that many
michael@0 1646 // iterations.
michael@0 1647 for (uint32_t iterationCounter = 0;
michael@0 1648 iterationCounter < mNumItems; iterationCounter++) {
michael@0 1649 // Set every not-yet-frozen item's used main size to its
michael@0 1650 // flex base size, and subtract all the used main sizes from our
michael@0 1651 // total amount of space to determine the 'available free space'
michael@0 1652 // (positive or negative) to be distributed among our flexible items.
michael@0 1653 nscoord availableFreeSpace = spaceAvailableForFlexItemsContentBoxes;
michael@0 1654 for (FlexItem* item = mItems.getFirst(); item; item = item->getNext()) {
michael@0 1655 if (!item->IsFrozen()) {
michael@0 1656 item->SetMainSize(item->GetFlexBaseSize());
michael@0 1657 }
michael@0 1658 availableFreeSpace -= item->GetMainSize();
michael@0 1659 }
michael@0 1660
michael@0 1661 PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG,
michael@0 1662 (" available free space = %d\n", availableFreeSpace));
michael@0 1663
michael@0 1664 // If sign of free space matches the type of flexing that we're doing, give
michael@0 1665 // each flexible item a portion of availableFreeSpace.
michael@0 1666 if ((availableFreeSpace > 0 && isUsingFlexGrow) ||
michael@0 1667 (availableFreeSpace < 0 && !isUsingFlexGrow)) {
michael@0 1668
michael@0 1669 // STRATEGY: On each item, we compute & store its "share" of the total
michael@0 1670 // flex weight that we've seen so far:
michael@0 1671 // curFlexWeight / runningFlexWeightSum
michael@0 1672 //
michael@0 1673 // Then, when we go to actually distribute the space (in the next loop),
michael@0 1674 // we can simply walk backwards through the elements and give each item
michael@0 1675 // its "share" multiplied by the remaining available space.
michael@0 1676 //
michael@0 1677 // SPECIAL CASE: If the sum of the flex weights is larger than the
michael@0 1678 // maximum representable float (overflowing to infinity), then we can't
michael@0 1679 // sensibly divide out proportional shares anymore. In that case, we
michael@0 1680 // simply treat the flex item(s) with the largest flex weights as if
michael@0 1681 // their weights were infinite (dwarfing all the others), and we
michael@0 1682 // distribute all of the available space among them.
michael@0 1683 float runningFlexWeightSum = 0.0f;
michael@0 1684 float largestFlexWeight = 0.0f;
michael@0 1685 uint32_t numItemsWithLargestFlexWeight = 0;
michael@0 1686 for (FlexItem* item = mItems.getFirst(); item; item = item->getNext()) {
michael@0 1687 float curFlexWeight = item->GetFlexWeightToUse(isUsingFlexGrow);
michael@0 1688 MOZ_ASSERT(curFlexWeight >= 0.0f, "weights are non-negative");
michael@0 1689
michael@0 1690 runningFlexWeightSum += curFlexWeight;
michael@0 1691 if (NS_finite(runningFlexWeightSum)) {
michael@0 1692 if (curFlexWeight == 0.0f) {
michael@0 1693 item->SetShareOfFlexWeightSoFar(0.0f);
michael@0 1694 } else {
michael@0 1695 item->SetShareOfFlexWeightSoFar(curFlexWeight /
michael@0 1696 runningFlexWeightSum);
michael@0 1697 }
michael@0 1698 } // else, the sum of weights overflows to infinity, in which
michael@0 1699 // case we don't bother with "SetShareOfFlexWeightSoFar" since
michael@0 1700 // we know we won't use it. (instead, we'll just give every
michael@0 1701 // item with the largest flex weight an equal share of space.)
michael@0 1702
michael@0 1703 // Update our largest-flex-weight tracking vars
michael@0 1704 if (curFlexWeight > largestFlexWeight) {
michael@0 1705 largestFlexWeight = curFlexWeight;
michael@0 1706 numItemsWithLargestFlexWeight = 1;
michael@0 1707 } else if (curFlexWeight == largestFlexWeight) {
michael@0 1708 numItemsWithLargestFlexWeight++;
michael@0 1709 }
michael@0 1710 }
michael@0 1711
michael@0 1712 if (runningFlexWeightSum != 0.0f) { // no distribution if no flexibility
michael@0 1713 PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG,
michael@0 1714 (" Distributing available space:"));
michael@0 1715 // NOTE: It's important that we traverse our items in *reverse* order
michael@0 1716 // here, for correct width distribution according to the items'
michael@0 1717 // "ShareOfFlexWeightSoFar" progressively-calculated values.
michael@0 1718 for (FlexItem* item = mItems.getLast(); item;
michael@0 1719 item = item->getPrevious()) {
michael@0 1720
michael@0 1721 if (!item->IsFrozen()) {
michael@0 1722 // To avoid rounding issues, we compute the change in size for this
michael@0 1723 // item, and then subtract it from the remaining available space.
michael@0 1724 nscoord sizeDelta = 0;
michael@0 1725 if (NS_finite(runningFlexWeightSum)) {
michael@0 1726 float myShareOfRemainingSpace =
michael@0 1727 item->GetShareOfFlexWeightSoFar();
michael@0 1728
michael@0 1729 MOZ_ASSERT(myShareOfRemainingSpace >= 0.0f &&
michael@0 1730 myShareOfRemainingSpace <= 1.0f,
michael@0 1731 "my share should be nonnegative fractional amount");
michael@0 1732
michael@0 1733 if (myShareOfRemainingSpace == 1.0f) {
michael@0 1734 // (We special-case 1.0f to avoid float error from converting
michael@0 1735 // availableFreeSpace from integer*1.0f --> float --> integer)
michael@0 1736 sizeDelta = availableFreeSpace;
michael@0 1737 } else if (myShareOfRemainingSpace > 0.0f) {
michael@0 1738 sizeDelta = NSToCoordRound(availableFreeSpace *
michael@0 1739 myShareOfRemainingSpace);
michael@0 1740 }
michael@0 1741 } else if (item->GetFlexWeightToUse(isUsingFlexGrow) ==
michael@0 1742 largestFlexWeight) {
michael@0 1743 // Total flexibility is infinite, so we're just distributing
michael@0 1744 // the available space equally among the items that are tied for
michael@0 1745 // having the largest weight (and this is one of those items).
michael@0 1746 sizeDelta =
michael@0 1747 NSToCoordRound(availableFreeSpace /
michael@0 1748 float(numItemsWithLargestFlexWeight));
michael@0 1749 numItemsWithLargestFlexWeight--;
michael@0 1750 }
michael@0 1751
michael@0 1752 availableFreeSpace -= sizeDelta;
michael@0 1753
michael@0 1754 item->SetMainSize(item->GetMainSize() + sizeDelta);
michael@0 1755 PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG,
michael@0 1756 (" child %p receives %d, for a total of %d\n",
michael@0 1757 item, sizeDelta, item->GetMainSize()));
michael@0 1758 }
michael@0 1759 }
michael@0 1760 }
michael@0 1761 }
michael@0 1762
michael@0 1763 // Fix min/max violations:
michael@0 1764 nscoord totalViolation = 0; // keeps track of adjustments for min/max
michael@0 1765 PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG,
michael@0 1766 (" Checking for violations:"));
michael@0 1767
michael@0 1768 for (FlexItem* item = mItems.getFirst(); item; item = item->getNext()) {
michael@0 1769 if (!item->IsFrozen()) {
michael@0 1770 if (item->GetMainSize() < item->GetMainMinSize()) {
michael@0 1771 // min violation
michael@0 1772 totalViolation += item->GetMainMinSize() - item->GetMainSize();
michael@0 1773 item->SetMainSize(item->GetMainMinSize());
michael@0 1774 item->SetHadMinViolation();
michael@0 1775 } else if (item->GetMainSize() > item->GetMainMaxSize()) {
michael@0 1776 // max violation
michael@0 1777 totalViolation += item->GetMainMaxSize() - item->GetMainSize();
michael@0 1778 item->SetMainSize(item->GetMainMaxSize());
michael@0 1779 item->SetHadMaxViolation();
michael@0 1780 }
michael@0 1781 }
michael@0 1782 }
michael@0 1783
michael@0 1784 FreezeOrRestoreEachFlexibleSize(totalViolation,
michael@0 1785 iterationCounter + 1 == mNumItems);
michael@0 1786
michael@0 1787 PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG,
michael@0 1788 (" Total violation: %d\n", totalViolation));
michael@0 1789
michael@0 1790 if (totalViolation == 0) {
michael@0 1791 break;
michael@0 1792 }
michael@0 1793 }
michael@0 1794
michael@0 1795 // Post-condition: all lengths should've been frozen.
michael@0 1796 #ifdef DEBUG
michael@0 1797 for (const FlexItem* item = mItems.getFirst(); item; item = item->getNext()) {
michael@0 1798 MOZ_ASSERT(item->IsFrozen(),
michael@0 1799 "All flexible lengths should've been resolved");
michael@0 1800 }
michael@0 1801 #endif // DEBUG
michael@0 1802 }
michael@0 1803
michael@0 1804 MainAxisPositionTracker::
michael@0 1805 MainAxisPositionTracker(const FlexboxAxisTracker& aAxisTracker,
michael@0 1806 const FlexLine* aLine,
michael@0 1807 uint8_t aJustifyContent,
michael@0 1808 nscoord aContentBoxMainSize)
michael@0 1809 : PositionTracker(aAxisTracker.GetMainAxis()),
michael@0 1810 mPackingSpaceRemaining(aContentBoxMainSize), // we chip away at this below
michael@0 1811 mNumAutoMarginsInMainAxis(0),
michael@0 1812 mNumPackingSpacesRemaining(0),
michael@0 1813 mJustifyContent(aJustifyContent)
michael@0 1814 {
michael@0 1815 // mPackingSpaceRemaining is initialized to the container's main size. Now
michael@0 1816 // we'll subtract out the main sizes of our flex items, so that it ends up
michael@0 1817 // with the *actual* amount of packing space.
michael@0 1818 for (const FlexItem* item = aLine->GetFirstItem(); item;
michael@0 1819 item = item->getNext()) {
michael@0 1820 mPackingSpaceRemaining -= item->GetOuterMainSize(mAxis);
michael@0 1821 mNumAutoMarginsInMainAxis += item->GetNumAutoMarginsInAxis(mAxis);
michael@0 1822 }
michael@0 1823
michael@0 1824 if (mPackingSpaceRemaining <= 0) {
michael@0 1825 // No available packing space to use for resolving auto margins.
michael@0 1826 mNumAutoMarginsInMainAxis = 0;
michael@0 1827 }
michael@0 1828
michael@0 1829 // If packing space is negative, 'space-between' behaves like 'flex-start',
michael@0 1830 // and 'space-around' behaves like 'center'. In those cases, it's simplest to
michael@0 1831 // just pretend we have a different 'justify-content' value and share code.
michael@0 1832 if (mPackingSpaceRemaining < 0) {
michael@0 1833 if (mJustifyContent == NS_STYLE_JUSTIFY_CONTENT_SPACE_BETWEEN) {
michael@0 1834 mJustifyContent = NS_STYLE_JUSTIFY_CONTENT_FLEX_START;
michael@0 1835 } else if (mJustifyContent == NS_STYLE_JUSTIFY_CONTENT_SPACE_AROUND) {
michael@0 1836 mJustifyContent = NS_STYLE_JUSTIFY_CONTENT_CENTER;
michael@0 1837 }
michael@0 1838 }
michael@0 1839
michael@0 1840 // If our main axis is (internally) reversed, swap the justify-content
michael@0 1841 // "flex-start" and "flex-end" behaviors:
michael@0 1842 if (aAxisTracker.AreAxesInternallyReversed()) {
michael@0 1843 if (mJustifyContent == NS_STYLE_JUSTIFY_CONTENT_FLEX_START) {
michael@0 1844 mJustifyContent = NS_STYLE_JUSTIFY_CONTENT_FLEX_END;
michael@0 1845 } else if (mJustifyContent == NS_STYLE_JUSTIFY_CONTENT_FLEX_END) {
michael@0 1846 mJustifyContent = NS_STYLE_JUSTIFY_CONTENT_FLEX_START;
michael@0 1847 }
michael@0 1848 }
michael@0 1849
michael@0 1850 // Figure out how much space we'll set aside for auto margins or
michael@0 1851 // packing spaces, and advance past any leading packing-space.
michael@0 1852 if (mNumAutoMarginsInMainAxis == 0 &&
michael@0 1853 mPackingSpaceRemaining != 0 &&
michael@0 1854 !aLine->IsEmpty()) {
michael@0 1855 switch (mJustifyContent) {
michael@0 1856 case NS_STYLE_JUSTIFY_CONTENT_FLEX_START:
michael@0 1857 // All packing space should go at the end --> nothing to do here.
michael@0 1858 break;
michael@0 1859 case NS_STYLE_JUSTIFY_CONTENT_FLEX_END:
michael@0 1860 // All packing space goes at the beginning
michael@0 1861 mPosition += mPackingSpaceRemaining;
michael@0 1862 break;
michael@0 1863 case NS_STYLE_JUSTIFY_CONTENT_CENTER:
michael@0 1864 // Half the packing space goes at the beginning
michael@0 1865 mPosition += mPackingSpaceRemaining / 2;
michael@0 1866 break;
michael@0 1867 case NS_STYLE_JUSTIFY_CONTENT_SPACE_BETWEEN:
michael@0 1868 MOZ_ASSERT(mPackingSpaceRemaining >= 0,
michael@0 1869 "negative packing space should make us use 'flex-start' "
michael@0 1870 "instead of 'space-between'");
michael@0 1871 // 1 packing space between each flex item, no packing space at ends.
michael@0 1872 mNumPackingSpacesRemaining = aLine->NumItems() - 1;
michael@0 1873 break;
michael@0 1874 case NS_STYLE_JUSTIFY_CONTENT_SPACE_AROUND:
michael@0 1875 MOZ_ASSERT(mPackingSpaceRemaining >= 0,
michael@0 1876 "negative packing space should make us use 'center' "
michael@0 1877 "instead of 'space-around'");
michael@0 1878 // 1 packing space between each flex item, plus half a packing space
michael@0 1879 // at beginning & end. So our number of full packing-spaces is equal
michael@0 1880 // to the number of flex items.
michael@0 1881 mNumPackingSpacesRemaining = aLine->NumItems();
michael@0 1882 if (mNumPackingSpacesRemaining > 0) {
michael@0 1883 // The edges (start/end) share one full packing space
michael@0 1884 nscoord totalEdgePackingSpace =
michael@0 1885 mPackingSpaceRemaining / mNumPackingSpacesRemaining;
michael@0 1886
michael@0 1887 // ...and we'll use half of that right now, at the start
michael@0 1888 mPosition += totalEdgePackingSpace / 2;
michael@0 1889 // ...but we need to subtract all of it right away, so that we won't
michael@0 1890 // hand out any of it to intermediate packing spaces.
michael@0 1891 mPackingSpaceRemaining -= totalEdgePackingSpace;
michael@0 1892 mNumPackingSpacesRemaining--;
michael@0 1893 }
michael@0 1894 break;
michael@0 1895 default:
michael@0 1896 MOZ_CRASH("Unexpected justify-content value");
michael@0 1897 }
michael@0 1898 }
michael@0 1899
michael@0 1900 MOZ_ASSERT(mNumPackingSpacesRemaining == 0 ||
michael@0 1901 mNumAutoMarginsInMainAxis == 0,
michael@0 1902 "extra space should either go to packing space or to "
michael@0 1903 "auto margins, but not to both");
michael@0 1904 }
michael@0 1905
michael@0 1906 void
michael@0 1907 MainAxisPositionTracker::ResolveAutoMarginsInMainAxis(FlexItem& aItem)
michael@0 1908 {
michael@0 1909 if (mNumAutoMarginsInMainAxis) {
michael@0 1910 const nsStyleSides& styleMargin = aItem.Frame()->StyleMargin()->mMargin;
michael@0 1911 for (uint32_t i = 0; i < eNumAxisEdges; i++) {
michael@0 1912 Side side = kAxisOrientationToSidesMap[mAxis][i];
michael@0 1913 if (styleMargin.GetUnit(side) == eStyleUnit_Auto) {
michael@0 1914 // NOTE: This integer math will skew the distribution of remainder
michael@0 1915 // app-units towards the end, which is fine.
michael@0 1916 nscoord curAutoMarginSize =
michael@0 1917 mPackingSpaceRemaining / mNumAutoMarginsInMainAxis;
michael@0 1918
michael@0 1919 MOZ_ASSERT(aItem.GetMarginComponentForSide(side) == 0,
michael@0 1920 "Expecting auto margins to have value '0' before we "
michael@0 1921 "resolve them");
michael@0 1922 aItem.SetMarginComponentForSide(side, curAutoMarginSize);
michael@0 1923
michael@0 1924 mNumAutoMarginsInMainAxis--;
michael@0 1925 mPackingSpaceRemaining -= curAutoMarginSize;
michael@0 1926 }
michael@0 1927 }
michael@0 1928 }
michael@0 1929 }
michael@0 1930
michael@0 1931 void
michael@0 1932 MainAxisPositionTracker::TraversePackingSpace()
michael@0 1933 {
michael@0 1934 if (mNumPackingSpacesRemaining) {
michael@0 1935 MOZ_ASSERT(mJustifyContent == NS_STYLE_JUSTIFY_CONTENT_SPACE_BETWEEN ||
michael@0 1936 mJustifyContent == NS_STYLE_JUSTIFY_CONTENT_SPACE_AROUND,
michael@0 1937 "mNumPackingSpacesRemaining only applies for "
michael@0 1938 "space-between/space-around");
michael@0 1939
michael@0 1940 MOZ_ASSERT(mPackingSpaceRemaining >= 0,
michael@0 1941 "ran out of packing space earlier than we expected");
michael@0 1942
michael@0 1943 // NOTE: This integer math will skew the distribution of remainder
michael@0 1944 // app-units towards the end, which is fine.
michael@0 1945 nscoord curPackingSpace =
michael@0 1946 mPackingSpaceRemaining / mNumPackingSpacesRemaining;
michael@0 1947
michael@0 1948 mPosition += curPackingSpace;
michael@0 1949 mNumPackingSpacesRemaining--;
michael@0 1950 mPackingSpaceRemaining -= curPackingSpace;
michael@0 1951 }
michael@0 1952 }
michael@0 1953
michael@0 1954 CrossAxisPositionTracker::
michael@0 1955 CrossAxisPositionTracker(FlexLine* aFirstLine,
michael@0 1956 uint8_t aAlignContent,
michael@0 1957 nscoord aContentBoxCrossSize,
michael@0 1958 bool aIsCrossSizeDefinite,
michael@0 1959 const FlexboxAxisTracker& aAxisTracker)
michael@0 1960 : PositionTracker(aAxisTracker.GetCrossAxis()),
michael@0 1961 mPackingSpaceRemaining(0),
michael@0 1962 mNumPackingSpacesRemaining(0),
michael@0 1963 mAlignContent(aAlignContent)
michael@0 1964 {
michael@0 1965 MOZ_ASSERT(aFirstLine, "null first line pointer");
michael@0 1966
michael@0 1967 if (aIsCrossSizeDefinite && !aFirstLine->getNext()) {
michael@0 1968 // "If the flex container has only a single line (even if it's a
michael@0 1969 // multi-line flex container) and has a definite cross size, the cross
michael@0 1970 // size of the flex line is the flex container's inner cross size."
michael@0 1971 // SOURCE: http://dev.w3.org/csswg/css-flexbox/#algo-line-break
michael@0 1972 // NOTE: This means (by definition) that there's no packing space, which
michael@0 1973 // means we don't need to be concerned with "align-conent" at all and we
michael@0 1974 // can return early. This is handy, because this is the usual case (for
michael@0 1975 // single-line flexbox).
michael@0 1976 aFirstLine->SetLineCrossSize(aContentBoxCrossSize);
michael@0 1977 return;
michael@0 1978 }
michael@0 1979
michael@0 1980 // NOTE: The rest of this function should essentially match
michael@0 1981 // MainAxisPositionTracker's constructor, though with FlexLines instead of
michael@0 1982 // FlexItems, and with the additional value "stretch" (and of course with
michael@0 1983 // cross sizes instead of main sizes.)
michael@0 1984
michael@0 1985 // Figure out how much packing space we have (container's cross size minus
michael@0 1986 // all the lines' cross sizes). Also, share this loop to count how many
michael@0 1987 // lines we have. (We need that count in some cases below.)
michael@0 1988 mPackingSpaceRemaining = aContentBoxCrossSize;
michael@0 1989 uint32_t numLines = 0;
michael@0 1990 for (FlexLine* line = aFirstLine; line; line = line->getNext()) {
michael@0 1991 mPackingSpaceRemaining -= line->GetLineCrossSize();
michael@0 1992 numLines++;
michael@0 1993 }
michael@0 1994
michael@0 1995 // If packing space is negative, 'space-between' and 'stretch' behave like
michael@0 1996 // 'flex-start', and 'space-around' behaves like 'center'. In those cases,
michael@0 1997 // it's simplest to just pretend we have a different 'align-content' value
michael@0 1998 // and share code.
michael@0 1999 if (mPackingSpaceRemaining < 0) {
michael@0 2000 if (mAlignContent == NS_STYLE_ALIGN_CONTENT_SPACE_BETWEEN ||
michael@0 2001 mAlignContent == NS_STYLE_ALIGN_CONTENT_STRETCH) {
michael@0 2002 mAlignContent = NS_STYLE_ALIGN_CONTENT_FLEX_START;
michael@0 2003 } else if (mAlignContent == NS_STYLE_ALIGN_CONTENT_SPACE_AROUND) {
michael@0 2004 mAlignContent = NS_STYLE_ALIGN_CONTENT_CENTER;
michael@0 2005 }
michael@0 2006 }
michael@0 2007
michael@0 2008 // If our cross axis is (internally) reversed, swap the align-content
michael@0 2009 // "flex-start" and "flex-end" behaviors:
michael@0 2010 if (aAxisTracker.AreAxesInternallyReversed()) {
michael@0 2011 if (mAlignContent == NS_STYLE_ALIGN_CONTENT_FLEX_START) {
michael@0 2012 mAlignContent = NS_STYLE_ALIGN_CONTENT_FLEX_END;
michael@0 2013 } else if (mAlignContent == NS_STYLE_ALIGN_CONTENT_FLEX_END) {
michael@0 2014 mAlignContent = NS_STYLE_ALIGN_CONTENT_FLEX_START;
michael@0 2015 }
michael@0 2016 }
michael@0 2017
michael@0 2018 // Figure out how much space we'll set aside for packing spaces, and advance
michael@0 2019 // past any leading packing-space.
michael@0 2020 if (mPackingSpaceRemaining != 0) {
michael@0 2021 switch (mAlignContent) {
michael@0 2022 case NS_STYLE_ALIGN_CONTENT_FLEX_START:
michael@0 2023 // All packing space should go at the end --> nothing to do here.
michael@0 2024 break;
michael@0 2025 case NS_STYLE_ALIGN_CONTENT_FLEX_END:
michael@0 2026 // All packing space goes at the beginning
michael@0 2027 mPosition += mPackingSpaceRemaining;
michael@0 2028 break;
michael@0 2029 case NS_STYLE_ALIGN_CONTENT_CENTER:
michael@0 2030 // Half the packing space goes at the beginning
michael@0 2031 mPosition += mPackingSpaceRemaining / 2;
michael@0 2032 break;
michael@0 2033 case NS_STYLE_ALIGN_CONTENT_SPACE_BETWEEN:
michael@0 2034 MOZ_ASSERT(mPackingSpaceRemaining >= 0,
michael@0 2035 "negative packing space should make us use 'flex-start' "
michael@0 2036 "instead of 'space-between'");
michael@0 2037 // 1 packing space between each flex line, no packing space at ends.
michael@0 2038 mNumPackingSpacesRemaining = numLines - 1;
michael@0 2039 break;
michael@0 2040 case NS_STYLE_ALIGN_CONTENT_SPACE_AROUND: {
michael@0 2041 MOZ_ASSERT(mPackingSpaceRemaining >= 0,
michael@0 2042 "negative packing space should make us use 'center' "
michael@0 2043 "instead of 'space-around'");
michael@0 2044 // 1 packing space between each flex line, plus half a packing space
michael@0 2045 // at beginning & end. So our number of full packing-spaces is equal
michael@0 2046 // to the number of flex lines.
michael@0 2047 mNumPackingSpacesRemaining = numLines;
michael@0 2048 // The edges (start/end) share one full packing space
michael@0 2049 nscoord totalEdgePackingSpace =
michael@0 2050 mPackingSpaceRemaining / mNumPackingSpacesRemaining;
michael@0 2051
michael@0 2052 // ...and we'll use half of that right now, at the start
michael@0 2053 mPosition += totalEdgePackingSpace / 2;
michael@0 2054 // ...but we need to subtract all of it right away, so that we won't
michael@0 2055 // hand out any of it to intermediate packing spaces.
michael@0 2056 mPackingSpaceRemaining -= totalEdgePackingSpace;
michael@0 2057 mNumPackingSpacesRemaining--;
michael@0 2058 break;
michael@0 2059 }
michael@0 2060 case NS_STYLE_ALIGN_CONTENT_STRETCH: {
michael@0 2061 // Split space equally between the lines:
michael@0 2062 MOZ_ASSERT(mPackingSpaceRemaining > 0,
michael@0 2063 "negative packing space should make us use 'flex-start' "
michael@0 2064 "instead of 'stretch' (and we shouldn't bother with this "
michael@0 2065 "code if we have 0 packing space)");
michael@0 2066
michael@0 2067 uint32_t numLinesLeft = numLines;
michael@0 2068 for (FlexLine* line = aFirstLine; line; line = line->getNext()) {
michael@0 2069 // Our share is the amount of space remaining, divided by the number
michael@0 2070 // of lines remainig.
michael@0 2071 MOZ_ASSERT(numLinesLeft > 0, "miscalculated num lines");
michael@0 2072 nscoord shareOfExtraSpace = mPackingSpaceRemaining / numLinesLeft;
michael@0 2073 nscoord newSize = line->GetLineCrossSize() + shareOfExtraSpace;
michael@0 2074 line->SetLineCrossSize(newSize);
michael@0 2075
michael@0 2076 mPackingSpaceRemaining -= shareOfExtraSpace;
michael@0 2077 numLinesLeft--;
michael@0 2078 }
michael@0 2079 MOZ_ASSERT(numLinesLeft == 0, "miscalculated num lines");
michael@0 2080 break;
michael@0 2081 }
michael@0 2082 default:
michael@0 2083 MOZ_CRASH("Unexpected align-content value");
michael@0 2084 }
michael@0 2085 }
michael@0 2086 }
michael@0 2087
michael@0 2088 void
michael@0 2089 CrossAxisPositionTracker::TraversePackingSpace()
michael@0 2090 {
michael@0 2091 if (mNumPackingSpacesRemaining) {
michael@0 2092 MOZ_ASSERT(mAlignContent == NS_STYLE_ALIGN_CONTENT_SPACE_BETWEEN ||
michael@0 2093 mAlignContent == NS_STYLE_ALIGN_CONTENT_SPACE_AROUND,
michael@0 2094 "mNumPackingSpacesRemaining only applies for "
michael@0 2095 "space-between/space-around");
michael@0 2096
michael@0 2097 MOZ_ASSERT(mPackingSpaceRemaining >= 0,
michael@0 2098 "ran out of packing space earlier than we expected");
michael@0 2099
michael@0 2100 // NOTE: This integer math will skew the distribution of remainder
michael@0 2101 // app-units towards the end, which is fine.
michael@0 2102 nscoord curPackingSpace =
michael@0 2103 mPackingSpaceRemaining / mNumPackingSpacesRemaining;
michael@0 2104
michael@0 2105 mPosition += curPackingSpace;
michael@0 2106 mNumPackingSpacesRemaining--;
michael@0 2107 mPackingSpaceRemaining -= curPackingSpace;
michael@0 2108 }
michael@0 2109 }
michael@0 2110
michael@0 2111 SingleLineCrossAxisPositionTracker::
michael@0 2112 SingleLineCrossAxisPositionTracker(const FlexboxAxisTracker& aAxisTracker)
michael@0 2113 : PositionTracker(aAxisTracker.GetCrossAxis())
michael@0 2114 {
michael@0 2115 }
michael@0 2116
michael@0 2117 void
michael@0 2118 FlexLine::ComputeCrossSizeAndBaseline(const FlexboxAxisTracker& aAxisTracker)
michael@0 2119 {
michael@0 2120 nscoord crossStartToFurthestBaseline = nscoord_MIN;
michael@0 2121 nscoord crossEndToFurthestBaseline = nscoord_MIN;
michael@0 2122 nscoord largestOuterCrossSize = 0;
michael@0 2123 for (const FlexItem* item = mItems.getFirst(); item; item = item->getNext()) {
michael@0 2124 nscoord curOuterCrossSize =
michael@0 2125 item->GetOuterCrossSize(aAxisTracker.GetCrossAxis());
michael@0 2126
michael@0 2127 if (item->GetAlignSelf() == NS_STYLE_ALIGN_ITEMS_BASELINE &&
michael@0 2128 item->GetNumAutoMarginsInAxis(aAxisTracker.GetCrossAxis()) == 0) {
michael@0 2129 // FIXME: Once we support "writing-mode", we'll have to do baseline
michael@0 2130 // alignment in vertical flex containers here (w/ horizontal cross-axes).
michael@0 2131
michael@0 2132 // Find distance from our item's cross-start and cross-end margin-box
michael@0 2133 // edges to its baseline.
michael@0 2134 //
michael@0 2135 // Here's a diagram of a flex-item that we might be doing this on.
michael@0 2136 // "mmm" is the margin-box, "bbb" is the border-box. The bottom of
michael@0 2137 // the text "BASE" is the baseline.
michael@0 2138 //
michael@0 2139 // ---(cross-start)---
michael@0 2140 // ___ ___ ___
michael@0 2141 // mmmmmmmmmmmm | |margin-start |
michael@0 2142 // m m | _|_ ___ |
michael@0 2143 // m bbbbbbbb m |curOuterCrossSize | |crossStartToBaseline
michael@0 2144 // m b b m | |ascent |
michael@0 2145 // m b BASE b m | _|_ _|_
michael@0 2146 // m b b m | |
michael@0 2147 // m bbbbbbbb m | |crossEndToBaseline
michael@0 2148 // m m | |
michael@0 2149 // mmmmmmmmmmmm _|_ _|_
michael@0 2150 //
michael@0 2151 // ---(cross-end)---
michael@0 2152 //
michael@0 2153 // We already have the curOuterCrossSize, margin-start, and the ascent.
michael@0 2154 // * We can get crossStartToBaseline by adding margin-start + ascent.
michael@0 2155 // * If we subtract that from the curOuterCrossSize, we get
michael@0 2156 // crossEndToBaseline.
michael@0 2157
michael@0 2158 nscoord crossStartToBaseline =
michael@0 2159 item->GetBaselineOffsetFromOuterCrossEdge(aAxisTracker.GetCrossAxis(),
michael@0 2160 eAxisEdge_Start);
michael@0 2161 nscoord crossEndToBaseline = curOuterCrossSize - crossStartToBaseline;
michael@0 2162
michael@0 2163 // Now, update our "largest" values for these (across all the flex items
michael@0 2164 // in this flex line), so we can use them in computing the line's cross
michael@0 2165 // size below:
michael@0 2166 crossStartToFurthestBaseline = std::max(crossStartToFurthestBaseline,
michael@0 2167 crossStartToBaseline);
michael@0 2168 crossEndToFurthestBaseline = std::max(crossEndToFurthestBaseline,
michael@0 2169 crossEndToBaseline);
michael@0 2170 } else {
michael@0 2171 largestOuterCrossSize = std::max(largestOuterCrossSize, curOuterCrossSize);
michael@0 2172 }
michael@0 2173 }
michael@0 2174
michael@0 2175 // The line's baseline offset is the distance from the line's edge (start or
michael@0 2176 // end, depending on whether we've flipped the axes) to the furthest
michael@0 2177 // item-baseline. The item(s) with that baseline will be exactly aligned with
michael@0 2178 // the line's edge.
michael@0 2179 mBaselineOffset = aAxisTracker.AreAxesInternallyReversed() ?
michael@0 2180 crossEndToFurthestBaseline : crossStartToFurthestBaseline;
michael@0 2181
michael@0 2182 // The line's cross-size is the larger of:
michael@0 2183 // (a) [largest cross-start-to-baseline + largest baseline-to-cross-end] of
michael@0 2184 // all baseline-aligned items with no cross-axis auto margins...
michael@0 2185 // and
michael@0 2186 // (b) largest cross-size of all other children.
michael@0 2187 mLineCrossSize = std::max(crossStartToFurthestBaseline +
michael@0 2188 crossEndToFurthestBaseline,
michael@0 2189 largestOuterCrossSize);
michael@0 2190 }
michael@0 2191
michael@0 2192 void
michael@0 2193 FlexItem::ResolveStretchedCrossSize(nscoord aLineCrossSize,
michael@0 2194 const FlexboxAxisTracker& aAxisTracker)
michael@0 2195 {
michael@0 2196 AxisOrientationType crossAxis = aAxisTracker.GetCrossAxis();
michael@0 2197 // We stretch IFF we are align-self:stretch, have no auto margins in
michael@0 2198 // cross axis, and have cross-axis size property == "auto". If any of those
michael@0 2199 // conditions don't hold up, we won't stretch.
michael@0 2200 if (mAlignSelf != NS_STYLE_ALIGN_ITEMS_STRETCH ||
michael@0 2201 GetNumAutoMarginsInAxis(crossAxis) != 0 ||
michael@0 2202 eStyleUnit_Auto != GetSizePropertyForAxis(mFrame, crossAxis).GetUnit()) {
michael@0 2203 return;
michael@0 2204 }
michael@0 2205
michael@0 2206 // If we've already been stretched, we can bail out early, too.
michael@0 2207 // No need to redo the calculation.
michael@0 2208 if (mIsStretched) {
michael@0 2209 return;
michael@0 2210 }
michael@0 2211
michael@0 2212 // Reserve space for margins & border & padding, and then use whatever
michael@0 2213 // remains as our item's cross-size (clamped to its min/max range).
michael@0 2214 nscoord stretchedSize = aLineCrossSize -
michael@0 2215 GetMarginBorderPaddingSizeInAxis(crossAxis);
michael@0 2216
michael@0 2217 stretchedSize = NS_CSS_MINMAX(stretchedSize, mCrossMinSize, mCrossMaxSize);
michael@0 2218
michael@0 2219 // Update the cross-size & make a note that it's stretched, so we know to
michael@0 2220 // override the reflow state's computed cross-size in our final reflow.
michael@0 2221 SetCrossSize(stretchedSize);
michael@0 2222 mIsStretched = true;
michael@0 2223 }
michael@0 2224
michael@0 2225 void
michael@0 2226 SingleLineCrossAxisPositionTracker::
michael@0 2227 ResolveAutoMarginsInCrossAxis(const FlexLine& aLine,
michael@0 2228 FlexItem& aItem)
michael@0 2229 {
michael@0 2230 // Subtract the space that our item is already occupying, to see how much
michael@0 2231 // space (if any) is available for its auto margins.
michael@0 2232 nscoord spaceForAutoMargins = aLine.GetLineCrossSize() -
michael@0 2233 aItem.GetOuterCrossSize(mAxis);
michael@0 2234
michael@0 2235 if (spaceForAutoMargins <= 0) {
michael@0 2236 return; // No available space --> nothing to do
michael@0 2237 }
michael@0 2238
michael@0 2239 uint32_t numAutoMargins = aItem.GetNumAutoMarginsInAxis(mAxis);
michael@0 2240 if (numAutoMargins == 0) {
michael@0 2241 return; // No auto margins --> nothing to do.
michael@0 2242 }
michael@0 2243
michael@0 2244 // OK, we have at least one auto margin and we have some available space.
michael@0 2245 // Give each auto margin a share of the space.
michael@0 2246 const nsStyleSides& styleMargin = aItem.Frame()->StyleMargin()->mMargin;
michael@0 2247 for (uint32_t i = 0; i < eNumAxisEdges; i++) {
michael@0 2248 Side side = kAxisOrientationToSidesMap[mAxis][i];
michael@0 2249 if (styleMargin.GetUnit(side) == eStyleUnit_Auto) {
michael@0 2250 MOZ_ASSERT(aItem.GetMarginComponentForSide(side) == 0,
michael@0 2251 "Expecting auto margins to have value '0' before we "
michael@0 2252 "update them");
michael@0 2253
michael@0 2254 // NOTE: integer divison is fine here; numAutoMargins is either 1 or 2.
michael@0 2255 // If it's 2 & spaceForAutoMargins is odd, 1st margin gets smaller half.
michael@0 2256 nscoord curAutoMarginSize = spaceForAutoMargins / numAutoMargins;
michael@0 2257 aItem.SetMarginComponentForSide(side, curAutoMarginSize);
michael@0 2258 numAutoMargins--;
michael@0 2259 spaceForAutoMargins -= curAutoMarginSize;
michael@0 2260 }
michael@0 2261 }
michael@0 2262 }
michael@0 2263
michael@0 2264 void
michael@0 2265 SingleLineCrossAxisPositionTracker::
michael@0 2266 EnterAlignPackingSpace(const FlexLine& aLine,
michael@0 2267 const FlexItem& aItem,
michael@0 2268 const FlexboxAxisTracker& aAxisTracker)
michael@0 2269 {
michael@0 2270 // We don't do align-self alignment on items that have auto margins
michael@0 2271 // in the cross axis.
michael@0 2272 if (aItem.GetNumAutoMarginsInAxis(mAxis)) {
michael@0 2273 return;
michael@0 2274 }
michael@0 2275
michael@0 2276 uint8_t alignSelf = aItem.GetAlignSelf();
michael@0 2277 // NOTE: 'stretch' behaves like 'flex-start' once we've stretched any
michael@0 2278 // auto-sized items (which we've already done).
michael@0 2279 if (alignSelf == NS_STYLE_ALIGN_ITEMS_STRETCH) {
michael@0 2280 alignSelf = NS_STYLE_ALIGN_ITEMS_FLEX_START;
michael@0 2281 }
michael@0 2282
michael@0 2283 // If our cross axis is (internally) reversed, swap the align-self
michael@0 2284 // "flex-start" and "flex-end" behaviors:
michael@0 2285 if (aAxisTracker.AreAxesInternallyReversed()) {
michael@0 2286 if (alignSelf == NS_STYLE_ALIGN_ITEMS_FLEX_START) {
michael@0 2287 alignSelf = NS_STYLE_ALIGN_ITEMS_FLEX_END;
michael@0 2288 } else if (alignSelf == NS_STYLE_ALIGN_ITEMS_FLEX_END) {
michael@0 2289 alignSelf = NS_STYLE_ALIGN_ITEMS_FLEX_START;
michael@0 2290 }
michael@0 2291 }
michael@0 2292
michael@0 2293 switch (alignSelf) {
michael@0 2294 case NS_STYLE_ALIGN_ITEMS_FLEX_START:
michael@0 2295 // No space to skip over -- we're done.
michael@0 2296 break;
michael@0 2297 case NS_STYLE_ALIGN_ITEMS_FLEX_END:
michael@0 2298 mPosition += aLine.GetLineCrossSize() - aItem.GetOuterCrossSize(mAxis);
michael@0 2299 break;
michael@0 2300 case NS_STYLE_ALIGN_ITEMS_CENTER:
michael@0 2301 // Note: If cross-size is odd, the "after" space will get the extra unit.
michael@0 2302 mPosition +=
michael@0 2303 (aLine.GetLineCrossSize() - aItem.GetOuterCrossSize(mAxis)) / 2;
michael@0 2304 break;
michael@0 2305 case NS_STYLE_ALIGN_ITEMS_BASELINE: {
michael@0 2306 // Normally, baseline-aligned items are collectively aligned with the
michael@0 2307 // line's cross-start edge; however, if our cross axis is (internally)
michael@0 2308 // reversed, we instead align them with the cross-end edge.
michael@0 2309 nscoord itemBaselineOffset =
michael@0 2310 aItem.GetBaselineOffsetFromOuterCrossEdge(mAxis,
michael@0 2311 aAxisTracker.AreAxesInternallyReversed() ?
michael@0 2312 eAxisEdge_End : eAxisEdge_Start);
michael@0 2313
michael@0 2314 nscoord lineBaselineOffset = aLine.GetBaselineOffset();
michael@0 2315
michael@0 2316 NS_ASSERTION(lineBaselineOffset >= itemBaselineOffset,
michael@0 2317 "failed at finding largest baseline offset");
michael@0 2318
michael@0 2319 // How much do we need to adjust our position (from the line edge),
michael@0 2320 // to get the item's baseline to hit the line's baseline offset:
michael@0 2321 nscoord baselineDiff = lineBaselineOffset - itemBaselineOffset;
michael@0 2322
michael@0 2323 if (aAxisTracker.AreAxesInternallyReversed()) {
michael@0 2324 // Advance to align item w/ line's flex-end edge (as in FLEX_END case):
michael@0 2325 mPosition += aLine.GetLineCrossSize() - aItem.GetOuterCrossSize(mAxis);
michael@0 2326 // ...and step *back* by the baseline adjustment:
michael@0 2327 mPosition -= baselineDiff;
michael@0 2328 } else {
michael@0 2329 // mPosition is already at line's flex-start edge.
michael@0 2330 // From there, we step *forward* by the baseline adjustment:
michael@0 2331 mPosition += baselineDiff;
michael@0 2332 }
michael@0 2333 break;
michael@0 2334 }
michael@0 2335 default:
michael@0 2336 NS_NOTREACHED("Unexpected align-self value");
michael@0 2337 break;
michael@0 2338 }
michael@0 2339 }
michael@0 2340
michael@0 2341 FlexboxAxisTracker::FlexboxAxisTracker(
michael@0 2342 nsFlexContainerFrame* aFlexContainerFrame)
michael@0 2343 : mAreAxesInternallyReversed(false)
michael@0 2344 {
michael@0 2345 const nsStylePosition* pos = aFlexContainerFrame->StylePosition();
michael@0 2346 uint32_t flexDirection = pos->mFlexDirection;
michael@0 2347 uint32_t cssDirection =
michael@0 2348 aFlexContainerFrame->StyleVisibility()->mDirection;
michael@0 2349
michael@0 2350 MOZ_ASSERT(cssDirection == NS_STYLE_DIRECTION_LTR ||
michael@0 2351 cssDirection == NS_STYLE_DIRECTION_RTL,
michael@0 2352 "Unexpected computed value for 'direction' property");
michael@0 2353 // (Not asserting for flexDirection here; it's checked by the switch below.)
michael@0 2354
michael@0 2355 // These are defined according to writing-modes' definitions of
michael@0 2356 // start/end (for the inline dimension) and before/after (for the block
michael@0 2357 // dimension), here:
michael@0 2358 // http://www.w3.org/TR/css3-writing-modes/#logical-directions
michael@0 2359 // (NOTE: I'm intentionally not calling this "inlineAxis"/"blockAxis", since
michael@0 2360 // those terms have explicit definition in the writing-modes spec, which are
michael@0 2361 // the opposite of how I'd be using them here.)
michael@0 2362 // XXXdholbert Once we support the 'writing-mode' property, use its value
michael@0 2363 // here to further customize inlineDimension & blockDimension.
michael@0 2364
michael@0 2365 // Inline dimension ("start-to-end"):
michael@0 2366 AxisOrientationType inlineDimension =
michael@0 2367 cssDirection == NS_STYLE_DIRECTION_RTL ? eAxis_RL : eAxis_LR;
michael@0 2368
michael@0 2369 // Block dimension ("before-to-after"):
michael@0 2370 AxisOrientationType blockDimension = eAxis_TB;
michael@0 2371
michael@0 2372 // Determine main axis:
michael@0 2373 switch (flexDirection) {
michael@0 2374 case NS_STYLE_FLEX_DIRECTION_ROW:
michael@0 2375 mMainAxis = inlineDimension;
michael@0 2376 break;
michael@0 2377 case NS_STYLE_FLEX_DIRECTION_ROW_REVERSE:
michael@0 2378 mMainAxis = GetReverseAxis(inlineDimension);
michael@0 2379 break;
michael@0 2380 case NS_STYLE_FLEX_DIRECTION_COLUMN:
michael@0 2381 mMainAxis = blockDimension;
michael@0 2382 break;
michael@0 2383 case NS_STYLE_FLEX_DIRECTION_COLUMN_REVERSE:
michael@0 2384 mMainAxis = GetReverseAxis(blockDimension);
michael@0 2385 break;
michael@0 2386 default:
michael@0 2387 MOZ_CRASH("Unexpected computed value for 'flex-flow' property");
michael@0 2388 }
michael@0 2389
michael@0 2390 // Determine cross axis:
michael@0 2391 // (This is set up so that a bogus |flexDirection| value will
michael@0 2392 // give us blockDimension.
michael@0 2393 if (flexDirection == NS_STYLE_FLEX_DIRECTION_COLUMN ||
michael@0 2394 flexDirection == NS_STYLE_FLEX_DIRECTION_COLUMN_REVERSE) {
michael@0 2395 mCrossAxis = inlineDimension;
michael@0 2396 } else {
michael@0 2397 mCrossAxis = blockDimension;
michael@0 2398 }
michael@0 2399
michael@0 2400 // "flex-wrap: wrap-reverse" reverses our cross axis.
michael@0 2401 if (pos->mFlexWrap == NS_STYLE_FLEX_WRAP_WRAP_REVERSE) {
michael@0 2402 mCrossAxis = GetReverseAxis(mCrossAxis);
michael@0 2403 }
michael@0 2404
michael@0 2405 // Master switch to enable/disable bug 983427's code for reversing our axes
michael@0 2406 // and reversing some logic, to avoid reflowing children in bottom-to-top
michael@0 2407 // order. (This switch can be removed eventually, but for now, it allows
michael@0 2408 // this special-case code path to be compared against the normal code path.)
michael@0 2409 static bool sPreventBottomToTopChildOrdering = true;
michael@0 2410
michael@0 2411 if (sPreventBottomToTopChildOrdering) {
michael@0 2412 // If either axis is bottom-to-top, we flip both axes (and set a flag
michael@0 2413 // so that we can flip some logic to make the reversal transparent).
michael@0 2414 if (eAxis_BT == mMainAxis || eAxis_BT == mCrossAxis) {
michael@0 2415 mMainAxis = GetReverseAxis(mMainAxis);
michael@0 2416 mCrossAxis = GetReverseAxis(mCrossAxis);
michael@0 2417 mAreAxesInternallyReversed = true;
michael@0 2418 }
michael@0 2419 }
michael@0 2420
michael@0 2421 MOZ_ASSERT(IsAxisHorizontal(mMainAxis) != IsAxisHorizontal(mCrossAxis),
michael@0 2422 "main & cross axes should be in different dimensions");
michael@0 2423 }
michael@0 2424
michael@0 2425 // Allocates a new FlexLine, adds it to the given LinkedList (at the front or
michael@0 2426 // back depending on aShouldInsertAtFront), and returns a pointer to it.
michael@0 2427 static FlexLine*
michael@0 2428 AddNewFlexLineToList(LinkedList<FlexLine>& aLines,
michael@0 2429 bool aShouldInsertAtFront)
michael@0 2430 {
michael@0 2431 FlexLine* newLine = new FlexLine();
michael@0 2432 if (aShouldInsertAtFront) {
michael@0 2433 aLines.insertFront(newLine);
michael@0 2434 } else {
michael@0 2435 aLines.insertBack(newLine);
michael@0 2436 }
michael@0 2437 return newLine;
michael@0 2438 }
michael@0 2439
michael@0 2440 nsresult
michael@0 2441 nsFlexContainerFrame::GenerateFlexLines(
michael@0 2442 nsPresContext* aPresContext,
michael@0 2443 const nsHTMLReflowState& aReflowState,
michael@0 2444 nscoord aContentBoxMainSize,
michael@0 2445 nscoord aAvailableHeightForContent,
michael@0 2446 const nsTArray<StrutInfo>& aStruts,
michael@0 2447 const FlexboxAxisTracker& aAxisTracker,
michael@0 2448 LinkedList<FlexLine>& aLines)
michael@0 2449 {
michael@0 2450 MOZ_ASSERT(aLines.isEmpty(), "Expecting outparam to start out empty");
michael@0 2451
michael@0 2452 const bool isSingleLine =
michael@0 2453 NS_STYLE_FLEX_WRAP_NOWRAP == aReflowState.mStylePosition->mFlexWrap;
michael@0 2454
michael@0 2455 // If we're transparently reversing axes, then we'll need to link up our
michael@0 2456 // FlexItems and FlexLines in the reverse order, so that the rest of flex
michael@0 2457 // layout (with flipped axes) will still produce the correct result.
michael@0 2458 // Here, we declare a convenience bool that we'll pass when adding a new
michael@0 2459 // FlexLine or FlexItem, to make us insert it at the beginning of its list
michael@0 2460 // (so the list ends up reversed).
michael@0 2461 const bool shouldInsertAtFront = aAxisTracker.AreAxesInternallyReversed();
michael@0 2462
michael@0 2463 // We have at least one FlexLine. Even an empty flex container has a single
michael@0 2464 // (empty) flex line.
michael@0 2465 FlexLine* curLine = AddNewFlexLineToList(aLines, shouldInsertAtFront);
michael@0 2466
michael@0 2467 nscoord wrapThreshold;
michael@0 2468 if (isSingleLine) {
michael@0 2469 // Not wrapping. Set threshold to sentinel value that tells us not to wrap.
michael@0 2470 wrapThreshold = NS_UNCONSTRAINEDSIZE;
michael@0 2471 } else {
michael@0 2472 // Wrapping! Set wrap threshold to flex container's content-box main-size.
michael@0 2473 wrapThreshold = aContentBoxMainSize;
michael@0 2474
michael@0 2475 // If the flex container doesn't have a definite content-box main-size
michael@0 2476 // (e.g. if we're 'height:auto'), make sure we at least wrap when we hit
michael@0 2477 // its max main-size.
michael@0 2478 if (wrapThreshold == NS_UNCONSTRAINEDSIZE) {
michael@0 2479 const nscoord flexContainerMaxMainSize =
michael@0 2480 GET_MAIN_COMPONENT(aAxisTracker,
michael@0 2481 aReflowState.ComputedMaxWidth(),
michael@0 2482 aReflowState.ComputedMaxHeight());
michael@0 2483
michael@0 2484 wrapThreshold = flexContainerMaxMainSize;
michael@0 2485 }
michael@0 2486
michael@0 2487 // Also: if we're vertical and paginating, we may need to wrap sooner
michael@0 2488 // (before we run off the end of the page)
michael@0 2489 if (!IsAxisHorizontal(aAxisTracker.GetMainAxis()) &&
michael@0 2490 aAvailableHeightForContent != NS_UNCONSTRAINEDSIZE) {
michael@0 2491 wrapThreshold = std::min(wrapThreshold, aAvailableHeightForContent);
michael@0 2492 }
michael@0 2493 }
michael@0 2494
michael@0 2495 // Tracks the index of the next strut, in aStruts (and when this hits
michael@0 2496 // aStruts.Length(), that means there are no more struts):
michael@0 2497 uint32_t nextStrutIdx = 0;
michael@0 2498
michael@0 2499 // Overall index of the current flex item in the flex container. (This gets
michael@0 2500 // checked against entries in aStruts.)
michael@0 2501 uint32_t itemIdxInContainer = 0;
michael@0 2502
michael@0 2503 for (nsFrameList::Enumerator e(mFrames); !e.AtEnd(); e.Next()) {
michael@0 2504 nsIFrame* childFrame = e.get();
michael@0 2505
michael@0 2506 // Honor "page-break-before", if we're multi-line and this line isn't empty:
michael@0 2507 if (!isSingleLine && !curLine->IsEmpty() &&
michael@0 2508 childFrame->StyleDisplay()->mBreakBefore) {
michael@0 2509 curLine = AddNewFlexLineToList(aLines, shouldInsertAtFront);
michael@0 2510 }
michael@0 2511
michael@0 2512 nsAutoPtr<FlexItem> item;
michael@0 2513 if (nextStrutIdx < aStruts.Length() &&
michael@0 2514 aStruts[nextStrutIdx].mItemIdx == itemIdxInContainer) {
michael@0 2515
michael@0 2516 // Use the simplified "strut" FlexItem constructor:
michael@0 2517 item = new FlexItem(childFrame, aStruts[nextStrutIdx].mStrutCrossSize);
michael@0 2518 nextStrutIdx++;
michael@0 2519 } else {
michael@0 2520 item = GenerateFlexItemForChild(aPresContext, childFrame,
michael@0 2521 aReflowState, aAxisTracker);
michael@0 2522
michael@0 2523 nsresult rv = ResolveFlexItemMaxContentSizing(aPresContext, *item,
michael@0 2524 aReflowState, aAxisTracker);
michael@0 2525 NS_ENSURE_SUCCESS(rv,rv);
michael@0 2526 }
michael@0 2527
michael@0 2528 nscoord itemInnerHypotheticalMainSize = item->GetMainSize();
michael@0 2529 nscoord itemOuterHypotheticalMainSize =
michael@0 2530 item->GetOuterMainSize(aAxisTracker.GetMainAxis());
michael@0 2531
michael@0 2532 // Check if we need to wrap |item| to a new line
michael@0 2533 // (i.e. check if its outer hypothetical main size pushes our line over
michael@0 2534 // the threshold)
michael@0 2535 if (wrapThreshold != NS_UNCONSTRAINEDSIZE &&
michael@0 2536 !curLine->IsEmpty() && // No need to wrap at start of a line.
michael@0 2537 wrapThreshold < (curLine->GetTotalOuterHypotheticalMainSize() +
michael@0 2538 itemOuterHypotheticalMainSize)) {
michael@0 2539 curLine = AddNewFlexLineToList(aLines, shouldInsertAtFront);
michael@0 2540 }
michael@0 2541
michael@0 2542 // Add item to current flex line (and update the line's bookkeeping about
michael@0 2543 // how large its items collectively are).
michael@0 2544 curLine->AddItem(item.forget(), shouldInsertAtFront,
michael@0 2545 itemInnerHypotheticalMainSize,
michael@0 2546 itemOuterHypotheticalMainSize);
michael@0 2547
michael@0 2548 // Honor "page-break-after", if we're multi-line and have more children:
michael@0 2549 if (!isSingleLine && childFrame->GetNextSibling() &&
michael@0 2550 childFrame->StyleDisplay()->mBreakAfter) {
michael@0 2551 curLine = AddNewFlexLineToList(aLines, shouldInsertAtFront);
michael@0 2552 }
michael@0 2553 itemIdxInContainer++;
michael@0 2554 }
michael@0 2555
michael@0 2556 return NS_OK;
michael@0 2557 }
michael@0 2558
michael@0 2559 // Retrieves the content-box main-size of our flex container from the
michael@0 2560 // reflow state (specifically, the main-size of *this continuation* of the
michael@0 2561 // flex container).
michael@0 2562 nscoord
michael@0 2563 nsFlexContainerFrame::GetMainSizeFromReflowState(
michael@0 2564 const nsHTMLReflowState& aReflowState,
michael@0 2565 const FlexboxAxisTracker& aAxisTracker)
michael@0 2566 {
michael@0 2567 if (IsAxisHorizontal(aAxisTracker.GetMainAxis())) {
michael@0 2568 // Horizontal case is easy -- our main size is our computed width
michael@0 2569 // (which is already resolved).
michael@0 2570 return aReflowState.ComputedWidth();
michael@0 2571 }
michael@0 2572
michael@0 2573 return GetEffectiveComputedHeight(aReflowState);
michael@0 2574 }
michael@0 2575
michael@0 2576 // Returns the largest outer hypothetical main-size of any line in |aLines|.
michael@0 2577 // (i.e. the hypothetical main-size of the largest line)
michael@0 2578 static nscoord
michael@0 2579 GetLargestLineMainSize(const FlexLine* aFirstLine)
michael@0 2580 {
michael@0 2581 nscoord largestLineOuterSize = 0;
michael@0 2582 for (const FlexLine* line = aFirstLine; line; line = line->getNext()) {
michael@0 2583 largestLineOuterSize = std::max(largestLineOuterSize,
michael@0 2584 line->GetTotalOuterHypotheticalMainSize());
michael@0 2585 }
michael@0 2586 return largestLineOuterSize;
michael@0 2587 }
michael@0 2588
michael@0 2589 // Returns the content-box main-size of our flex container, based on the
michael@0 2590 // available height (if appropriate) and the main-sizes of the flex items.
michael@0 2591 static nscoord
michael@0 2592 ClampFlexContainerMainSize(const nsHTMLReflowState& aReflowState,
michael@0 2593 const FlexboxAxisTracker& aAxisTracker,
michael@0 2594 nscoord aUnclampedMainSize,
michael@0 2595 nscoord aAvailableHeightForContent,
michael@0 2596 const FlexLine* aFirstLine,
michael@0 2597 nsReflowStatus& aStatus)
michael@0 2598 {
michael@0 2599 MOZ_ASSERT(aFirstLine, "null first line pointer");
michael@0 2600
michael@0 2601 if (IsAxisHorizontal(aAxisTracker.GetMainAxis())) {
michael@0 2602 // Horizontal case is easy -- our main size should already be resolved
michael@0 2603 // before we get a call to Reflow. We don't have to worry about doing
michael@0 2604 // page-breaking or shrinkwrapping in the horizontal axis.
michael@0 2605 return aUnclampedMainSize;
michael@0 2606 }
michael@0 2607
michael@0 2608 if (aUnclampedMainSize != NS_INTRINSICSIZE) {
michael@0 2609 // Vertical case, with fixed height:
michael@0 2610 if (aAvailableHeightForContent == NS_UNCONSTRAINEDSIZE ||
michael@0 2611 aUnclampedMainSize < aAvailableHeightForContent) {
michael@0 2612 // Not in a fragmenting context, OR no need to fragment because we have
michael@0 2613 // more available height than we need. Either way, just use our fixed
michael@0 2614 // height. (Note that the reflow state has already done the appropriate
michael@0 2615 // min/max-height clamping.)
michael@0 2616 return aUnclampedMainSize;
michael@0 2617 }
michael@0 2618
michael@0 2619 // Fragmenting *and* our fixed height is too tall for available height:
michael@0 2620 // Mark incomplete so we get a next-in-flow, and take up all of the
michael@0 2621 // available height (or the amount of height required by our children, if
michael@0 2622 // that's larger; but of course not more than our own computed height).
michael@0 2623 // XXXdholbert For now, we don't support pushing children to our next
michael@0 2624 // continuation or splitting children, so "amount of height required by
michael@0 2625 // our children" is just the main-size (height) of our longest flex line.
michael@0 2626 NS_FRAME_SET_INCOMPLETE(aStatus);
michael@0 2627 nscoord largestLineOuterSize = GetLargestLineMainSize(aFirstLine);
michael@0 2628
michael@0 2629 if (largestLineOuterSize <= aAvailableHeightForContent) {
michael@0 2630 return aAvailableHeightForContent;
michael@0 2631 }
michael@0 2632 return std::min(aUnclampedMainSize, largestLineOuterSize);
michael@0 2633 }
michael@0 2634
michael@0 2635 // Vertical case, with auto-height:
michael@0 2636 // Resolve auto-height to the largest FlexLine-length, clamped to our
michael@0 2637 // computed min/max main-size properties (min-height & max-height).
michael@0 2638 // XXXdholbert Handle constrained-aAvailableHeightForContent case here.
michael@0 2639 nscoord largestLineOuterSize = GetLargestLineMainSize(aFirstLine);
michael@0 2640 return NS_CSS_MINMAX(largestLineOuterSize,
michael@0 2641 aReflowState.ComputedMinHeight(),
michael@0 2642 aReflowState.ComputedMaxHeight());
michael@0 2643 }
michael@0 2644
michael@0 2645 nscoord
michael@0 2646 nsFlexContainerFrame::ComputeCrossSize(const nsHTMLReflowState& aReflowState,
michael@0 2647 const FlexboxAxisTracker& aAxisTracker,
michael@0 2648 nscoord aSumLineCrossSizes,
michael@0 2649 nscoord aAvailableHeightForContent,
michael@0 2650 bool* aIsDefinite,
michael@0 2651 nsReflowStatus& aStatus)
michael@0 2652 {
michael@0 2653 MOZ_ASSERT(aIsDefinite, "outparam pointer must be non-null");
michael@0 2654
michael@0 2655 if (IsAxisHorizontal(aAxisTracker.GetCrossAxis())) {
michael@0 2656 // Cross axis is horizontal: our cross size is our computed width
michael@0 2657 // (which is already resolved).
michael@0 2658 *aIsDefinite = true;
michael@0 2659 return aReflowState.ComputedWidth();
michael@0 2660 }
michael@0 2661
michael@0 2662 nscoord effectiveComputedHeight = GetEffectiveComputedHeight(aReflowState);
michael@0 2663 if (effectiveComputedHeight != NS_INTRINSICSIZE) {
michael@0 2664 // Cross-axis is vertical, and we have a fixed height:
michael@0 2665 *aIsDefinite = true;
michael@0 2666 if (aAvailableHeightForContent == NS_UNCONSTRAINEDSIZE ||
michael@0 2667 effectiveComputedHeight < aAvailableHeightForContent) {
michael@0 2668 // Not in a fragmenting context, OR no need to fragment because we have
michael@0 2669 // more available height than we need. Either way, just use our fixed
michael@0 2670 // height. (Note that the reflow state has already done the appropriate
michael@0 2671 // min/max-height clamping.)
michael@0 2672 return effectiveComputedHeight;
michael@0 2673 }
michael@0 2674
michael@0 2675 // Fragmenting *and* our fixed height is too tall for available height:
michael@0 2676 // Mark incomplete so we get a next-in-flow, and take up all of the
michael@0 2677 // available height (or the amount of height required by our children, if
michael@0 2678 // that's larger; but of course not more than our own computed height).
michael@0 2679 // XXXdholbert For now, we don't support pushing children to our next
michael@0 2680 // continuation or splitting children, so "amount of height required by
michael@0 2681 // our children" is just our line-height.
michael@0 2682 NS_FRAME_SET_INCOMPLETE(aStatus);
michael@0 2683 if (aSumLineCrossSizes <= aAvailableHeightForContent) {
michael@0 2684 return aAvailableHeightForContent;
michael@0 2685 }
michael@0 2686 return std::min(effectiveComputedHeight, aSumLineCrossSizes);
michael@0 2687 }
michael@0 2688
michael@0 2689 // Cross axis is vertical and we have auto-height: shrink-wrap our line(s),
michael@0 2690 // subject to our min-size / max-size constraints in that (vertical) axis.
michael@0 2691 // XXXdholbert Handle constrained-aAvailableHeightForContent case here.
michael@0 2692 *aIsDefinite = false;
michael@0 2693 return NS_CSS_MINMAX(aSumLineCrossSizes,
michael@0 2694 aReflowState.ComputedMinHeight(),
michael@0 2695 aReflowState.ComputedMaxHeight());
michael@0 2696 }
michael@0 2697
michael@0 2698 void
michael@0 2699 FlexLine::PositionItemsInMainAxis(uint8_t aJustifyContent,
michael@0 2700 nscoord aContentBoxMainSize,
michael@0 2701 const FlexboxAxisTracker& aAxisTracker)
michael@0 2702 {
michael@0 2703 MainAxisPositionTracker mainAxisPosnTracker(aAxisTracker, this,
michael@0 2704 aJustifyContent,
michael@0 2705 aContentBoxMainSize);
michael@0 2706 for (FlexItem* item = mItems.getFirst(); item; item = item->getNext()) {
michael@0 2707 nscoord itemMainBorderBoxSize =
michael@0 2708 item->GetMainSize() +
michael@0 2709 item->GetBorderPaddingSizeInAxis(mainAxisPosnTracker.GetAxis());
michael@0 2710
michael@0 2711 // Resolve any main-axis 'auto' margins on aChild to an actual value.
michael@0 2712 mainAxisPosnTracker.ResolveAutoMarginsInMainAxis(*item);
michael@0 2713
michael@0 2714 // Advance our position tracker to child's upper-left content-box corner,
michael@0 2715 // and use that as its position in the main axis.
michael@0 2716 mainAxisPosnTracker.EnterMargin(item->GetMargin());
michael@0 2717 mainAxisPosnTracker.EnterChildFrame(itemMainBorderBoxSize);
michael@0 2718
michael@0 2719 item->SetMainPosition(mainAxisPosnTracker.GetPosition());
michael@0 2720
michael@0 2721 mainAxisPosnTracker.ExitChildFrame(itemMainBorderBoxSize);
michael@0 2722 mainAxisPosnTracker.ExitMargin(item->GetMargin());
michael@0 2723 mainAxisPosnTracker.TraversePackingSpace();
michael@0 2724 }
michael@0 2725 }
michael@0 2726
michael@0 2727 // Helper method to take care of children who ASK_FOR_BASELINE, when
michael@0 2728 // we need their baseline.
michael@0 2729 static void
michael@0 2730 ResolveReflowedChildAscent(nsIFrame* aFrame,
michael@0 2731 nsHTMLReflowMetrics& aChildDesiredSize)
michael@0 2732 {
michael@0 2733 if (aChildDesiredSize.TopAscent() == nsHTMLReflowMetrics::ASK_FOR_BASELINE) {
michael@0 2734 // Use GetFirstLineBaseline(), or just GetBaseline() if that fails.
michael@0 2735 nscoord ascent;
michael@0 2736 if (nsLayoutUtils::GetFirstLineBaseline(aFrame, &ascent)) {
michael@0 2737 aChildDesiredSize.SetTopAscent(ascent);
michael@0 2738 } else {
michael@0 2739 aChildDesiredSize.SetTopAscent(aFrame->GetBaseline());
michael@0 2740 }
michael@0 2741 }
michael@0 2742 }
michael@0 2743
michael@0 2744 /**
michael@0 2745 * Given the flex container's "logical ascent" (i.e. distance from the
michael@0 2746 * flex container's content-box cross-start edge to its baseline), returns
michael@0 2747 * its actual physical ascent value (the distance from the *border-box* top
michael@0 2748 * edge to its baseline).
michael@0 2749 */
michael@0 2750 static nscoord
michael@0 2751 ComputePhysicalAscentFromLogicalAscent(nscoord aLogicalAscent,
michael@0 2752 nscoord aContentBoxCrossSize,
michael@0 2753 const nsHTMLReflowState& aReflowState,
michael@0 2754 const FlexboxAxisTracker& aAxisTracker)
michael@0 2755 {
michael@0 2756 return aReflowState.ComputedPhysicalBorderPadding().top +
michael@0 2757 PhysicalPosFromLogicalPos(aLogicalAscent, aContentBoxCrossSize,
michael@0 2758 aAxisTracker.GetCrossAxis());
michael@0 2759 }
michael@0 2760
michael@0 2761 nsresult
michael@0 2762 nsFlexContainerFrame::SizeItemInCrossAxis(
michael@0 2763 nsPresContext* aPresContext,
michael@0 2764 const FlexboxAxisTracker& aAxisTracker,
michael@0 2765 nsHTMLReflowState& aChildReflowState,
michael@0 2766 FlexItem& aItem)
michael@0 2767 {
michael@0 2768 // In vertical flexbox (with horizontal cross-axis), we can just trust the
michael@0 2769 // reflow state's computed-width as our cross-size. We also don't need to
michael@0 2770 // record the baseline because we'll have converted any "align-self:baseline"
michael@0 2771 // items to be "align-self:flex-start" in the FlexItem constructor.
michael@0 2772 // FIXME: Once we support writing-mode (vertical text), we will be able to
michael@0 2773 // have baseline-aligned items in a vertical flexbox, and we'll need to
michael@0 2774 // record baseline information here.
michael@0 2775 if (IsAxisHorizontal(aAxisTracker.GetCrossAxis())) {
michael@0 2776 MOZ_ASSERT(aItem.GetAlignSelf() != NS_STYLE_ALIGN_ITEMS_BASELINE,
michael@0 2777 "In vert flex container, we depend on FlexItem constructor to "
michael@0 2778 "convert 'align-self: baseline' to 'align-self: flex-start'");
michael@0 2779 aItem.SetCrossSize(aChildReflowState.ComputedWidth());
michael@0 2780 return NS_OK;
michael@0 2781 }
michael@0 2782
michael@0 2783 MOZ_ASSERT(!aItem.HadMeasuringReflow(),
michael@0 2784 "We shouldn't need more than one measuring reflow");
michael@0 2785
michael@0 2786 if (aItem.GetAlignSelf() == NS_STYLE_ALIGN_ITEMS_STRETCH) {
michael@0 2787 // This item's got "align-self: stretch", so we probably imposed a
michael@0 2788 // stretched computed height on it during its previous reflow. We're
michael@0 2789 // not imposing that height for *this* measuring reflow, so we need to
michael@0 2790 // tell it to treat this reflow as a vertical resize (regardless of
michael@0 2791 // whether any of its ancestors are being resized).
michael@0 2792 aChildReflowState.mFlags.mVResize = true;
michael@0 2793 }
michael@0 2794 nsHTMLReflowMetrics childDesiredSize(aChildReflowState);
michael@0 2795 nsReflowStatus childReflowStatus;
michael@0 2796 const uint32_t flags = NS_FRAME_NO_MOVE_FRAME;
michael@0 2797 nsresult rv = ReflowChild(aItem.Frame(), aPresContext,
michael@0 2798 childDesiredSize, aChildReflowState,
michael@0 2799 0, 0, flags, childReflowStatus);
michael@0 2800 aItem.SetHadMeasuringReflow();
michael@0 2801 NS_ENSURE_SUCCESS(rv, rv);
michael@0 2802
michael@0 2803 // XXXdholbert Once we do pagination / splitting, we'll need to actually
michael@0 2804 // handle incomplete childReflowStatuses. But for now, we give our kids
michael@0 2805 // unconstrained available height, which means they should always complete.
michael@0 2806 MOZ_ASSERT(NS_FRAME_IS_COMPLETE(childReflowStatus),
michael@0 2807 "We gave flex item unconstrained available height, so it "
michael@0 2808 "should be complete");
michael@0 2809
michael@0 2810 // Tell the child we're done with its initial reflow.
michael@0 2811 // (Necessary for e.g. GetBaseline() to work below w/out asserting)
michael@0 2812 rv = FinishReflowChild(aItem.Frame(), aPresContext,
michael@0 2813 childDesiredSize, &aChildReflowState, 0, 0, flags);
michael@0 2814 NS_ENSURE_SUCCESS(rv, rv);
michael@0 2815
michael@0 2816 // Save the sizing info that we learned from this reflow
michael@0 2817 // -----------------------------------------------------
michael@0 2818
michael@0 2819 // Tentatively store the child's desired content-box cross-size.
michael@0 2820 // Note that childDesiredSize is the border-box size, so we have to
michael@0 2821 // subtract border & padding to get the content-box size.
michael@0 2822 // (Note that at this point in the code, we know our cross axis is vertical,
michael@0 2823 // so we don't bother with making aAxisTracker pick the cross-axis component
michael@0 2824 // for us.)
michael@0 2825 nscoord crossAxisBorderPadding = aItem.GetBorderPadding().TopBottom();
michael@0 2826 if (childDesiredSize.Height() < crossAxisBorderPadding) {
michael@0 2827 // Child's requested size isn't large enough for its border/padding!
michael@0 2828 // This is OK for the trivial nsFrame::Reflow() impl, but other frame
michael@0 2829 // classes should know better. So, if we get here, the child had better be
michael@0 2830 // an instance of nsFrame (i.e. it should return null from GetType()).
michael@0 2831 // XXXdholbert Once we've fixed bug 765861, we should upgrade this to an
michael@0 2832 // assertion that trivially passes if bug 765861's flag has been flipped.
michael@0 2833 NS_WARN_IF_FALSE(!aItem.Frame()->GetType(),
michael@0 2834 "Child should at least request space for border/padding");
michael@0 2835 aItem.SetCrossSize(0);
michael@0 2836 } else {
michael@0 2837 // (normal case)
michael@0 2838 aItem.SetCrossSize(childDesiredSize.Height() - crossAxisBorderPadding);
michael@0 2839 }
michael@0 2840
michael@0 2841 // If we need to do baseline-alignment, store the child's ascent.
michael@0 2842 if (aItem.GetAlignSelf() == NS_STYLE_ALIGN_ITEMS_BASELINE) {
michael@0 2843 ResolveReflowedChildAscent(aItem.Frame(), childDesiredSize);
michael@0 2844 aItem.SetAscent(childDesiredSize.TopAscent());
michael@0 2845 }
michael@0 2846
michael@0 2847 return NS_OK;
michael@0 2848 }
michael@0 2849
michael@0 2850 void
michael@0 2851 FlexLine::PositionItemsInCrossAxis(nscoord aLineStartPosition,
michael@0 2852 const FlexboxAxisTracker& aAxisTracker)
michael@0 2853 {
michael@0 2854 SingleLineCrossAxisPositionTracker lineCrossAxisPosnTracker(aAxisTracker);
michael@0 2855
michael@0 2856 for (FlexItem* item = mItems.getFirst(); item; item = item->getNext()) {
michael@0 2857 // First, stretch the item's cross size (if appropriate), and resolve any
michael@0 2858 // auto margins in this axis.
michael@0 2859 item->ResolveStretchedCrossSize(mLineCrossSize, aAxisTracker);
michael@0 2860 lineCrossAxisPosnTracker.ResolveAutoMarginsInCrossAxis(*this, *item);
michael@0 2861
michael@0 2862 // Compute the cross-axis position of this item
michael@0 2863 nscoord itemCrossBorderBoxSize =
michael@0 2864 item->GetCrossSize() +
michael@0 2865 item->GetBorderPaddingSizeInAxis(aAxisTracker.GetCrossAxis());
michael@0 2866 lineCrossAxisPosnTracker.EnterAlignPackingSpace(*this, *item, aAxisTracker);
michael@0 2867 lineCrossAxisPosnTracker.EnterMargin(item->GetMargin());
michael@0 2868 lineCrossAxisPosnTracker.EnterChildFrame(itemCrossBorderBoxSize);
michael@0 2869
michael@0 2870 item->SetCrossPosition(aLineStartPosition +
michael@0 2871 lineCrossAxisPosnTracker.GetPosition());
michael@0 2872
michael@0 2873 // Back out to cross-axis edge of the line.
michael@0 2874 lineCrossAxisPosnTracker.ResetPosition();
michael@0 2875 }
michael@0 2876 }
michael@0 2877
michael@0 2878 nsresult
michael@0 2879 nsFlexContainerFrame::Reflow(nsPresContext* aPresContext,
michael@0 2880 nsHTMLReflowMetrics& aDesiredSize,
michael@0 2881 const nsHTMLReflowState& aReflowState,
michael@0 2882 nsReflowStatus& aStatus)
michael@0 2883 {
michael@0 2884 DO_GLOBAL_REFLOW_COUNT("nsFlexContainerFrame");
michael@0 2885 DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus);
michael@0 2886 PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG,
michael@0 2887 ("Reflow() for nsFlexContainerFrame %p\n", this));
michael@0 2888
michael@0 2889 if (IsFrameTreeTooDeep(aReflowState, aDesiredSize, aStatus)) {
michael@0 2890 return NS_OK;
michael@0 2891 }
michael@0 2892
michael@0 2893 // We (and our children) can only depend on our ancestor's height if we have
michael@0 2894 // a percent-height, or if we're positioned and we have "top" and "bottom"
michael@0 2895 // set and have height:auto. (There are actually other cases, too -- e.g. if
michael@0 2896 // our parent is itself a vertical flex container and we're flexible -- but
michael@0 2897 // we'll let our ancestors handle those sorts of cases.)
michael@0 2898 const nsStylePosition* stylePos = StylePosition();
michael@0 2899 if (stylePos->mHeight.HasPercent() ||
michael@0 2900 (StyleDisplay()->IsAbsolutelyPositionedStyle() &&
michael@0 2901 eStyleUnit_Auto == stylePos->mHeight.GetUnit() &&
michael@0 2902 eStyleUnit_Auto != stylePos->mOffset.GetTopUnit() &&
michael@0 2903 eStyleUnit_Auto != stylePos->mOffset.GetBottomUnit())) {
michael@0 2904 AddStateBits(NS_FRAME_CONTAINS_RELATIVE_HEIGHT);
michael@0 2905 }
michael@0 2906
michael@0 2907 #ifdef DEBUG
michael@0 2908 SanityCheckAnonymousFlexItems();
michael@0 2909 #endif // DEBUG
michael@0 2910
michael@0 2911 // If we've never reordered our children, then we can trust that they're
michael@0 2912 // already in DOM-order, and we only need to consider their "order" property
michael@0 2913 // when checking them for sortedness & sorting them.
michael@0 2914 //
michael@0 2915 // After we actually sort them, though, we can't trust that they're in DOM
michael@0 2916 // order anymore. So, from that point on, our sort & sorted-order-checking
michael@0 2917 // operations need to use a fancier LEQ function that also takes DOM order
michael@0 2918 // into account, so that we can honor the spec's requirement that frames w/
michael@0 2919 // equal "order" values are laid out in DOM order.
michael@0 2920
michael@0 2921 if (!HasAnyStateBits(NS_STATE_FLEX_CHILDREN_REORDERED)) {
michael@0 2922 if (SortChildrenIfNeeded<IsOrderLEQ>()) {
michael@0 2923 AddStateBits(NS_STATE_FLEX_CHILDREN_REORDERED);
michael@0 2924 }
michael@0 2925 } else {
michael@0 2926 SortChildrenIfNeeded<IsOrderLEQWithDOMFallback>();
michael@0 2927 }
michael@0 2928
michael@0 2929 const FlexboxAxisTracker axisTracker(this);
michael@0 2930
michael@0 2931 // If we're being fragmented into a constrained height, subtract off
michael@0 2932 // borderpadding-top from it, to get the available height for our
michael@0 2933 // content box. (Don't subtract if we're skipping top border/padding,
michael@0 2934 // though.)
michael@0 2935 nscoord availableHeightForContent = aReflowState.AvailableHeight();
michael@0 2936 if (availableHeightForContent != NS_UNCONSTRAINEDSIZE &&
michael@0 2937 !(GetSkipSides() & (1 << NS_SIDE_TOP))) {
michael@0 2938 availableHeightForContent -= aReflowState.ComputedPhysicalBorderPadding().top;
michael@0 2939 // (Don't let that push availableHeightForContent below zero, though):
michael@0 2940 availableHeightForContent = std::max(availableHeightForContent, 0);
michael@0 2941 }
michael@0 2942
michael@0 2943 nscoord contentBoxMainSize = GetMainSizeFromReflowState(aReflowState,
michael@0 2944 axisTracker);
michael@0 2945
michael@0 2946 nsAutoTArray<StrutInfo, 1> struts;
michael@0 2947 nsresult rv = DoFlexLayout(aPresContext, aDesiredSize, aReflowState, aStatus,
michael@0 2948 contentBoxMainSize, availableHeightForContent,
michael@0 2949 struts, axisTracker);
michael@0 2950
michael@0 2951 if (NS_SUCCEEDED(rv) && !struts.IsEmpty()) {
michael@0 2952 // We're restarting flex layout, with new knowledge of collapsed items.
michael@0 2953 rv = DoFlexLayout(aPresContext, aDesiredSize, aReflowState, aStatus,
michael@0 2954 contentBoxMainSize, availableHeightForContent,
michael@0 2955 struts, axisTracker);
michael@0 2956 }
michael@0 2957
michael@0 2958 return rv;
michael@0 2959 }
michael@0 2960
michael@0 2961 // RAII class to clean up a list of FlexLines.
michael@0 2962 // Specifically, this removes each line from the list, deletes all the
michael@0 2963 // FlexItems in its list, and deletes the FlexLine.
michael@0 2964 class MOZ_STACK_CLASS AutoFlexLineListClearer
michael@0 2965 {
michael@0 2966 public:
michael@0 2967 AutoFlexLineListClearer(LinkedList<FlexLine>& aLines
michael@0 2968 MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
michael@0 2969 : mLines(aLines)
michael@0 2970 {
michael@0 2971 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
michael@0 2972 }
michael@0 2973
michael@0 2974 ~AutoFlexLineListClearer()
michael@0 2975 {
michael@0 2976 while (FlexLine* line = mLines.popFirst()) {
michael@0 2977 while (FlexItem* item = line->mItems.popFirst()) {
michael@0 2978 delete item;
michael@0 2979 }
michael@0 2980 delete line;
michael@0 2981 }
michael@0 2982 }
michael@0 2983
michael@0 2984 private:
michael@0 2985 LinkedList<FlexLine>& mLines;
michael@0 2986 MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
michael@0 2987 };
michael@0 2988
michael@0 2989 nsresult
michael@0 2990 nsFlexContainerFrame::DoFlexLayout(nsPresContext* aPresContext,
michael@0 2991 nsHTMLReflowMetrics& aDesiredSize,
michael@0 2992 const nsHTMLReflowState& aReflowState,
michael@0 2993 nsReflowStatus& aStatus,
michael@0 2994 nscoord aContentBoxMainSize,
michael@0 2995 nscoord aAvailableHeightForContent,
michael@0 2996 nsTArray<StrutInfo>& aStruts,
michael@0 2997 const FlexboxAxisTracker& aAxisTracker)
michael@0 2998 {
michael@0 2999 aStatus = NS_FRAME_COMPLETE;
michael@0 3000
michael@0 3001 LinkedList<FlexLine> lines;
michael@0 3002 AutoFlexLineListClearer cleanupLines(lines);
michael@0 3003
michael@0 3004 nsresult rv = GenerateFlexLines(aPresContext, aReflowState,
michael@0 3005 aContentBoxMainSize,
michael@0 3006 aAvailableHeightForContent,
michael@0 3007 aStruts, aAxisTracker, lines);
michael@0 3008 NS_ENSURE_SUCCESS(rv, rv);
michael@0 3009
michael@0 3010 aContentBoxMainSize =
michael@0 3011 ClampFlexContainerMainSize(aReflowState, aAxisTracker,
michael@0 3012 aContentBoxMainSize, aAvailableHeightForContent,
michael@0 3013 lines.getFirst(), aStatus);
michael@0 3014
michael@0 3015 for (FlexLine* line = lines.getFirst(); line; line = line->getNext()) {
michael@0 3016 line->ResolveFlexibleLengths(aContentBoxMainSize);
michael@0 3017 }
michael@0 3018
michael@0 3019 // Cross Size Determination - Flexbox spec section 9.4
michael@0 3020 // ===================================================
michael@0 3021 // Calculate the hypothetical cross size of each item:
michael@0 3022 nscoord sumLineCrossSizes = 0;
michael@0 3023 for (FlexLine* line = lines.getFirst(); line; line = line->getNext()) {
michael@0 3024 for (FlexItem* item = line->GetFirstItem(); item; item = item->getNext()) {
michael@0 3025 // (If the item's already been stretched, or it's a strut, then it
michael@0 3026 // already knows its cross size. Don't bother trying to recalculate it.)
michael@0 3027 if (!item->IsStretched() && !item->IsStrut()) {
michael@0 3028 nsHTMLReflowState childReflowState(aPresContext, aReflowState,
michael@0 3029 item->Frame(),
michael@0 3030 nsSize(aReflowState.ComputedWidth(),
michael@0 3031 NS_UNCONSTRAINEDSIZE));
michael@0 3032 // Override computed main-size
michael@0 3033 if (IsAxisHorizontal(aAxisTracker.GetMainAxis())) {
michael@0 3034 childReflowState.SetComputedWidth(item->GetMainSize());
michael@0 3035 } else {
michael@0 3036 childReflowState.SetComputedHeight(item->GetMainSize());
michael@0 3037 }
michael@0 3038
michael@0 3039 nsresult rv = SizeItemInCrossAxis(aPresContext, aAxisTracker,
michael@0 3040 childReflowState, *item);
michael@0 3041 NS_ENSURE_SUCCESS(rv, rv);
michael@0 3042 }
michael@0 3043 }
michael@0 3044 // Now that we've finished with this line's items, size the line itself:
michael@0 3045 line->ComputeCrossSizeAndBaseline(aAxisTracker);
michael@0 3046 sumLineCrossSizes += line->GetLineCrossSize();
michael@0 3047 }
michael@0 3048
michael@0 3049 bool isCrossSizeDefinite;
michael@0 3050 const nscoord contentBoxCrossSize =
michael@0 3051 ComputeCrossSize(aReflowState, aAxisTracker, sumLineCrossSizes,
michael@0 3052 aAvailableHeightForContent, &isCrossSizeDefinite, aStatus);
michael@0 3053
michael@0 3054 // Set up state for cross-axis alignment, at a high level (outside the
michael@0 3055 // scope of a particular flex line)
michael@0 3056 CrossAxisPositionTracker
michael@0 3057 crossAxisPosnTracker(lines.getFirst(),
michael@0 3058 aReflowState.mStylePosition->mAlignContent,
michael@0 3059 contentBoxCrossSize, isCrossSizeDefinite,
michael@0 3060 aAxisTracker);
michael@0 3061
michael@0 3062 // Now that we know the cross size of each line (including
michael@0 3063 // "align-content:stretch" adjustments, from the CrossAxisPositionTracker
michael@0 3064 // constructor), we can create struts for any flex items with
michael@0 3065 // "visibility: collapse" (and restart flex layout).
michael@0 3066 if (aStruts.IsEmpty()) { // (Don't make struts if we already did)
michael@0 3067 BuildStrutInfoFromCollapsedItems(lines.getFirst(), aStruts);
michael@0 3068 if (!aStruts.IsEmpty()) {
michael@0 3069 // Restart flex layout, using our struts.
michael@0 3070 return NS_OK;
michael@0 3071 }
michael@0 3072 }
michael@0 3073
michael@0 3074 // If the container should derive its baseline from the first FlexLine,
michael@0 3075 // do that here (while crossAxisPosnTracker is conveniently pointing
michael@0 3076 // at the cross-start edge of that line, which the line's baseline offset is
michael@0 3077 // measured from):
michael@0 3078 nscoord flexContainerAscent;
michael@0 3079 if (!aAxisTracker.AreAxesInternallyReversed()) {
michael@0 3080 nscoord firstLineBaselineOffset = lines.getFirst()->GetBaselineOffset();
michael@0 3081 if (firstLineBaselineOffset == nscoord_MIN) {
michael@0 3082 // No baseline-aligned items in line. Use sentinel value to prompt us to
michael@0 3083 // get baseline from the first FlexItem after we've reflowed it.
michael@0 3084 flexContainerAscent = nscoord_MIN;
michael@0 3085 } else {
michael@0 3086 flexContainerAscent =
michael@0 3087 ComputePhysicalAscentFromLogicalAscent(
michael@0 3088 crossAxisPosnTracker.GetPosition() + firstLineBaselineOffset,
michael@0 3089 contentBoxCrossSize, aReflowState, aAxisTracker);
michael@0 3090 }
michael@0 3091 }
michael@0 3092
michael@0 3093 for (FlexLine* line = lines.getFirst(); line; line = line->getNext()) {
michael@0 3094
michael@0 3095 // Main-Axis Alignment - Flexbox spec section 9.5
michael@0 3096 // ==============================================
michael@0 3097 line->PositionItemsInMainAxis(aReflowState.mStylePosition->mJustifyContent,
michael@0 3098 aContentBoxMainSize,
michael@0 3099 aAxisTracker);
michael@0 3100
michael@0 3101 // Cross-Axis Alignment - Flexbox spec section 9.6
michael@0 3102 // ===============================================
michael@0 3103 line->PositionItemsInCrossAxis(crossAxisPosnTracker.GetPosition(),
michael@0 3104 aAxisTracker);
michael@0 3105 crossAxisPosnTracker.TraverseLine(*line);
michael@0 3106 crossAxisPosnTracker.TraversePackingSpace();
michael@0 3107 }
michael@0 3108
michael@0 3109 // If the container should derive its baseline from the last FlexLine,
michael@0 3110 // do that here (while crossAxisPosnTracker is conveniently pointing
michael@0 3111 // at the cross-end edge of that line, which the line's baseline offset is
michael@0 3112 // measured from):
michael@0 3113 if (aAxisTracker.AreAxesInternallyReversed()) {
michael@0 3114 nscoord lastLineBaselineOffset = lines.getLast()->GetBaselineOffset();
michael@0 3115 if (lastLineBaselineOffset == nscoord_MIN) {
michael@0 3116 // No baseline-aligned items in line. Use sentinel value to prompt us to
michael@0 3117 // get baseline from the last FlexItem after we've reflowed it.
michael@0 3118 flexContainerAscent = nscoord_MIN;
michael@0 3119 } else {
michael@0 3120 flexContainerAscent =
michael@0 3121 ComputePhysicalAscentFromLogicalAscent(
michael@0 3122 crossAxisPosnTracker.GetPosition() - lastLineBaselineOffset,
michael@0 3123 contentBoxCrossSize, aReflowState, aAxisTracker);
michael@0 3124 }
michael@0 3125 }
michael@0 3126
michael@0 3127 // Before giving each child a final reflow, calculate the origin of the
michael@0 3128 // flex container's content box (with respect to its border-box), so that
michael@0 3129 // we can compute our flex item's final positions.
michael@0 3130 nsMargin containerBorderPadding(aReflowState.ComputedPhysicalBorderPadding());
michael@0 3131 ApplySkipSides(containerBorderPadding, &aReflowState);
michael@0 3132 const nsPoint containerContentBoxOrigin(containerBorderPadding.left,
michael@0 3133 containerBorderPadding.top);
michael@0 3134
michael@0 3135 // FINAL REFLOW: Give each child frame another chance to reflow, now that
michael@0 3136 // we know its final size and position.
michael@0 3137 for (const FlexLine* line = lines.getFirst(); line; line = line->getNext()) {
michael@0 3138 for (const FlexItem* item = line->GetFirstItem(); item;
michael@0 3139 item = item->getNext()) {
michael@0 3140 nsPoint physicalPosn = aAxisTracker.PhysicalPointFromLogicalPoint(
michael@0 3141 item->GetMainPosition(),
michael@0 3142 item->GetCrossPosition(),
michael@0 3143 aContentBoxMainSize,
michael@0 3144 contentBoxCrossSize);
michael@0 3145 // Adjust physicalPosn to be relative to the container's border-box
michael@0 3146 // (i.e. its frame rect), instead of the container's content-box:
michael@0 3147 physicalPosn += containerContentBoxOrigin;
michael@0 3148
michael@0 3149 nsHTMLReflowState childReflowState(aPresContext, aReflowState,
michael@0 3150 item->Frame(),
michael@0 3151 nsSize(aReflowState.ComputedWidth(),
michael@0 3152 NS_UNCONSTRAINEDSIZE));
michael@0 3153
michael@0 3154 // Keep track of whether we've overriden the child's computed height
michael@0 3155 // and/or width, so we can set its resize flags accordingly.
michael@0 3156 bool didOverrideComputedWidth = false;
michael@0 3157 bool didOverrideComputedHeight = false;
michael@0 3158
michael@0 3159 // Override computed main-size
michael@0 3160 if (IsAxisHorizontal(aAxisTracker.GetMainAxis())) {
michael@0 3161 childReflowState.SetComputedWidth(item->GetMainSize());
michael@0 3162 didOverrideComputedWidth = true;
michael@0 3163 } else {
michael@0 3164 childReflowState.SetComputedHeight(item->GetMainSize());
michael@0 3165 didOverrideComputedHeight = true;
michael@0 3166 }
michael@0 3167
michael@0 3168 // Override reflow state's computed cross-size, for stretched items.
michael@0 3169 if (item->IsStretched()) {
michael@0 3170 MOZ_ASSERT(item->GetAlignSelf() == NS_STYLE_ALIGN_ITEMS_STRETCH,
michael@0 3171 "stretched item w/o 'align-self: stretch'?");
michael@0 3172 if (IsAxisHorizontal(aAxisTracker.GetCrossAxis())) {
michael@0 3173 childReflowState.SetComputedWidth(item->GetCrossSize());
michael@0 3174 didOverrideComputedWidth = true;
michael@0 3175 } else {
michael@0 3176 // If this item's height is stretched, it's a relative height.
michael@0 3177 item->Frame()->AddStateBits(NS_FRAME_CONTAINS_RELATIVE_HEIGHT);
michael@0 3178 childReflowState.SetComputedHeight(item->GetCrossSize());
michael@0 3179 didOverrideComputedHeight = true;
michael@0 3180 }
michael@0 3181 }
michael@0 3182
michael@0 3183 // XXXdholbert Might need to actually set the correct margins in the
michael@0 3184 // reflow state at some point, so that they can be saved on the frame for
michael@0 3185 // UsedMarginProperty(). Maybe doesn't matter though...?
michael@0 3186
michael@0 3187 // If we're overriding the computed width or height, *and* we had an
michael@0 3188 // earlier "measuring" reflow, then this upcoming reflow needs to be
michael@0 3189 // treated as a resize.
michael@0 3190 if (item->HadMeasuringReflow()) {
michael@0 3191 if (didOverrideComputedWidth) {
michael@0 3192 // (This is somewhat redundant, since the reflow state already
michael@0 3193 // sets mHResize whenever our computed width has changed since the
michael@0 3194 // previous reflow. Still, it's nice for symmetry, and it may become
michael@0 3195 // necessary once we support orthogonal flows.)
michael@0 3196 childReflowState.mFlags.mHResize = true;
michael@0 3197 }
michael@0 3198 if (didOverrideComputedHeight) {
michael@0 3199 childReflowState.mFlags.mVResize = true;
michael@0 3200 }
michael@0 3201 }
michael@0 3202 // NOTE: Be very careful about doing anything else with childReflowState
michael@0 3203 // after this point, because some of its methods (e.g. SetComputedWidth)
michael@0 3204 // internally call InitResizeFlags and stomp on mVResize & mHResize.
michael@0 3205
michael@0 3206 nsHTMLReflowMetrics childDesiredSize(childReflowState);
michael@0 3207 nsReflowStatus childReflowStatus;
michael@0 3208 nsresult rv = ReflowChild(item->Frame(), aPresContext,
michael@0 3209 childDesiredSize, childReflowState,
michael@0 3210 physicalPosn.x, physicalPosn.y,
michael@0 3211 0, childReflowStatus);
michael@0 3212 NS_ENSURE_SUCCESS(rv, rv);
michael@0 3213
michael@0 3214 // XXXdholbert Once we do pagination / splitting, we'll need to actually
michael@0 3215 // handle incomplete childReflowStatuses. But for now, we give our kids
michael@0 3216 // unconstrained available height, which means they should always
michael@0 3217 // complete.
michael@0 3218 MOZ_ASSERT(NS_FRAME_IS_COMPLETE(childReflowStatus),
michael@0 3219 "We gave flex item unconstrained available height, so it "
michael@0 3220 "should be complete");
michael@0 3221
michael@0 3222 childReflowState.ApplyRelativePositioning(&physicalPosn);
michael@0 3223
michael@0 3224 rv = FinishReflowChild(item->Frame(), aPresContext,
michael@0 3225 childDesiredSize, &childReflowState,
michael@0 3226 physicalPosn.x, physicalPosn.y, 0);
michael@0 3227 NS_ENSURE_SUCCESS(rv, rv);
michael@0 3228
michael@0 3229 // If this is our first child and we haven't established a baseline for
michael@0 3230 // the container yet (i.e. if we don't have 'align-self: baseline' on any
michael@0 3231 // children), then use this child's baseline as the container's baseline.
michael@0 3232 if (item->Frame() == mFrames.FirstChild() &&
michael@0 3233 flexContainerAscent == nscoord_MIN) {
michael@0 3234 ResolveReflowedChildAscent(item->Frame(), childDesiredSize);
michael@0 3235
michael@0 3236 // (We use GetNormalPosition() instead of physicalPosn because we don't
michael@0 3237 // want relative positioning on the child to affect the baseline that we
michael@0 3238 // read from it).
michael@0 3239 flexContainerAscent = item->Frame()->GetNormalPosition().y +
michael@0 3240 childDesiredSize.TopAscent();
michael@0 3241 }
michael@0 3242 }
michael@0 3243 }
michael@0 3244
michael@0 3245 nsSize desiredContentBoxSize =
michael@0 3246 aAxisTracker.PhysicalSizeFromLogicalSizes(aContentBoxMainSize,
michael@0 3247 contentBoxCrossSize);
michael@0 3248
michael@0 3249 aDesiredSize.Width() = desiredContentBoxSize.width +
michael@0 3250 containerBorderPadding.LeftRight();
michael@0 3251 // Does *NOT* include bottom border/padding yet (we add that a bit lower down)
michael@0 3252 aDesiredSize.Height() = desiredContentBoxSize.height +
michael@0 3253 containerBorderPadding.top;
michael@0 3254
michael@0 3255 if (flexContainerAscent == nscoord_MIN) {
michael@0 3256 // Still don't have our baseline set -- this happens if we have no
michael@0 3257 // children (or if our children are huge enough that they have nscoord_MIN
michael@0 3258 // as their baseline... in which case, we'll use the wrong baseline, but no
michael@0 3259 // big deal)
michael@0 3260 NS_WARN_IF_FALSE(lines.getFirst()->IsEmpty(),
michael@0 3261 "Have flex items but didn't get an ascent - that's odd "
michael@0 3262 "(or there are just gigantic sizes involved)");
michael@0 3263 // Per spec, just use the bottom of content-box.
michael@0 3264 flexContainerAscent = aDesiredSize.Height();
michael@0 3265 }
michael@0 3266 aDesiredSize.SetTopAscent(flexContainerAscent);
michael@0 3267
michael@0 3268 // Now: If we're complete, add bottom border/padding to desired height
michael@0 3269 // (unless that pushes us over available height, in which case we become
michael@0 3270 // incomplete (unless we already weren't asking for any height, in which case
michael@0 3271 // we stay complete to avoid looping forever)).
michael@0 3272 // NOTE: If we're auto-height, we allow our bottom border/padding to push us
michael@0 3273 // over the available height without requesting a continuation, for
michael@0 3274 // consistency with the behavior of "display:block" elements.
michael@0 3275 if (NS_FRAME_IS_COMPLETE(aStatus)) {
michael@0 3276 // NOTE: We can't use containerBorderPadding.bottom for this, because if
michael@0 3277 // we're auto-height, ApplySkipSides will have zeroed it (because it
michael@0 3278 // assumed we might get a continuation). We have the correct value in
michael@0 3279 // aReflowState.ComputedPhyiscalBorderPadding().bottom, though, so we use that.
michael@0 3280 nscoord desiredHeightWithBottomBP =
michael@0 3281 aDesiredSize.Height() + aReflowState.ComputedPhysicalBorderPadding().bottom;
michael@0 3282
michael@0 3283 if (aReflowState.AvailableHeight() == NS_UNCONSTRAINEDSIZE ||
michael@0 3284 aDesiredSize.Height() == 0 ||
michael@0 3285 desiredHeightWithBottomBP <= aReflowState.AvailableHeight() ||
michael@0 3286 aReflowState.ComputedHeight() == NS_INTRINSICSIZE) {
michael@0 3287 // Update desired height to include bottom border/padding
michael@0 3288 aDesiredSize.Height() = desiredHeightWithBottomBP;
michael@0 3289 } else {
michael@0 3290 // We couldn't fit bottom border/padding, so we'll need a continuation.
michael@0 3291 NS_FRAME_SET_INCOMPLETE(aStatus);
michael@0 3292 }
michael@0 3293 }
michael@0 3294
michael@0 3295 // Overflow area = union(my overflow area, kids' overflow areas)
michael@0 3296 aDesiredSize.SetOverflowAreasToDesiredBounds();
michael@0 3297 for (nsFrameList::Enumerator e(mFrames); !e.AtEnd(); e.Next()) {
michael@0 3298 ConsiderChildOverflow(aDesiredSize.mOverflowAreas, e.get());
michael@0 3299 }
michael@0 3300
michael@0 3301 FinishReflowWithAbsoluteFrames(aPresContext, aDesiredSize,
michael@0 3302 aReflowState, aStatus);
michael@0 3303
michael@0 3304 NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize)
michael@0 3305 return NS_OK;
michael@0 3306 }
michael@0 3307
michael@0 3308 /* virtual */ nscoord
michael@0 3309 nsFlexContainerFrame::GetMinWidth(nsRenderingContext* aRenderingContext)
michael@0 3310 {
michael@0 3311 nscoord minWidth = 0;
michael@0 3312 DISPLAY_MIN_WIDTH(this, minWidth);
michael@0 3313
michael@0 3314 FlexboxAxisTracker axisTracker(this);
michael@0 3315
michael@0 3316 for (nsFrameList::Enumerator e(mFrames); !e.AtEnd(); e.Next()) {
michael@0 3317 nscoord childMinWidth =
michael@0 3318 nsLayoutUtils::IntrinsicForContainer(aRenderingContext, e.get(),
michael@0 3319 nsLayoutUtils::MIN_WIDTH);
michael@0 3320 // For a horizontal single-line flex container, the intrinsic min width is
michael@0 3321 // the sum of its items' min widths.
michael@0 3322 // For a vertical flex container, or for a multi-line horizontal flex
michael@0 3323 // container, the intrinsic min width is the max of its items' min widths.
michael@0 3324 if (IsAxisHorizontal(axisTracker.GetMainAxis()) &&
michael@0 3325 NS_STYLE_FLEX_WRAP_NOWRAP == StylePosition()->mFlexWrap) {
michael@0 3326 minWidth += childMinWidth;
michael@0 3327 } else {
michael@0 3328 minWidth = std::max(minWidth, childMinWidth);
michael@0 3329 }
michael@0 3330 }
michael@0 3331 return minWidth;
michael@0 3332 }
michael@0 3333
michael@0 3334 /* virtual */ nscoord
michael@0 3335 nsFlexContainerFrame::GetPrefWidth(nsRenderingContext* aRenderingContext)
michael@0 3336 {
michael@0 3337 nscoord prefWidth = 0;
michael@0 3338 DISPLAY_PREF_WIDTH(this, prefWidth);
michael@0 3339
michael@0 3340 // XXXdholbert Optimization: We could cache our intrinsic widths like
michael@0 3341 // nsBlockFrame does (and return it early from this function if it's set).
michael@0 3342 // Whenever anything happens that might change it, set it to
michael@0 3343 // NS_INTRINSIC_WIDTH_UNKNOWN (like nsBlockFrame::MarkIntrinsicWidthsDirty
michael@0 3344 // does)
michael@0 3345 FlexboxAxisTracker axisTracker(this);
michael@0 3346
michael@0 3347 for (nsFrameList::Enumerator e(mFrames); !e.AtEnd(); e.Next()) {
michael@0 3348 nscoord childPrefWidth =
michael@0 3349 nsLayoutUtils::IntrinsicForContainer(aRenderingContext, e.get(),
michael@0 3350 nsLayoutUtils::PREF_WIDTH);
michael@0 3351 if (IsAxisHorizontal(axisTracker.GetMainAxis())) {
michael@0 3352 prefWidth += childPrefWidth;
michael@0 3353 } else {
michael@0 3354 prefWidth = std::max(prefWidth, childPrefWidth);
michael@0 3355 }
michael@0 3356 }
michael@0 3357 return prefWidth;
michael@0 3358 }

mercurial