Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/dom/ElementVisibilityObserver.h" | |
| 6 | |
| 7 #include "core/dom/Element.h" | |
| 8 #include "core/dom/IntersectionObserverEntry.h" | |
| 9 #include "wtf/Functional.h" | |
| 10 | |
| 11 namespace blink { | |
| 12 | |
| 13 ElementVisibilityObserver::ElementVisibilityObserver(Element* element, std::uniq ue_ptr<VisibilityCallback> callback) | |
| 14 : m_element(element) | |
| 15 , m_callback(std::move(callback)) | |
| 16 { | |
| 17 } | |
| 18 | |
| 19 ElementVisibilityObserver::~ElementVisibilityObserver() = default; | |
| 20 | |
| 21 void ElementVisibilityObserver::start() | |
| 22 { | |
| 23 DCHECK(!m_intersectionObserver); | |
| 24 m_intersectionObserver = IntersectionObserver::create( | |
| 25 Vector<Length>(), Vector<float>({std::numeric_limits<float>::min()}), &m _element->document(), | |
| 26 WTF::bind<const HeapVector<Member<IntersectionObserverEntry>>&>(&Element VisibilityObserver::onVisibilityChanged, WeakPersistentThisPointer<ElementVisibi lityObserver>(this))); | |
| 27 DCHECK(m_intersectionObserver); | |
| 28 m_intersectionObserver->observe(m_element); | |
| 29 } | |
| 30 | |
| 31 void ElementVisibilityObserver::stop() | |
| 32 { | |
| 33 DCHECK(m_intersectionObserver); | |
| 34 | |
| 35 m_intersectionObserver->unobserve(m_element); | |
| 36 m_intersectionObserver = nullptr; | |
| 37 } | |
| 38 | |
| 39 DEFINE_TRACE(ElementVisibilityObserver) | |
| 40 { | |
| 41 visitor->trace(m_element); | |
| 42 visitor->trace(m_intersectionObserver); | |
| 43 } | |
| 44 | |
| 45 void ElementVisibilityObserver::onVisibilityChanged(const HeapVector<Member<Inte rsectionObserverEntry>>& entries) | |
| 46 { | |
| 47 DCHECK_EQ(entries.size(), 1u); | |
|
szager1
2016/06/21 21:18:05
This is a bad assumption; observations are taken e
mlamouri (slow - plz ping)
2016/06/21 21:25:50
Good to know! These small things comforts me in ha
| |
| 48 | |
| 49 bool isVisible = entries[0]->intersectionRatio() > 0.f; | |
| 50 (*m_callback.get())(isVisible); | |
| 51 } | |
| 52 | |
| 53 } // namespace blink | |
| OLD | NEW |