1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ScrollbarActivity.h"
#include "nsIScrollbarMediator.h"
#include "nsIContent.h"
#include "nsIFrame.h"
#include "nsContentUtils.h"
#include "nsITimer.h"
#include "nsQueryFrame.h"
#include "PresShell.h"
#include "nsLayoutUtils.h"
#include "nsScrollbarFrame.h"
#include "nsRefreshDriver.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/Document.h"
#include "mozilla/LookAndFeel.h"
#include "mozilla/ScrollContainerFrame.h"
namespace mozilla::layout {
using mozilla::dom::Element;
NS_IMPL_ISUPPORTS(ScrollbarActivity, nsIDOMEventListener)
static bool DisplayOnMouseMove() {
return LookAndFeel::GetInt(LookAndFeel::IntID::ScrollbarDisplayOnMouseMove);
}
void ScrollbarActivity::Destroy() {
StopListeningForScrollAreaEvents();
CancelFadeTimer();
}
void ScrollbarActivity::ActivityOccurred() {
ActivityStarted();
ActivityStopped();
}
static void SetScrollbarActive(Element* aScrollbar, bool aIsActive) {
if (!aScrollbar) {
return;
}
if (aIsActive) {
if (nsScrollbarFrame* sf = do_QueryFrame(aScrollbar->GetPrimaryFrame())) {
sf->WillBecomeActive();
}
}
aScrollbar->SetBoolAttr(nsGkAtoms::active, aIsActive);
}
void ScrollbarActivity::ActivityStarted() {
const bool wasActive = IsActive();
mNestedActivityCounter++;
if (wasActive) {
return;
}
CancelFadeTimer();
if (mScrollbarEffectivelyVisible) {
return;
}
StartListeningForScrollAreaEvents();
SetScrollbarActive(GetHorizontalScrollbar(), true);
SetScrollbarActive(GetVerticalScrollbar(), true);
mScrollbarEffectivelyVisible = true;
}
void ScrollbarActivity::ActivityStopped() {
if (!IsActive()) {
// This can happen if there was a frame reconstruction while the activity
// was ongoing. In this case we just do nothing. We should probably handle
// this case better.
return;
}
mNestedActivityCounter--;
if (IsActive()) {
return;
}
StartFadeTimer();
}
NS_IMETHODIMP
ScrollbarActivity::HandleEvent(dom::Event* aEvent) {
if (!mScrollbarEffectivelyVisible && !DisplayOnMouseMove()) {
return NS_OK;
}
nsAutoString type;
aEvent->GetType(type);
auto* targetContent =
nsIContent::FromEventTargetOrNull(aEvent->GetOriginalTarget());
if (type.EqualsLiteral("mousemove")) {
// Mouse motions anywhere in the scrollable frame should keep the
// scrollbars visible, but we have to be careful as content descendants of
// our scrollable content aren't necessarily scrolled by our scroll frame
// (if they are out of flow and their containing block is not a descendant
// of our scroll frame) and we don't want those to activate us.
nsIFrame* scrollFrame = do_QueryFrame(mScrollableFrame);
MOZ_ASSERT(scrollFrame);
ScrollContainerFrame* scrollContainerFrame = do_QueryFrame(scrollFrame);
nsIFrame* targetFrame =
targetContent ? targetContent->GetPrimaryFrame() : nullptr;
if ((scrollContainerFrame &&
scrollContainerFrame->IsRootScrollFrameOfDocument()) ||
!targetFrame ||
nsLayoutUtils::IsAncestorFrameCrossDocInProcess(
scrollFrame, targetFrame,
scrollFrame->PresShell()->GetRootFrame())) {
ActivityOccurred();
}
return NS_OK;
}
return NS_OK;
}
void ScrollbarActivity::StartListeningForScrollAreaEvents() {
if (mListeningForScrollAreaEvents) {
return;
}
nsIFrame* scrollArea = do_QueryFrame(mScrollableFrame);
scrollArea->GetContent()->AddEventListener(u"mousemove"_ns, this, true);
mListeningForScrollAreaEvents = true;
}
void ScrollbarActivity::StopListeningForScrollAreaEvents() {
if (!mListeningForScrollAreaEvents) {
return;
}
nsIFrame* scrollArea = do_QueryFrame(mScrollableFrame);
scrollArea->GetContent()->RemoveEventListener(u"mousemove"_ns, this, true);
mListeningForScrollAreaEvents = false;
}
void ScrollbarActivity::CancelFadeTimer() {
if (mFadeTimer) {
mFadeTimer->Cancel();
}
}
void ScrollbarActivity::StartFadeTimer() {
CancelFadeTimer();
if (StaticPrefs::layout_testing_overlay_scrollbars_always_visible()) {
return;
}
if (!mFadeTimer) {
mFadeTimer = NS_NewTimer();
}
mFadeTimer->InitWithNamedFuncCallback(
[](nsITimer*, void* aClosure) {
RefPtr<ScrollbarActivity> activity =
static_cast<ScrollbarActivity*>(aClosure);
activity->BeginFade();
},
this, LookAndFeel::GetInt(LookAndFeel::IntID::ScrollbarFadeBeginDelay),
nsITimer::TYPE_ONE_SHOT, "ScrollbarActivity::FadeBeginTimerFired");
}
void ScrollbarActivity::BeginFade() {
MOZ_ASSERT(!IsActive());
mScrollbarEffectivelyVisible = false;
SetScrollbarActive(GetHorizontalScrollbar(), false);
SetScrollbarActive(GetVerticalScrollbar(), false);
}
Element* ScrollbarActivity::GetScrollbarContent(bool aVertical) {
nsIFrame* box = mScrollableFrame->GetScrollbarBox(aVertical);
return box ? box->GetContent()->AsElement() : nullptr;
}
} // namespace mozilla::layout
|