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

Unified Diff: third_party/WebKit/Source/core/page/scrolling/SnapCoordinator.cpp

Issue 1188563005: Compute snap offsets (both repeat and element based) (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: remove m_frame Created 5 years, 2 months 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/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..dd2f487784ee6efb9b2a582c83c43d0b429c0c36
--- /dev/null
+++ b/third_party/WebKit/Source/core/page/scrolling/SnapCoordinator.cpp
@@ -0,0 +1,206 @@
+// Copyright 2015 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 "config.h"
+
+#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_elementMap()
+{
+}
+
+SnapCoordinator::~SnapCoordinator() { }
+
+PassOwnPtrWillBeRawPtr<SnapCoordinator> SnapCoordinator::create()
+{
+ return adoptPtrWillBeNoop(new SnapCoordinator());
+}
+
+SnapElementSet& SnapCoordinator::ensureSnapElementSet(const Element& container)
+{
+ SnapElementMap::AddResult addResult = m_elementMap.add(&container, nullptr);
+ if (addResult.isNewEntry)
esprehn 2015/10/20 21:03:35 This should just be in the ElementRareData, you do
majidvp 2015/11/23 15:43:57 Moved to LayoutBoxRareData as discussed above.
+ addResult.storedValue->value = adoptPtrWillBeNoop(new SnapElementSet);
+
+ return *addResult.storedValue->value;
+}
+
+void SnapCoordinator::addSnapContainer(const Element& container)
+{
+ ensureSnapElementSet(container);
+}
+
+void SnapCoordinator::removeSnapContainer(const Element& container)
+{
+ // TODO(majidvp): We should track these removals and report them to snap manager.
+ m_elementMap.remove(&container);
+}
+
+void SnapCoordinator::addSnapElement(const Element& container, const Element& element)
+{
+ ensureSnapElementSet(container).add(&element);
+}
+
+void SnapCoordinator::removeSnapElement(const Element& element)
+{
+ if (const Element* container = findSnapContainer(element)) {
+ if (m_elementMap.contains(container)) {
+ m_elementMap.get(container)->remove(&element);
+ }
+ }
+}
+
+void SnapCoordinator::snapElementDidChange(const Element& element, Vector<LengthPoint> snapCoordinates)
+{
+ if (snapCoordinates.isEmpty()) {
+ removeSnapElement(element);
+ return;
+ }
+
+ const Element* snapContainer = findSnapContainer(element);
+
+ if (snapContainer) {
+ // TODO(majidvp): store snap container in snap element itself to avoid re-computation
+ addSnapElement(*snapContainer, element);
+ } else {
+ // TODO(majidvp): keep track of snap elements that do not have any container
+ // so that we check them again when a new container is added to the page.
+ }
+}
+
+void SnapCoordinator::snapContainerDidChange(const Element& snapContainer, ScrollSnapType scrollSnapType)
+{
+ if (scrollSnapType == ScrollSnapTypeNone)
+ removeSnapContainer(snapContainer);
+ else
+ addSnapContainer(snapContainer);
+}
+
+bool isSnapContainer(const ComputedStyle* style)
esprehn 2015/10/20 21:03:35 static
majidvp 2015/10/22 21:03:03 Acknowledged.
majidvp 2015/11/23 15:43:57 Done.
+{
+ return style && style->scrollSnapType() != ScrollSnapTypeNone;
+}
+
+bool isSnapElement(const ComputedStyle* style)
esprehn 2015/10/20 21:03:35 static
majidvp 2015/10/22 21:03:03 Acknowledged.
majidvp 2015/11/23 15:43:57 Done.
+{
+ return style && !style->scrollSnapCoordinate().isEmpty();
+}
+
+const Element* SnapCoordinator::findSnapContainer(const Element& element)
+{
+ LayoutBox* curBox = element.layoutObject()->enclosingBox();
+ while (curBox) {
+ Node* curNode = curBox->node();
esprehn 2015/10/20 21:03:35 you're walking the dom here, this should just be N
majidvp 2015/10/22 21:03:03 We are not walking the DOM tree. The aim here is t
+ if (curNode && curNode->isElementNode() && isSnapContainer(curNode->computedStyle())) {
+ // TODO(majidvp): upcoming spec change will require "scroll-snap-points: elements" to be present as well
+ return toElement(curNode);
+ }
+ curBox = curBox->containingBlock();
+ }
+
+ return nullptr;
+}
+
+// Translate local snap coordinates into snap container space
+Vector<FloatPoint> localToContainerSnapCoordinates(const Element& container, const Element& element)
+{
+ Vector<FloatPoint> result;
+
+ LayoutBox* localBox = element.layoutBox();
+ LayoutBox* containerBox = container.layoutBox();
+ LayoutPoint scrollOffset(containerBox->scrollLeft(), containerBox->scrollTop());
+
+ Vector<LengthPoint> snapCoordinates = element.computedStyle()->scrollSnapCoordinate();
esprehn 2015/10/20 21:03:35 const ref? this is more copies
majidvp 2015/10/22 21:03:03 Acknowledged.
majidvp 2015/11/23 15:43:57 Done.
+ for (auto& coordinate : snapCoordinates) {
+ FloatPoint localPoint = floatPointForLengthPoint(coordinate, FloatSize(localBox->size()));
+ FloatPoint containerPoint = localBox->localToContainerPoint(localPoint, containerBox);
+ containerPoint.moveBy(scrollOffset);
+ result.append(containerPoint);
+ }
+
+ return result;
+}
+
+Vector<double> SnapCoordinator::snapOffsets(const Element& snapContainer, ScrollbarOrientation orientation)
+{
+ const ComputedStyle* style = snapContainer.computedStyle();
+ const LayoutBox* box = snapContainer.layoutBox();
+ ASSERT(style);
+ ASSERT(box);
+
+ ScrollSnapPoints snapPoints = (orientation == HorizontalScrollbar) ? style->scrollSnapPointsX() : style->scrollSnapPointsY();
+
+ Vector<double> result;
+ LayoutUnit clientSize = (orientation == HorizontalScrollbar) ? box->clientWidth(): box->clientHeight();
+ LayoutUnit scrollSize = (orientation == HorizontalScrollbar) ? box->scrollWidth(): box->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(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 element to snap container
+ bool didAddElementSnapOffset = false;
+ if (SnapElementSet* snapElements = m_elementMap.get(&snapContainer)) {
+ for (auto& element : *snapElements) {
+ Vector<FloatPoint> snapCoordinates = localToContainerSnapCoordinates(snapContainer, *element);
+ for (const FloatPoint& snapCoordinate : snapCoordinates) {
+ float snapOffset = (orientation == HorizontalScrollbar) ? snapCoordinate.x() : snapCoordinate.y();
+ if (snapOffset > scrollSize - clientSize)
+ continue;
+ result.append(snapOffset);
+ didAddElementSnapOffset = true;
+ }
+ }
+ }
+
+ if (didAddElementSnapOffset)
+ std::sort(result.begin(), result.end());
+
+ return result;
+}
+
+#ifndef NDEBUG
+
+void SnapCoordinator::showSnapElementMap()
+{
+ for (auto& pair : m_elementMap)
+ showSnapElementsFor(pair.key);
+}
+
+void SnapCoordinator::showSnapElementsFor(const Element* container)
+{
+ const char* prefix = " ";
+ container->showNodePathForThis();
+ WTFLogAlways("%p", container);
+ if (SnapElementSet* snapElements = m_elementMap.get(container)) {
+ WTFLogAlways("(%d snap elements)", snapElements->size());
+ for (auto& element : *snapElements) {
+ element->showNode(prefix);
+ }
+ } else {
+ WTFLogAlways("%sNo snap elements for this node\n", prefix);
+ }
+}
+
+#endif
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698