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/IntersectionObserverCallback.h" | |
| 9 #include "core/frame/LocalFrame.h" | |
| 10 | |
| 11 namespace blink { | |
| 12 | |
| 13 class ElementVisibilityObserver::ElementVisibilityCallback : public Intersection ObserverCallback { | |
|
esprehn
2016/06/14 16:01:18
move into IntersectionObserver constructor for C++
| |
| 14 public: | |
| 15 ElementVisibilityCallback(ElementVisibilityObserver* observer) | |
| 16 : m_observer(observer) | |
| 17 {} | |
| 18 | |
| 19 void handleEvent(const HeapVector<Member<IntersectionObserverEntry>>& entrie s, IntersectionObserver&) override | |
| 20 { | |
| 21 DCHECK_EQ(entries.size(), 1u); | |
| 22 | |
| 23 bool isVisible = entries[0]->intersectionRatio() > 0.f; | |
| 24 (*m_observer->m_callback.get())(isVisible); | |
| 25 } | |
| 26 | |
| 27 ExecutionContext* getExecutionContext() const override | |
| 28 { | |
| 29 return &m_observer->m_element->document(); | |
| 30 } | |
| 31 | |
| 32 DEFINE_INLINE_TRACE() | |
| 33 { | |
| 34 IntersectionObserverCallback::trace(visitor); | |
| 35 visitor->trace(m_observer); | |
| 36 } | |
| 37 | |
| 38 private: | |
| 39 Member<ElementVisibilityObserver> m_observer; | |
| 40 }; | |
| 41 | |
| 42 ElementVisibilityObserver::ElementVisibilityObserver(Element* element, std::uniq ue_ptr<VisibilityCallback> callback) | |
| 43 : m_element(element) | |
| 44 , m_callback(std::move(callback)) | |
| 45 , m_intersectionObserverCallback(new ElementVisibilityCallback(this)) | |
| 46 { | |
| 47 } | |
| 48 | |
| 49 ElementVisibilityObserver::~ElementVisibilityObserver() = default; | |
| 50 | |
| 51 void ElementVisibilityObserver::start() | |
| 52 { | |
| 53 DCHECK(!m_intersectionObserver); | |
| 54 | |
| 55 Frame* mainFrame = m_element->document().frame()->tree().top(); | |
| 56 if (!mainFrame || !mainFrame->isLocalFrame()) | |
| 57 return; | |
| 58 | |
| 59 Node* root = toLocalFrame(mainFrame)->document(); | |
| 60 m_intersectionObserver = new IntersectionObserver(*m_intersectionObserverCal lback, *root, Vector<Length>(), Vector<float>({0.f})); | |
|
esprehn
2016/06/14 16:01:18
Please add IntersectionObserver::create() or const
| |
| 61 m_intersectionObserver->observe(m_element); | |
| 62 } | |
| 63 | |
| 64 void ElementVisibilityObserver::stop() | |
| 65 { | |
| 66 DCHECK(m_intersectionObserver); | |
| 67 | |
| 68 m_intersectionObserver->unobserve(m_element); | |
| 69 m_intersectionObserver = nullptr; | |
| 70 } | |
| 71 | |
| 72 DEFINE_TRACE(ElementVisibilityObserver) | |
| 73 { | |
| 74 visitor->trace(m_element); | |
| 75 visitor->trace(m_intersectionObserver); | |
| 76 visitor->trace(m_intersectionObserverCallback); | |
| 77 } | |
| 78 | |
| 79 } // namespace blink | |
| OLD | NEW |