Bug 673875: Reproduce the bounce behavior when reaching the top/bottom of the page on OSX. r=smichaud,felipe,masayuki

This commit is contained in:
Stephen Pohl 2013-10-11 15:48:53 -04:00
parent 55cde06d2a
commit 8e7e13c21d
5 changed files with 181 additions and 61 deletions

View File

@ -62,8 +62,9 @@ let gGestureSupport = {
switch (aEvent.type) {
case "MozSwipeGestureStart":
aEvent.preventDefault();
this._setupSwipeGesture(aEvent);
if (this._setupSwipeGesture(aEvent)) {
aEvent.preventDefault();
}
break;
case "MozSwipeGestureUpdate":
aEvent.preventDefault();
@ -179,23 +180,43 @@ let gGestureSupport = {
*
* @param aEvent
* The swipe gesture start event.
* @return true if swipe gestures could successfully be set up, false
* othwerwise.
*/
_setupSwipeGesture: function GS__setupSwipeGesture(aEvent) {
if (!this._swipeNavigatesHistory(aEvent))
return;
if (!this._swipeNavigatesHistory(aEvent)) {
return false;
}
let isVerticalSwipe = false;
if (gHistorySwipeAnimation.active) {
if (aEvent.direction == aEvent.DIRECTION_UP) {
if (content.pageYOffset > 0) {
return false;
}
isVerticalSwipe = true;
} else if (aEvent.direction == aEvent.DIRECTION_DOWN) {
if (content.pageYOffset < content.scrollMaxY) {
return false;
}
isVerticalSwipe = true;
}
}
let canGoBack = gHistorySwipeAnimation.canGoBack();
let canGoForward = gHistorySwipeAnimation.canGoForward();
let isLTR = gHistorySwipeAnimation.isLTR;
if (canGoBack)
if (canGoBack) {
aEvent.allowedDirections |= isLTR ? aEvent.DIRECTION_LEFT :
aEvent.DIRECTION_RIGHT;
if (canGoForward)
}
if (canGoForward) {
aEvent.allowedDirections |= isLTR ? aEvent.DIRECTION_RIGHT :
aEvent.DIRECTION_LEFT;
}
gHistorySwipeAnimation.startAnimation();
gHistorySwipeAnimation.startAnimation(isVerticalSwipe);
this._doUpdate = function GS__doUpdate(aEvent) {
gHistorySwipeAnimation.updateAnimation(aEvent.delta);
@ -207,6 +228,8 @@ let gGestureSupport = {
this._doUpdate = function (aEvent) {};
this._doEnd = function (aEvent) {};
}
return true;
},
/**
@ -552,8 +575,10 @@ let gHistorySwipeAnimation = {
this._startingIndex = -1;
this._historyIndex = -1;
this._boxWidth = -1;
this._boxHeight = -1;
this._maxSnapshots = this._getMaxSnapshots();
this._lastSwipeDir = "";
this._direction = "horizontal";
// We only want to activate history swipe animations if we store snapshots.
// If we don't store any, we handle horizontal swipes without animations.
@ -584,14 +609,28 @@ let gHistorySwipeAnimation = {
/**
* Starts the swipe animation and handles fast swiping (i.e. a swipe animation
* is already in progress when a new one is initiated).
*
* @param aIsVerticalSwipe
* Whether we're dealing with a vertical swipe or not.
*/
startAnimation: function HSA_startAnimation() {
startAnimation: function HSA_startAnimation(aIsVerticalSwipe) {
this._direction = aIsVerticalSwipe ? "vertical" : "horizontal";
if (this.isAnimationRunning()) {
gBrowser.stop();
this._lastSwipeDir = "RELOAD"; // just ensure that != ""
this._canGoBack = this.canGoBack();
this._canGoForward = this.canGoForward();
this._handleFastSwiping();
// If this is a horizontal scroll, or if this is a vertical scroll that
// was started while a horizontal scroll was still running, handle it as
// as a fast swipe. In the case of the latter scenario, this allows us to
// start the vertical animation without first loading the final page, or
// taking another snapshot. If vertical scrolls are initiated repeatedly
// without prior horizontal scroll we skip this and restart the animation
// from 0.
if (this._direction == "horizontal" || this._lastSwipeDir != "") {
gBrowser.stop();
this._lastSwipeDir = "RELOAD"; // just ensure that != ""
this._canGoBack = this.canGoBack();
this._canGoForward = this.canGoForward();
this._handleFastSwiping();
}
}
else {
this._startingIndex = gBrowser.webNavigation.sessionHistory.index;
@ -623,19 +662,24 @@ let gHistorySwipeAnimation = {
* swipe gesture.
*/
updateAnimation: function HSA_updateAnimation(aVal) {
if (!this.isAnimationRunning())
if (!this.isAnimationRunning()) {
return;
}
// We use the following value to decrease the bounce effect when swiping
// back/forward past the browsing history. This value was determined
// experimentally.
// We use the following value to decrease the bounce effect when scrolling
// to the top or bottom of the page, or when swiping back/forward past the
// browsing history. This value was determined experimentally.
let dampValue = 4;
if ((aVal >= 0 && this.isLTR) ||
(aVal <= 0 && !this.isLTR)) {
if (this._direction == "vertical") {
this._prevBox.collapsed = true;
this._nextBox.collapsed = true;
this._positionBox(this._curBox, -1 * aVal / dampValue);
} else if ((aVal >= 0 && this.isLTR) ||
(aVal <= 0 && !this.isLTR)) {
let tempDampValue = 1;
if (this._canGoBack)
if (this._canGoBack) {
this._prevBox.collapsed = false;
else {
} else {
tempDampValue = dampValue;
this._prevBox.collapsed = true;
}
@ -647,11 +691,7 @@ let gHistorySwipeAnimation = {
// The forward page should be pushed offscreen all the way to the right.
this._positionBox(this._nextBox, 1);
}
else {
if (aVal < -1)
aVal = -1; // Cap value to avoid sliding the page further than allowed.
} else {
// The intention is to go forward. If there is a page to go forward to,
// it should slide in from the right (LTR) or left (RTL).
// Otherwise, the current page should slide to the left (LTR) or
@ -663,8 +703,7 @@ let gHistorySwipeAnimation = {
let offset = this.isLTR ? 1 : -1;
this._positionBox(this._curBox, 0);
this._positionBox(this._nextBox, offset + aVal);
}
else {
} else {
this._prevBox.collapsed = true;
this._positionBox(this._curBox, aVal / dampValue);
}
@ -835,7 +874,9 @@ let gHistorySwipeAnimation = {
"box");
this._container.appendChild(this._nextBox);
this._boxWidth = this._curBox.getBoundingClientRect().width; // cache width
// Cache width and height.
this._boxWidth = this._curBox.getBoundingClientRect().width;
this._boxHeight = this._curBox.getBoundingClientRect().height;
},
/**
@ -849,6 +890,7 @@ let gHistorySwipeAnimation = {
this._container.parentNode.removeChild(this._container);
this._container = null;
this._boxWidth = -1;
this._boxHeight = -1;
},
/**
@ -876,7 +918,14 @@ let gHistorySwipeAnimation = {
* The position (in X coordinates) to move the box element to.
*/
_positionBox: function HSA__positionBox(aBox, aPosition) {
aBox.style.transform = "translateX(" + this._boxWidth * aPosition + "px)";
let transform = "";
if (this._direction == "vertical")
transform = "translateY(" + this._boxHeight * aPosition + "px)";
else
transform = "translateX(" + this._boxWidth * aPosition + "px)";
aBox.style.transform = transform;
},
/**

View File

@ -3189,6 +3189,9 @@ nsEventStateManager::PostHandleEvent(nsPresContext* aPresContext,
nsIScrollableFrame* scrollTarget =
ComputeScrollTarget(aTargetFrame, wheelEvent,
COMPUTE_DEFAULT_ACTION_TARGET);
if (!scrollTarget) {
wheelEvent->mViewPortIsOverscrolled = true;
}
wheelEvent->overflowDeltaX = wheelEvent->deltaX;
wheelEvent->overflowDeltaY = wheelEvent->deltaY;
WheelPrefs::GetInstance()->

View File

@ -318,7 +318,8 @@ public:
deltaMode(nsIDOMWheelEvent::DOM_DELTA_PIXEL),
customizedByUserPrefs(false), isMomentum(false), isPixelOnlyDevice(false),
lineOrPageDeltaX(0), lineOrPageDeltaY(0), scrollType(SCROLL_DEFAULT),
overflowDeltaX(0.0), overflowDeltaY(0.0)
overflowDeltaX(0.0), overflowDeltaY(0.0),
mViewPortIsOverscrolled(false)
{
}
@ -399,6 +400,12 @@ public:
double overflowDeltaX;
double overflowDeltaY;
// Whether or not the parent of the currently overscrolled frame is the
// ViewPort. This is false in situations when an element on the page is being
// overscrolled (such as a text field), but true when the 'page' is being
// overscrolled.
bool mViewPortIsOverscrolled;
void AssignWheelEventData(const WidgetWheelEvent& aEvent, bool aCopyTargets)
{
AssignMouseEventBaseData(aEvent, aCopyTargets);
@ -415,6 +422,7 @@ public:
scrollType = aEvent.scrollType;
overflowDeltaX = aEvent.overflowDeltaX;
overflowDeltaY = aEvent.overflowDeltaY;
mViewPortIsOverscrolled = aEvent.mViewPortIsOverscrolled;
}
};

View File

@ -290,6 +290,7 @@ typedef NSInteger NSEventGestureAxis;
#ifdef __LP64__
// Support for fluid swipe tracking.
BOOL* mCancelSwipeAnimation;
uint32_t mCurrentSwipeDir;
#endif
// Whether this uses off-main-thread compositing.
@ -360,7 +361,9 @@ typedef NSInteger NSEventGestureAxis;
// Support for fluid swipe tracking.
#ifdef __LP64__
- (void)maybeTrackScrollEventAsSwipe:(NSEvent *)anEvent
scrollOverflow:(double)overflow;
scrollOverflowX:(double)anOverflowX
scrollOverflowY:(double)anOverflowY
viewPortIsOverscrolled:(BOOL)aViewPortIsOverscrolled;
#endif
- (void)setUsingOMTCompositor:(BOOL)aUseOMTC;

View File

@ -2796,6 +2796,7 @@ NSEvent* gLastDragMouseDownEvent = nil;
#ifdef __LP64__
mCancelSwipeAnimation = nil;
mCurrentSwipeDir = 0;
#endif
mTopLeftCornerMask = NULL;
@ -4093,17 +4094,20 @@ NSEvent* gLastDragMouseDownEvent = nil;
delta:0.0];
}
// Support fluid swipe tracking on OS X 10.7 and higher. We must be careful
// to only invoke this support on a horizontal two-finger gesture that really
// Support fluid swipe tracking on OS X 10.7 and higher. We must be careful
// to only invoke this support on a two-finger gesture that really
// is a swipe (and not a scroll) -- in other words, the app is responsible
// for deciding which is which. But once the decision is made, the OS tracks
// for deciding which is which. But once the decision is made, the OS tracks
// the swipe until it has finished, and decides whether or not it succeeded.
// A swipe has the same functionality as the Back and Forward buttons. For
// now swipe animation is unsupported (e.g. no bounces). This method is
// partly based on Apple sample code available at
// http://developer.apple.com/library/mac/#releasenotes/Cocoa/AppKit.html
// A horizontal swipe has the same functionality as the Back and Forward
// buttons.
// This method is partly based on Apple sample code available at
// developer.apple.com/library/mac/#releasenotes/Cocoa/AppKitOlderNotes.html
// (under Fluid Swipe Tracking API).
- (void)maybeTrackScrollEventAsSwipe:(NSEvent *)anEvent
scrollOverflow:(double)overflow
scrollOverflowX:(double)anOverflowX
scrollOverflowY:(double)anOverflowY
viewPortIsOverscrolled:(BOOL)aViewPortIsOverscrolled
{
if (!nsCocoaFeatures::OnLionOrLater()) {
return;
@ -4118,6 +4122,12 @@ NSEvent* gLastDragMouseDownEvent = nil;
return;
}
// We should only track scroll events as swipe if the viewport is being
// overscrolled.
if (!aViewPortIsOverscrolled) {
return;
}
// Verify that this is a scroll wheel event with proper phase to be tracked
// by the OS.
if ([anEvent type] != NSScrollWheel || [anEvent phase] == NSEventPhaseNone) {
@ -4125,12 +4135,10 @@ NSEvent* gLastDragMouseDownEvent = nil;
}
// Only initiate tracking if the user has tried to scroll past the edge of
// the current page (as indicated by 'overflow' being non-zero). Gecko only
// sets WidgetMouseScrollEvent.scrollOverflow when it's processing
// NS_MOUSE_PIXEL_SCROLL events (not NS_MOUSE_SCROLL events).
// WidgetMouseScrollEvent.scrollOverflow only indicates left or right overflow
// for horizontal NS_MOUSE_PIXEL_SCROLL events.
if (!overflow) {
// the current page (as indicated by 'anOverflowX' or 'anOverflowY' being
// non-zero). Gecko only sets WidgetMouseScrollEvent.scrollOverflow when it's
// processing NS_MOUSE_PIXEL_SCROLL events (not NS_MOUSE_SCROLL events).
if (anOverflowX == 0.0 && anOverflowY == 0.0) {
return;
}
@ -4139,18 +4147,20 @@ NSEvent* gLastDragMouseDownEvent = nil;
deltaX = [anEvent scrollingDeltaX];
deltaY = [anEvent scrollingDeltaY];
} else {
deltaX = [anEvent deltaX];
deltaY = [anEvent deltaY];
return;
}
uint32_t vDirs = (uint32_t)nsIDOMSimpleGestureEvent::DIRECTION_DOWN |
(uint32_t)nsIDOMSimpleGestureEvent::DIRECTION_UP;
uint32_t direction = 0;
// Only initiate horizontal tracking for events whose horizontal element is
// at least eight times larger than its vertical element. This minimizes
// performance problems with vertical scrolls (by minimizing the possibility
// that they'll be misinterpreted as horizontal swipes), while still
// tolerating a small vertical element to a true horizontal swipe. The number
// '8' was arrived at by trial and error.
if (overflow != 0.0 && deltaX != 0.0 &&
if (anOverflowX != 0.0 && deltaX != 0.0 &&
fabsf(deltaX) > fabsf(deltaY) * 8) {
// Only initiate horizontal tracking for gestures that have just begun --
// otherwise a scroll to one side of the page can have a swipe tacked on
@ -4164,10 +4174,35 @@ NSEvent* gLastDragMouseDownEvent = nil;
} else {
direction = (uint32_t)nsIDOMSimpleGestureEvent::DIRECTION_LEFT;
}
}
// Only initiate vertical tracking for events whose vertical element is
// at least two times larger than its horizontal element. This minimizes
// performance problems. The number '2' was arrived at by trial and error.
else if (anOverflowY != 0.0 && deltaY != 0.0 &&
fabsf(deltaY) > fabsf(deltaX) * 2) {
if (deltaY < 0.0) {
direction = (uint32_t)nsIDOMSimpleGestureEvent::DIRECTION_DOWN;
} else {
direction = (uint32_t)nsIDOMSimpleGestureEvent::DIRECTION_UP;
}
if ((mCurrentSwipeDir & vDirs) && (mCurrentSwipeDir != direction)) {
// If a swipe is currently being tracked kill it -- it's been interrupted
// by another gesture event.
if (mCancelSwipeAnimation && *mCancelSwipeAnimation == NO) {
*mCancelSwipeAnimation = YES;
mCancelSwipeAnimation = nil;
[self sendSwipeEndEvent:anEvent allowedDirections:0];
}
return;
}
} else {
return;
}
// Track the direction we're going in.
mCurrentSwipeDir = direction;
// If a swipe is currently being tracked kill it -- it's been interrupted
// by another gesture event.
if (mCancelSwipeAnimation && *mCancelSwipeAnimation == NO) {
@ -4189,8 +4224,14 @@ NSEvent* gLastDragMouseDownEvent = nil;
return;
}
double min = (allowedDirections & nsIDOMSimpleGestureEvent::DIRECTION_RIGHT) ? -1 : 0;
double max = (allowedDirections & nsIDOMSimpleGestureEvent::DIRECTION_LEFT) ? 1 : 0;
CGFloat min = 0.0;
CGFloat max = 0.0;
if (!(direction & vDirs)) {
min = (allowedDirections & nsIDOMSimpleGestureEvent::DIRECTION_RIGHT) ?
-1.0 : 0.0;
max = (allowedDirections & nsIDOMSimpleGestureEvent::DIRECTION_LEFT) ?
1.0 : 0.0;
}
__block BOOL animationCanceled = NO;
__block BOOL geckoSwipeEventSent = NO;
@ -4228,13 +4269,15 @@ NSEvent* gLastDragMouseDownEvent = nil;
if (animationCanceled || !mGeckoChild || gestureAmount == 0.0) {
*stop = YES;
animationCanceled = YES;
if (gestureAmount == 0.0) {
if (gestureAmount == 0.0 ||
((direction & vDirs) && (direction != mCurrentSwipeDir))) {
if (mCancelSwipeAnimation)
*mCancelSwipeAnimation = YES;
mCancelSwipeAnimation = nil;
[self sendSwipeEndEvent:anEvent
allowedDirections:allowedDirectionsCopy];
}
mCurrentSwipeDir = 0;
return;
}
@ -4268,6 +4311,7 @@ NSEvent* gLastDragMouseDownEvent = nil;
if (isComplete) {
[self sendSwipeEndEvent:anEvent allowedDirections:allowedDirectionsCopy];
mCurrentSwipeDir = 0;
mCancelSwipeAnimation = nil;
}
}];
@ -4780,18 +4824,31 @@ static int32_t RoundUp(double aDouble)
NPCocoaEventScrollWheel,
&cocoaEvent);
mGeckoChild->DispatchWindowEvent(wheelEvent);
if (!mGeckoChild) {
return;
#ifdef __LP64__
// Only dispatch this event if we're not currently tracking a scroll event as
// swipe.
if (!mCancelSwipeAnimation || *mCancelSwipeAnimation == YES) {
#endif // #ifdef __LP64__
mGeckoChild->DispatchWindowEvent(wheelEvent);
if (!mGeckoChild) {
return;
}
#ifdef __LP64__
} else {
// Manually set these members here since we didn't dispatch the event.
wheelEvent.overflowDeltaX = wheelEvent.deltaX;
wheelEvent.overflowDeltaY = wheelEvent.deltaY;
wheelEvent.mViewPortIsOverscrolled = true;
}
#ifdef __LP64__
// overflowDeltaX tells us when the user has tried to scroll past the edge
// of a page to the left or the right (in those cases it's non-zero).
if (wheelEvent.deltaMode == nsIDOMWheelEvent::DOM_DELTA_PIXEL &&
wheelEvent.deltaX != 0.0) {
// overflowDeltaX and overflowDeltaY tell us when the user has tried to
// scroll past the edge of a page (in those cases it's non-zero).
if ((wheelEvent.deltaMode == nsIDOMWheelEvent::DOM_DELTA_PIXEL) &&
(wheelEvent.deltaX != 0.0 || wheelEvent.deltaY != 0.0)) {
[self maybeTrackScrollEventAsSwipe:theEvent
scrollOverflow:wheelEvent.overflowDeltaX];
scrollOverflowX:wheelEvent.overflowDeltaX
scrollOverflowY:wheelEvent.overflowDeltaY
viewPortIsOverscrolled:wheelEvent.mViewPortIsOverscrolled];
}
#endif // #ifdef __LP64__