| 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/ElementVisibilityObserver.h" | 5 #include "core/dom/ElementVisibilityObserver.h" |
| 6 | 6 |
| 7 #include "core/dom/Element.h" | 7 #include "core/dom/Element.h" |
| 8 #include "core/dom/IntersectionObserverEntry.h" | 8 #include "core/dom/IntersectionObserverEntry.h" |
| 9 #include "core/frame/LocalFrame.h" | 9 #include "core/frame/LocalFrame.h" |
| 10 #include "platform/wtf/Functional.h" | 10 #include "platform/wtf/Functional.h" |
| 11 | 11 |
| 12 namespace blink { | 12 namespace blink { |
| 13 | 13 |
| 14 ElementVisibilityObserver::ElementVisibilityObserver( | 14 ElementVisibilityObserver::ElementVisibilityObserver( |
| 15 Element* element, | 15 Element* element, |
| 16 std::unique_ptr<VisibilityCallback> callback) | 16 std::unique_ptr<VisibilityCallback> callback) |
| 17 : element_(element), callback_(std::move(callback)) {} | 17 : element_(element), callback_(std::move(callback)) {} |
| 18 | 18 |
| 19 ElementVisibilityObserver::~ElementVisibilityObserver() = default; | 19 ElementVisibilityObserver::~ElementVisibilityObserver() = default; |
| 20 | 20 |
| 21 void ElementVisibilityObserver::Start() { | 21 void ElementVisibilityObserver::Start(float threshold) { |
| 22 DCHECK(!intersection_observer_); | 22 DCHECK(!intersection_observer_); |
| 23 | 23 |
| 24 ExecutionContext* context = element_->GetExecutionContext(); | 24 ExecutionContext* context = element_->GetExecutionContext(); |
| 25 DCHECK(context->IsDocument()); | 25 DCHECK(context->IsDocument()); |
| 26 Document& document = ToDocument(*context); | 26 Document& document = ToDocument(*context); |
| 27 | 27 |
| 28 intersection_observer_ = IntersectionObserver::Create( | 28 intersection_observer_ = IntersectionObserver::Create( |
| 29 Vector<Length>(), Vector<float>({std::numeric_limits<float>::min()}), | 29 {} /* root_margin */, {threshold}, &document, |
| 30 &document, | |
| 31 WTF::Bind(&ElementVisibilityObserver::OnVisibilityChanged, | 30 WTF::Bind(&ElementVisibilityObserver::OnVisibilityChanged, |
| 32 WrapWeakPersistent(this))); | 31 WrapWeakPersistent(this))); |
| 33 DCHECK(intersection_observer_); | 32 DCHECK(intersection_observer_); |
| 34 | 33 |
| 35 intersection_observer_->observe(element_.Release()); | 34 intersection_observer_->observe(element_.Release()); |
| 36 } | 35 } |
| 37 | 36 |
| 38 void ElementVisibilityObserver::Stop() { | 37 void ElementVisibilityObserver::Stop() { |
| 39 DCHECK(intersection_observer_); | 38 DCHECK(intersection_observer_); |
| 40 | 39 |
| 41 intersection_observer_->disconnect(); | 40 intersection_observer_->disconnect(); |
| 42 intersection_observer_ = nullptr; | 41 intersection_observer_ = nullptr; |
| 43 } | 42 } |
| 44 | 43 |
| 45 void ElementVisibilityObserver::DeliverObservationsForTesting() { | 44 void ElementVisibilityObserver::DeliverObservationsForTesting() { |
| 46 intersection_observer_->Deliver(); | 45 intersection_observer_->Deliver(); |
| 47 } | 46 } |
| 48 | 47 |
| 49 DEFINE_TRACE(ElementVisibilityObserver) { | 48 DEFINE_TRACE(ElementVisibilityObserver) { |
| 50 visitor->Trace(element_); | 49 visitor->Trace(element_); |
| 51 visitor->Trace(intersection_observer_); | 50 visitor->Trace(intersection_observer_); |
| 52 } | 51 } |
| 53 | 52 |
| 54 void ElementVisibilityObserver::OnVisibilityChanged( | 53 void ElementVisibilityObserver::OnVisibilityChanged( |
| 55 const HeapVector<Member<IntersectionObserverEntry>>& entries) { | 54 const HeapVector<Member<IntersectionObserverEntry>>& entries) { |
| 56 bool is_visible = entries.back()->intersectionRatio() > 0.f; | 55 bool is_visible = entries.back()->intersectionRatio() > |
| 56 intersection_observer_->thresholds()[0]; |
| 57 (*callback_.get())(is_visible); | 57 (*callback_.get())(is_visible); |
| 58 } | 58 } |
| 59 | 59 |
| 60 } // namespace blink | 60 } // namespace blink |
| OLD | NEW |