Files
atom/src/cursors-component.coffee
Nathan Sobo 388763e7cd Wait to measure characters if editor is hidden
Also, when characters *are* measured, request a display update
2014-06-19 04:19:51 -06:00

61 lines
1.9 KiB
CoffeeScript

React = require 'react-atom-fork'
{div} = require 'reactionary-atom-fork'
{debounce, toArray, isEqualForProperties, isEqual} = require 'underscore-plus'
SubscriberMixin = require './subscriber-mixin'
CursorComponent = require './cursor-component'
module.exports =
CursorsComponent = React.createClass
displayName: 'CursorsComponent'
mixins: [SubscriberMixin]
cursorBlinkIntervalHandle: null
render: ->
{editor, cursorScreenRanges, scrollTop, scrollLeft, defaultCharWidth} = @props
{blinkOff} = @state
className = 'cursors'
className += ' blink-off' if blinkOff
div {className},
if @isMounted()
for key, screenRange of cursorScreenRanges
CursorComponent({key, editor, screenRange, scrollTop, scrollLeft, defaultCharWidth})
getInitialState: ->
blinkOff: false
componentDidMount: ->
@startBlinkingCursors()
componentWillUnmount: ->
@stopBlinkingCursors()
shouldComponentUpdate: (newProps, newState) ->
not newState.blinkOff is @state.blinkOff or
not isEqualForProperties(newProps, @props,
'cursorScreenRanges', 'scrollTop', 'scrollLeft', 'lineHeightInPixels',
'defaultCharWidth', 'scopedCharacterWidthsChangeCount'
)
componentWillUpdate: (newProps) ->
@pauseCursorBlinking() if @props.cursorScreenRanges and not isEqual(newProps.cursorScreenRanges, @props.cursorScreenRanges)
startBlinkingCursors: ->
@toggleCursorBlinkHandle = setInterval(@toggleCursorBlink, @props.cursorBlinkPeriod / 2) if @isMounted()
startBlinkingCursorsAfterDelay: null # Created lazily
stopBlinkingCursors: ->
clearInterval(@toggleCursorBlinkHandle)
toggleCursorBlink: ->
@setState(blinkOff: not @state.blinkOff)
pauseCursorBlinking: ->
@state.blinkOff = false
@stopBlinkingCursors()
@startBlinkingCursorsAfterDelay ?= debounce(@startBlinkingCursors, @props.cursorBlinkResumeDelay)
@startBlinkingCursorsAfterDelay()