OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "core/dom/IntersectionObservation.h" |
| 7 |
| 8 #include "core/dom/IntersectionObserver.h" |
| 9 #include "core/layout/LayoutObject.h" |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 IntersectionObservation::IntersectionObservation(IntersectionObserver& observer,
WeakPtrWillBeRawPtr<Element> target, bool active) |
| 14 : m_observer(observer) |
| 15 , m_target(target) |
| 16 , m_active(active) |
| 17 , m_lastVisibleRatio(0) |
| 18 { |
| 19 } |
| 20 |
| 21 void IntersectionObservation::setActive(bool active) |
| 22 { |
| 23 if (active == m_active) |
| 24 return; |
| 25 m_active = active; |
| 26 if (active) |
| 27 observer()->trackingDocument()->intersectionObservationRegistry()->addOb
servation(*this); |
| 28 else |
| 29 observer()->trackingDocument()->intersectionObservationRegistry()->remov
eObservation(*this); |
| 30 } |
| 31 |
| 32 void IntersectionObservation::computeIntersectionObservations(double timestamp) |
| 33 { |
| 34 // TODO: rootMargin is not yet supported; need to clarify how it interacts w
ith |
| 35 // ancestor clipping (https://github.com/slightlyoff/IntersectionObserver/is
sues/70) |
| 36 Element* targetElement = target(); |
| 37 if (!targetElement || !isActive()) |
| 38 return; |
| 39 Element* rootElement = observer()->root(); |
| 40 // Only process observers for which this element is target, not root. |
| 41 if (rootElement && rootElement == targetElement) |
| 42 return; |
| 43 LayoutObject* targetLayoutObject = targetElement->layoutObject(); |
| 44 // TODO: Support SVG |
| 45 if (!targetLayoutObject || (!targetLayoutObject->isBox() && !targetLayoutObj
ect->isInline())) |
| 46 return; |
| 47 |
| 48 LayoutRect targetRect, rootRect, intersectionRect; |
| 49 if (!targetElement->computeIntersection(rootElement, targetRect, rootRect, i
ntersectionRect)) |
| 50 return; |
| 51 |
| 52 float intersectionArea = intersectionRect.size().width().toFloat() * interse
ctionRect.size().height().toFloat(); |
| 53 float targetArea = targetRect.size().width().toFloat() * targetRect.size().h
eight().toFloat(); |
| 54 if (!targetArea) |
| 55 return; |
| 56 float newVisibleRatio = intersectionArea / targetArea; |
| 57 size_t newThresholdIndex = observer()->firstThresholdGreaterThan(newVisibleR
atio); |
| 58 size_t oldThresholdIndex = observer()->firstThresholdGreaterThan(lastVisible
Ratio()); |
| 59 setLastVisibleRatio(newVisibleRatio); |
| 60 if (oldThresholdIndex != newThresholdIndex) |
| 61 observer()->enqueueIntersectionObserverEntry(new IntersectionObserverEnt
ry(timestamp, pixelSnappedIntRect(targetRect), pixelSnappedIntRect(rootRect), pi
xelSnappedIntRect(intersectionRect), targetElement)); |
| 62 } |
| 63 |
| 64 void IntersectionObservation::disconnect() |
| 65 { |
| 66 observer()->disconnect(*this); |
| 67 } |
| 68 |
| 69 DEFINE_TRACE(IntersectionObservation) |
| 70 { |
| 71 visitor->trace(m_observer); |
| 72 visitor->trace(m_target); |
| 73 } |
| 74 |
| 75 } // namespace blink { |
OLD | NEW |