ScrollbarActivity.h (3468B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef ScrollbarActivity_h___ 8 #define ScrollbarActivity_h___ 9 10 #include "mozilla/Assertions.h" 11 #include "nsCOMPtr.h" 12 #include "nsIDOMEventListener.h" 13 14 class nsIContent; 15 class nsIScrollbarMediator; 16 class nsITimer; 17 18 namespace mozilla { 19 20 namespace dom { 21 class Element; 22 class EventTarget; 23 } // namespace dom 24 25 namespace layout { 26 27 /** 28 * ScrollbarActivity 29 * 30 * This class manages scrollbar behavior that imitates the native Mac OS X 31 * Lion overlay scrollbar behavior: Scrollbars are only shown while "scrollbar 32 * activity" occurs, and they're hidden with a fade animation after a short 33 * delay. 34 * 35 * Scrollbar activity has these states: 36 * - inactive: 37 * Scrollbars are hidden. 38 * - ongoing activity: 39 * Scrollbars are visible and being operated on in some way, for example 40 * because they're hovered or pressed. 41 * - active, but waiting for fade out 42 * Scrollbars are still completely visible but are about to fade away. 43 * - fading out 44 * Scrollbars are subject to a fade-out animation. 45 * 46 * Initial scrollbar activity needs to be reported by the scrollbar holder that 47 * owns the ScrollbarActivity instance. This needs to happen via a call to 48 * ActivityOccurred(), for example when the current scroll position or the size 49 * of the scroll area changes. 50 * 51 * As soon as scrollbars are visible, the ScrollbarActivity class manages the 52 * rest of the activity behavior: It ensures that mouse motions inside the 53 * scroll area keep the scrollbars visible, and that scrollbars don't fade away 54 * while they're being hovered / dragged. It also sets a sticky hover attribute 55 * on the most recently hovered scrollbar. 56 * 57 * ScrollbarActivity falls into hibernation after the scrollbars have faded 58 * out. It only starts acting after the next call to ActivityOccurred() / 59 * ActivityStarted(). 60 */ 61 62 class ScrollbarActivity final : public nsIDOMEventListener { 63 public: 64 explicit ScrollbarActivity(nsIScrollbarMediator* aScrollableFrame) 65 : mScrollableFrame(aScrollableFrame) { 66 MOZ_ASSERT(mScrollableFrame); 67 } 68 69 NS_DECL_ISUPPORTS 70 NS_DECL_NSIDOMEVENTLISTENER 71 72 void Destroy(); 73 74 void ActivityOccurred(); 75 void ActivityStarted(); 76 void ActivityStopped(); 77 78 bool IsActive() const { return mNestedActivityCounter; } 79 80 protected: 81 virtual ~ScrollbarActivity() = default; 82 83 void StartFadeTimer(); 84 void CancelFadeTimer(); 85 void BeginFade(); 86 87 void StartListeningForScrollAreaEvents(); 88 void StopListeningForScrollAreaEvents(); 89 90 dom::Element* GetScrollbarContent(bool aVertical); 91 dom::Element* GetHorizontalScrollbar() { return GetScrollbarContent(false); } 92 dom::Element* GetVerticalScrollbar() { return GetScrollbarContent(true); } 93 94 nsIScrollbarMediator* const mScrollableFrame; 95 nsCOMPtr<nsITimer> mFadeTimer; 96 uint32_t mNestedActivityCounter = 0; 97 // This boolean is true from the point activity starts to the point BeginFade 98 // runs, and effectively reflects the "active" attribute of the scrollbar. 99 bool mScrollbarEffectivelyVisible = false; 100 bool mListeningForScrollAreaEvents = false; 101 }; 102 103 } // namespace layout 104 } // namespace mozilla 105 106 #endif /* ScrollbarActivity_h___ */