Index: third_party/WebKit/Source/core/dom/IntersectionObservation.cpp |
diff --git a/third_party/WebKit/Source/core/dom/IntersectionObservation.cpp b/third_party/WebKit/Source/core/dom/IntersectionObservation.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..faa6b57ba40fe3ccf65dd472a7bd044f946e1c4f |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/dom/IntersectionObservation.cpp |
@@ -0,0 +1,88 @@ |
+// 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 "core/dom/IntersectionObservation.h" |
+ |
+#include "core/dom/IntersectionObserver.h" |
+#include "core/layout/LayoutObject.h" |
+ |
+namespace blink { |
+ |
+PassRefPtrWillBeRawPtr<IntersectionObservation> IntersectionObservation::create(IntersectionObserver& observer, WeakPtr<Element> target, bool active) |
+{ |
+ return adoptRefWillBeNoop(new IntersectionObservation(observer, target, active)); |
+} |
+ |
+IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, WeakPtr<Element> target, bool active) |
+ : m_observer(observer) |
+ , m_target(target) |
+ , m_active(active) |
+ , m_lastVisibleRatio(0) |
+{ |
+} |
+ |
+const IntersectionObserver* IntersectionObservation::observer() const |
+{ |
+ return m_observer.get(); |
+} |
+ |
+IntersectionObserver* IntersectionObservation::observer() |
+{ |
+ return m_observer.get(); |
+} |
+ |
+void IntersectionObservation::setActive(bool active) |
+{ |
+ if (active == m_active) |
+ return; |
+ m_active = active; |
+ if (active) |
+ observer()->trackingDocument()->intersectionObservationRegistry()->addObservation(*this); |
+ else |
+ observer()->trackingDocument()->intersectionObservationRegistry()->removeObservation(*this); |
+} |
+ |
+void IntersectionObservation::computeIntersectionObservations(double timestamp) |
+{ |
+ // TODO: rootMargin is not yet supported; need to clarify how it interacts with |
+ // ancestor clipping (https://github.com/slightlyoff/IntersectionObserver/issues/70) |
+ if (!isActive() || !target()) |
+ return; |
+ Element* root = observer()->root(); |
+ // Only process observers for which this element is target, not root. |
+ if (root && root == target()) |
+ return; |
+ LayoutObject* targetLayoutObject = target()->layoutObject(); |
+ // TODO: Support SVG |
+ if (!targetLayoutObject || (!targetLayoutObject->isBox() && !targetLayoutObject->isInline())) |
+ return; |
+ |
+ LayoutRect targetRect, rootRect, intersectionRect; |
+ if (!target()->computeIntersection(root, targetRect, rootRect, intersectionRect)) |
+ return; |
+ float intersectionArea = intersectionRect.size().width().toFloat() * intersectionRect.size().height().toFloat(); |
+ float targetArea = targetRect.size().width().toFloat() * targetRect.size().height().toFloat(); |
+ if (!targetArea) |
+ return; |
+ float newVisibleRatio = intersectionArea / targetArea; |
+ size_t newThresholdIndex = observer()->firstThresholdGreaterThan(newVisibleRatio); |
+ size_t oldThresholdIndex = observer()->firstThresholdGreaterThan(lastVisibleRatio()); |
+ setLastVisibleRatio(newVisibleRatio); |
+ if (oldThresholdIndex != newThresholdIndex) |
+ observer()->enqueueIntersectionObserverEntry(IntersectionObserverEntry::create(timestamp, pixelSnappedIntRect(targetRect), pixelSnappedIntRect(rootRect), pixelSnappedIntRect(intersectionRect), target())); |
eae
2015/11/16 23:34:44
Please wrap this line, 212 columns is a bit too wi
|
+} |
+ |
+void IntersectionObservation::disconnect() |
+{ |
+ observer()->disconnect(*this); |
+} |
+ |
+DEFINE_TRACE(IntersectionObservation) |
+{ |
+ visitor->trace(m_observer); |
+ visitor->trace(m_target); |
+} |
+ |
+} // namespace blink { |