 
     shouldStopScrolling = (progress) ->
       if activationId == time
-        # This is the most recent animator and the key is still pressed, so keep going.
+        # This is the most recently-activated animator and we haven't yet seen its keyup event, so keep going.
         false
       else
         duration <= progress

  # Return the amount by which the scroll position has changed.
  element[axisName] - before

# Scroll by a relative amount (a number) in some direction, possibly smoothly.
doScrollBy = do ->
  time = 0
  lastActivationId = -1
  keyupHandler = null

  # When the keyboard is repeating and then the key is released, sometimes the last key event is delivered
  # *after* the keyup event.  This could cause indefinite scrolling with no key pressed, because no further
  # keyup event is delivered (so we behave as if the key is still pressed).  To get around this, we briefly
  # hold a lock; this prevents a new smooth scrolling event from starting too soon after the end of the
  # previous one.
  smoothScrollLock = false

  (element, direction, amount, wantSmooth) ->
    axisName = scrollProperties[direction].axisName

    unless wantSmooth and settings.get "smoothScroll"
      return performScroll element, axisName, amount

    if lastActivationId == time or smoothScrollLock
      # This is a keyboard repeat (and the most-recently activated animator -- which is still active -- will
      # keep going), or it's too soon to begin smooth scrolling again.
      return

    if not keyupHandler
      keyupHandler = root.handlerStack.push keyup: ->
        time += 1
        setTimeout((-> smoothScrollLock = false), 50)
        smoothScrollLock = true # Hereby also returning true, so allowing further propagation of the event.

    lastActivationId = activationId = ++time

    duration = 100 # Duration in ms.
    fudgeFactor = 25

          # One final call of performScroll to check the visibility of the activated element.
          performScroll(element, axisName, 0, true)
          window.cancelAnimationFrame(animatorId)
      else
        animatorId = window.requestAnimationFrame(animate)

