2015-08-27 23:07:59 +03:00
|
|
|
/* -*- 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 "SwipeTracker.h"
|
|
|
|
|
|
|
|
#include "InputData.h"
|
2017-01-06 10:29:47 +03:00
|
|
|
#include "mozilla/FlushType.h"
|
2019-04-13 04:05:21 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2022-02-02 05:12:55 +03:00
|
|
|
#include "mozilla/StaticPrefs_widget.h"
|
2022-08-03 02:08:46 +03:00
|
|
|
#include "mozilla/StaticPrefs_browser.h"
|
2015-08-27 23:07:59 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "mozilla/TouchEvents.h"
|
2018-02-09 19:17:10 +03:00
|
|
|
#include "mozilla/dom/SimpleGestureEventBinding.h"
|
2015-08-27 23:07:59 +03:00
|
|
|
#include "nsAlgorithm.h"
|
2022-01-05 12:08:05 +03:00
|
|
|
#include "nsIWidget.h"
|
2020-08-05 00:17:50 +03:00
|
|
|
#include "nsRefreshDriver.h"
|
2015-08-27 23:07:59 +03:00
|
|
|
#include "UnitTransforms.h"
|
|
|
|
|
2022-01-05 12:08:05 +03:00
|
|
|
// These values were tweaked to make the physics feel similar to the native
|
|
|
|
// swipe.
|
2015-08-27 23:07:59 +03:00
|
|
|
static const double kSpringForce = 250.0;
|
2022-05-09 00:50:10 +03:00
|
|
|
static const double kSwipeSuccessThreshold = 0.25;
|
2015-08-27 23:07:59 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
static already_AddRefed<nsRefreshDriver> GetRefreshDriver(nsIWidget& aWidget) {
|
|
|
|
nsIWidgetListener* widgetListener = aWidget.GetWidgetListener();
|
2022-01-05 12:08:05 +03:00
|
|
|
PresShell* presShell =
|
|
|
|
widgetListener ? widgetListener->GetPresShell() : nullptr;
|
|
|
|
nsPresContext* presContext =
|
|
|
|
presShell ? presShell->GetPresContext() : nullptr;
|
|
|
|
RefPtr<nsRefreshDriver> refreshDriver =
|
|
|
|
presContext ? presContext->RefreshDriver() : nullptr;
|
2015-08-27 23:07:59 +03:00
|
|
|
return refreshDriver.forget();
|
|
|
|
}
|
|
|
|
|
2022-01-05 12:08:05 +03:00
|
|
|
SwipeTracker::SwipeTracker(nsIWidget& aWidget,
|
|
|
|
const PanGestureInput& aSwipeStartEvent,
|
|
|
|
uint32_t aAllowedDirections,
|
|
|
|
uint32_t aSwipeDirection)
|
2015-08-27 23:07:59 +03:00
|
|
|
: mWidget(aWidget),
|
|
|
|
mRefreshDriver(GetRefreshDriver(mWidget)),
|
|
|
|
mAxis(0.0, 0.0, 0.0, kSpringForce, 1.0),
|
|
|
|
mEventPosition(RoundedToInt(ViewAs<LayoutDevicePixel>(
|
|
|
|
aSwipeStartEvent.mPanStartPoint,
|
2015-11-19 03:32:37 +03:00
|
|
|
PixelCastJustification::LayoutDeviceIsScreenForUntransformedEvent))),
|
2015-08-27 23:07:59 +03:00
|
|
|
mLastEventTimeStamp(aSwipeStartEvent.mTimeStamp),
|
|
|
|
mAllowedDirections(aAllowedDirections),
|
|
|
|
mSwipeDirection(aSwipeDirection),
|
|
|
|
mGestureAmount(0.0),
|
|
|
|
mCurrentVelocity(0.0),
|
|
|
|
mEventsAreControllingSwipe(true),
|
|
|
|
mEventsHaveStartedNewGesture(false),
|
|
|
|
mRegisteredWithRefreshDriver(false) {
|
2016-12-20 14:58:04 +03:00
|
|
|
SendSwipeEvent(eSwipeGestureStart, 0, 0.0, aSwipeStartEvent.mTimeStamp);
|
2022-05-26 05:29:10 +03:00
|
|
|
ProcessEvent(aSwipeStartEvent, /* aProcessingFirstEvent = */ true);
|
2015-08-27 23:07:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SwipeTracker::Destroy() { UnregisterFromRefreshDriver(); }
|
|
|
|
|
|
|
|
SwipeTracker::~SwipeTracker() {
|
2022-01-05 12:08:05 +03:00
|
|
|
MOZ_ASSERT(!mRegisteredWithRefreshDriver,
|
|
|
|
"Destroy needs to be called before deallocating");
|
2015-08-27 23:07:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
double SwipeTracker::SwipeSuccessTargetValue() const {
|
2022-01-05 12:08:05 +03:00
|
|
|
return (mSwipeDirection == dom::SimpleGestureEvent_Binding::DIRECTION_RIGHT)
|
|
|
|
? -1.0
|
|
|
|
: 1.0;
|
2015-08-27 23:07:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
double SwipeTracker::ClampToAllowedRange(double aGestureAmount) const {
|
|
|
|
// gestureAmount needs to stay between -1 and 0 when swiping right and
|
|
|
|
// between 0 and 1 when swiping left.
|
2022-01-05 12:08:05 +03:00
|
|
|
double min =
|
|
|
|
(mSwipeDirection == dom::SimpleGestureEvent_Binding::DIRECTION_RIGHT)
|
|
|
|
? -1.0
|
|
|
|
: 0.0;
|
|
|
|
double max =
|
|
|
|
(mSwipeDirection == dom::SimpleGestureEvent_Binding::DIRECTION_LEFT)
|
|
|
|
? 1.0
|
|
|
|
: 0.0;
|
2015-08-27 23:07:59 +03:00
|
|
|
return clamped(aGestureAmount, min, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SwipeTracker::ComputeSwipeSuccess() const {
|
|
|
|
double targetValue = SwipeSuccessTargetValue();
|
|
|
|
|
|
|
|
// If the fingers were moving away from the target direction when they were
|
|
|
|
// lifted from the touchpad, abort the swipe.
|
2022-02-15 01:12:20 +03:00
|
|
|
if (mCurrentVelocity * targetValue <
|
|
|
|
-StaticPrefs::widget_swipe_velocity_twitch_tolerance()) {
|
2015-08-27 23:07:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (mGestureAmount * targetValue +
|
2022-02-15 01:12:20 +03:00
|
|
|
mCurrentVelocity * targetValue *
|
|
|
|
StaticPrefs::widget_swipe_success_velocity_contribution()) >=
|
2022-05-09 00:50:10 +03:00
|
|
|
kSwipeSuccessThreshold;
|
2015-08-27 23:07:59 +03:00
|
|
|
}
|
|
|
|
|
2022-05-26 05:29:10 +03:00
|
|
|
nsEventStatus SwipeTracker::ProcessEvent(
|
|
|
|
const PanGestureInput& aEvent, bool aProcessingFirstEvent /* = false */) {
|
2021-04-30 12:08:08 +03:00
|
|
|
// If the fingers have already been lifted or the swipe direction is where
|
|
|
|
// navigation is impossible, don't process this event for swiping.
|
|
|
|
if (!mEventsAreControllingSwipe || !SwipingInAllowedDirection()) {
|
2015-08-27 23:07:59 +03:00
|
|
|
// Return nsEventStatus_eConsumeNoDefault for events from the swipe gesture
|
|
|
|
// and nsEventStatus_eIgnore for events of subsequent scroll gestures.
|
|
|
|
if (aEvent.mType == PanGestureInput::PANGESTURE_MAYSTART ||
|
|
|
|
aEvent.mType == PanGestureInput::PANGESTURE_START) {
|
|
|
|
mEventsHaveStartedNewGesture = true;
|
|
|
|
}
|
2022-01-05 12:08:05 +03:00
|
|
|
return mEventsHaveStartedNewGesture ? nsEventStatus_eIgnore
|
|
|
|
: nsEventStatus_eConsumeNoDefault;
|
2015-08-27 23:07:59 +03:00
|
|
|
}
|
|
|
|
|
2022-01-05 12:08:05 +03:00
|
|
|
double delta = -aEvent.mPanDisplacement.x /
|
2022-04-06 12:51:17 +03:00
|
|
|
mWidget.GetDefaultScaleInternal() /
|
|
|
|
StaticPrefs::widget_swipe_whole_page_pixel_size();
|
2015-08-27 23:07:59 +03:00
|
|
|
mGestureAmount = ClampToAllowedRange(mGestureAmount + delta);
|
|
|
|
if (aEvent.mType != PanGestureInput::PANGESTURE_END) {
|
2022-05-26 05:29:10 +03:00
|
|
|
if (!aProcessingFirstEvent) {
|
|
|
|
double elapsedSeconds = std::max(
|
|
|
|
0.008, (aEvent.mTimeStamp - mLastEventTimeStamp).ToSeconds());
|
|
|
|
mCurrentVelocity = delta / elapsedSeconds;
|
|
|
|
}
|
2015-08-27 23:07:59 +03:00
|
|
|
mLastEventTimeStamp = aEvent.mTimeStamp;
|
2022-05-26 05:29:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const bool computedSwipeSuccess = ComputeSwipeSuccess();
|
|
|
|
|
|
|
|
// The velocity component might push us over the success threshold, in which
|
|
|
|
// case we want to pass the success threshold in the event we send so that the
|
|
|
|
// UI draws as 100% opacity to indicate such. We don't want to include the
|
|
|
|
// velocity in the amount we put on the event if we aren't over the success
|
|
|
|
// threshold because that would lead to the opacity decreasing even if the
|
|
|
|
// user continues to increase the swipe distance. If we do compute swipe
|
|
|
|
// success here and the user does not lift their fingers and then decreases
|
|
|
|
// the total swipe so that we go below the success threshold the opacity would
|
|
|
|
// also decrease in that case but that seems okay.
|
2022-08-03 02:08:46 +03:00
|
|
|
// We don't want above tweak if we move the UI along with the opacity change
|
|
|
|
// since it forces the UI element jump to the last position and jump back to
|
|
|
|
// the original position if the navigation didn't happen.
|
2022-05-26 05:29:10 +03:00
|
|
|
double eventAmount = mGestureAmount;
|
2022-08-03 02:08:46 +03:00
|
|
|
if (computedSwipeSuccess &&
|
|
|
|
StaticPrefs::browser_swipe_navigation_icon_move_distance() == 0) {
|
2022-05-26 05:29:10 +03:00
|
|
|
eventAmount = kSwipeSuccessThreshold;
|
|
|
|
if (mGestureAmount < 0.f) {
|
|
|
|
eventAmount = -eventAmount;
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 13:50:41 +03:00
|
|
|
|
|
|
|
// If ComputeSwipeSuccess returned false because the users fingers were moving
|
|
|
|
// slightly away from the target direction then we do not want to display
|
|
|
|
// the UI as if we were at the success threshold as that would give a false
|
|
|
|
// indication that navigation would happen.
|
|
|
|
if (!computedSwipeSuccess && (eventAmount >= kSwipeSuccessThreshold ||
|
|
|
|
eventAmount <= -kSwipeSuccessThreshold)) {
|
|
|
|
eventAmount = 0.999 * kSwipeSuccessThreshold;
|
|
|
|
if (mGestureAmount < 0.f) {
|
|
|
|
eventAmount = -eventAmount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 05:29:10 +03:00
|
|
|
SendSwipeEvent(eSwipeGestureUpdate, 0, eventAmount, aEvent.mTimeStamp);
|
|
|
|
|
|
|
|
if (aEvent.mType == PanGestureInput::PANGESTURE_END) {
|
2015-08-27 23:07:59 +03:00
|
|
|
mEventsAreControllingSwipe = false;
|
2022-05-26 05:29:10 +03:00
|
|
|
if (computedSwipeSuccess) {
|
2016-12-20 14:58:04 +03:00
|
|
|
// Let's use same timestamp as previous event because this is caused by
|
|
|
|
// the preceding event.
|
|
|
|
SendSwipeEvent(eSwipeGesture, mSwipeDirection, 0.0, aEvent.mTimeStamp);
|
2022-05-06 08:55:52 +03:00
|
|
|
UnregisterFromRefreshDriver();
|
|
|
|
NS_DispatchToMainThread(
|
|
|
|
NS_NewRunnableFunction("SwipeTracker::SwipeFinished",
|
|
|
|
[swipeTracker = RefPtr<SwipeTracker>(this),
|
|
|
|
timeStamp = aEvent.mTimeStamp] {
|
|
|
|
swipeTracker->SwipeFinished(timeStamp);
|
|
|
|
}));
|
2022-05-06 08:55:51 +03:00
|
|
|
} else {
|
2022-08-03 13:50:41 +03:00
|
|
|
StartAnimating(eventAmount, 0.0);
|
2015-08-27 23:07:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:50:41 +03:00
|
|
|
void SwipeTracker::StartAnimating(double aStartValue, double aTargetValue) {
|
|
|
|
mAxis.SetPosition(aStartValue);
|
2015-08-27 23:07:59 +03:00
|
|
|
mAxis.SetDestination(aTargetValue);
|
|
|
|
mAxis.SetVelocity(mCurrentVelocity);
|
|
|
|
|
|
|
|
mLastAnimationFrameTime = TimeStamp::Now();
|
|
|
|
|
|
|
|
// Add ourselves as a refresh driver observer. The refresh driver
|
|
|
|
// will call WillRefresh for each animation frame until we
|
|
|
|
// unregister ourselves.
|
|
|
|
MOZ_ASSERT(!mRegisteredWithRefreshDriver);
|
|
|
|
if (mRefreshDriver) {
|
2022-01-05 12:08:05 +03:00
|
|
|
mRefreshDriver->AddRefreshObserver(this, FlushType::Style,
|
|
|
|
"Swipe animation");
|
2015-08-27 23:07:59 +03:00
|
|
|
mRegisteredWithRefreshDriver = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwipeTracker::WillRefresh(mozilla::TimeStamp aTime) {
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
mAxis.Simulate(now - mLastAnimationFrameTime);
|
|
|
|
mLastAnimationFrameTime = now;
|
|
|
|
|
2022-04-06 12:51:17 +03:00
|
|
|
bool isFinished =
|
|
|
|
mAxis.IsFinished(1.0 / StaticPrefs::widget_swipe_whole_page_pixel_size());
|
2015-08-27 23:07:59 +03:00
|
|
|
mGestureAmount = (isFinished ? mAxis.GetDestination() : mAxis.GetPosition());
|
2016-12-20 14:58:04 +03:00
|
|
|
SendSwipeEvent(eSwipeGestureUpdate, 0, mGestureAmount, now);
|
2015-08-27 23:07:59 +03:00
|
|
|
|
|
|
|
if (isFinished) {
|
|
|
|
UnregisterFromRefreshDriver();
|
2016-12-20 14:58:04 +03:00
|
|
|
SwipeFinished(now);
|
2015-08-27 23:07:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 14:58:04 +03:00
|
|
|
void SwipeTracker::CancelSwipe(const TimeStamp& aTimeStamp) {
|
|
|
|
SendSwipeEvent(eSwipeGestureEnd, 0, 0.0, aTimeStamp);
|
2015-08-27 23:07:59 +03:00
|
|
|
}
|
|
|
|
|
2016-12-20 14:58:04 +03:00
|
|
|
void SwipeTracker::SwipeFinished(const TimeStamp& aTimeStamp) {
|
|
|
|
SendSwipeEvent(eSwipeGestureEnd, 0, 0.0, aTimeStamp);
|
2015-08-27 23:07:59 +03:00
|
|
|
mWidget.SwipeFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwipeTracker::UnregisterFromRefreshDriver() {
|
|
|
|
if (mRegisteredWithRefreshDriver) {
|
|
|
|
MOZ_ASSERT(mRefreshDriver, "How were we able to register, then?");
|
2017-01-06 10:30:31 +03:00
|
|
|
mRefreshDriver->RemoveRefreshObserver(this, FlushType::Style);
|
2015-08-27 23:07:59 +03:00
|
|
|
}
|
|
|
|
mRegisteredWithRefreshDriver = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ WidgetSimpleGestureEvent SwipeTracker::CreateSwipeGestureEvent(
|
2022-01-05 12:08:05 +03:00
|
|
|
EventMessage aMsg, nsIWidget* aWidget,
|
|
|
|
const LayoutDeviceIntPoint& aPosition, const TimeStamp& aTimeStamp) {
|
2016-12-20 14:58:04 +03:00
|
|
|
// XXX Why isn't this initialized with nsCocoaUtils::InitInputEvent()?
|
2015-08-27 23:07:59 +03:00
|
|
|
WidgetSimpleGestureEvent geckoEvent(true, aMsg, aWidget);
|
2016-03-31 11:03:00 +03:00
|
|
|
geckoEvent.mModifiers = 0;
|
2016-12-20 14:58:04 +03:00
|
|
|
// XXX How about geckoEvent.mTime?
|
|
|
|
geckoEvent.mTimeStamp = aTimeStamp;
|
2016-04-18 17:09:02 +03:00
|
|
|
geckoEvent.mRefPoint = aPosition;
|
2019-04-21 22:42:37 +03:00
|
|
|
geckoEvent.mButtons = 0;
|
2015-08-27 23:07:59 +03:00
|
|
|
return geckoEvent;
|
|
|
|
}
|
|
|
|
|
2022-01-05 12:08:05 +03:00
|
|
|
bool SwipeTracker::SendSwipeEvent(EventMessage aMsg, uint32_t aDirection,
|
|
|
|
double aDelta, const TimeStamp& aTimeStamp) {
|
2015-08-27 23:07:59 +03:00
|
|
|
WidgetSimpleGestureEvent geckoEvent =
|
2016-12-20 14:58:04 +03:00
|
|
|
CreateSwipeGestureEvent(aMsg, &mWidget, mEventPosition, aTimeStamp);
|
2016-05-09 22:16:54 +03:00
|
|
|
geckoEvent.mDirection = aDirection;
|
2016-05-09 22:16:55 +03:00
|
|
|
geckoEvent.mDelta = aDelta;
|
2016-05-09 22:16:54 +03:00
|
|
|
geckoEvent.mAllowedDirections = mAllowedDirections;
|
2015-08-27 23:07:59 +03:00
|
|
|
return mWidget.DispatchWindowEvent(geckoEvent);
|
|
|
|
}
|
|
|
|
|
2022-01-05 12:08:07 +03:00
|
|
|
// static
|
|
|
|
bool SwipeTracker::CanTriggerSwipe(const PanGestureInput& aPanInput) {
|
2022-02-02 05:12:55 +03:00
|
|
|
if (StaticPrefs::widget_disable_swipe_tracker()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-05 12:08:07 +03:00
|
|
|
if (aPanInput.mType != PanGestureInput::PANGESTURE_START) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
return std::abs(aPanInput.mPanDisplacement.x) >
|
|
|
|
std::abs(aPanInput.mPanDisplacement.y) * 8;
|
|
|
|
}
|
|
|
|
|
2015-08-27 23:07:59 +03:00
|
|
|
} // namespace mozilla
|