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 PassRefPtrWillBeRawPtr<IntersectionObservation> IntersectionObservation::create( IntersectionObserver& observer, WeakPtr<Element> target, bool active) | |
14 { | |
15 return adoptRefWillBeNoop(new IntersectionObservation(observer, target, acti ve)); | |
16 } | |
17 | |
18 IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, WeakPtr<Element> target, bool active) | |
19 : m_observer(observer) | |
20 , m_target(target) | |
21 , m_active(active) | |
22 , m_lastVisibleRatio(0) | |
23 { | |
24 } | |
25 | |
26 const IntersectionObserver* IntersectionObservation::observer() const | |
27 { | |
28 return m_observer.get(); | |
29 } | |
30 | |
31 IntersectionObserver* IntersectionObservation::observer() | |
32 { | |
33 return m_observer.get(); | |
34 } | |
35 | |
36 void IntersectionObservation::setActive(bool active) | |
37 { | |
38 if (active == m_active) | |
39 return; | |
40 m_active = active; | |
41 if (active) | |
42 observer()->trackingDocument()->intersectionObservationRegistry()->addOb servation(*this); | |
43 else | |
44 observer()->trackingDocument()->intersectionObservationRegistry()->remov eObservation(*this); | |
45 } | |
46 | |
47 void IntersectionObservation::computeIntersectionObservations(double timestamp) | |
48 { | |
49 // TODO: rootMargin is not yet supported; need to clarify how it interacts w ith | |
50 // ancestor clipping (https://github.com/slightlyoff/IntersectionObserver/is sues/70) | |
51 if (!isActive() || !target()) | |
52 return; | |
53 Element* root = observer()->root(); | |
54 // Only process observers for which this element is target, not root. | |
55 if (root && root == target()) | |
56 return; | |
57 LayoutObject* targetLayoutObject = target()->layoutObject(); | |
58 // TODO: Support SVG | |
59 if (!targetLayoutObject || (!targetLayoutObject->isBox() && !targetLayoutObj ect->isInline())) | |
60 return; | |
61 | |
62 LayoutRect targetRect, rootRect, intersectionRect; | |
63 if (!target()->computeIntersection(root, targetRect, rootRect, intersectionR ect)) | |
64 return; | |
65 float intersectionArea = intersectionRect.size().width().toFloat() * interse ctionRect.size().height().toFloat(); | |
66 float targetArea = targetRect.size().width().toFloat() * targetRect.size().h eight().toFloat(); | |
67 if (!targetArea) | |
68 return; | |
69 float newVisibleRatio = intersectionArea / targetArea; | |
70 size_t newThresholdIndex = observer()->firstThresholdGreaterThan(newVisibleR atio); | |
71 size_t oldThresholdIndex = observer()->firstThresholdGreaterThan(lastVisible Ratio()); | |
72 setLastVisibleRatio(newVisibleRatio); | |
73 if (oldThresholdIndex != newThresholdIndex) | |
74 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
| |
75 } | |
76 | |
77 void IntersectionObservation::disconnect() | |
78 { | |
79 observer()->disconnect(*this); | |
80 } | |
81 | |
82 DEFINE_TRACE(IntersectionObservation) | |
83 { | |
84 visitor->trace(m_observer); | |
85 visitor->trace(m_target); | |
86 } | |
87 | |
88 } // namespace blink { | |
OLD | NEW |