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

Side by Side Diff: third_party/WebKit/Source/core/dom/IntersectionObservation.cpp

Issue 1449623002: IntersectionObserver: second cut. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: sigbjornf nits Created 4 years, 11 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 unified diff | Download patch
OLDNEW
(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 "core/dom/IntersectionObservation.h"
6
7 #include "core/dom/ElementRareData.h"
8 #include "core/dom/IntersectionObserver.h"
9 #include "core/frame/FrameView.h"
10 #include "core/layout/LayoutBox.h"
11 #include "core/layout/LayoutText.h"
12 #include "core/layout/LayoutView.h"
13 #include "core/paint/PaintLayer.h"
14
15 namespace blink {
16
17 IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, Element& target, bool shouldReportRootBounds)
18 : m_observer(observer)
19 , m_target(target.ensureIntersectionObserverData().createWeakPtr(&target))
20 , m_active(true)
21 , m_shouldReportRootBounds(shouldReportRootBounds)
22 , m_lastThresholdIndex(0)
23 {
24 }
25
26 void IntersectionObservation::initializeGeometry(IntersectionGeometry& geometry)
27 {
28 LayoutObject* targetLayoutObject = m_target->layoutObject();
29 if (targetLayoutObject->isBoxModelObject())
30 geometry.targetRect = toLayoutBoxModelObject(targetLayoutObject)->visual OverflowRect();
31 else
32 geometry.targetRect = toLayoutText(targetLayoutObject)->visualOverflowRe ct();
33 geometry.intersectionRect = geometry.targetRect;
34 }
35
36 void IntersectionObservation::clipToRoot(LayoutRect& rect)
37 {
38 // Map and clip rect into root element coordinates.
39 // TODO(szager): the writing mode flipping needs a test.
40 LayoutObject* rootLayoutObject = m_observer->rootLayoutObject();
41 LayoutObject* targetLayoutObject = m_target->layoutObject();
42 targetLayoutObject->mapToVisibleRectInContainerSpace(toLayoutBoxModelObject( rootLayoutObject), rect, nullptr);
43 if (rootLayoutObject->hasOverflowClip()) {
44 LayoutBox* rootLayoutBox = toLayoutBox(rootLayoutObject);
45 LayoutRect clipRect(LayoutPoint(), LayoutSize(rootLayoutBox->layer()->si ze()));
46 m_observer->applyRootMargin(clipRect);
47 rootLayoutBox->flipForWritingMode(rect);
48 rect.intersect(clipRect);
49 rootLayoutBox->flipForWritingMode(rect);
50 }
51 }
52
53 void IntersectionObservation::clipToFrameView(IntersectionGeometry& geometry)
54 {
55 Element* rootElement = m_observer->root();
56 LayoutObject* rootLayoutObject = m_observer->rootLayoutObject();
57 if (rootElement == rootElement->document().documentElement()) {
58 geometry.rootRect = LayoutRect(rootElement->document().view()->visibleCo ntentRect());
59 m_observer->applyRootMargin(geometry.rootRect);
60 geometry.intersectionRect.intersect(geometry.rootRect);
61 } else {
62 if (rootLayoutObject->isBox())
63 geometry.rootRect = LayoutRect(toLayoutBox(rootLayoutObject)->absolu teContentBox());
64 else
65 geometry.rootRect = LayoutRect(rootLayoutObject->absoluteBoundingBox Rect());
66 m_observer->applyRootMargin(geometry.rootRect);
67 }
68
69 LayoutPoint scrollPosition(rootElement->document().view()->scrollPosition()) ;
70 geometry.targetRect.moveBy(-scrollPosition);
71 geometry.intersectionRect.moveBy(-scrollPosition);
72 geometry.rootRect.moveBy(-scrollPosition);
73 }
74
75 static void mapRectToDocumentCoordinates(LayoutObject& layoutObject, LayoutRect& rect)
76 {
77 rect = LayoutRect(layoutObject.localToAbsoluteQuad(FloatQuad(FloatRect(rect) ), UseTransforms | ApplyContainerFlip | TraverseDocumentBoundaries).boundingBox( ));
78 }
79
80 bool IntersectionObservation::computeGeometry(IntersectionGeometry& geometry)
81 {
82 LayoutObject* rootLayoutObject = m_observer->rootLayoutObject();
83 LayoutObject* targetLayoutObject = m_target->layoutObject();
84 if (!rootLayoutObject->isBoxModelObject())
85 return false;
86 if (!targetLayoutObject->isBoxModelObject() && !targetLayoutObject->isText() )
87 return false;
88
89 // Initialize targetRect and intersectionRect to bounds of target, in target 's coordinate space.
90 initializeGeometry(geometry);
91
92 // TODO(szager): Support intersection observations for zero-area targets. F or now, we just
93 // punt on the observation.
94 if (!geometry.targetRect.size().width() || !geometry.targetRect.size().heigh t())
95 return false;
96
97 // Clip intersectionRect to the root, and map it to root coordinates.
98 clipToRoot(geometry.intersectionRect);
99
100 // Map targetRect into document coordinates.
101 mapRectToDocumentCoordinates(*targetLayoutObject, geometry.targetRect);
102
103 // Map intersectionRect into document coordinates.
104 mapRectToDocumentCoordinates(*rootLayoutObject, geometry.intersectionRect);
105
106 // Clip intersectionRect to FrameView visible area if necessary, and map all geometry to frame coordinates.
107 clipToFrameView(geometry);
108
109 if (geometry.intersectionRect.size().isZero())
110 geometry.intersectionRect = LayoutRect();
111 if (!m_shouldReportRootBounds)
112 geometry.rootRect = LayoutRect();
113
114 return true;
115 }
116
117 void IntersectionObservation::computeIntersectionObservations(double timestamp)
118 {
119 Element* targetElement = target();
120 if (!targetElement || !isActive())
121 return;
122 LayoutObject* targetLayoutObject = targetElement->layoutObject();
123 // TODO(szager): Support SVG
124 if (!targetLayoutObject || (!targetLayoutObject->isBox() && !targetLayoutObj ect->isInline()))
125 return;
126
127 IntersectionGeometry geometry;
128 if (!computeGeometry(geometry))
129 return;
130
131 float intersectionArea = geometry.intersectionRect.size().width().toFloat() * geometry.intersectionRect.size().height().toFloat();
132 float targetArea = geometry.targetRect.size().width().toFloat() * geometry.t argetRect.size().height().toFloat();
133 if (!targetArea)
134 return;
135 float newVisibleRatio = intersectionArea / targetArea;
136 unsigned newThresholdIndex = observer().firstThresholdGreaterThan(newVisible Ratio);
137 if (m_lastThresholdIndex != newThresholdIndex) {
138 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry(
139 timestamp / 1000.0,
140 pixelSnappedIntRect(geometry.targetRect),
141 pixelSnappedIntRect(geometry.rootRect),
142 pixelSnappedIntRect(geometry.intersectionRect),
143 targetElement);
144 observer().enqueueIntersectionObserverEntry(*newEntry);
145 }
146 setLastThresholdIndex(newThresholdIndex);
147 }
148
149 void IntersectionObservation::disconnect()
150 {
151 observer().disconnect(*this);
haraken 2016/01/02 13:47:40 This wouldn't be necessary because the Observation
152 if (m_target)
153 m_target->ensureIntersectionObserverData().removeObservation(this->obser ver());
154 }
155
156 DEFINE_TRACE(IntersectionObservation)
157 {
158 visitor->trace(m_observer);
159 visitor->trace(m_target);
160 }
161
162 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698