layout/generic/ScrollbarActivity.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "ScrollbarActivity.h"
michael@0 7 #include "nsIScrollbarOwner.h"
michael@0 8 #include "nsIContent.h"
michael@0 9 #include "nsIDOMEvent.h"
michael@0 10 #include "nsIDOMElementCSSInlineStyle.h"
michael@0 11 #include "nsIDOMCSSStyleDeclaration.h"
michael@0 12 #include "nsIFrame.h"
michael@0 13 #include "nsContentUtils.h"
michael@0 14 #include "nsAString.h"
michael@0 15 #include "nsQueryFrame.h"
michael@0 16 #include "nsComponentManagerUtils.h"
michael@0 17 #include "mozilla/LookAndFeel.h"
michael@0 18 #include "mozilla/Preferences.h"
michael@0 19
michael@0 20 namespace mozilla {
michael@0 21 namespace layout {
michael@0 22
michael@0 23 NS_IMPL_ISUPPORTS(ScrollbarActivity, nsIDOMEventListener)
michael@0 24
michael@0 25 static bool
michael@0 26 GetForceAlwaysVisiblePref()
michael@0 27 {
michael@0 28 static bool sForceAlwaysVisible;
michael@0 29 static bool sForceAlwaysVisiblePrefCached = false;
michael@0 30 if (!sForceAlwaysVisiblePrefCached) {
michael@0 31 Preferences::AddBoolVarCache(&sForceAlwaysVisible,
michael@0 32 "layout.testing.overlay-scrollbars.always-visible");
michael@0 33 sForceAlwaysVisiblePrefCached = true;
michael@0 34 }
michael@0 35 return sForceAlwaysVisible;
michael@0 36 }
michael@0 37
michael@0 38 void
michael@0 39 ScrollbarActivity::QueryLookAndFeelVals()
michael@0 40 {
michael@0 41 // Fade animation constants
michael@0 42 mScrollbarFadeBeginDelay =
michael@0 43 LookAndFeel::GetInt(LookAndFeel::eIntID_ScrollbarFadeBeginDelay);
michael@0 44 mScrollbarFadeDuration =
michael@0 45 LookAndFeel::GetInt(LookAndFeel::eIntID_ScrollbarFadeDuration);
michael@0 46 // Controls whether we keep the mouse move listener so we can display the
michael@0 47 // scrollbars whenever the user moves the mouse within the scroll area.
michael@0 48 mDisplayOnMouseMove =
michael@0 49 LookAndFeel::GetInt(LookAndFeel::eIntID_ScrollbarDisplayOnMouseMove);
michael@0 50 }
michael@0 51
michael@0 52 void
michael@0 53 ScrollbarActivity::Destroy()
michael@0 54 {
michael@0 55 StopListeningForScrollbarEvents();
michael@0 56 StopListeningForScrollAreaEvents();
michael@0 57 UnregisterFromRefreshDriver();
michael@0 58 CancelFadeBeginTimer();
michael@0 59 }
michael@0 60
michael@0 61 void
michael@0 62 ScrollbarActivity::ActivityOccurred()
michael@0 63 {
michael@0 64 ActivityStarted();
michael@0 65 ActivityStopped();
michael@0 66 }
michael@0 67
michael@0 68 void
michael@0 69 ScrollbarActivity::ActivityStarted()
michael@0 70 {
michael@0 71 mNestedActivityCounter++;
michael@0 72 CancelFadeBeginTimer();
michael@0 73 if (!SetIsFading(false)) {
michael@0 74 return;
michael@0 75 }
michael@0 76 UnregisterFromRefreshDriver();
michael@0 77 StartListeningForScrollbarEvents();
michael@0 78 StartListeningForScrollAreaEvents();
michael@0 79 SetIsActive(true);
michael@0 80
michael@0 81 NS_ASSERTION(mIsActive, "need to be active during activity");
michael@0 82 NS_ASSERTION(!mIsFading, "must not be fading during activity");
michael@0 83 }
michael@0 84
michael@0 85 void
michael@0 86 ScrollbarActivity::ActivityStopped()
michael@0 87 {
michael@0 88 NS_ASSERTION(IsActivityOngoing(), "activity stopped while none was going on");
michael@0 89 NS_ASSERTION(mIsActive, "need to be active during activity");
michael@0 90 NS_ASSERTION(!mIsFading, "must not be fading during ongoing activity");
michael@0 91
michael@0 92 mNestedActivityCounter--;
michael@0 93
michael@0 94 if (!IsActivityOngoing()) {
michael@0 95 StartFadeBeginTimer();
michael@0 96
michael@0 97 NS_ASSERTION(mIsActive, "need to be active right after activity");
michael@0 98 NS_ASSERTION(!mIsFading, "must not be fading right after activity");
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 NS_IMETHODIMP
michael@0 103 ScrollbarActivity::HandleEvent(nsIDOMEvent* aEvent)
michael@0 104 {
michael@0 105 if (!mDisplayOnMouseMove && !mIsActive)
michael@0 106 return NS_OK;
michael@0 107
michael@0 108 nsAutoString type;
michael@0 109 aEvent->GetType(type);
michael@0 110
michael@0 111 if (type.EqualsLiteral("mousemove")) {
michael@0 112 // Mouse motions anywhere in the scrollable frame should keep the
michael@0 113 // scrollbars visible.
michael@0 114 ActivityOccurred();
michael@0 115 return NS_OK;
michael@0 116 }
michael@0 117
michael@0 118 nsCOMPtr<nsIDOMEventTarget> target;
michael@0 119 aEvent->GetOriginalTarget(getter_AddRefs(target));
michael@0 120 nsCOMPtr<nsIContent> targetContent = do_QueryInterface(target);
michael@0 121
michael@0 122 HandleEventForScrollbar(type, targetContent, GetHorizontalScrollbar(),
michael@0 123 &mHScrollbarHovered);
michael@0 124 HandleEventForScrollbar(type, targetContent, GetVerticalScrollbar(),
michael@0 125 &mVScrollbarHovered);
michael@0 126
michael@0 127 return NS_OK;
michael@0 128 }
michael@0 129
michael@0 130 void
michael@0 131 ScrollbarActivity::WillRefresh(TimeStamp aTime)
michael@0 132 {
michael@0 133 NS_ASSERTION(mIsActive, "should only fade while scrollbars are visible");
michael@0 134 NS_ASSERTION(!IsActivityOngoing(), "why weren't we unregistered from the refresh driver when scrollbar activity started?");
michael@0 135 NS_ASSERTION(mIsFading, "should only animate fading during fade");
michael@0 136
michael@0 137 if (!UpdateOpacity(aTime)) {
michael@0 138 return;
michael@0 139 }
michael@0 140
michael@0 141 if (!IsStillFading(aTime)) {
michael@0 142 EndFade();
michael@0 143 }
michael@0 144 }
michael@0 145
michael@0 146 bool
michael@0 147 ScrollbarActivity::IsStillFading(TimeStamp aTime)
michael@0 148 {
michael@0 149 return !mFadeBeginTime.IsNull() && (aTime - mFadeBeginTime < FadeDuration());
michael@0 150 }
michael@0 151
michael@0 152 void
michael@0 153 ScrollbarActivity::HandleEventForScrollbar(const nsAString& aType,
michael@0 154 nsIContent* aTarget,
michael@0 155 nsIContent* aScrollbar,
michael@0 156 bool* aStoredHoverState)
michael@0 157 {
michael@0 158 if (!aTarget || !aScrollbar ||
michael@0 159 !nsContentUtils::ContentIsDescendantOf(aTarget, aScrollbar))
michael@0 160 return;
michael@0 161
michael@0 162 if (aType.EqualsLiteral("mousedown")) {
michael@0 163 ActivityStarted();
michael@0 164 } else if (aType.EqualsLiteral("mouseup")) {
michael@0 165 ActivityStopped();
michael@0 166 } else if (aType.EqualsLiteral("mouseover") ||
michael@0 167 aType.EqualsLiteral("mouseout")) {
michael@0 168 bool newHoveredState = aType.EqualsLiteral("mouseover");
michael@0 169 if (newHoveredState && !*aStoredHoverState) {
michael@0 170 ActivityStarted();
michael@0 171 HoveredScrollbar(aScrollbar);
michael@0 172 } else if (*aStoredHoverState && !newHoveredState) {
michael@0 173 ActivityStopped();
michael@0 174 // Don't call HoveredScrollbar(nullptr) here because we want the hover
michael@0 175 // attribute to stick until the scrollbars are hidden.
michael@0 176 }
michael@0 177 *aStoredHoverState = newHoveredState;
michael@0 178 }
michael@0 179 }
michael@0 180
michael@0 181 void
michael@0 182 ScrollbarActivity::StartListeningForScrollbarEvents()
michael@0 183 {
michael@0 184 if (mListeningForScrollbarEvents)
michael@0 185 return;
michael@0 186
michael@0 187 mHorizontalScrollbar = do_QueryInterface(GetHorizontalScrollbar());
michael@0 188 mVerticalScrollbar = do_QueryInterface(GetVerticalScrollbar());
michael@0 189
michael@0 190 AddScrollbarEventListeners(mHorizontalScrollbar);
michael@0 191 AddScrollbarEventListeners(mVerticalScrollbar);
michael@0 192
michael@0 193 mListeningForScrollbarEvents = true;
michael@0 194 }
michael@0 195
michael@0 196 void
michael@0 197 ScrollbarActivity::StopListeningForScrollbarEvents()
michael@0 198 {
michael@0 199 if (!mListeningForScrollbarEvents)
michael@0 200 return;
michael@0 201
michael@0 202 RemoveScrollbarEventListeners(mHorizontalScrollbar);
michael@0 203 RemoveScrollbarEventListeners(mVerticalScrollbar);
michael@0 204
michael@0 205 mHorizontalScrollbar = nullptr;
michael@0 206 mVerticalScrollbar = nullptr;
michael@0 207 mListeningForScrollbarEvents = false;
michael@0 208 }
michael@0 209
michael@0 210 void
michael@0 211 ScrollbarActivity::StartListeningForScrollAreaEvents()
michael@0 212 {
michael@0 213 if (mListeningForScrollAreaEvents)
michael@0 214 return;
michael@0 215
michael@0 216 nsIFrame* scrollArea = do_QueryFrame(mScrollableFrame);
michael@0 217 nsCOMPtr<nsIDOMEventTarget> scrollAreaTarget
michael@0 218 = do_QueryInterface(scrollArea->GetContent());
michael@0 219 if (scrollAreaTarget) {
michael@0 220 scrollAreaTarget->AddEventListener(NS_LITERAL_STRING("mousemove"), this,
michael@0 221 true);
michael@0 222 }
michael@0 223 mListeningForScrollAreaEvents = true;
michael@0 224 }
michael@0 225
michael@0 226 void
michael@0 227 ScrollbarActivity::StopListeningForScrollAreaEvents()
michael@0 228 {
michael@0 229 if (!mListeningForScrollAreaEvents)
michael@0 230 return;
michael@0 231
michael@0 232 nsIFrame* scrollArea = do_QueryFrame(mScrollableFrame);
michael@0 233 nsCOMPtr<nsIDOMEventTarget> scrollAreaTarget = do_QueryInterface(scrollArea->GetContent());
michael@0 234 if (scrollAreaTarget) {
michael@0 235 scrollAreaTarget->RemoveEventListener(NS_LITERAL_STRING("mousemove"), this, true);
michael@0 236 }
michael@0 237 mListeningForScrollAreaEvents = false;
michael@0 238 }
michael@0 239
michael@0 240 void
michael@0 241 ScrollbarActivity::AddScrollbarEventListeners(nsIDOMEventTarget* aScrollbar)
michael@0 242 {
michael@0 243 if (aScrollbar) {
michael@0 244 aScrollbar->AddEventListener(NS_LITERAL_STRING("mousedown"), this, true);
michael@0 245 aScrollbar->AddEventListener(NS_LITERAL_STRING("mouseup"), this, true);
michael@0 246 aScrollbar->AddEventListener(NS_LITERAL_STRING("mouseover"), this, true);
michael@0 247 aScrollbar->AddEventListener(NS_LITERAL_STRING("mouseout"), this, true);
michael@0 248 }
michael@0 249 }
michael@0 250
michael@0 251 void
michael@0 252 ScrollbarActivity::RemoveScrollbarEventListeners(nsIDOMEventTarget* aScrollbar)
michael@0 253 {
michael@0 254 if (aScrollbar) {
michael@0 255 aScrollbar->RemoveEventListener(NS_LITERAL_STRING("mousedown"), this, true);
michael@0 256 aScrollbar->RemoveEventListener(NS_LITERAL_STRING("mouseup"), this, true);
michael@0 257 aScrollbar->RemoveEventListener(NS_LITERAL_STRING("mouseover"), this, true);
michael@0 258 aScrollbar->RemoveEventListener(NS_LITERAL_STRING("mouseout"), this, true);
michael@0 259 }
michael@0 260 }
michael@0 261
michael@0 262 void
michael@0 263 ScrollbarActivity::BeginFade()
michael@0 264 {
michael@0 265 NS_ASSERTION(mIsActive, "can't begin fade when we're already inactive");
michael@0 266 NS_ASSERTION(!IsActivityOngoing(), "why wasn't the fade begin timer cancelled when scrollbar activity started?");
michael@0 267 NS_ASSERTION(!mIsFading, "shouldn't be fading just yet");
michael@0 268
michael@0 269 CancelFadeBeginTimer();
michael@0 270 mFadeBeginTime = TimeStamp::Now();
michael@0 271 if (!SetIsFading(true)) {
michael@0 272 return;
michael@0 273 }
michael@0 274 RegisterWithRefreshDriver();
michael@0 275
michael@0 276 NS_ASSERTION(mIsActive, "only fade while scrollbars are visible");
michael@0 277 NS_ASSERTION(mIsFading, "should be fading now");
michael@0 278 }
michael@0 279
michael@0 280 void
michael@0 281 ScrollbarActivity::EndFade()
michael@0 282 {
michael@0 283 NS_ASSERTION(mIsActive, "still need to be active at this point");
michael@0 284 NS_ASSERTION(!IsActivityOngoing(), "why wasn't the fade end timer cancelled when scrollbar activity started?");
michael@0 285
michael@0 286 if (!SetIsFading(false)) {
michael@0 287 return;
michael@0 288 }
michael@0 289 SetIsActive(false);
michael@0 290 UnregisterFromRefreshDriver();
michael@0 291 StopListeningForScrollbarEvents();
michael@0 292 if (!mDisplayOnMouseMove) {
michael@0 293 StopListeningForScrollAreaEvents();
michael@0 294 }
michael@0 295
michael@0 296 NS_ASSERTION(!mIsActive, "should have gone inactive after fade end");
michael@0 297 NS_ASSERTION(!mIsFading, "shouldn't be fading anymore");
michael@0 298 }
michael@0 299
michael@0 300 void
michael@0 301 ScrollbarActivity::RegisterWithRefreshDriver()
michael@0 302 {
michael@0 303 nsRefreshDriver* refreshDriver = GetRefreshDriver();
michael@0 304 if (refreshDriver) {
michael@0 305 refreshDriver->AddRefreshObserver(this, Flush_Style);
michael@0 306 }
michael@0 307 }
michael@0 308
michael@0 309 void
michael@0 310 ScrollbarActivity::UnregisterFromRefreshDriver()
michael@0 311 {
michael@0 312 nsRefreshDriver* refreshDriver = GetRefreshDriver();
michael@0 313 if (refreshDriver) {
michael@0 314 refreshDriver->RemoveRefreshObserver(this, Flush_Style);
michael@0 315 }
michael@0 316 }
michael@0 317
michael@0 318 static void
michael@0 319 SetBooleanAttribute(nsIContent* aContent, nsIAtom* aAttribute, bool aValue)
michael@0 320 {
michael@0 321 if (aContent) {
michael@0 322 if (aValue) {
michael@0 323 aContent->SetAttr(kNameSpaceID_None, aAttribute,
michael@0 324 NS_LITERAL_STRING("true"), true);
michael@0 325 } else {
michael@0 326 aContent->UnsetAttr(kNameSpaceID_None, aAttribute, true);
michael@0 327 }
michael@0 328 }
michael@0 329 }
michael@0 330
michael@0 331 void
michael@0 332 ScrollbarActivity::SetIsActive(bool aNewActive)
michael@0 333 {
michael@0 334 if (mIsActive == aNewActive)
michael@0 335 return;
michael@0 336
michael@0 337 mIsActive = aNewActive;
michael@0 338 if (!mIsActive) {
michael@0 339 // Clear sticky scrollbar hover status.
michael@0 340 HoveredScrollbar(nullptr);
michael@0 341 }
michael@0 342
michael@0 343 SetBooleanAttribute(GetHorizontalScrollbar(), nsGkAtoms::active, mIsActive);
michael@0 344 SetBooleanAttribute(GetVerticalScrollbar(), nsGkAtoms::active, mIsActive);
michael@0 345 }
michael@0 346
michael@0 347 static void
michael@0 348 SetOpacityOnElement(nsIContent* aContent, double aOpacity)
michael@0 349 {
michael@0 350 nsCOMPtr<nsIDOMElementCSSInlineStyle> inlineStyleContent =
michael@0 351 do_QueryInterface(aContent);
michael@0 352 if (inlineStyleContent) {
michael@0 353 nsCOMPtr<nsIDOMCSSStyleDeclaration> decl;
michael@0 354 inlineStyleContent->GetStyle(getter_AddRefs(decl));
michael@0 355 if (decl) {
michael@0 356 nsAutoString str;
michael@0 357 str.AppendFloat(aOpacity);
michael@0 358 decl->SetProperty(NS_LITERAL_STRING("opacity"), str, EmptyString());
michael@0 359 }
michael@0 360 }
michael@0 361 }
michael@0 362
michael@0 363 bool
michael@0 364 ScrollbarActivity::UpdateOpacity(TimeStamp aTime)
michael@0 365 {
michael@0 366 double progress = (aTime - mFadeBeginTime) / FadeDuration();
michael@0 367 double opacity = 1.0 - std::max(0.0, std::min(1.0, progress));
michael@0 368
michael@0 369 // 'this' may be getting destroyed during SetOpacityOnElement calls.
michael@0 370 nsWeakFrame weakFrame((do_QueryFrame(mScrollableFrame)));
michael@0 371 SetOpacityOnElement(GetHorizontalScrollbar(), opacity);
michael@0 372 if (!weakFrame.IsAlive()) {
michael@0 373 return false;
michael@0 374 }
michael@0 375 SetOpacityOnElement(GetVerticalScrollbar(), opacity);
michael@0 376 if (!weakFrame.IsAlive()) {
michael@0 377 return false;
michael@0 378 }
michael@0 379 return true;
michael@0 380 }
michael@0 381
michael@0 382 static void
michael@0 383 UnsetOpacityOnElement(nsIContent* aContent)
michael@0 384 {
michael@0 385 nsCOMPtr<nsIDOMElementCSSInlineStyle> inlineStyleContent =
michael@0 386 do_QueryInterface(aContent);
michael@0 387 if (inlineStyleContent) {
michael@0 388 nsCOMPtr<nsIDOMCSSStyleDeclaration> decl;
michael@0 389 inlineStyleContent->GetStyle(getter_AddRefs(decl));
michael@0 390 if (decl) {
michael@0 391 nsAutoString dummy;
michael@0 392 decl->RemoveProperty(NS_LITERAL_STRING("opacity"), dummy);
michael@0 393 }
michael@0 394 }
michael@0 395 }
michael@0 396
michael@0 397 bool
michael@0 398 ScrollbarActivity::SetIsFading(bool aNewFading)
michael@0 399 {
michael@0 400 if (mIsFading == aNewFading)
michael@0 401 return true;
michael@0 402
michael@0 403 mIsFading = aNewFading;
michael@0 404 if (!mIsFading) {
michael@0 405 mFadeBeginTime = TimeStamp();
michael@0 406 // 'this' may be getting destroyed during UnsetOpacityOnElement calls.
michael@0 407 nsWeakFrame weakFrame((do_QueryFrame(mScrollableFrame)));
michael@0 408 UnsetOpacityOnElement(GetHorizontalScrollbar());
michael@0 409 if (!weakFrame.IsAlive()) {
michael@0 410 return false;
michael@0 411 }
michael@0 412 UnsetOpacityOnElement(GetVerticalScrollbar());
michael@0 413 if (!weakFrame.IsAlive()) {
michael@0 414 return false;
michael@0 415 }
michael@0 416 }
michael@0 417 return true;
michael@0 418 }
michael@0 419
michael@0 420 void
michael@0 421 ScrollbarActivity::StartFadeBeginTimer()
michael@0 422 {
michael@0 423 if (GetForceAlwaysVisiblePref()) {
michael@0 424 return;
michael@0 425 }
michael@0 426 if (!mFadeBeginTimer) {
michael@0 427 mFadeBeginTimer = do_CreateInstance("@mozilla.org/timer;1");
michael@0 428 }
michael@0 429 mFadeBeginTimer->InitWithFuncCallback(FadeBeginTimerFired, this,
michael@0 430 mScrollbarFadeBeginDelay,
michael@0 431 nsITimer::TYPE_ONE_SHOT);
michael@0 432 }
michael@0 433
michael@0 434 void
michael@0 435 ScrollbarActivity::CancelFadeBeginTimer()
michael@0 436 {
michael@0 437 if (mFadeBeginTimer) {
michael@0 438 mFadeBeginTimer->Cancel();
michael@0 439 }
michael@0 440 }
michael@0 441
michael@0 442 void
michael@0 443 ScrollbarActivity::HoveredScrollbar(nsIContent* aScrollbar)
michael@0 444 {
michael@0 445 SetBooleanAttribute(GetHorizontalScrollbar(), nsGkAtoms::hover, false);
michael@0 446 SetBooleanAttribute(GetVerticalScrollbar(), nsGkAtoms::hover, false);
michael@0 447 SetBooleanAttribute(aScrollbar, nsGkAtoms::hover, true);
michael@0 448 }
michael@0 449
michael@0 450 nsRefreshDriver*
michael@0 451 ScrollbarActivity::GetRefreshDriver()
michael@0 452 {
michael@0 453 nsIFrame* scrollableFrame = do_QueryFrame(mScrollableFrame);
michael@0 454 return scrollableFrame->PresContext()->RefreshDriver();
michael@0 455 }
michael@0 456
michael@0 457 nsIContent*
michael@0 458 ScrollbarActivity::GetScrollbarContent(bool aVertical)
michael@0 459 {
michael@0 460 nsIFrame* box = mScrollableFrame->GetScrollbarBox(aVertical);
michael@0 461 return box ? box->GetContent() : nullptr;
michael@0 462 }
michael@0 463
michael@0 464 } // namespace layout
michael@0 465 } // namespace mozilla

mercurial