Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/layout/generic/ScrollbarActivity.h
Line
Count
Source (jump to first uncovered line)
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/Attributes.h"
11
#include "nsCOMPtr.h"
12
#include "nsIDOMEventListener.h"
13
#include "mozilla/TimeStamp.h"
14
#include "nsRefreshDriver.h"
15
16
class nsIContent;
17
class nsIScrollbarMediator;
18
class nsITimer;
19
20
namespace mozilla {
21
22
namespace dom {
23
class Element;
24
class EventTarget;
25
} // namespace dom
26
27
namespace layout {
28
29
/**
30
 * ScrollbarActivity
31
 *
32
 * This class manages scrollbar behavior that imitates the native Mac OS X
33
 * Lion overlay scrollbar behavior: Scrollbars are only shown while "scrollbar
34
 * activity" occurs, and they're hidden with a fade animation after a short
35
 * delay.
36
 *
37
 * Scrollbar activity has these states:
38
 *  - inactive:
39
 *      Scrollbars are hidden.
40
 *  - ongoing activity:
41
 *      Scrollbars are visible and being operated on in some way, for example
42
 *      because they're hovered or pressed.
43
 *  - active, but waiting for fade out
44
 *      Scrollbars are still completely visible but are about to fade away.
45
 *  - fading out
46
 *      Scrollbars are subject to a fade-out animation.
47
 *
48
 * Initial scrollbar activity needs to be reported by the scrollbar holder that
49
 * owns the ScrollbarActivity instance. This needs to happen via a call to
50
 * ActivityOccurred(), for example when the current scroll position or the size
51
 * of the scroll area changes.
52
 *
53
 * As soon as scrollbars are visible, the ScrollbarActivity class manages the
54
 * rest of the activity behavior: It ensures that mouse motions inside the
55
 * scroll area keep the scrollbars visible, and that scrollbars don't fade away
56
 * while they're being hovered / dragged. It also sets a sticky hover attribute
57
 * on the most recently hovered scrollbar.
58
 *
59
 * ScrollbarActivity falls into hibernation after the scrollbars have faded
60
 * out. It only starts acting after the next call to ActivityOccurred() /
61
 * ActivityStarted().
62
 */
63
64
class ScrollbarActivity final : public nsIDOMEventListener,
65
                                public nsARefreshObserver
66
{
67
public:
68
  explicit ScrollbarActivity(nsIScrollbarMediator* aScrollableFrame)
69
   : mScrollableFrame(aScrollableFrame)
70
   , mNestedActivityCounter(0)
71
   , mIsActive(false)
72
   , mIsFading(false)
73
   , mListeningForScrollbarEvents(false)
74
   , mListeningForScrollAreaEvents(false)
75
   , mHScrollbarHovered(false)
76
   , mVScrollbarHovered(false)
77
   , mDisplayOnMouseMove(false)
78
   , mScrollbarFadeBeginDelay(0)
79
   , mScrollbarFadeDuration(0)
80
0
  {
81
0
    QueryLookAndFeelVals();
82
0
  }
83
84
  NS_DECL_ISUPPORTS
85
  NS_DECL_NSIDOMEVENTLISTENER
86
87
  void Destroy();
88
89
  void ActivityOccurred();
90
  void ActivityStarted();
91
  void ActivityStopped();
92
93
  virtual void WillRefresh(TimeStamp aTime) override;
94
95
0
  static void FadeBeginTimerFired(nsITimer* aTimer, void* aSelf) {
96
0
    RefPtr<ScrollbarActivity> scrollbarActivity(
97
0
      reinterpret_cast<ScrollbarActivity*>(aSelf));
98
0
    scrollbarActivity->BeginFade();
99
0
  }
100
101
protected:
102
0
  virtual ~ScrollbarActivity() {}
103
104
  bool IsActivityOngoing()
105
0
  { return mNestedActivityCounter > 0; }
106
  bool IsStillFading(TimeStamp aTime);
107
  void QueryLookAndFeelVals();
108
109
  void HandleEventForScrollbar(const nsAString& aType,
110
                               nsIContent* aTarget,
111
                               dom::Element* aScrollbar,
112
                               bool* aStoredHoverState);
113
114
  void SetIsActive(bool aNewActive);
115
  bool SetIsFading(bool aNewFading); // returns false if 'this' was destroyed
116
117
  void BeginFade();
118
  void EndFade();
119
120
  void StartFadeBeginTimer();
121
  void CancelFadeBeginTimer();
122
123
  void StartListeningForScrollbarEvents();
124
  void StartListeningForScrollAreaEvents();
125
  void StopListeningForScrollbarEvents();
126
  void StopListeningForScrollAreaEvents();
127
  void AddScrollbarEventListeners(dom::EventTarget* aScrollbar);
128
  void RemoveScrollbarEventListeners(dom::EventTarget* aScrollbar);
129
130
  void RegisterWithRefreshDriver();
131
  void UnregisterFromRefreshDriver();
132
133
  bool UpdateOpacity(TimeStamp aTime); // returns false if 'this' was destroyed
134
  void HoveredScrollbar(dom::Element* aScrollbar);
135
136
  nsRefreshDriver* GetRefreshDriver();
137
  dom::Element* GetScrollbarContent(bool aVertical);
138
0
  dom::Element* GetHorizontalScrollbar() { return GetScrollbarContent(false); }
139
0
  dom::Element* GetVerticalScrollbar() { return GetScrollbarContent(true); }
140
141
0
  const TimeDuration FadeDuration() {
142
0
    return TimeDuration::FromMilliseconds(mScrollbarFadeDuration);
143
0
  }
144
145
  nsIScrollbarMediator* mScrollableFrame;
146
  TimeStamp mFadeBeginTime;
147
  nsCOMPtr<nsITimer> mFadeBeginTimer;
148
  nsCOMPtr<dom::EventTarget> mHorizontalScrollbar; // null while inactive
149
  nsCOMPtr<dom::EventTarget> mVerticalScrollbar;   // null while inactive
150
  int mNestedActivityCounter;
151
  bool mIsActive;
152
  bool mIsFading;
153
  bool mListeningForScrollbarEvents;
154
  bool mListeningForScrollAreaEvents;
155
  bool mHScrollbarHovered;
156
  bool mVScrollbarHovered;
157
158
  // LookAndFeel values we load on creation
159
  bool mDisplayOnMouseMove;
160
  int mScrollbarFadeBeginDelay;
161
  int mScrollbarFadeDuration;
162
};
163
164
} // namespace layout
165
} // namespace mozilla
166
167
#endif /* ScrollbarActivity_h___ */