Bug 737758: part 3 V2: Cleanups and semantic consistency. r=roc a=akeybl
authorAvi Halachmi (:avih)
Mon, 26 Mar 2012 11:53:45 +0200
changeset 92003 4e89e0ae835499519d46dbb3a7f74af7a36d701f
parent 92002 5096a64c4c3993ac45c486487a4ca2330d53bcb6
child 92004 670caaf9d22bc0b98774ad391573a9626c5bc029
push idunknown
push userunknown
push dateunknown
reviewersroc, akeybl
bugs737758
milestone13.0a2
Bug 737758: part 3 V2: Cleanups and semantic consistency. r=roc a=akeybl
layout/generic/nsGfxScrollFrame.cpp
layout/generic/nsGfxScrollFrame.h
modules/libpref/src/init/all.js
--- a/layout/generic/nsGfxScrollFrame.cpp
+++ b/layout/generic/nsGfxScrollFrame.cpp
@@ -1318,17 +1318,17 @@ public:
     if (mScrollTimer) mScrollTimer->Cancel();
   }
 
   nsPoint PositionAt(TimeStamp aTime);
   nsSize VelocityAt(TimeStamp aTime); // In nscoords per second
 
   void InitSmoothScroll(TimeStamp aTime, nsPoint aCurrentPos,
                         nsSize aCurrentVelocity, nsPoint aDestination,
-                        nsIAtom *aProfile);
+                        nsIAtom *aOrigin);
 
   bool IsFinished(TimeStamp aTime) {
     return aTime > mStartTime + mDuration; // XXX or if we've hit the wall
   }
 
   nsCOMPtr<nsITimer> mScrollTimer;
   TimeStamp mStartTime;
 
@@ -1336,27 +1336,25 @@ public:
   // reduce duration fluctuations). When AsyncScroll is constructed and no
   // previous timestamps are available (indicated with mIsFirstIteration),
   // initialize mPrevStartTime using imaginary previous timestamps with maximum
   // relevant intervals between them.
   TimeStamp mPrevStartTime[3];
   bool mIsFirstIteration;
 
   // Cached Preferences values to avoid re-reading them when extending an existing
-  // animation for the same profile (can be as frequent as every 10(!)ms for a quick
-  // roll of the mouse wheel).
-  // These values are minimum and maximum animation duration per profile, is smoothness
-  // enabled for that profile, and a global ratio which defines how longer is the
-  // animation's duration compared to the average recent events intervals (such
-  // that for a relatively consistent events rate, the next event arrives before
-  // current animation ends)
-  nsIAtom* mProfile;
-  PRInt32 mProfileMinMS;
-  PRInt32 mProfileMaxMS;
-  bool    mIsProfileSmoothnessEnabled;
+  // animation for the same event origin (can be as frequent as every 10(!)ms for 
+  // a quick roll of the mouse wheel).
+  // These values are minimum and maximum animation duration per event origin,
+  // and a global ratio which defines how longer is the animation's duration
+  // compared to the average recent events intervals (such that for a relatively
+  // consistent events rate, the next event arrives before current animation ends)
+  nsCOMPtr<nsIAtom> mOrigin;
+  PRInt32 mOriginMinMS;
+  PRInt32 mOriginMaxMS;
   double  mIntervalRatio;
 
   TimeDuration mDuration;
   nsPoint mStartPos;
   nsPoint mDestination;
   nsSMILKeySpline mTimingFunctionX;
   nsSMILKeySpline mTimingFunctionY;
   bool mIsSmoothScroll;
@@ -1371,17 +1369,17 @@ protected:
                             nscoord aStart, nscoord aDestination);
 
   // Initializes the timing function in such a way that the current velocity is
   // preserved.
   void InitTimingFunction(nsSMILKeySpline& aTimingFunction,
                           nscoord aCurrentPos, nscoord aCurrentVelocity,
                           nscoord aDestination);
 
-  void InitDuration(nsIAtom *aProfile);
+  void InitDuration(nsIAtom *aOrigin);
 };
 
 nsPoint
 nsGfxScrollFrameInner::AsyncScroll::PositionAt(TimeStamp aTime) {
   double progressX = mTimingFunctionX.GetSplineValue(ProgressAt(aTime));
   double progressY = mTimingFunctionY.GetSplineValue(ProgressAt(aTime));
   return nsPoint((1 - progressX) * mStartPos.x + progressX * mDestination.x,
                  (1 - progressY) * mStartPos.y + progressY * mDestination.y);
@@ -1392,66 +1390,68 @@ nsGfxScrollFrameInner::AsyncScroll::Velo
   double timeProgress = ProgressAt(aTime);
   return nsSize(VelocityComponent(timeProgress, mTimingFunctionX,
                                   mStartPos.x, mDestination.x),
                 VelocityComponent(timeProgress, mTimingFunctionY,
                                   mStartPos.y, mDestination.y));
 }
 
 /*
- * Calculate/update mDuration, possibly dynamically according to events rate and smoothness profile.
+ * Calculate/update mDuration, possibly dynamically according to events rate and event origin.
  * (also maintain previous timestamps - which are only used here).
  */
 void
-nsGfxScrollFrameInner::AsyncScroll::InitDuration(nsIAtom *aProfile) {
-  // Read preferences only on first iteration or for a different profile.
-  if (mIsFirstIteration || aProfile != mProfile) {
-    mProfile = aProfile;
-    mProfileMinMS = mProfileMaxMS = 0;
-    mIsProfileSmoothnessEnabled = false;
+nsGfxScrollFrameInner::AsyncScroll::InitDuration(nsIAtom *aOrigin) {
+  if (!aOrigin){
+    aOrigin = nsGkAtoms::other;
+  }
+
+  // Read preferences only on first iteration or for a different event origin.
+  if (mIsFirstIteration || aOrigin != mOrigin) {
+    mOrigin = aOrigin;
+    mOriginMinMS = mOriginMaxMS = 0;
+    bool isOriginSmoothnessEnabled = false;
     mIntervalRatio = 1;
 
     // Default values for all preferences are defined in all.js
-    if (aProfile) {
-      static const PRInt32 kDefaultMinMS = 150, kDefaultMaxMS = 150;
-      static const bool kDefaultIsSmoothEnabled = true;
-
-      nsCAutoString profileName;
-      aProfile->ToUTF8String(profileName);
-      nsCAutoString prefBase = NS_LITERAL_CSTRING("general.smoothScroll.") + profileName;
-
-      mIsProfileSmoothnessEnabled = Preferences::GetBool(prefBase.get(), kDefaultIsSmoothEnabled);
-      if (mIsProfileSmoothnessEnabled) {
-        nsCAutoString prefMin = prefBase + NS_LITERAL_CSTRING(".durationMinMS");
-        nsCAutoString prefMax = prefBase + NS_LITERAL_CSTRING(".durationMaxMS");
-        mProfileMinMS = Preferences::GetInt(prefMin.get(), kDefaultMinMS);
-        mProfileMaxMS = Preferences::GetInt(prefMax.get(), kDefaultMaxMS);
-
-        static const PRInt32 kSmoothScrollMaxAllowedAnimationDurationMS = 10000;
-        mProfileMinMS = clamped(mProfileMinMS, 0,             kSmoothScrollMaxAllowedAnimationDurationMS);
-        mProfileMaxMS = clamped(mProfileMaxMS, mProfileMinMS, kSmoothScrollMaxAllowedAnimationDurationMS);
-      }
-
-      // Keep the animation duration longer than the average event intervals
-      //   (to "connect" consecutive scroll animations before the scroll comes to a stop).
-      static const double kDefaultDurationToIntervalRatio = 2; // Duplicated at all.js
-      mIntervalRatio = Preferences::GetInt("general.smoothScroll.durationToIntervalRatio",
-                                           kDefaultDurationToIntervalRatio * 100) / 100.0;
-
-      // Duration should be at least as long as the intervals -> ratio is at least 1
-      mIntervalRatio = NS_MAX(1.0, mIntervalRatio);
+    static const PRInt32 kDefaultMinMS = 150, kDefaultMaxMS = 150;
+    static const bool kDefaultIsSmoothEnabled = true;
+
+    nsCAutoString originName;
+    aOrigin->ToUTF8String(originName);
+    nsCAutoString prefBase = NS_LITERAL_CSTRING("general.smoothScroll.") + originName;
+
+    isOriginSmoothnessEnabled = Preferences::GetBool(prefBase.get(), kDefaultIsSmoothEnabled);
+    if (isOriginSmoothnessEnabled) {
+      nsCAutoString prefMin = prefBase + NS_LITERAL_CSTRING(".durationMinMS");
+      nsCAutoString prefMax = prefBase + NS_LITERAL_CSTRING(".durationMaxMS");
+      mOriginMinMS = Preferences::GetInt(prefMin.get(), kDefaultMinMS);
+      mOriginMaxMS = Preferences::GetInt(prefMax.get(), kDefaultMaxMS);
+
+      static const PRInt32 kSmoothScrollMaxAllowedAnimationDurationMS = 10000;
+      mOriginMaxMS = clamped(mOriginMaxMS, 0, kSmoothScrollMaxAllowedAnimationDurationMS);
+      mOriginMinMS = clamped(mOriginMinMS, 0, mOriginMaxMS);
     }
 
+    // Keep the animation duration longer than the average event intervals
+    //   (to "connect" consecutive scroll animations before the scroll comes to a stop).
+    static const double kDefaultDurationToIntervalRatio = 2; // Duplicated at all.js
+    mIntervalRatio = Preferences::GetInt("general.smoothScroll.durationToIntervalRatio",
+                                         kDefaultDurationToIntervalRatio * 100) / 100.0;
+
+    // Duration should be at least as long as the intervals -> ratio is at least 1
+    mIntervalRatio = NS_MAX(1.0, mIntervalRatio);
+
     if (mIsFirstIteration) {
       // Starting a new scroll (i.e. not when extending an existing scroll animation),
       //   create imaginary prev timestamps with maximum relevant intervals between them.
       mIsFirstIteration = false;
 
       // Longest relevant interval (which results in maximum duration)
-      TimeDuration maxDelta = TimeDuration::FromMilliseconds(mProfileMaxMS / mIntervalRatio);
+      TimeDuration maxDelta = TimeDuration::FromMilliseconds(mOriginMaxMS / mIntervalRatio);
       mPrevStartTime[0] = mStartTime         - maxDelta;
       mPrevStartTime[1] = mPrevStartTime[0]  - maxDelta;
       mPrevStartTime[2] = mPrevStartTime[1]  - maxDelta;
     }
   }
 
   // Average last 3 delta durations (rounding errors up to 2ms are negligible for us)
   PRInt32 eventsDeltaMs = (mStartTime - mPrevStartTime[2]).ToMilliseconds() / 3;
@@ -1459,31 +1459,31 @@ nsGfxScrollFrameInner::AsyncScroll::Init
   mPrevStartTime[1] = mPrevStartTime[0];
   mPrevStartTime[0] = mStartTime;
 
   // Modulate duration according to events rate (quicker events -> shorter durations).
   // The desired effect is to use longer duration when scrolling slowly, such that
   // it's easier to follow, but reduce the duration to make it feel more snappy when
   // scrolling quickly. To reduce fluctuations of the duration, we average event
   // intervals using the recent 4 timestamps (now + three prev -> 3 intervals).
-  PRInt32 durationMS = clamped<PRInt32>(eventsDeltaMs * mIntervalRatio, mProfileMinMS, mProfileMaxMS);
+  PRInt32 durationMS = clamped<PRInt32>(eventsDeltaMs * mIntervalRatio, mOriginMinMS, mOriginMaxMS);
 
   mDuration = TimeDuration::FromMilliseconds(durationMS);
 }
 
 void
 nsGfxScrollFrameInner::AsyncScroll::InitSmoothScroll(TimeStamp aTime,
                                                      nsPoint aCurrentPos,
                                                      nsSize aCurrentVelocity,
                                                      nsPoint aDestination,
-                                                     nsIAtom *aProfile) {
+                                                     nsIAtom *aOrigin) {
   mStartTime = aTime;
   mStartPos = aCurrentPos;
   mDestination = aDestination;
-  InitDuration(aProfile);
+  InitDuration(aOrigin);
   InitTimingFunction(mTimingFunctionX, mStartPos.x, aCurrentVelocity.width, aDestination.x);
   InitTimingFunction(mTimingFunctionY, mStartPos.y, aCurrentVelocity.height, aDestination.y);
 }
 
 
 nscoord
 nsGfxScrollFrameInner::AsyncScroll::VelocityComponent(double aTimeProgress,
                                                       nsSMILKeySpline& aTimingFunction,
@@ -1641,19 +1641,19 @@ nsGfxScrollFrameInner::AsyncScrollCallba
   }
 }
 
 /*
  * this method wraps calls to ScrollToImpl(), either in one shot or incrementally,
  *  based on the setting of the smoothness scroll pref
  */
 void
-nsGfxScrollFrameInner::ScrollToWithSmoothnessProfile(nsPoint aScrollPosition,
-                                                     nsIScrollableFrame::ScrollMode aMode,
-                                                     nsIAtom *aProfile)
+nsGfxScrollFrameInner::ScrollToWithOrigin(nsPoint aScrollPosition,
+                                          nsIScrollableFrame::ScrollMode aMode,
+                                          nsIAtom *aOrigin)
 {
   if (ShouldClampScrollPosition()) {
     mDestination = ClampScrollPosition(aScrollPosition);
   } else {
     mDestination = aScrollPosition;
   }
 
   if (aMode == nsIScrollableFrame::INSTANT) {
@@ -1695,17 +1695,17 @@ nsGfxScrollFrameInner::ScrollToWithSmoot
         AsyncScrollCallback, this, 0, nsITimer::TYPE_ONE_SHOT);
     }
   }
 
   mAsyncScroll->mIsSmoothScroll = isSmoothScroll;
 
   if (isSmoothScroll) {
     mAsyncScroll->InitSmoothScroll(now, currentPosition, currentVelocity,
-                                   mDestination, aProfile);
+                                   mDestination, aOrigin);
   }
 }
 
 // We can't use nsContainerFrame::PositionChildViews here because
 // we don't want to invalidate views that have moved.
 static void AdjustViews(nsIFrame* aFrame)
 {
   nsIView* view = aFrame->GetView();
@@ -2358,17 +2358,17 @@ nsGfxScrollFrameInner::ScrollBy(nsIntPoi
   }
   default:
     NS_ERROR("Invalid scroll mode");
     return;
   }
 
   nsPoint newPos = mDestination +
     nsPoint(aDelta.x*deltaMultiplier.width, aDelta.y*deltaMultiplier.height);
-  ScrollToWithSmoothnessProfile(newPos, aMode, aOrigin);
+  ScrollToWithOrigin(newPos, aMode, aOrigin);
 
   if (aOverflow) {
     nsPoint clampAmount = mDestination - newPos;
     float appUnitsPerDevPixel = mOuter->PresContext()->AppUnitsPerDevPixel();
     *aOverflow = nsIntPoint(
         NSAppUnitsToIntPixels(NS_ABS(clampAmount.x), appUnitsPerDevPixel),
         NSAppUnitsToIntPixels(NS_ABS(clampAmount.y), appUnitsPerDevPixel));
   }
@@ -2774,19 +2774,19 @@ void nsGfxScrollFrameInner::CurPosAttrib
   bool isSmooth = aContent->HasAttr(kNameSpaceID_None, nsGkAtoms::smooth);
   if (isSmooth) {
     // Make sure an attribute-setting callback occurs even if the view
     // didn't actually move yet.  We need to make sure other listeners
     // see that the scroll position is not (yet) what they thought it
     // was.
     UpdateScrollbarPosition();
   }
-  ScrollToWithSmoothnessProfile(dest,
-                                isSmooth ? nsIScrollableFrame::SMOOTH : nsIScrollableFrame::INSTANT,
-                                nsGkAtoms::scrollbars);
+  ScrollToWithOrigin(dest,
+                     isSmooth ? nsIScrollableFrame::SMOOTH : nsIScrollableFrame::INSTANT,
+                     nsGkAtoms::scrollbars);
 }
 
 /* ============= Scroll events ========== */
 
 NS_IMETHODIMP
 nsGfxScrollFrameInner::ScrollEvent::Run()
 {
   if (mInner)
--- a/layout/generic/nsGfxScrollFrame.h
+++ b/layout/generic/nsGfxScrollFrame.h
@@ -176,17 +176,17 @@ public:
     return pt;
   }
   nsRect GetScrollRange() const;
 
   nsPoint RestrictToDevPixels(const nsPoint& aPt, nsIntPoint* aPtDevPx, bool aShouldClamp) const;
   nsPoint ClampScrollPosition(const nsPoint& aPt) const;
   static void AsyncScrollCallback(nsITimer *aTimer, void* anInstance);
   void ScrollTo(nsPoint aScrollPosition, nsIScrollableFrame::ScrollMode aMode) {
-    ScrollToWithSmoothnessProfile(aScrollPosition, aMode, nsGkAtoms::other);
+    ScrollToWithOrigin(aScrollPosition, aMode, nsGkAtoms::other);
   };
   void ScrollToImpl(nsPoint aScrollPosition);
   void ScrollVisual(nsPoint aOldScrolledFramePosition);
   void ScrollBy(nsIntPoint aDelta, nsIScrollableFrame::ScrollUnit aUnit,
                 nsIScrollableFrame::ScrollMode aMode, nsIntPoint* aOverflow, nsIAtom *aOrigin = nsnull);
   void ScrollToRestoredPosition();
   nsSize GetLineScrollAmount() const;
   nsSize GetPageScrollAmount() const;
@@ -339,20 +339,19 @@ public:
   // If true, the resizer is collapsed and not displayed
   bool mCollapsedResizer:1;
 
   // If true, the layer should always be active because we always build a layer.
   // Used for asynchronous scrolling.
   bool mShouldBuildLayer:1;
 
 protected:
-  void ScrollToWithSmoothnessProfile(nsPoint aScrollPosition,
-                                     nsIScrollableFrame::ScrollMode aMode,
-                                     nsIAtom *aProfile); // nsnull indicates no smooth scroll
-
+  void ScrollToWithOrigin(nsPoint aScrollPosition,
+                          nsIScrollableFrame::ScrollMode aMode,
+                          nsIAtom *aOrigin); // nsnull indicates "other" origin
 };
 
 /**
  * The scroll frame creates and manages the scrolling view
  *
  * It only supports having a single child frame that typically is an area
  * frame, but doesn't have to be. The child frame must have a view, though
  *
--- a/modules/libpref/src/init/all.js
+++ b/modules/libpref/src/init/all.js
@@ -1327,17 +1327,17 @@ pref("mousewheel.horizscroll.withaltkey.
 pref("mousewheel.horizscroll.withaltkey.numlines",-1);
 pref("mousewheel.horizscroll.withaltkey.sysnumlines",false);
 pref("mousewheel.horizscroll.withmetakey.action",0);
 pref("mousewheel.horizscroll.withmetakey.numlines",1);
 pref("mousewheel.horizscroll.withmetakey.sysnumlines",true);
 
 // These define the smooth scroll behavior (min ms, max ms) for different triggers
 // Some triggers:
-// Pixels: Discrete mouse wheel events, Synaptics touchpads on windows (generate wheel events)
+// mouseWheel: Discrete mouse wheel events, Synaptics touchpads on windows (generate wheel events)
 // Lines:  Up/Down/Left/Right KB arrows
 // Pages:  Page up/down, Space
 // Scrollbars: Clicking scrollbars arrows, clicking scrollbars tracks
 // Note: Currently OS X trackpad and magic mouse don't use our smooth scrolling
 // Note: These are relevant only when "general.smoothScroll" is enabled
 pref("general.smoothScroll.mouseWheel.durationMinMS", 200);
 pref("general.smoothScroll.mouseWheel.durationMaxMS", 400);
 pref("general.smoothScroll.pixels.durationMinMS", 150);
@@ -1345,16 +1345,17 @@ pref("general.smoothScroll.pixels.durati
 pref("general.smoothScroll.lines.durationMinMS", 150);
 pref("general.smoothScroll.lines.durationMaxMS", 150);
 pref("general.smoothScroll.pages.durationMinMS", 150);
 pref("general.smoothScroll.pages.durationMaxMS", 150);
 pref("general.smoothScroll.scrollbars.durationMinMS", 150);
 pref("general.smoothScroll.scrollbars.durationMaxMS", 150);
 pref("general.smoothScroll.other.durationMinMS", 150);
 pref("general.smoothScroll.other.durationMaxMS", 150);
+// Enable disable smooth scrolling for different triggers (when "general.smoothScroll" is enabled)
 pref("general.smoothScroll.mouseWheel", true);
 pref("general.smoothScroll.pixels", true);
 pref("general.smoothScroll.lines", true);
 pref("general.smoothScroll.pages", true);
 pref("general.smoothScroll.scrollbars", true);
 pref("general.smoothScroll.other", true);
 // To connect consecutive scroll events into a continuous flow, the animation's duration
 // should be longer than scroll events intervals (or else the scroll will stop