Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(513)

Unified Diff: third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp

Issue 1308273010: Adapt and reland old position sticky implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Force main thread scrolling, set experimental status, and address review comments. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp
diff --git a/third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp b/third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp
index 0c7af6cbd0553824d52db1650f1c05ee4ec47012..c217c9ec45eddb0108e1dd03e6c960ba1cd5ecee 100644
--- a/third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp
+++ b/third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp
@@ -178,6 +178,20 @@ void LayoutBoxModelObject::styleWillChange(StyleDifference diff, const ComputedS
LayoutObject::styleWillChange(diff, newStyle);
}
+static inline LayoutBox* findScrollAncestor(const LayoutObject* startObject)
+{
+ LayoutBox* curBox = startObject->containingBlock();
+
+ // Scrolling propagates along the containing block chain.
+ while (curBox && !curBox->isLayoutView()) {
chrishtr 2015/12/11 02:06:12 Use enclosingLayer()->scrollParent() instead.
flackr 2016/01/19 15:40:48 ancestorScrollingLayer (and scrollParent, which is
+ if (curBox->hasOverflowClip())
+ return curBox;
+ curBox = curBox->containingBlock();
+ }
+
+ return nullptr;
+}
+
void LayoutBoxModelObject::styleDidChange(StyleDifference diff, const ComputedStyle* oldStyle)
{
bool hadTransform = hasTransformRelatedProperty();
@@ -268,14 +282,32 @@ void LayoutBoxModelObject::styleDidChange(StyleDifference diff, const ComputedSt
}
if (FrameView *frameView = view()->frameView()) {
- bool newStyleIsViewportConstained = style()->hasViewportConstrainedPosition();
- bool oldStyleIsViewportConstrained = oldStyle && oldStyle->hasViewportConstrainedPosition();
+ bool newStyleIsViewportConstained = style()->position() == FixedPosition;
+ bool oldStyleIsViewportConstrained = oldStyle && oldStyle->position() == FixedPosition;
+ bool newStyleIsSticky = style()->position() == StickyPosition;
+ bool oldStyleIsSticky = oldStyle && oldStyle->position() == StickyPosition;
+
+ // Sticky positioned elements are only viewport constrained if they have no ancestor scroller.
+ if (newStyleIsSticky || oldStyleIsSticky) {
+ if (!findScrollAncestor(this)) {
+ newStyleIsViewportConstained |= newStyleIsSticky;
+ oldStyleIsViewportConstrained |= oldStyleIsSticky;
+ }
+ }
+
if (newStyleIsViewportConstained != oldStyleIsViewportConstrained) {
if (newStyleIsViewportConstained && layer())
frameView->addViewportConstrainedObject(this);
else
frameView->removeViewportConstrainedObject(this);
}
+
+ if (newStyleIsSticky != oldStyleIsSticky) {
+ if (newStyleIsSticky)
+ frameView->addStickyPositionObject();
+ else
+ frameView->removeStickyPositionObject();
+ }
}
}
@@ -608,6 +640,118 @@ LayoutSize LayoutBoxModelObject::relativePositionOffset() const
return offset;
}
+void LayoutBoxModelObject::computeStickyPositionConstraints(StickyPositionViewportConstraints& constraints, const LayoutRect& constrainingRect) const
+{
+ LayoutBlock* containingBlock = this->containingBlock();
+
+ LayoutRect containerContentRect = containingBlock->contentBoxRect();
+ LayoutUnit maxWidth = containingBlock->availableLogicalWidth();
+
+ // Sticky positioned element ignore any override logical width on the containing block (as they don't call
+ // containingBlockLogicalWidthForContent). It's unclear whether this is totally fine.
+ // Compute the container-relative area within which the sticky element is allowed to move.
+ containerContentRect.contractEdges(
+ minimumValueForLength(style()->marginTop(), maxWidth),
+ minimumValueForLength(style()->marginRight(), maxWidth),
+ minimumValueForLength(style()->marginBottom(), maxWidth),
+ minimumValueForLength(style()->marginLeft(), maxWidth));
+
+ // Map to the view to avoid including page scale factor.
+ constraints.setAbsoluteContainingBlockRect(LayoutRect(containingBlock->localToContainerQuad(FloatRect(containerContentRect), view()).boundingBox()));
+
+ LayoutRect stickyBoxRect = isLayoutInline()
+ ? LayoutRect(toLayoutInline(this)->linesBoundingBox())
+ : toLayoutBox(this)->frameRect();
+ LayoutRect flippedStickyBoxRect = stickyBoxRect;
+ containingBlock->flipForWritingMode(flippedStickyBoxRect);
+ LayoutPoint stickyLocation = flippedStickyBoxRect.location();
+
+ // FIXME: sucks to call localToAbsolute again, but we can't just offset from the previously computed rect if there are transforms.
+ // Map to the view to avoid including page scale factor.
+ LayoutRect absContainerFrame = LayoutRect(containingBlock->localToContainerQuad(FloatRect(FloatPoint(), FloatSize(containingBlock->size())), view()).boundingBox());
chrishtr 2015/12/11 02:06:12 I don't think this should be in LayoutView coordin
flackr 2016/01/19 15:40:48 So this is specifically excluding page scale, whic
+
+ if (containingBlock->hasOverflowClip()) {
+ LayoutSize scrollOffset(containingBlock->layer()->scrollableArea()->adjustedScrollOffset());
+ stickyLocation -= scrollOffset;
+ }
+
+ // We can't call localToAbsolute on |this| because that will recur. FIXME: For now, assume that |this| is not transformed.
+ constraints.setAbsoluteStickyBoxRect(LayoutRect(LayoutPoint(absContainerFrame.location()) + stickyLocation, flippedStickyBoxRect.size()));
+
+ LayoutUnit horizontalOffsets = minimumValueForLength(style()->right(), constrainingRect.width()) +
+ minimumValueForLength(style()->left(), constrainingRect.width());
+ bool skipRight = false;
+ bool skipLeft = false;
+ if (!style()->left().isAuto() && !style()->right().isAuto()) {
+ if (horizontalOffsets > containerContentRect.width()
+ || horizontalOffsets + containerContentRect.width() > constrainingRect.width()) {
+ skipRight = style()->isLeftToRightDirection();
+ skipLeft = !skipRight;
+ }
+ }
+
+ if (!style()->left().isAuto() && !skipLeft) {
+ constraints.setLeftOffset(minimumValueForLength(style()->left(), constrainingRect.width()));
+ constraints.addAnchorEdge(ViewportConstraints::AnchorEdgeLeft);
+ }
+
+ if (!style()->right().isAuto() && !skipRight) {
+ constraints.setRightOffset(minimumValueForLength(style()->right(), constrainingRect.width()));
+ constraints.addAnchorEdge(ViewportConstraints::AnchorEdgeRight);
+ }
+
+ bool skipBottom = false;
+ // FIXME(ostap): Exclude top or bottom edge offset depending on the writing mode when related
+ // sections are fixed in spec: http://lists.w3.org/Archives/Public/www-style/2014May/0286.html
+ LayoutUnit verticalOffsets = minimumValueForLength(style()->top(), constrainingRect.width()) +
+ minimumValueForLength(style()->bottom(), constrainingRect.width());
+ if (!style()->top().isAuto() && !style()->bottom().isAuto()) {
+ if (verticalOffsets > containerContentRect.height()
+ || verticalOffsets + containerContentRect.height() > constrainingRect.height()) {
+ skipBottom = true;
+ }
+ }
+
+ if (!style()->top().isAuto()) {
+ constraints.setTopOffset(minimumValueForLength(style()->top(), constrainingRect.height()));
+ constraints.addAnchorEdge(ViewportConstraints::AnchorEdgeTop);
+ }
+
+ if (!style()->bottom().isAuto() && !skipBottom) {
+ constraints.setBottomOffset(minimumValueForLength(style()->bottom(), constrainingRect.height()));
+ constraints.addAnchorEdge(ViewportConstraints::AnchorEdgeBottom);
+ }
+}
+
+LayoutRect LayoutBoxModelObject::computeStickyConstrainingRect() const
+{
+ LayoutRect constrainingRect;
+
+ ASSERT(hasLayer());
+ LayoutBox* enclosingClippingBox = findScrollAncestor(this);
+ if (enclosingClippingBox) {
+ LayoutRect clipRect = enclosingClippingBox->overflowClipRect(LayoutPoint());
+ clipRect.move(enclosingClippingBox->paddingLeft(), enclosingClippingBox->paddingTop());
+ clipRect.contract(LayoutSize(enclosingClippingBox->paddingLeft() + enclosingClippingBox->paddingRight(),
+ enclosingClippingBox->paddingTop() + enclosingClippingBox->paddingBottom()));
+ constrainingRect = LayoutRect(enclosingClippingBox->localToContainerQuad(FloatRect(clipRect), view()).boundingBox());
+ } else {
+ constrainingRect = LayoutRect(view()->frameView()->visibleContentRect());
+ }
+
+ return constrainingRect;
+}
+
+LayoutSize LayoutBoxModelObject::stickyPositionOffset() const
+{
+ LayoutRect constrainingRect = computeStickyConstrainingRect();
chrishtr 2015/12/11 02:06:12 This will compute these rects from scratch on ever
flackr 2016/01/19 15:40:48 I've been trying to wrap my head around how we can
chrishtr 2016/01/30 01:49:02 What do you mean exactly that "LayoutInline::layou
flackr 2016/02/03 22:48:38 My original thinking was that I may need to call l
chrishtr 2016/02/08 21:46:55 No, in FrameView::updateLifecyclePhasesInternal, a
+ StickyPositionViewportConstraints constraints;
+ computeStickyPositionConstraints(constraints, constrainingRect);
+
+ // The sticky offset is physical, so we can just return the delta computed in absolute coords (though it may be wrong with transforms).
+ return LayoutSize(constraints.computeStickyOffset(constrainingRect));
+}
+
LayoutPoint LayoutBoxModelObject::adjustedPositionRelativeToOffsetParent(const LayoutPoint& startPoint) const
{
// If the element is the HTML body element or doesn't have a parent
@@ -630,7 +774,7 @@ LayoutPoint LayoutBoxModelObject::adjustedPositionRelativeToOffsetParent(const L
referencePoint.move(-toLayoutBox(offsetParent)->borderLeft(), -toLayoutBox(offsetParent)->borderTop());
if (!isOutOfFlowPositioned() || flowThreadContainingBlock()) {
if (isInFlowPositioned())
- referencePoint.move(relativePositionOffset());
+ referencePoint.move(offsetForInFlowPosition());
LayoutObject* current;
for (current = parent(); current != offsetParent && current->parent(); current = current->parent()) {
@@ -652,7 +796,13 @@ LayoutPoint LayoutBoxModelObject::adjustedPositionRelativeToOffsetParent(const L
LayoutSize LayoutBoxModelObject::offsetForInFlowPosition() const
{
- return isRelPositioned() ? relativePositionOffset() : LayoutSize();
+ if (isRelPositioned())
+ return relativePositionOffset();
+
+ if (isStickyPositioned())
+ return stickyPositionOffset();
+
+ return LayoutSize();
}
LayoutUnit LayoutBoxModelObject::offsetLeft() const
@@ -975,7 +1125,7 @@ const LayoutObject* LayoutBoxModelObject::pushMappingToContainer(const LayoutBox
return nullptr;
bool isInline = isLayoutInline();
- bool isFixedPos = !isInline && style()->position() == FixedPosition;
+ bool isFixedPosition = !isInline && style()->position() == FixedPosition;
bool hasTransform = !isInline && hasLayer() && layer()->transform();
LayoutSize adjustmentForSkippedAncestor;
@@ -987,16 +1137,19 @@ const LayoutObject* LayoutBoxModelObject::pushMappingToContainer(const LayoutBox
bool offsetDependsOnPoint = false;
LayoutSize containerOffset = offsetFromContainer(container, LayoutPoint(), &offsetDependsOnPoint);
+ LayoutSize stickyOffset;
+ if (style()->position() == StickyPosition)
+ stickyOffset = offsetForInFlowPosition();
bool preserve3D = container->style()->preserves3D() || style()->preserves3D();
if (shouldUseTransformFromContainer(container)) {
TransformationMatrix t;
getTransformFromContainer(container, containerOffset, t);
t.translateRight(adjustmentForSkippedAncestor.width().toFloat(), adjustmentForSkippedAncestor.height().toFloat());
- geometryMap.push(this, t, preserve3D, offsetDependsOnPoint, isFixedPos, hasTransform);
+ geometryMap.push(this, t, preserve3D, offsetDependsOnPoint, isFixedPosition, hasTransform, LayoutSize(), stickyOffset);
} else {
containerOffset += adjustmentForSkippedAncestor;
- geometryMap.push(this, containerOffset, preserve3D, offsetDependsOnPoint, isFixedPos, hasTransform);
+ geometryMap.push(this, containerOffset, preserve3D, offsetDependsOnPoint, isFixedPosition, hasTransform, LayoutSize(), stickyOffset);
}
return ancestorSkipped ? ancestorToStopAt : container;

Powered by Google App Engine
This is Rietveld 408576698