Files
palemoon27/layout/xul/nsSliderFrame.cpp
T
roytam1 0af135f24d import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1249212 part 3 - Fix active duration calculation when iteration duration is infinity; r=boris (acc98b1948)
- Bug 1249212 part 4 - Add tests for activeDuration; r=boris (354da323ef)
- Bug 1249212 part 5 - Remove max() clamping from endTime calculation; r=boris (613cd6f5b7)
- Bug 1249212 part 6 - Add tests for endTime calculation; r=boris (583a09f942)
- Bug 1249212 part 7 - Calculate the endTime in GetComputedTimingAt; r=boris (ac73c4a853)
- No bug - Fix whitespace and four test descriptions in keyframe-effect tests; r=whitespace-only DONTBUILD (844a12ba4d)
- Bug 1249212 part 1 - Simplify various keyframe-effect tests; r=boris (58c7f5ef19)
- Bug 1241929 - remove PurgeActiveWorker() from install job when skip waiting is set. r=bkelly (248aadb430)
- Bug 1238954 P1 Purge potentially controlled document entries when evicting service worker registrations. r=ehsan (094bb54de1)
- Bug 1238954 P2 Fix csp service worker tests to register and unregister separate scopes. r=ehsan (71f0237c11)
- Bug 1227015 P2 Move mScriptSpec from registration job into script job base. r=ehsan (754753f0f6)
- Bug 1227015 P3 Require a script spec on install jobs. r=ehsan (ebdbf860b9)
- Bug 1227015 P4 Make register job always require an explicit script spec. r=ehsan (b97ec80a56)
- Bug 1227015 P5 Remove ServiceWorkerRegistrationInfo mScriptSpec. r=ehsan (4410fd9fd0)
- Bug 1227015 P6 Abort updates if the script spec has changed. r=ehsan (01962f2915)
- Bug 1232444 Only store service worker registrations after install is successful. r=baku (f773638ec6)
- Bug 1231974 P1 Remove unneeded ServiceWorkerScriptJobBase abstract class. r=baku (c514e53d82)
- Bug 1231974 P2 Store scope on ServiceWorkerJobBase. r=baku (d723846d45)
- Bug 1231974 P3 Store the principal on the ServiceWorkerJobBase. r=baku (550fa7f1d4)
- Bug 1231974 P4 Lazy load registration and verify it does not change in service worker jobs. r=baku (db44008f8f)
- Bug 1231974 P6 Abort update-triggered install jobs if the service worker script has changed. r=baku (3e7572968f)
- Bug 1237992 - service worker activate should be executed after install onstatechange events are fired. r=bkelly (58de5a2e57)
- No bug - Correct the order of two newly added web-platform tests in manifest. DONTBUILD (e14b162237)
- Bug 1189581 - Make service-workers/service-worker/fetch-cors-xhr.https.html pass; r=bkelly (e1dbe77c87)
- Bug 1243942 - Bypass service workers for WebSocket handshake channels; r=bkelly (845fd17110)
- Bug 1217089 - Remove fetch-request-html-imports.https.html; r=bkelly (528ea318b6)
- Bug 1188545 - Update expected results for some service worker tests. a=testonly (ba29add4a5)
- Bug 1213119 Lengthen timeout in test checking for failure to load iframes and windows. r=ehsan (d25f8f9b01)
- Bug 1189671 - Fix getregistrations test. r=jgraham (2407499bc5)
- Bug 1217367 - Add a wpt test that verified coalesced .update() calls resolve properly. r=bkelly (7e3f4a92e1)
- Bug 1200677 - Import navigation-redirect.https.html for non-e10s. r=jdm (6e6d98a255)
- Bug 1251498: Implement IDBKeyRange.includes(). r=baku (89c839adaf)
- Bug 1248338 - Implement iterationStart; r=birtles (5dcb1341c3)
- Bug 1244635 - Part1 Add enddelay implementation in dom/animation/AnimationEffectTiming.cpp r=bz,hiro (e0a9f3a798)
- Bug 1244635 - Part2 Add enddelay tests in dom/animation/test/chrome r=hiro (98d2f5ada5)
- Bug 1244635 - Part3 Add enddelay tests in testing/web-platform/tests/web-animations r=hiro (bb10a41382)
- Bug 1244635 - Part4 Add enddelay tests in layout/style/test r=hiro (ceda978f3e)
- Bug 1237173 - Part1: Move TimingParam struct to a new file. r=birtles (ad46be831b)
- Bug 1251804 - Use the ImageContainer's size and not the intrinsic size when computing the transform in nsDisplayImage::ConfigureLayer. r=tn (23bd3774ff)
- Bug 1247554 - Budget creation of AGRs by frame area; r=mattwoodrow (c68183c77e)
- Require mix-blend mode support in all compositors and layer managers. (bug 1209278 part 1, r=mstange) (e00752064f)
- Remove lazy scrollinfo hoisting introduced in bug 1193557. (bug 1209278 part 2, r=mstange) (394c117b8d)
- Bug 1152049 - Apply all scroll clips when computing plugin clips in content. r=tn (9c44108dda)
- Bug 1238564 - Anticipate async scrolling when computing the scroll clipped bounds of a display list. r=roc (4c28888eb6)
- Bug 1238564 - Allow constructing nsDisplayWrapList with a given scroll clip. r=roc (dd6d6d1a02)
- Fold nsDisplayBlendContainer constructors. (bug 1209278 part 3, r=mstange) (e28cb8d062)
- Bug 1253052. Only schedule paint when apz aware event listener is added if event regions/apz are enabled. r=kats (4a2dcd2e31)
- Bug 1252929: Next.1. Make layers.max-active preference not need a restart. r=mstange (412d52ff81)
- Bug 1247554 - Mark nsPresContext::GetVisibleArea as const; r=mattwoodrow (8ad99e89a2)
- Bug 1104916 - Implement CSS media query display-mode. r=cam (6bf38de62c)
- Bug 1247098 - Mark nsIPresShell::GetResolution and nsPresContext::IsRootContentDocument as const. r=tnikkel (2514fe01c2)
- Bug 1253078 - Switch MOZ_STYLO environmental variable to MOZ_DISABLE_STYLO. r=dholbert (8c6dfe008e)
- Bug 1232181 - Notify plugins about scroll state. r=roc (1eccaad547)
- Bug 1198663. Tolerate null Image in Android NPAPI plugins. r=snorp (bfceeacbe8)
- Bug 1232181 - Add an image layer for plugin frames that represent windowed plugins on platforms that support scroll capture. r=roc (c064e01b19)
- Bug 1171182 - Browser Zoom Query for NPAPI; r=bsmedberg r=jaas (95c60d2190)
- Bug 1243639 - Remove Honeycomb flash-related code. r=snorp (437bdfa5e8)
- Bug 1177367 - Don't fall through to non-e10s plugin widget creation when e10s creation fails. r=aklotz (7fb106e260)
- Bug 1250634 - Part 0: Fix MacroAssembler support for store16(). r=jandem (99fdf6fd42)
- Bug 1250634 - Part 1: Refactor FreeSpan management to be less indirect and confusing. r=terrence, r=jandem (50f57a129c)
- Bug 1253094, part 5 - Stop using DebugOnly for class/struct members in layout/. r=mats (bcb94cc110)
- Bug 1238564 - Set the innermost possible scroll clip on opacity items during creation. r=mattwoodrow (da5072cb75)
- Bug 1192910 - Ensure we flush paints on the main thread during an APZ flush. r=mstange (ff4631bcd8)
- Bug 1254263 - Add a flag to allow disabling paint-skipping. r=mstange (90375f4f31)
- Bug 1248913 - nsDisplayListBuilder doesn't need to know what blend modes it contains, just whether it contains any. r=mattwoodrow (92c987df65)
- Bug 1248913 - Add a constructor argument to nsDisplayMixBlendMode that lets you specify the blend mode. r=mattwoodrow (66234cc69c)
- Bug 1238564 - When building a fixed/sticky display item, don't restore the clip until we're ready to build that item so that inner items aren't unnecessarily clipped. r=roc (752f531f08)
- Bug 1238564 - Include mIsAsyncScrollable information in DisplayItemScrollClip::ToString. r=roc (b6ac4899eb)
- Bug 1238564 - Don't do another pass over the display list to figure out ancestor scroll clips. r=mattwoodrow (12ad134528)
- Bug 1238564 - Get rid of cross stacking context parent scroll clip. r=mattwoodrow (536faa6ba6)
- Bug 1248913 - Rename nsDisplayMixBlendMode to nsDisplayBlendMode. r=mattwoodrow (34a0704841)
- Bug 1248913 - Let nsDisplayBackgroundImage specify the background blend mode. r=mattwoodrow (cc671af9e3)
- Bug 619500: Part 1. Default sizing for specified size of SVG images which have no constraints; r=dholbert r=seth (b0f7ec56a8)
- Bug 619500: Part 2. When drawing an SVG image as a CSS border-image, use preverveAspectRatio="none"; r=dholbert (1dad64f38c)
- Bug 619500: Part 3. svg-as-borderimage test cases; r=dholbert (896d339cf9)
- Bug 619500: Part 4. Remove one unused data member in SVGDrawingParameters; r=dholbert (f16e9a5927)
- Bug 1230415 - Use DrawTarget instead of gfxContext in PaintBorderWithStyleBorder(). r=roc. (f00cccb284)
- Bug 1248913 - Build nsDisplayBlendMode items for background-blend-mode. r=mattwoodrow (105515a553)
- Bug 1248913 - Remove mCanBeActive and second nsDisplayBlendContainer constructor. r=mattwoodrow (d984db61f5)
- Bug 1248913 - Make nsDisplayBlendContainer active or inactive based on its contents. r=mattwoodrow (f44fa4f479)
- Bug 1248913 - Reftest. (58a1507343)
- Bug 1251833 - Part 1: Move allocation into FreeSpan and move firstFreeSpan to the top of Arenas. r=terrence (3f4d239785)
- Bug 1251833 - Part 2: Clean up the various iterators a bit. r=terrence (300aa5673d)
- Bug 1251527 - Don't override visible regions for background-attachment:fixed. r=mstange (ee2d53334d)
- Backout e00a02282951 (bug 1232229) as we no longer need the diagnostics. (22f0063541)
- Bug 1250718 - Don't flatten opacity to an intermediate surface when used in the middle of preserve-3d. r=thinker (9df994b834)
- Bug 1196114 - Part 1: Add SetPerformanceWarning. r=birtles (165f9cbcfc)
- Bug 1196114 - Part 2: Add AnimationPropertyStatus interface and KeyframeEffectReadOnly.runningStatus(). r=birtles,smaug (4e3d2d55e0)
- Bug 1196114 - Part 3: Set AnimationPerformanceWarning messages. r=birtles (cd3497e128)
- Bug 1196114 - Part 4: Localize messages for animation performance warnings. r=birtles (1b1676b7b6)
- Bug 1196114 - Part 5: Store performce warning information as enum type. r=birtles (d2ec6643d3)
- Bug 1221378: Properly root object passed to the allocation metadata callback. r=fitzgen (37831b769c)
- Bug 1225005 - Clamp negative values in containing block size. r=roc (8e525d5265)
- Bug 1192245 - Fix tests that fail with incremental zeal r=terrence (6ca6f63135)
- Bug 1249367 - Make background finalization a GC phase (and clean up Zones properly); r=jonco (43d9a1fafe)
- Bug 1119537 - Make decommit a proper GC phase; r=jonco (638492711e)
- Bug 1232229 - Add assertions to prevent nursery allocation when setting up OMT parse tasks r=terrence (e12cd405c2)
- Backed out changeset 2a613f5a5866 (bug 1119537) for hazard failures (623123a764)
- Bug 1249896 - Part 6: Add gc namespace for Arena::thingsSpan. r=terrence (ba1e1d41b0)
- Bug 1251833 - Part 3: Merge ArenaHeader into Arena. r=terrence (e8bd53b44e)
- Bug 1232181 - Plugin module plumbing for retrieving scroll captures and updating plugin instance content scroll state. r=roc (2309d423cc)
- Bug 1232181 - Add a few win resource helpers. r=aklotz (1421a9695a)
- Bug 1232181 - Add support for capturing plugin windows on Windows. r=aklotz (4ccbfeb19e)
2024-02-06 10:15:07 +08:00

1418 lines
40 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
//
// Eric Vaughan
// Netscape Communications
//
// See documentation in associated header file
//
#include "nsSliderFrame.h"
#include "gfxPrefs.h"
#include "nsStyleContext.h"
#include "nsPresContext.h"
#include "nsIContent.h"
#include "nsCOMPtr.h"
#include "nsNameSpaceManager.h"
#include "nsGkAtoms.h"
#include "nsHTMLParts.h"
#include "nsIPresShell.h"
#include "nsCSSRendering.h"
#include "nsIDOMMouseEvent.h"
#include "nsScrollbarButtonFrame.h"
#include "nsISliderListener.h"
#include "nsIScrollableFrame.h"
#include "nsIScrollbarMediator.h"
#include "nsScrollbarFrame.h"
#include "nsRepeatService.h"
#include "nsBoxLayoutState.h"
#include "nsSprocketLayout.h"
#include "nsIServiceManager.h"
#include "nsContentUtils.h"
#include "nsLayoutUtils.h"
#include "nsDisplayList.h"
#include "mozilla/Preferences.h"
#include "mozilla/LookAndFeel.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/layers/AsyncDragMetrics.h"
#include "mozilla/layers/InputAPZContext.h"
#include <algorithm>
using namespace mozilla;
using mozilla::layers::AsyncDragMetrics;
using mozilla::layers::InputAPZContext;
bool nsSliderFrame::gMiddlePref = false;
int32_t nsSliderFrame::gSnapMultiplier;
// Turn this on if you want to debug slider frames.
#undef DEBUG_SLIDER
static already_AddRefed<nsIContent>
GetContentOfBox(nsIFrame *aBox)
{
nsCOMPtr<nsIContent> content = aBox->GetContent();
return content.forget();
}
nsIFrame*
NS_NewSliderFrame (nsIPresShell* aPresShell, nsStyleContext* aContext)
{
return new (aPresShell) nsSliderFrame(aContext);
}
NS_IMPL_FRAMEARENA_HELPERS(nsSliderFrame)
NS_QUERYFRAME_HEAD(nsSliderFrame)
NS_QUERYFRAME_ENTRY(nsSliderFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsBoxFrame)
nsSliderFrame::nsSliderFrame(nsStyleContext* aContext):
nsBoxFrame(aContext),
mCurPos(0),
mChange(0),
mDragFinished(true),
mUserChanged(false),
mScrollingWithAPZ(false)
{
}
// stop timer
nsSliderFrame::~nsSliderFrame()
{
}
void
nsSliderFrame::Init(nsIContent* aContent,
nsContainerFrame* aParent,
nsIFrame* aPrevInFlow)
{
nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
static bool gotPrefs = false;
if (!gotPrefs) {
gotPrefs = true;
gMiddlePref = Preferences::GetBool("middlemouse.scrollbarPosition");
gSnapMultiplier = Preferences::GetInt("slider.snapMultiplier");
}
mCurPos = GetCurrentPosition(aContent);
}
void
nsSliderFrame::RemoveFrame(ChildListID aListID,
nsIFrame* aOldFrame)
{
nsBoxFrame::RemoveFrame(aListID, aOldFrame);
if (mFrames.IsEmpty())
RemoveListener();
}
void
nsSliderFrame::InsertFrames(ChildListID aListID,
nsIFrame* aPrevFrame,
nsFrameList& aFrameList)
{
bool wasEmpty = mFrames.IsEmpty();
nsBoxFrame::InsertFrames(aListID, aPrevFrame, aFrameList);
if (wasEmpty)
AddListener();
}
void
nsSliderFrame::AppendFrames(ChildListID aListID,
nsFrameList& aFrameList)
{
// if we have no children and on was added then make sure we add the
// listener
bool wasEmpty = mFrames.IsEmpty();
nsBoxFrame::AppendFrames(aListID, aFrameList);
if (wasEmpty)
AddListener();
}
int32_t
nsSliderFrame::GetCurrentPosition(nsIContent* content)
{
return GetIntegerAttribute(content, nsGkAtoms::curpos, 0);
}
int32_t
nsSliderFrame::GetMinPosition(nsIContent* content)
{
return GetIntegerAttribute(content, nsGkAtoms::minpos, 0);
}
int32_t
nsSliderFrame::GetMaxPosition(nsIContent* content)
{
return GetIntegerAttribute(content, nsGkAtoms::maxpos, 100);
}
int32_t
nsSliderFrame::GetIncrement(nsIContent* content)
{
return GetIntegerAttribute(content, nsGkAtoms::increment, 1);
}
int32_t
nsSliderFrame::GetPageIncrement(nsIContent* content)
{
return GetIntegerAttribute(content, nsGkAtoms::pageincrement, 10);
}
int32_t
nsSliderFrame::GetIntegerAttribute(nsIContent* content, nsIAtom* atom, int32_t defaultValue)
{
nsAutoString value;
content->GetAttr(kNameSpaceID_None, atom, value);
if (!value.IsEmpty()) {
nsresult error;
// convert it to an integer
defaultValue = value.ToInteger(&error);
}
return defaultValue;
}
class nsValueChangedRunnable : public nsRunnable
{
public:
nsValueChangedRunnable(nsISliderListener* aListener,
nsIAtom* aWhich,
int32_t aValue,
bool aUserChanged)
: mListener(aListener), mWhich(aWhich),
mValue(aValue), mUserChanged(aUserChanged)
{}
NS_IMETHODIMP Run()
{
return mListener->ValueChanged(nsDependentAtomString(mWhich),
mValue, mUserChanged);
}
nsCOMPtr<nsISliderListener> mListener;
nsCOMPtr<nsIAtom> mWhich;
int32_t mValue;
bool mUserChanged;
};
class nsDragStateChangedRunnable : public nsRunnable
{
public:
nsDragStateChangedRunnable(nsISliderListener* aListener,
bool aDragBeginning)
: mListener(aListener),
mDragBeginning(aDragBeginning)
{}
NS_IMETHODIMP Run()
{
return mListener->DragStateChanged(mDragBeginning);
}
nsCOMPtr<nsISliderListener> mListener;
bool mDragBeginning;
};
nsresult
nsSliderFrame::AttributeChanged(int32_t aNameSpaceID,
nsIAtom* aAttribute,
int32_t aModType)
{
nsresult rv = nsBoxFrame::AttributeChanged(aNameSpaceID, aAttribute,
aModType);
// if the current position changes
if (aAttribute == nsGkAtoms::curpos) {
CurrentPositionChanged();
} else if (aAttribute == nsGkAtoms::minpos ||
aAttribute == nsGkAtoms::maxpos) {
// bounds check it.
nsIFrame* scrollbarBox = GetScrollbar();
nsCOMPtr<nsIContent> scrollbar;
scrollbar = GetContentOfBox(scrollbarBox);
int32_t current = GetCurrentPosition(scrollbar);
int32_t min = GetMinPosition(scrollbar);
int32_t max = GetMaxPosition(scrollbar);
// inform the parent <scale> that the minimum or maximum changed
nsIFrame* parent = GetParent();
if (parent) {
nsCOMPtr<nsISliderListener> sliderListener = do_QueryInterface(parent->GetContent());
if (sliderListener) {
nsContentUtils::AddScriptRunner(
new nsValueChangedRunnable(sliderListener, aAttribute,
aAttribute == nsGkAtoms::minpos ? min : max, false));
}
}
if (current < min || current > max)
{
int32_t direction = 0;
if (current < min || max < min) {
current = min;
direction = -1;
} else if (current > max) {
current = max;
direction = 1;
}
// set the new position and notify observers
nsScrollbarFrame* scrollbarFrame = do_QueryFrame(scrollbarBox);
if (scrollbarFrame) {
nsIScrollbarMediator* mediator = scrollbarFrame->GetScrollbarMediator();
scrollbarFrame->SetIncrementToWhole(direction);
if (mediator) {
mediator->ScrollByWhole(scrollbarFrame, direction,
nsIScrollbarMediator::ENABLE_SNAP);
}
}
// 'this' might be destroyed here
nsContentUtils::AddScriptRunner(
new nsSetAttrRunnable(scrollbar, nsGkAtoms::curpos, current));
}
}
if (aAttribute == nsGkAtoms::minpos ||
aAttribute == nsGkAtoms::maxpos ||
aAttribute == nsGkAtoms::pageincrement ||
aAttribute == nsGkAtoms::increment) {
PresContext()->PresShell()->
FrameNeedsReflow(this, nsIPresShell::eStyleChange, NS_FRAME_IS_DIRTY);
}
return rv;
}
void
nsSliderFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsRect& aDirtyRect,
const nsDisplayListSet& aLists)
{
if (aBuilder->IsForEventDelivery() && isDraggingThumb()) {
// This is EVIL, we shouldn't be messing with event delivery just to get
// thumb mouse drag events to arrive at the slider!
aLists.Outlines()->AppendNewToTop(new (aBuilder)
nsDisplayEventReceiver(aBuilder, this));
return;
}
nsBoxFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
}
void
nsSliderFrame::BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder,
const nsRect& aDirtyRect,
const nsDisplayListSet& aLists)
{
// if we are too small to have a thumb don't paint it.
nsIFrame* thumb = nsBox::GetChildBox(this);
if (thumb) {
nsRect thumbRect(thumb->GetRect());
nsMargin m;
thumb->GetMargin(m);
thumbRect.Inflate(m);
nsRect crect;
GetClientRect(crect);
if (crect.width < thumbRect.width || crect.height < thumbRect.height)
return;
// If this scrollbar is the scrollbar of an actively scrolled scroll frame,
// layerize the scrollbar thumb, wrap it in its own ContainerLayer and
// attach scrolling information to it.
// We do this here and not in the thumb's nsBoxFrame::BuildDisplayList so
// that the event region that gets created for the thumb is included in
// the nsDisplayOwnLayer contents.
uint32_t flags = 0;
mozilla::layers::FrameMetrics::ViewID scrollTargetId =
mozilla::layers::FrameMetrics::NULL_SCROLL_ID;
float scrollbarThumbRatio = 0.0f;
aBuilder->GetScrollbarInfo(&scrollTargetId, &flags);
bool thumbGetsLayer = (scrollTargetId != layers::FrameMetrics::NULL_SCROLL_ID);
nsLayoutUtils::SetScrollbarThumbLayerization(thumb, thumbGetsLayer);
if (thumbGetsLayer) {
nsDisplayListCollection tempLists;
nsBoxFrame::BuildDisplayListForChildren(aBuilder, aDirtyRect, tempLists);
// This is a bit of a hack. Collect up all descendant display items
// and merge them into a single Content() list.
nsDisplayList masterList;
masterList.AppendToTop(tempLists.BorderBackground());
masterList.AppendToTop(tempLists.BlockBorderBackgrounds());
masterList.AppendToTop(tempLists.Floats());
masterList.AppendToTop(tempLists.Content());
masterList.AppendToTop(tempLists.PositionedDescendants());
masterList.AppendToTop(tempLists.Outlines());
// Wrap the list to make it its own layer.
aLists.Content()->AppendNewToTop(new (aBuilder)
nsDisplayOwnLayer(aBuilder, this, &masterList, flags, scrollTargetId,
scrollbarThumbRatio));
return;
}
}
nsBoxFrame::BuildDisplayListForChildren(aBuilder, aDirtyRect, aLists);
}
NS_IMETHODIMP
nsSliderFrame::DoLayout(nsBoxLayoutState& aState)
{
// get the thumb should be our only child
nsIFrame* thumbBox = nsBox::GetChildBox(this);
if (!thumbBox) {
SyncLayout(aState);
return NS_OK;
}
EnsureOrient();
#ifdef DEBUG_LAYOUT
if (mState & NS_STATE_DEBUG_WAS_SET) {
if (mState & NS_STATE_SET_TO_DEBUG)
SetDebug(aState, true);
else
SetDebug(aState, false);
}
#endif
// get the content area inside our borders
nsRect clientRect;
GetClientRect(clientRect);
// get the scrollbar
nsIFrame* scrollbarBox = GetScrollbar();
nsCOMPtr<nsIContent> scrollbar;
scrollbar = GetContentOfBox(scrollbarBox);
// get the thumb's pref size
nsSize thumbSize = thumbBox->GetPrefSize(aState);
if (IsHorizontal())
thumbSize.height = clientRect.height;
else
thumbSize.width = clientRect.width;
int32_t curPos = GetCurrentPosition(scrollbar);
int32_t minPos = GetMinPosition(scrollbar);
int32_t maxPos = GetMaxPosition(scrollbar);
int32_t pageIncrement = GetPageIncrement(scrollbar);
maxPos = std::max(minPos, maxPos);
curPos = clamped(curPos, minPos, maxPos);
nscoord& availableLength = IsHorizontal() ? clientRect.width : clientRect.height;
nscoord& thumbLength = IsHorizontal() ? thumbSize.width : thumbSize.height;
if ((pageIncrement + maxPos - minPos) > 0 && thumbBox->GetFlex() > 0) {
float ratio = float(pageIncrement) / float(maxPos - minPos + pageIncrement);
thumbLength = std::max(thumbLength, NSToCoordRound(availableLength * ratio));
}
// Round the thumb's length to device pixels.
nsPresContext* presContext = PresContext();
thumbLength = presContext->DevPixelsToAppUnits(
presContext->AppUnitsToDevPixels(thumbLength));
// mRatio translates the thumb position in app units to the value.
mRatio = (minPos != maxPos) ? float(availableLength - thumbLength) / float(maxPos - minPos) : 1;
// in reverse mode, curpos is reversed such that lower values are to the
// right or bottom and increase leftwards or upwards. In this case, use the
// offset from the end instead of the beginning.
bool reverse = mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::dir,
nsGkAtoms::reverse, eCaseMatters);
nscoord pos = reverse ? (maxPos - curPos) : (curPos - minPos);
// set the thumb's coord to be the current pos * the ratio.
nsRect thumbRect(clientRect.x, clientRect.y, thumbSize.width, thumbSize.height);
int32_t& thumbPos = (IsHorizontal() ? thumbRect.x : thumbRect.y);
thumbPos += NSToCoordRound(pos * mRatio);
nsRect oldThumbRect(thumbBox->GetRect());
LayoutChildAt(aState, thumbBox, thumbRect);
SyncLayout(aState);
// Redraw only if thumb changed size.
if (!oldThumbRect.IsEqualInterior(thumbRect))
Redraw(aState);
return NS_OK;
}
nsresult
nsSliderFrame::HandleEvent(nsPresContext* aPresContext,
WidgetGUIEvent* aEvent,
nsEventStatus* aEventStatus)
{
NS_ENSURE_ARG_POINTER(aEventStatus);
// If a web page calls event.preventDefault() we still want to
// scroll when scroll arrow is clicked. See bug 511075.
if (!mContent->IsInNativeAnonymousSubtree() &&
nsEventStatus_eConsumeNoDefault == *aEventStatus) {
return NS_OK;
}
if (!mDragFinished && !isDraggingThumb()) {
StopDrag();
return NS_OK;
}
nsIFrame* scrollbarBox = GetScrollbar();
nsCOMPtr<nsIContent> scrollbar;
scrollbar = GetContentOfBox(scrollbarBox);
bool isHorizontal = IsHorizontal();
if (isDraggingThumb())
{
switch (aEvent->mMessage) {
case eTouchMove:
case eMouseMove: {
if (mScrollingWithAPZ) {
break;
}
nsPoint eventPoint;
if (!GetEventPoint(aEvent, eventPoint)) {
break;
}
if (mChange) {
// On Linux the destination point is determined by the initial click
// on the scrollbar track and doesn't change until the mouse button
// is released.
#ifndef MOZ_WIDGET_GTK
// On the other platforms we need to update the destination point now.
mDestinationPoint = eventPoint;
StopRepeat();
StartRepeat();
#endif
break;
}
nscoord pos = isHorizontal ? eventPoint.x : eventPoint.y;
nsIFrame* thumbFrame = mFrames.FirstChild();
if (!thumbFrame) {
return NS_OK;
}
// take our current position and subtract the start location
pos -= mDragStart;
bool isMouseOutsideThumb = false;
if (gSnapMultiplier) {
nsSize thumbSize = thumbFrame->GetSize();
if (isHorizontal) {
// horizontal scrollbar - check if mouse is above or below thumb
// XXXbz what about looking at the .y of the thumb's rect? Is that
// always zero here?
if (eventPoint.y < -gSnapMultiplier * thumbSize.height ||
eventPoint.y > thumbSize.height +
gSnapMultiplier * thumbSize.height)
isMouseOutsideThumb = true;
}
else {
// vertical scrollbar - check if mouse is left or right of thumb
if (eventPoint.x < -gSnapMultiplier * thumbSize.width ||
eventPoint.x > thumbSize.width +
gSnapMultiplier * thumbSize.width)
isMouseOutsideThumb = true;
}
}
if (aEvent->mClass == eTouchEventClass) {
*aEventStatus = nsEventStatus_eConsumeNoDefault;
}
if (isMouseOutsideThumb)
{
SetCurrentThumbPosition(scrollbar, mThumbStart, false, false);
return NS_OK;
}
// set it
SetCurrentThumbPosition(scrollbar, pos, false, true); // with snapping
}
break;
case eTouchEnd:
case eMouseUp:
if (ShouldScrollForEvent(aEvent)) {
StopDrag();
//we MUST call nsFrame HandleEvent for mouse ups to maintain the selection state and capture state.
return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
}
break;
default:
break;
}
//return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
return NS_OK;
} else if (ShouldScrollToClickForEvent(aEvent)) {
nsPoint eventPoint;
if (!GetEventPoint(aEvent, eventPoint)) {
return NS_OK;
}
nscoord pos = isHorizontal ? eventPoint.x : eventPoint.y;
// adjust so that the middle of the thumb is placed under the click
nsIFrame* thumbFrame = mFrames.FirstChild();
if (!thumbFrame) {
return NS_OK;
}
nsSize thumbSize = thumbFrame->GetSize();
nscoord thumbLength = isHorizontal ? thumbSize.width : thumbSize.height;
// set it
nsWeakFrame weakFrame(this);
// should aMaySnap be true here?
SetCurrentThumbPosition(scrollbar, pos - thumbLength/2, false, false);
NS_ENSURE_TRUE(weakFrame.IsAlive(), NS_OK);
DragThumb(true);
#ifdef MOZ_WIDGET_GTK
nsCOMPtr<nsIContent> thumb = thumbFrame->GetContent();
thumb->SetAttr(kNameSpaceID_None, nsGkAtoms::active, NS_LITERAL_STRING("true"), true);
#endif
if (aEvent->mClass == eTouchEventClass) {
*aEventStatus = nsEventStatus_eConsumeNoDefault;
}
if (isHorizontal)
mThumbStart = thumbFrame->GetPosition().x;
else
mThumbStart = thumbFrame->GetPosition().y;
mDragStart = pos - mThumbStart;
}
#ifdef MOZ_WIDGET_GTK
else if (ShouldScrollForEvent(aEvent) &&
aEvent->mClass == eMouseEventClass &&
aEvent->AsMouseEvent()->button == WidgetMouseEvent::eRightButton) {
// HandlePress and HandleRelease are usually called via
// nsFrame::HandleEvent, but only for the left mouse button.
if (aEvent->mMessage == eMouseDown) {
HandlePress(aPresContext, aEvent, aEventStatus);
} else if (aEvent->mMessage == eMouseUp) {
HandleRelease(aPresContext, aEvent, aEventStatus);
}
return NS_OK;
}
#endif
// XXX hack until handle release is actually called in nsframe.
// if (aEvent->mMessage == eMouseOut ||
// aEvent->mMessage == NS_MOUSE_RIGHT_BUTTON_UP ||
// aEvent->mMessage == NS_MOUSE_LEFT_BUTTON_UP) {
// HandleRelease(aPresContext, aEvent, aEventStatus);
// }
if (aEvent->mMessage == eMouseOut && mChange)
HandleRelease(aPresContext, aEvent, aEventStatus);
return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
}
// Helper function to collect the "scroll to click" metric. Beware of
// caching this, users expect to be able to change the system preference
// and see the browser change its behavior immediately.
bool
nsSliderFrame::GetScrollToClick()
{
if (GetScrollbar() != this) {
return LookAndFeel::GetInt(LookAndFeel::eIntID_ScrollToClick, false);
}
if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::movetoclick,
nsGkAtoms::_true, eCaseMatters)) {
return true;
}
if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::movetoclick,
nsGkAtoms::_false, eCaseMatters)) {
return false;
}
#ifdef XP_MACOSX
return true;
#else
return false;
#endif
}
nsIFrame*
nsSliderFrame::GetScrollbar()
{
// if we are in a scrollbar then return the scrollbar's content node
// if we are not then return ours.
nsIFrame* scrollbar;
nsScrollbarButtonFrame::GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);
if (scrollbar == nullptr)
return this;
return scrollbar->IsBoxFrame() ? scrollbar : this;
}
void
nsSliderFrame::PageUpDown(nscoord change)
{
// on a page up or down get our page increment. We get this by getting the scrollbar we are in and
// asking it for the current position and the page increment. If we are not in a scrollbar we will
// get the values from our own node.
nsIFrame* scrollbarBox = GetScrollbar();
nsCOMPtr<nsIContent> scrollbar;
scrollbar = GetContentOfBox(scrollbarBox);
nscoord pageIncrement = GetPageIncrement(scrollbar);
int32_t curpos = GetCurrentPosition(scrollbar);
int32_t minpos = GetMinPosition(scrollbar);
int32_t maxpos = GetMaxPosition(scrollbar);
// get the new position and make sure it is in bounds
int32_t newpos = curpos + change * pageIncrement;
if (newpos < minpos || maxpos < minpos)
newpos = minpos;
else if (newpos > maxpos)
newpos = maxpos;
SetCurrentPositionInternal(scrollbar, newpos, true);
}
// called when the current position changed and we need to update the thumb's location
void
nsSliderFrame::CurrentPositionChanged()
{
nsIFrame* scrollbarBox = GetScrollbar();
nsCOMPtr<nsIContent> scrollbar;
scrollbar = GetContentOfBox(scrollbarBox);
// get the current position
int32_t curPos = GetCurrentPosition(scrollbar);
// do nothing if the position did not change
if (mCurPos == curPos)
return;
// get our current min and max position from our content node
int32_t minPos = GetMinPosition(scrollbar);
int32_t maxPos = GetMaxPosition(scrollbar);
maxPos = std::max(minPos, maxPos);
curPos = clamped(curPos, minPos, maxPos);
// get the thumb's rect
nsIFrame* thumbFrame = mFrames.FirstChild();
if (!thumbFrame)
return; // The thumb may stream in asynchronously via XBL.
nsRect thumbRect = thumbFrame->GetRect();
nsRect clientRect;
GetClientRect(clientRect);
// figure out the new rect
nsRect newThumbRect(thumbRect);
bool reverse = mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::dir,
nsGkAtoms::reverse, eCaseMatters);
nscoord pos = reverse ? (maxPos - curPos) : (curPos - minPos);
if (IsHorizontal())
newThumbRect.x = clientRect.x + NSToCoordRound(pos * mRatio);
else
newThumbRect.y = clientRect.y + NSToCoordRound(pos * mRatio);
// avoid putting the scroll thumb at subpixel positions which cause needless invalidations
nscoord appUnitsPerPixel = PresContext()->AppUnitsPerDevPixel();
nsRect snappedThumbRect = ToAppUnits(newThumbRect.ToNearestPixels(appUnitsPerPixel), appUnitsPerPixel);
if (IsHorizontal()) {
newThumbRect.x = snappedThumbRect.x;
newThumbRect.width = snappedThumbRect.width;
} else {
newThumbRect.y = snappedThumbRect.y;
newThumbRect.height = snappedThumbRect.height;
}
newThumbRect = newThumbRect.Intersect(clientRect);
// set the rect
thumbFrame->SetRect(newThumbRect);
// Request a repaint of the scrollbar unless we have paint-skipping enabled
// and this is an APZ scroll.
nsIScrollableFrame* scrollableFrame = do_QueryFrame(GetScrollbar()->GetParent());
if (!gfxPrefs::APZPaintSkipping() ||
!scrollableFrame ||
scrollableFrame->LastScrollOrigin() != nsGkAtoms::apz) {
SchedulePaint();
}
mCurPos = curPos;
// inform the parent <scale> if it exists that the value changed
nsIFrame* parent = GetParent();
if (parent) {
nsCOMPtr<nsISliderListener> sliderListener = do_QueryInterface(parent->GetContent());
if (sliderListener) {
nsContentUtils::AddScriptRunner(
new nsValueChangedRunnable(sliderListener, nsGkAtoms::curpos, mCurPos, mUserChanged));
}
}
}
static void UpdateAttribute(nsIContent* aScrollbar, nscoord aNewPos, bool aNotify, bool aIsSmooth) {
nsAutoString str;
str.AppendInt(aNewPos);
if (aIsSmooth) {
aScrollbar->SetAttr(kNameSpaceID_None, nsGkAtoms::smooth, NS_LITERAL_STRING("true"), false);
}
aScrollbar->SetAttr(kNameSpaceID_None, nsGkAtoms::curpos, str, aNotify);
if (aIsSmooth) {
aScrollbar->UnsetAttr(kNameSpaceID_None, nsGkAtoms::smooth, false);
}
}
// Use this function when you want to set the scroll position via the position
// of the scrollbar thumb, e.g. when dragging the slider. This function scrolls
// the content in such a way that thumbRect.x/.y becomes aNewThumbPos.
void
nsSliderFrame::SetCurrentThumbPosition(nsIContent* aScrollbar, nscoord aNewThumbPos,
bool aIsSmooth, bool aMaySnap)
{
nsRect crect;
GetClientRect(crect);
nscoord offset = IsHorizontal() ? crect.x : crect.y;
int32_t newPos = NSToIntRound((aNewThumbPos - offset) / mRatio);
if (aMaySnap && mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::snap,
nsGkAtoms::_true, eCaseMatters)) {
// If snap="true", then the slider may only be set to min + (increment * x).
// Otherwise, the slider may be set to any positive integer.
int32_t increment = GetIncrement(aScrollbar);
newPos = NSToIntRound(newPos / float(increment)) * increment;
}
SetCurrentPosition(aScrollbar, newPos, aIsSmooth);
}
// Use this function when you know the target scroll position of the scrolled content.
// aNewPos should be passed to this function as a position as if the minpos is 0.
// That is, the minpos will be added to the position by this function. In a reverse
// direction slider, the newpos should be the distance from the end.
void
nsSliderFrame::SetCurrentPosition(nsIContent* aScrollbar, int32_t aNewPos,
bool aIsSmooth)
{
// get min and max position from our content node
int32_t minpos = GetMinPosition(aScrollbar);
int32_t maxpos = GetMaxPosition(aScrollbar);
// in reverse direction sliders, flip the value so that it goes from
// right to left, or bottom to top.
if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::dir,
nsGkAtoms::reverse, eCaseMatters))
aNewPos = maxpos - aNewPos;
else
aNewPos += minpos;
// get the new position and make sure it is in bounds
if (aNewPos < minpos || maxpos < minpos)
aNewPos = minpos;
else if (aNewPos > maxpos)
aNewPos = maxpos;
SetCurrentPositionInternal(aScrollbar, aNewPos, aIsSmooth);
}
void
nsSliderFrame::SetCurrentPositionInternal(nsIContent* aScrollbar, int32_t aNewPos,
bool aIsSmooth)
{
nsCOMPtr<nsIContent> scrollbar = aScrollbar;
nsIFrame* scrollbarBox = GetScrollbar();
nsWeakFrame weakFrame(this);
mUserChanged = true;
nsScrollbarFrame* scrollbarFrame = do_QueryFrame(scrollbarBox);
if (scrollbarFrame) {
// See if we have a mediator.
nsIScrollbarMediator* mediator = scrollbarFrame->GetScrollbarMediator();
if (mediator) {
nsCOMPtr<nsIContent> content = GetContent();
nscoord oldPos = nsPresContext::CSSPixelsToAppUnits(GetCurrentPosition(scrollbar));
nscoord newPos = nsPresContext::CSSPixelsToAppUnits(aNewPos);
mediator->ThumbMoved(scrollbarFrame, oldPos, newPos);
if (!weakFrame.IsAlive()) {
return;
}
CurrentPositionChanged();
mUserChanged = false;
return;
}
}
UpdateAttribute(scrollbar, aNewPos, true, aIsSmooth);
if (!weakFrame.IsAlive()) {
return;
}
mUserChanged = false;
#ifdef DEBUG_SLIDER
printf("Current Pos=%d\n",aNewPos);
#endif
}
nsIAtom*
nsSliderFrame::GetType() const
{
return nsGkAtoms::sliderFrame;
}
void
nsSliderFrame::SetInitialChildList(ChildListID aListID,
nsFrameList& aChildList)
{
nsBoxFrame::SetInitialChildList(aListID, aChildList);
if (aListID == kPrincipalList) {
AddListener();
}
}
nsresult
nsSliderMediator::HandleEvent(nsIDOMEvent* aEvent)
{
// Only process the event if the thumb is not being dragged.
if (mSlider && !mSlider->isDraggingThumb())
return mSlider->StartDrag(aEvent);
return NS_OK;
}
bool
nsSliderFrame::StartAPZDrag()
{
if (!gfxPlatform::GetPlatform()->SupportsApzDragInput()) {
return false;
}
nsContainerFrame* cf = GetScrollbar()->GetParent();
if (!cf) {
return false;
}
nsIContent* scrollableContent = cf->GetContent();
if (!scrollableContent) {
return false;
}
mozilla::layers::FrameMetrics::ViewID scrollTargetId;
bool hasID = nsLayoutUtils::FindIDFor(scrollableContent, &scrollTargetId);
bool hasAPZView = hasID && (scrollTargetId != layers::FrameMetrics::NULL_SCROLL_ID);
if (!hasAPZView) {
return false;
}
nsIFrame* scrollbarBox = GetScrollbar();
nsCOMPtr<nsIContent> scrollbar = GetContentOfBox(scrollbarBox);
// This rect is the range in which the scroll thumb can slide in.
nsRect sliderTrack = GetRect() - scrollbarBox->GetPosition();
CSSIntRect sliderTrackCSS = CSSIntRect::FromAppUnitsRounded(sliderTrack);
uint64_t inputblockId = InputAPZContext::GetInputBlockId();
uint32_t presShellId = PresContext()->PresShell()->GetPresShellId();
AsyncDragMetrics dragMetrics(scrollTargetId, presShellId, inputblockId,
NSAppUnitsToIntPixels(mDragStart,
float(AppUnitsPerCSSPixel())),
sliderTrackCSS,
IsHorizontal() ? AsyncDragMetrics::HORIZONTAL :
AsyncDragMetrics::VERTICAL);
if (!nsLayoutUtils::HasDisplayPort(scrollableContent)) {
return false;
}
// When we start an APZ drag, we wont get mouse events for the drag.
// APZ will consume them all and only notify us of the new scroll position.
this->GetNearestWidget()->StartAsyncScrollbarDrag(dragMetrics);
return true;
}
nsresult
nsSliderFrame::StartDrag(nsIDOMEvent* aEvent)
{
#ifdef DEBUG_SLIDER
printf("Begin dragging\n");
#endif
if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
nsGkAtoms::_true, eCaseMatters))
return NS_OK;
WidgetGUIEvent* event = aEvent->WidgetEventPtr()->AsGUIEvent();
if (!ShouldScrollForEvent(event)) {
return NS_OK;
}
nsPoint pt;
if (!GetEventPoint(event, pt)) {
return NS_OK;
}
bool isHorizontal = IsHorizontal();
nscoord pos = isHorizontal ? pt.x : pt.y;
// If we should scroll-to-click, first place the middle of the slider thumb
// under the mouse.
nsCOMPtr<nsIContent> scrollbar;
nscoord newpos = pos;
bool scrollToClick = ShouldScrollToClickForEvent(event);
if (scrollToClick) {
// adjust so that the middle of the thumb is placed under the click
nsIFrame* thumbFrame = mFrames.FirstChild();
if (!thumbFrame) {
return NS_OK;
}
nsSize thumbSize = thumbFrame->GetSize();
nscoord thumbLength = isHorizontal ? thumbSize.width : thumbSize.height;
newpos -= (thumbLength/2);
nsIFrame* scrollbarBox = GetScrollbar();
scrollbar = GetContentOfBox(scrollbarBox);
}
DragThumb(true);
if (scrollToClick) {
// should aMaySnap be true here?
SetCurrentThumbPosition(scrollbar, newpos, false, false);
}
nsIFrame* thumbFrame = mFrames.FirstChild();
if (!thumbFrame) {
return NS_OK;
}
#ifdef MOZ_WIDGET_GTK
nsCOMPtr<nsIContent> thumb = thumbFrame->GetContent();
thumb->SetAttr(kNameSpaceID_None, nsGkAtoms::active, NS_LITERAL_STRING("true"), true);
#endif
if (isHorizontal)
mThumbStart = thumbFrame->GetPosition().x;
else
mThumbStart = thumbFrame->GetPosition().y;
mDragStart = pos - mThumbStart;
mScrollingWithAPZ = StartAPZDrag();
#ifdef DEBUG_SLIDER
printf("Pressed mDragStart=%d\n",mDragStart);
#endif
return NS_OK;
}
nsresult
nsSliderFrame::StopDrag()
{
AddListener();
DragThumb(false);
mScrollingWithAPZ = false;
#ifdef MOZ_WIDGET_GTK
nsIFrame* thumbFrame = mFrames.FirstChild();
if (thumbFrame) {
nsCOMPtr<nsIContent> thumb = thumbFrame->GetContent();
thumb->UnsetAttr(kNameSpaceID_None, nsGkAtoms::active, true);
}
#endif
if (mChange) {
StopRepeat();
mChange = 0;
}
return NS_OK;
}
void
nsSliderFrame::DragThumb(bool aGrabMouseEvents)
{
mDragFinished = !aGrabMouseEvents;
// inform the parent <scale> that a drag is beginning or ending
nsIFrame* parent = GetParent();
if (parent) {
nsCOMPtr<nsISliderListener> sliderListener = do_QueryInterface(parent->GetContent());
if (sliderListener) {
nsContentUtils::AddScriptRunner(
new nsDragStateChangedRunnable(sliderListener, aGrabMouseEvents));
}
}
nsIPresShell::SetCapturingContent(aGrabMouseEvents ? GetContent() : nullptr,
aGrabMouseEvents ? CAPTURE_IGNOREALLOWED : 0);
}
bool
nsSliderFrame::isDraggingThumb()
{
return (nsIPresShell::GetCapturingContent() == GetContent());
}
void
nsSliderFrame::AddListener()
{
if (!mMediator) {
mMediator = new nsSliderMediator(this);
}
nsIFrame* thumbFrame = mFrames.FirstChild();
if (!thumbFrame) {
return;
}
thumbFrame->GetContent()->
AddSystemEventListener(NS_LITERAL_STRING("mousedown"), mMediator,
false, false);
thumbFrame->GetContent()->
AddSystemEventListener(NS_LITERAL_STRING("touchstart"), mMediator,
false, false);
}
void
nsSliderFrame::RemoveListener()
{
NS_ASSERTION(mMediator, "No listener was ever added!!");
nsIFrame* thumbFrame = mFrames.FirstChild();
if (!thumbFrame)
return;
thumbFrame->GetContent()->
RemoveSystemEventListener(NS_LITERAL_STRING("mousedown"), mMediator, false);
}
bool
nsSliderFrame::ShouldScrollForEvent(WidgetGUIEvent* aEvent)
{
switch (aEvent->mMessage) {
case eTouchStart:
case eTouchEnd:
return true;
case eMouseDown:
case eMouseUp: {
uint16_t button = aEvent->AsMouseEvent()->button;
#ifdef MOZ_WIDGET_GTK
return (button == WidgetMouseEvent::eLeftButton) ||
(button == WidgetMouseEvent::eRightButton && GetScrollToClick()) ||
(button == WidgetMouseEvent::eMiddleButton && gMiddlePref && !GetScrollToClick());
#else
return (button == WidgetMouseEvent::eLeftButton) ||
(button == WidgetMouseEvent::eMiddleButton && gMiddlePref);
#endif
}
default:
return false;
}
}
bool
nsSliderFrame::ShouldScrollToClickForEvent(WidgetGUIEvent* aEvent)
{
if (!ShouldScrollForEvent(aEvent)) {
return false;
}
if (aEvent->mMessage == eTouchStart) {
return GetScrollToClick();
}
if (aEvent->mMessage != eMouseDown) {
return false;
}
#if defined(XP_MACOSX) || defined(MOZ_WIDGET_GTK)
// On Mac and Linux, clicking the scrollbar thumb should never scroll to click.
if (IsEventOverThumb(aEvent)) {
return false;
}
#endif
WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
if (mouseEvent->button == WidgetMouseEvent::eLeftButton) {
#ifdef XP_MACOSX
bool invertPref = mouseEvent->IsAlt();
#else
bool invertPref = mouseEvent->IsShift();
#endif
return GetScrollToClick() != invertPref;
}
#ifdef MOZ_WIDGET_GTK
if (mouseEvent->button == WidgetMouseEvent::eRightButton) {
return !GetScrollToClick();
}
#endif
return true;
}
bool
nsSliderFrame::IsEventOverThumb(WidgetGUIEvent* aEvent)
{
nsIFrame* thumbFrame = mFrames.FirstChild();
if (!thumbFrame) {
return false;
}
nsPoint eventPoint;
if (!GetEventPoint(aEvent, eventPoint)) {
return false;
}
nsRect thumbRect = thumbFrame->GetRect();
#if defined(MOZ_WIDGET_GTK)
/* Scrollbar track can have padding, so it's better to check that eventPoint
* is inside of actual thumb, not just its one axis. The part of the scrollbar
* track adjacent to thumb can actually receive events in GTK3 */
return eventPoint.x >= thumbRect.x && eventPoint.x < thumbRect.XMost() &&
eventPoint.y >= thumbRect.y && eventPoint.y < thumbRect.YMost();
#else
bool isHorizontal = IsHorizontal();
nscoord eventPos = isHorizontal ? eventPoint.x : eventPoint.y;
nscoord thumbStart = isHorizontal ? thumbRect.x : thumbRect.y;
nscoord thumbEnd = isHorizontal ? thumbRect.XMost() : thumbRect.YMost();
return eventPos >= thumbStart && eventPos < thumbEnd;
#endif
}
NS_IMETHODIMP
nsSliderFrame::HandlePress(nsPresContext* aPresContext,
WidgetGUIEvent* aEvent,
nsEventStatus* aEventStatus)
{
if (!ShouldScrollForEvent(aEvent) || ShouldScrollToClickForEvent(aEvent)) {
return NS_OK;
}
if (IsEventOverThumb(aEvent)) {
return NS_OK;
}
nsIFrame* thumbFrame = mFrames.FirstChild();
if (!thumbFrame) // display:none?
return NS_OK;
if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
nsGkAtoms::_true, eCaseMatters))
return NS_OK;
nsRect thumbRect = thumbFrame->GetRect();
nscoord change = 1;
nsPoint eventPoint;
if (!GetEventPoint(aEvent, eventPoint)) {
return NS_OK;
}
if (IsHorizontal() ? eventPoint.x < thumbRect.x
: eventPoint.y < thumbRect.y)
change = -1;
mChange = change;
DragThumb(true);
// On Linux we want to keep scrolling in the direction indicated by |change|
// until the mouse is released. On the other platforms we want to stop
// scrolling as soon as the scrollbar thumb has reached the current mouse
// position.
#ifdef MOZ_WIDGET_GTK
nsRect clientRect;
GetClientRect(clientRect);
// Set the destination point to the very end of the scrollbar so that
// scrolling doesn't stop halfway through.
if (change > 0) {
mDestinationPoint = nsPoint(clientRect.width, clientRect.height);
}
else {
mDestinationPoint = nsPoint(0, 0);
}
#else
mDestinationPoint = eventPoint;
#endif
StartRepeat();
PageScroll(change);
return NS_OK;
}
NS_IMETHODIMP
nsSliderFrame::HandleRelease(nsPresContext* aPresContext,
WidgetGUIEvent* aEvent,
nsEventStatus* aEventStatus)
{
StopRepeat();
nsIFrame* scrollbar = GetScrollbar();
nsScrollbarFrame* sb = do_QueryFrame(scrollbar);
if (sb) {
nsIScrollbarMediator* m = sb->GetScrollbarMediator();
if (m) {
m->ScrollbarReleased(sb);
}
}
return NS_OK;
}
void
nsSliderFrame::DestroyFrom(nsIFrame* aDestructRoot)
{
// tell our mediator if we have one we are gone.
if (mMediator) {
mMediator->SetSlider(nullptr);
mMediator = nullptr;
}
StopRepeat();
// call base class Destroy()
nsBoxFrame::DestroyFrom(aDestructRoot);
}
nsSize
nsSliderFrame::GetPrefSize(nsBoxLayoutState& aState)
{
EnsureOrient();
return nsBoxFrame::GetPrefSize(aState);
}
nsSize
nsSliderFrame::GetMinSize(nsBoxLayoutState& aState)
{
EnsureOrient();
// our min size is just our borders and padding
return nsBox::GetMinSize(aState);
}
nsSize
nsSliderFrame::GetMaxSize(nsBoxLayoutState& aState)
{
EnsureOrient();
return nsBoxFrame::GetMaxSize(aState);
}
void
nsSliderFrame::EnsureOrient()
{
nsIFrame* scrollbarBox = GetScrollbar();
bool isHorizontal = (scrollbarBox->GetStateBits() & NS_STATE_IS_HORIZONTAL) != 0;
if (isHorizontal)
mState |= NS_STATE_IS_HORIZONTAL;
else
mState &= ~NS_STATE_IS_HORIZONTAL;
}
void
nsSliderFrame::Notify(void)
{
bool stop = false;
nsIFrame* thumbFrame = mFrames.FirstChild();
if (!thumbFrame) {
StopRepeat();
return;
}
nsRect thumbRect = thumbFrame->GetRect();
bool isHorizontal = IsHorizontal();
// See if the thumb has moved past our destination point.
// if it has we want to stop.
if (isHorizontal) {
if (mChange < 0) {
if (thumbRect.x < mDestinationPoint.x)
stop = true;
} else {
if (thumbRect.x + thumbRect.width > mDestinationPoint.x)
stop = true;
}
} else {
if (mChange < 0) {
if (thumbRect.y < mDestinationPoint.y)
stop = true;
} else {
if (thumbRect.y + thumbRect.height > mDestinationPoint.y)
stop = true;
}
}
if (stop) {
StopRepeat();
} else {
PageScroll(mChange);
}
}
void
nsSliderFrame::PageScroll(nscoord aChange)
{
if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::dir,
nsGkAtoms::reverse, eCaseMatters)) {
aChange = -aChange;
}
nsIFrame* scrollbar = GetScrollbar();
nsScrollbarFrame* sb = do_QueryFrame(scrollbar);
if (sb) {
nsIScrollbarMediator* m = sb->GetScrollbarMediator();
sb->SetIncrementToPage(aChange);
if (m) {
m->ScrollByPage(sb, aChange, nsIScrollbarMediator::ENABLE_SNAP);
return;
}
}
PageUpDown(aChange);
}
float
nsSliderFrame::GetThumbRatio() const
{
// mRatio is in thumb app units per scrolled css pixels. Convert it to a
// ratio of the thumb's CSS pixels per scrolled CSS pixels. (Note the thumb
// is in the scrollframe's parent's space whereas the scrolled CSS pixels
// are in the scrollframe's space).
return mRatio / mozilla::AppUnitsPerCSSPixel();
}
NS_IMPL_ISUPPORTS(nsSliderMediator,
nsIDOMEventListener)