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 <limits> |
| 9 |
8 #include "core/CoreExport.h" | 10 #include "core/CoreExport.h" |
9 #include "core/dom/IntersectionObserver.h" | 11 #include "core/dom/IntersectionObserver.h" |
10 #include "platform/heap/Heap.h" | 12 #include "platform/heap/Heap.h" |
11 #include "platform/heap/Member.h" | 13 #include "platform/heap/Member.h" |
12 | 14 |
13 namespace blink { | 15 namespace blink { |
14 | 16 |
15 class Element; | 17 class Element; |
16 | 18 |
17 // ElementVisibilityObserver is a helper class to be used to track the | 19 // ElementVisibilityObserver is a helper class to be used to track the |
18 // visibility of an Element in the viewport. Creating an | 20 // visibility of an Element in the viewport. Creating an |
19 // ElementVisibilityObserver is a no-op with regards to CPU cycle. The observing | 21 // 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()|. | 22 // has be started by calling |start()| and can be stopped with |stop()|. |
21 // When creating an instance, the caller will have to pass a callback taking | 23 // 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. | 24 // a boolean as an argument. The boolean will be the new visibility state. |
23 // The ElementVisibilityObserver is implemented on top of IntersectionObserver. | 25 // 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 | 26 // It is a layer meant to simplify the usage for C++ Blink code checking for the |
25 // visibility of an element. | 27 // visibility of an element. |
26 class CORE_EXPORT ElementVisibilityObserver final | 28 class CORE_EXPORT ElementVisibilityObserver final |
27 : public GarbageCollectedFinalized<ElementVisibilityObserver> { | 29 : public GarbageCollectedFinalized<ElementVisibilityObserver> { |
28 WTF_MAKE_NONCOPYABLE(ElementVisibilityObserver); | 30 WTF_MAKE_NONCOPYABLE(ElementVisibilityObserver); |
29 | 31 |
30 public: | 32 public: |
31 using VisibilityCallback = Function<void(bool), WTF::kSameThreadAffinity>; | 33 using VisibilityCallback = Function<void(bool), WTF::kSameThreadAffinity>; |
32 | 34 |
33 ElementVisibilityObserver(Element*, std::unique_ptr<VisibilityCallback>); | 35 ElementVisibilityObserver(Element*, std::unique_ptr<VisibilityCallback>); |
34 virtual ~ElementVisibilityObserver(); | 36 virtual ~ElementVisibilityObserver(); |
35 | 37 |
36 // The |threshold| is the minimum fraction that needs to be visible. | 38 // The |threshold| is the minimum fraction that needs to be visible. |
37 void Start(float threshold = 0.0); | 39 // See https://github.com/WICG/IntersectionObserver/issues/164 for why this |
| 40 // defaults to std::numeric_limits<float>::min() rather than zero. |
| 41 void Start(float threshold = std::numeric_limits<float>::min()); |
38 void Stop(); | 42 void Stop(); |
39 | 43 |
40 void DeliverObservationsForTesting(); | 44 void DeliverObservationsForTesting(); |
41 | 45 |
42 DECLARE_VIRTUAL_TRACE(); | 46 DECLARE_VIRTUAL_TRACE(); |
43 | 47 |
44 private: | 48 private: |
45 class ElementVisibilityCallback; | 49 class ElementVisibilityCallback; |
46 | 50 |
47 void OnVisibilityChanged( | 51 void OnVisibilityChanged( |
48 const HeapVector<Member<IntersectionObserverEntry>>&); | 52 const HeapVector<Member<IntersectionObserverEntry>>&); |
49 | 53 |
50 Member<Element> element_; | 54 Member<Element> element_; |
51 Member<IntersectionObserver> intersection_observer_; | 55 Member<IntersectionObserver> intersection_observer_; |
52 std::unique_ptr<VisibilityCallback> callback_; | 56 std::unique_ptr<VisibilityCallback> callback_; |
53 }; | 57 }; |
54 | 58 |
55 } // namespace blink | 59 } // namespace blink |
56 | 60 |
57 #endif // ElementVisibilityObserver_h | 61 #endif // ElementVisibilityObserver_h |
OLD | NEW |