Skip to content

Commit

Permalink
Pause cursor blink as part of the overall editor update
Browse files Browse the repository at this point in the history
This ensures we don't perform two updates of the cursors component when
cursors move as part of a larger change, such as typing text.
  • Loading branch information
Nathan Sobo committed Apr 22, 2014
1 parent ae9f79b commit 3a42346
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
13 changes: 6 additions & 7 deletions src/cursors-component.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,23 @@ CursorsComponent = React.createClass

componentDidMount: ->
{editor} = @props
@subscribe editor, 'cursors-moved', @pauseCursorBlinking
@startBlinkingCursors()

componentWillUnmount: ->
@stopBlinkingCursors()
clearInterval(@cursorBlinkIntervalHandle)

componentWillUpdate: ({cursorsMoved}) ->
@pauseCursorBlinking() if cursorsMoved

startBlinkingCursors: ->
@cursorBlinkIntervalHandle = setInterval(@toggleCursorBlink, @props.cursorBlinkPeriod / 2)

startBlinkingCursorsAfterDelay: null # Created lazily

stopBlinkingCursors: ->
clearInterval(@cursorBlinkIntervalHandle)
@setState(blinkCursorsOff: false)

toggleCursorBlink: -> @setState(blinkCursorsOff: not @state.blinkCursorsOff)

pauseCursorBlinking: ->
@stopBlinkingCursors()
@state.blinkCursorsOff = false
clearInterval(@cursorBlinkIntervalHandle)
@startBlinkingCursorsAfterDelay ?= debounce(@startBlinkingCursors, @props.cursorBlinkResumeDelay)
@startBlinkingCursorsAfterDelay()
14 changes: 11 additions & 3 deletions src/editor-component.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ EditorComponent = React.createClass
selectOnMouseMove: false
batchingUpdates: false
updateRequested: false
cursorsMoved: false

render: ->
{focused, fontSize, lineHeight, fontFamily, showIndentGuide} = @state
Expand All @@ -31,8 +32,10 @@ EditorComponent = React.createClass
GutterComponent({editor, visibleRowRange, scrollTop, @pendingChanges})

EditorScrollViewComponent {
ref: 'scrollView', editor, visibleRowRange, @pendingChanges, @onInputFocused, @onInputBlurred
cursorBlinkPeriod, cursorBlinkResumeDelay, showIndentGuide, fontSize, fontFamily, lineHeight
ref: 'scrollView', editor, visibleRowRange, @pendingChanges,
showIndentGuide, fontSize, fontFamily, lineHeight,
@cursorsMoved, cursorBlinkPeriod, cursorBlinkResumeDelay,
@onInputFocused, @onInputBlurred
}

ScrollbarComponent
Expand Down Expand Up @@ -74,13 +77,15 @@ EditorComponent = React.createClass

componentDidUpdate: ->
@pendingChanges.length = 0
@cursorsMoved = false
@props.parentView.trigger 'editor:display-updated'

observeEditor: ->
{editor} = @props
@subscribe editor, 'batched-updates-started', @onBatchedUpdatesStarted
@subscribe editor, 'batched-updates-ended', @onBatchedUpdatesEnded
@subscribe editor, 'screen-lines-changed', @onScreenLinesChanged
@subscribe editor, 'cursors-moved', @onCursorsMoved
@subscribe editor, 'selection-screen-range-changed', @requestUpdate
@subscribe editor, 'selection-added', @onSelectionAdded
@subscribe editor, 'selection-removed', @onSelectionAdded
Expand Down Expand Up @@ -272,7 +277,7 @@ EditorComponent = React.createClass
@updateRequested = false
@batchingUpdates = false
if updateRequested
@forceUpdate()
@requestUpdate()

onScreenLinesChanged: (change) ->
{editor} = @props
Expand All @@ -287,6 +292,9 @@ EditorComponent = React.createClass
{editor} = @props
@requestUpdate() if editor.selectionIntersectsVisibleRowRange(selection)

onCursorsMoved: ->
@cursorsMoved = true

getVisibleRowRange: ->
visibleRowRange = @props.editor.getVisibleRowRange()
if @visibleRowOverrides?
Expand Down
4 changes: 2 additions & 2 deletions src/editor-scroll-view-component.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ EditorScrollViewComponent = React.createClass

render: ->
{editor, fontSize, fontFamily, lineHeight, showIndentGuide, cursorBlinkPeriod, cursorBlinkResumeDelay} = @props
{visibleRowRange, pendingChanges, onInputFocused, onInputBlurred} = @props
{visibleRowRange, pendingChanges, cursorsMoved, onInputFocused, onInputBlurred} = @props
contentStyle =
height: editor.getScrollHeight()
WebkitTransform: "translate(#{-editor.getScrollLeft()}px, #{-editor.getScrollTop()}px)"
Expand All @@ -27,7 +27,7 @@ EditorScrollViewComponent = React.createClass
onBlur: onInputBlurred

div className: 'scroll-view-content', style: contentStyle, onMouseDown: @onMouseDown,
CursorsComponent({editor, cursorBlinkPeriod, cursorBlinkResumeDelay})
CursorsComponent({editor, cursorsMoved, cursorBlinkPeriod, cursorBlinkResumeDelay})
LinesComponent({ref: 'lines', editor, fontSize, fontFamily, lineHeight, visibleRowRange, pendingChanges, showIndentGuide})
div className: 'underlayer',
SelectionsComponent({editor})
Expand Down
6 changes: 3 additions & 3 deletions src/editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1474,9 +1474,9 @@ class Editor extends Model
@movingCursors = true
@batchUpdates =>
fn(cursor) for cursor in @getCursors()
@mergeCursors()
@movingCursors = false
@emit 'cursors-moved'
@mergeCursors()
@movingCursors = false
@emit 'cursors-moved'

cursorMoved: (event) ->
@emit 'cursor-moved', event
Expand Down

0 comments on commit 3a42346

Please sign in to comment.