Chromium Code Reviews| 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 "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 target.ensureIntersectionObserverData().addObservation(*this); | |
|
haraken
2015/12/22 01:13:30
The logic around here is messy...
- IntersectionO
esprehn
2015/12/22 07:50:32
Yeah it's nice if the constructor has no side effe
szager1
2015/12/22 07:53:41
I think this would be much more confusing, because
szager1
2015/12/22 08:49:50
Moved the addObservation() call into the caller.
| |
| 25 } | |
| 26 | |
| 27 void IntersectionObservation::initializeGeometry(IntersectionGeometry& geometry) | |
| 28 { | |
| 29 LayoutObject* targetLayoutObject = m_target->layoutObject(); | |
| 30 if (targetLayoutObject->isBoxModelObject()) | |
| 31 geometry.targetRect = toLayoutBoxModelObject(targetLayoutObject)->visual OverflowRect(); | |
| 32 else | |
| 33 geometry.targetRect = toLayoutText(targetLayoutObject)->visualOverflowRe ct(); | |
| 34 geometry.intersectionRect = geometry.targetRect; | |
| 35 } | |
| 36 | |
| 37 void IntersectionObservation::clipToRoot(LayoutRect& rect) | |
| 38 { | |
| 39 // Map and clip rect into root element coordinates. | |
| 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); | |
|
esprehn
2015/12/22 07:50:32
ugh, writing mode incantations all over. Do we hav
szager1
2015/12/22 08:49:49
No, this was cargo culted from LayoutBox::applyCac
| |
| 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) | |
|
esprehn
2015/12/22 07:50:32
reference for layoutObject
szager1
2015/12/22 08:49:50
Done.
| |
| 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() ) | |
|
esprehn
2015/12/22 07:50:32
hmm, how can target be a Text? observe() on a Text
szager1
2015/12/22 08:49:49
Yeah, I think <br> would do it. And I think there
| |
| 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(newVisibl eRatio); | |
| 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); | |
| 152 if (m_target) | |
| 153 m_target->ensureIntersectionObserverData().removeObservation(*this->obse rver()); | |
|
haraken
2015/12/22 01:13:30
I think you should clear m_target here.
szager1
2015/12/22 07:53:41
Can you explain why? In oilpan world, Intersectio
| |
| 154 } | |
| 155 | |
| 156 DEFINE_TRACE(IntersectionObservation) | |
| 157 { | |
| 158 visitor->trace(m_observer); | |
| 159 visitor->trace(m_target); | |
| 160 } | |
| 161 | |
| 162 } // namespace blink { | |
| OLD | NEW |