Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/dom/IntersectionObservation.h" | 5 #include "core/dom/IntersectionObservation.h" |
| 6 | 6 |
| 7 #include "core/dom/ElementRareData.h" | 7 #include "core/dom/ElementRareData.h" |
| 8 #include "core/dom/IntersectionObserver.h" | 8 #include "core/dom/IntersectionObserver.h" |
| 9 #include "core/frame/FrameView.h" | 9 #include "core/frame/FrameView.h" |
| 10 #include "core/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
| 11 #include "core/layout/LayoutBox.h" | 11 #include "core/layout/LayoutBox.h" |
| 12 #include "core/layout/LayoutView.h" | 12 #include "core/layout/LayoutView.h" |
| 13 #include "core/paint/PaintLayer.h" | 13 #include "core/paint/PaintLayer.h" |
| 14 | 14 |
| 15 namespace blink { | 15 namespace blink { |
| 16 | 16 |
| 17 IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, | 17 IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, |
| 18 Element& target, | 18 Element& target, |
| 19 bool shouldReportRootBounds) | 19 bool shouldReportRootBounds) |
| 20 : m_observer(observer), | 20 : m_observer(observer), |
| 21 m_target(&target), | 21 m_target(&target), |
| 22 m_shouldReportRootBounds(shouldReportRootBounds), | 22 m_shouldReportRootBounds(shouldReportRootBounds), |
| 23 m_lastThresholdIndex(0) {} | 23 m_lastThresholdIndex(0) {} |
| 24 | 24 |
| 25 IntersectionObservation::~IntersectionObservation() {} | |
| 26 | |
| 25 void IntersectionObservation::applyRootMargin(LayoutRect& rect) const { | 27 void IntersectionObservation::applyRootMargin(LayoutRect& rect) const { |
| 26 if (m_shouldReportRootBounds) | 28 if (m_shouldReportRootBounds) |
| 27 m_observer->applyRootMargin(rect); | 29 m_observer->applyRootMargin(rect); |
| 28 } | 30 } |
| 29 | 31 |
| 30 void IntersectionObservation::initializeGeometry( | 32 void IntersectionObservation::initializeGeometry( |
| 31 IntersectionGeometry& geometry) const { | 33 IntersectionGeometry& geometry) const { |
| 32 initializeTargetRect(geometry.targetRect); | 34 initializeTargetRect(geometry.targetRect); |
| 33 geometry.intersectionRect = geometry.targetRect; | 35 geometry.intersectionRect = geometry.targetRect; |
| 34 initializeRootRect(geometry.rootRect); | 36 initializeRootRect(geometry.rootRect); |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 if (m_target) | 250 if (m_target) |
| 249 target()->ensureIntersectionObserverData().removeObservation(observer()); | 251 target()->ensureIntersectionObserverData().removeObservation(observer()); |
| 250 m_observer.clear(); | 252 m_observer.clear(); |
| 251 } | 253 } |
| 252 | 254 |
| 253 DEFINE_TRACE(IntersectionObservation) { | 255 DEFINE_TRACE(IntersectionObservation) { |
| 254 visitor->trace(m_observer); | 256 visitor->trace(m_observer); |
| 255 visitor->trace(m_target); | 257 visitor->trace(m_target); |
| 256 } | 258 } |
| 257 | 259 |
| 260 IntersectViewportRatioObservation::IntersectViewportRatioObservation( | |
| 261 IntersectionObserver& observer, | |
| 262 Element& element) | |
| 263 : IntersectionObservation(observer, element, true) {} | |
| 264 | |
| 265 IntersectViewportRatioObservation::~IntersectViewportRatioObservation() {} | |
| 266 | |
| 267 void IntersectViewportRatioObservation::computeIntersectionObservations( | |
| 268 DOMHighResTimeStamp timestamp) { | |
| 269 IntersectionGeometry geometry; | |
| 270 if (!computeGeometry(geometry)) | |
| 271 return; | |
| 272 | |
| 273 float newVisibleRatio = 0; | |
| 274 if (!geometry.targetRect.isEmpty() && geometry.doesIntersect) { | |
|
miu
2016/11/14 21:03:33
This should check that |rootRect| is not empty ins
xjz
2016/11/15 23:03:39
Done.
| |
| 275 float intersectionArea = | |
| 276 geometry.intersectionRect.size().width().toFloat() * | |
| 277 geometry.intersectionRect.size().height().toFloat(); | |
| 278 float rootArea = geometry.rootRect.size().width().toFloat() * | |
| 279 geometry.rootRect.size().height().toFloat(); | |
| 280 newVisibleRatio = intersectionArea / rootArea; | |
| 281 } | |
| 282 if (newVisibleRatio != m_lastVisibleRatio) { | |
|
miu
2016/11/14 21:03:33
A second thought on this: Maybe we should send the
xjz
2016/11/15 23:03:39
Since we don't know the more intelligent logic for
| |
| 283 IntRect snappedRootBounds = pixelSnappedIntRect(geometry.rootRect); | |
| 284 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry( | |
| 285 timestamp, newVisibleRatio, pixelSnappedIntRect(geometry.targetRect), | |
| 286 &snappedRootBounds, pixelSnappedIntRect(geometry.intersectionRect), | |
| 287 target()); | |
| 288 observer().enqueueIntersectionObserverEntry(*newEntry); | |
| 289 m_lastVisibleRatio = newVisibleRatio; | |
| 290 } | |
| 291 } | |
| 292 | |
| 258 } // namespace blink | 293 } // namespace blink |
| OLD | NEW |