Update scroll position directly on mousewheel events

Previously, we were updating the scrollbars and relying on an async
scroll events to fire. But updating the scrollbars is expensive, so this
updates the model directly when the next animation frame fires instead.
This commit is contained in:
Nathan Sobo
2014-05-09 12:16:06 -06:00
parent 308960309d
commit 0ae8765a8a

View File

@@ -24,6 +24,8 @@ EditorComponent = React.createClass
gutterWidth: 0
refreshingScrollbars: false
measuringScrollbars: true
pendingVerticalScrollDelta: 0
pendingHorizontalScrollDelta: 0
render: ->
{focused, fontSize, lineHeight, fontFamily, showIndentGuide} = @state
@@ -311,14 +313,24 @@ EditorComponent = React.createClass
@pendingScrollLeft = null
onMouseWheel: (event) ->
event.preventDefault()
animationFramePending = @pendingHorizontalScrollDelta isnt 0 or @pendingVerticalScrollDelta isnt 0
# Only scroll in one direction at a time
{wheelDeltaX, wheelDeltaY} = event
if Math.abs(wheelDeltaX) > Math.abs(wheelDeltaY)
@refs.horizontalScrollbar.getDOMNode().scrollLeft -= wheelDeltaX
@pendingHorizontalScrollDelta -= wheelDeltaX
else
@refs.verticalScrollbar.getDOMNode().scrollTop -= wheelDeltaY
@pendingVerticalScrollDelta -= wheelDeltaY
event.preventDefault()
unless animationFramePending
requestAnimationFrame =>
{editor} = @props
editor.setScrollTop(editor.getScrollTop() + @pendingVerticalScrollDelta)
editor.setScrollLeft(editor.getScrollLeft() + @pendingHorizontalScrollDelta)
@pendingVerticalScrollDelta = 0
@pendingHorizontalScrollDelta = 0
onStylesheetsChanged: (stylesheet) ->
@refreshScrollbars() if @containsScrollbarSelector(stylesheet)