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 #ifndef ElementVisibilityObserver_h | 5 #ifndef ElementVisibilityObserver_h |
| 6 #define ElementVisibilityObserver_h | 6 #define ElementVisibilityObserver_h |
| 7 | 7 |
| 8 #include "core/CoreExport.h" | 8 #include "core/CoreExport.h" |
| 9 #include "core/dom/IntersectionObserver.h" | 9 #include "core/dom/IntersectionObserver.h" |
| 10 #include "platform/heap/Heap.h" | 10 #include "platform/heap/Heap.h" |
| 11 #include "platform/heap/Member.h" | 11 #include "platform/heap/Member.h" |
| 12 | 12 |
| 13 namespace blink { | 13 namespace blink { |
| 14 | 14 |
| 15 class Element; | 15 class Element; |
| 16 | 16 |
| 17 // ElementVisibilityObserver is a helper class to be used to track the | 17 // ElementVisibilityObserver is a helper class to be used to track the |
| 18 // visibility of an Element in the viewport. Creating an | 18 // visibility of an Element in the viewport. Creating an |
| 19 // ElementVisibilityObserver is a no-op with regards to CPU cycle. The observing | 19 // ElementVisibilityObserver is a no-op with regards to CPU cycle. The observing |
| 20 // has be started by calling |start()| and can be stopped with |stop()|. | 20 // has be started by calling |start()| can be stopped with |stop()|. |
| 21 // When creating an instance, the caller will have to pass a callback taking | 21 // When creating an instance, the caller will have to pass a callback taking |
| 22 // a boolean as an argument. The boolean will be the new visibility state. | 22 // a boolean as an argument. The boolean will be the new visibility state. |
| 23 // The ElementVisibilityObserver is implemented on top of IntersectionObserver. | 23 // The ElementVisibilityObserver is implemented on top of IntersectionObserver. |
| 24 // It is a layer meant to simplify the usage for C++ Blink code checking for the | 24 // It is a layer meant to simplify the usage for C++ Blink code checking for the |
| 25 // visibility of an element. | 25 // visibility of an element. |
| 26 class CORE_EXPORT ElementVisibilityObserver final | 26 class CORE_EXPORT ElementVisibilityObserver |
| 27 : public GarbageCollectedFinalized<ElementVisibilityObserver> { | 27 : public GarbageCollectedFinalized<ElementVisibilityObserver> { |
| 28 WTF_MAKE_NONCOPYABLE(ElementVisibilityObserver); | 28 WTF_MAKE_NONCOPYABLE(ElementVisibilityObserver); |
| 29 | 29 |
| 30 public: | 30 public: |
| 31 using VisibilityCallback = Function<void(bool), WTF::SameThreadAffinity>; | 31 using VisibilityCallback = Function<void(bool), WTF::SameThreadAffinity>; |
| 32 | 32 |
| 33 ElementVisibilityObserver(Element*, std::unique_ptr<VisibilityCallback>); | 33 ElementVisibilityObserver(Element*, std::unique_ptr<VisibilityCallback>); |
| 34 virtual ~ElementVisibilityObserver(); | 34 virtual ~ElementVisibilityObserver(); |
| 35 | 35 |
| 36 void start(); | 36 void start(); |
| 37 void stop(); | 37 void stop(); |
| 38 | 38 |
| 39 void deliverObservationsForTesting(); | 39 void deliverObservationsForTesting(); |
| 40 | 40 |
| 41 float ratio() { return m_ratio; } | |
| 42 | |
| 41 DECLARE_VIRTUAL_TRACE(); | 43 DECLARE_VIRTUAL_TRACE(); |
| 42 | 44 |
| 45 protected: | |
| 46 void startObserveElementViewportRatio(); | |
|
miu
2016/11/09 22:02:07
Instead of the separate method, you should make st
xjz
2016/11/11 01:07:29
Done.
| |
| 47 | |
| 43 private: | 48 private: |
| 44 class ElementVisibilityCallback; | 49 class ElementVisibilityCallback; |
| 45 | 50 |
| 46 void onVisibilityChanged( | 51 void onVisibilityChanged( |
| 47 const HeapVector<Member<IntersectionObserverEntry>>&); | 52 const HeapVector<Member<IntersectionObserverEntry>>&); |
| 53 void startInternal(bool isObservingElementViewportRatio); | |
| 48 | 54 |
| 49 Member<Element> m_element; | 55 Member<Element> m_element; |
| 50 Member<IntersectionObserver> m_intersectionObserver; | 56 Member<IntersectionObserver> m_intersectionObserver; |
| 51 std::unique_ptr<VisibilityCallback> m_callback; | 57 std::unique_ptr<VisibilityCallback> m_callback; |
| 58 float m_ratio = 0; | |
| 59 }; | |
| 60 | |
| 61 // ElementViewportRatioObserver monitors both the visibilty of the element and | |
| 62 // the element/viewport ratio. When creating an instance, the caller will have | |
| 63 // to pass a callback taking a boolean as an argument. The boolean will be the | |
| 64 // new visibility state. Caller can call ratio() at any time to get current | |
| 65 // element/viewport ratio. | |
| 66 class ElementViewportRatioObserver : public ElementVisibilityObserver { | |
| 67 WTF_MAKE_NONCOPYABLE(ElementViewportRatioObserver); | |
| 68 | |
| 69 public: | |
| 70 ElementViewportRatioObserver(Element*, std::unique_ptr<VisibilityCallback>); | |
|
miu
2016/11/09 22:02:07
Reading this subclass's usage comments, it seems l
xjz
2016/11/11 01:07:29
Done.
| |
| 71 void start(); | |
| 52 }; | 72 }; |
| 53 | 73 |
| 54 } // namespace blink | 74 } // namespace blink |
| 55 | 75 |
| 56 #endif // ElementVisibilityObserver_h | 76 #endif // ElementVisibilityObserver_h |
| OLD | NEW |