Chromium Code Reviews| Index: third_party/WebKit/Source/core/page/scrolling/SnapCoordinator.cpp |
| diff --git a/third_party/WebKit/Source/core/page/scrolling/SnapCoordinator.cpp b/third_party/WebKit/Source/core/page/scrolling/SnapCoordinator.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6743deb1b3240807cb257c9ff7f0a49b133f1ada |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/page/scrolling/SnapCoordinator.cpp |
| @@ -0,0 +1,165 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "SnapCoordinator.h" |
| + |
| +#include "core/dom/Element.h" |
| +#include "core/dom/Node.h" |
| +#include "core/dom/NodeComputedStyle.h" |
| +#include "core/layout/LayoutBlock.h" |
| +#include "core/layout/LayoutBox.h" |
| +#include "core/style/ComputedStyle.h" |
| +#include "platform/LengthFunctions.h" |
| + |
| +namespace blink { |
| + |
| +SnapCoordinator::SnapCoordinator() |
| + : m_snapContainers() |
| +{ |
| +} |
| + |
| +SnapCoordinator::~SnapCoordinator() { } |
| + |
| +PassOwnPtrWillBeRawPtr<SnapCoordinator> SnapCoordinator::create() |
| +{ |
| + return adoptPtrWillBeNoop(new SnapCoordinator()); |
| +} |
| + |
| +// Returns the scroll container that can be affected by this snap area. |
| +static LayoutBox* findSnapContainer(const LayoutBox& snapArea) |
| +{ |
| + // According to the new spec https://drafts.csswg.org/css-scroll-snap/#snap-model |
| + // "Snap positions must only affect the nearest ancestor (on the element’s |
| + // containing block chain) scroll container". |
| + Element* scrollingElement = snapArea.node()->document().scrollingElement(); |
| + LayoutBox* box = snapArea.containingBlock(); |
| + while (box && !box->hasOverflowClip() && box->node() != scrollingElement) |
| + box = box->containingBlock(); |
| + |
| + return box; |
| +} |
| + |
| +void SnapCoordinator::snapAreaDidChange(LayoutBox& snapArea, const Vector<LengthPoint>& snapCoordinates) |
| +{ |
| + if (snapCoordinates.isEmpty()) { |
| + snapArea.setSnapContainer(nullptr); |
| + return; |
| + } |
| + |
| + if (LayoutBox* snapContainer = findSnapContainer(snapArea)) { |
| + snapArea.setSnapContainer(snapContainer); |
| + } else { |
| + // TODO(majidvp): keep track of snap areas that do not have any |
| + // container so that we check them again when a new container is |
| + // added to the page. |
| + } |
| +} |
| + |
| +void SnapCoordinator::snapContainerDidChange(LayoutBox& snapContainer, ScrollSnapType scrollSnapType) |
| +{ |
| + if (scrollSnapType == ScrollSnapTypeNone) { |
| + // TODO(majidvp): Track and report these removals to CompositorWorker |
| + // instance responsible for snapping |
| + m_snapContainers.remove(&snapContainer); |
| + snapContainer.clearSnapAreas(); |
| + } else { |
| + m_snapContainers.add(&snapContainer); |
| + } |
| + |
| + // TODO(majidvp): Add logic to correctly handle orphaned snap areas here. |
| + // 1. Removing container: find a new snap container for its orphan snap |
| + // areas (most likely nearest ancestor of current container) otherwise add |
| + // them to an orphan list. |
| + // 2. Adding container: may take over snap areas from nearest ancestor snap |
| + // container or from existing areas in orphan pool. |
| +} |
| + |
| +// Translate local snap coordinates into snap container space |
|
skobes
2016/03/07 03:42:27
Since you are adjusting the output of localToAnces
majidvp
2016/05/03 17:23:36
Done.
|
| +static Vector<FloatPoint> localToContainerSnapCoordinates(const LayoutBox& containerBox, const LayoutBox& snapArea) |
| +{ |
| + Vector<FloatPoint> result; |
| + LayoutPoint scrollOffset(containerBox.scrollLeft(), containerBox.scrollTop()); |
| + |
| + const Vector<LengthPoint>& snapCoordinates = snapArea.style()->scrollSnapCoordinate(); |
| + for (auto& coordinate : snapCoordinates) { |
| + FloatPoint localPoint = floatPointForLengthPoint(coordinate, FloatSize(snapArea.size())); |
| + FloatPoint containerPoint = snapArea.localToAncestorPoint(localPoint, &containerBox); |
| + containerPoint.moveBy(scrollOffset); |
| + result.append(containerPoint); |
| + } |
| + return result; |
| +} |
| + |
| +Vector<double> SnapCoordinator::snapOffsets(const Element& element, ScrollbarOrientation orientation) |
|
skobes
2016/03/07 03:42:27
Where will this be called from?
majidvp
2016/05/03 17:23:36
At the moment this is only used in tests. But plan
|
| +{ |
| + const ComputedStyle* style = element.computedStyle(); |
| + const LayoutBox* snapContainer = element.layoutBox(); |
| + ASSERT(style); |
| + ASSERT(snapContainer); |
| + |
| + Vector<double> result; |
| + |
| + if (style->getScrollSnapType() == ScrollSnapTypeNone) |
| + return result; |
| + |
| + const ScrollSnapPoints& snapPoints = (orientation == HorizontalScrollbar) ? style->scrollSnapPointsX() : style->scrollSnapPointsY(); |
| + |
| + LayoutUnit clientSize = (orientation == HorizontalScrollbar) ? snapContainer->clientWidth() : snapContainer->clientHeight(); |
|
skobes
2016/03/07 03:42:27
I think this has some issues when the snap contain
|
| + LayoutUnit scrollSize = (orientation == HorizontalScrollbar) ? snapContainer->scrollWidth() : snapContainer->scrollHeight(); |
| + |
| + if (snapPoints.hasRepeat) { |
| + LayoutUnit repeat = valueForLength(snapPoints.repeatOffset, clientSize); |
| + |
| + // calc() values may be negative or zero in which case we clamp them to 1px. |
| + // See: https://lists.w3.org/Archives/Public/www-style/2015Jul/0075.html |
| + repeat = std::max<LayoutUnit>(repeat, LayoutUnit(1)); |
| + for (LayoutUnit offset = repeat; offset <= (scrollSize - clientSize); offset += repeat) { |
| + result.append(offset.toFloat()); |
| + } |
| + } |
| + |
| + // Compute element-based snap points by mapping the snap coordinates from |
| + // snap areas to snap container. |
| + bool didAddSnapAreaOffset = false; |
| + if (SnapAreaSet* snapAreas = snapContainer->snapAreas()) { |
| + for (auto& snapArea : *snapAreas) { |
| + Vector<FloatPoint> snapCoordinates = localToContainerSnapCoordinates(*snapContainer, *snapArea); |
| + for (const FloatPoint& snapCoordinate : snapCoordinates) { |
| + float snapOffset = (orientation == HorizontalScrollbar) ? snapCoordinate.x() : snapCoordinate.y(); |
| + if (snapOffset > scrollSize - clientSize) |
| + continue; |
| + result.append(snapOffset); |
| + didAddSnapAreaOffset = true; |
| + } |
| + } |
| + } |
| + |
| + if (didAddSnapAreaOffset) |
| + std::sort(result.begin(), result.end()); |
| + |
| + return result; |
| +} |
| + |
| +#ifndef NDEBUG |
| + |
| +void SnapCoordinator::showSnapAreaMap() |
| +{ |
| + for (auto& container : m_snapContainers) |
| + showSnapAreasFor(container); |
| +} |
| + |
| +void SnapCoordinator::showSnapAreasFor(const LayoutBox* container) |
| +{ |
| + const char* prefix = " "; |
| + container->node()->showNode(); |
| + if (SnapAreaSet* snapAreas = container->snapAreas()) { |
| + for (auto& snapArea : *snapAreas) { |
| + snapArea->node()->showNode(prefix); |
| + } |
| + } |
| +} |
| + |
| +#endif |
| + |
| +} // namespace blink |