Files
atom/src/app/cursor-view.coffee
Nathan Sobo f4f3002e6d Rename Cursor and CursorView events to passive-voice scheme
Any events emitted from DOM nodes should be prefixed with an identifier
for the node that emits them. This eliminates the possibility of ambiguity
when the events bubble up the DOM away from their emitter.
2013-01-02 13:48:11 -07:00

98 lines
2.3 KiB
CoffeeScript

{View} = require 'space-pen'
Anchor = require 'anchor'
Point = require 'point'
Range = require 'range'
_ = require 'underscore'
module.exports =
class CursorView extends View
@content: ->
@pre class: 'cursor idle', => @raw ' '
blinkPeriod: 800
editor: null
visible: true
needsUpdate: true
needsRemoval: false
shouldPauseBlinking: false
initialize: (@cursor, @editor) ->
@cursor.on 'moved.cursor-view', ({ autoscroll }) =>
@needsUpdate = true
@shouldPauseBlinking = true
@editor.requestDisplayUpdate()
@cursor.on 'visibility-changed.cursor-view', (visible) =>
@needsUpdate = true
@editor.requestDisplayUpdate()
@cursor.on 'destroyed.cursor-view', =>
@needsRemoval = true
@editor.requestDisplayUpdate()
remove: ->
@editor.removeCursorView(this)
@cursor.off('.cursor-view')
super
updateDisplay: ->
screenPosition = @getScreenPosition()
pixelPosition = @getPixelPosition()
unless _.isEqual(@lastPixelPosition, pixelPosition)
changedPosition = true
@css(pixelPosition)
@trigger 'cursor:moved'
if @shouldPauseBlinking
@resetBlinking()
else if !@startBlinkingTimeout
@startBlinking()
@setVisible(@cursor.isVisible() and not @editor.isFoldedAtScreenRow(screenPosition.row))
needsAutoscroll: ->
@cursor.needsAutoscroll
autoscrolled: ->
@cursor.autoscrolled()
getPixelPosition: ->
@editor.pixelPositionForScreenPosition(@getScreenPosition())
setVisible: (visible) ->
unless @visible == visible
@visible = visible
@toggle(@visible)
stopBlinking: ->
clearInterval(@blinkInterval) if @blinkInterval
@blinkInterval = null
this[0].classList.remove('blink-off')
startBlinking: ->
return if @blinkInterval?
blink = => @toggleClass('blink-off')
@blinkInterval = setInterval(blink, @blinkPeriod / 2)
resetBlinking: ->
@stopBlinking()
@startBlinking()
getBufferPosition: ->
@cursor.getBufferPosition()
getScreenPosition: ->
@cursor.getScreenPosition()
removeIdleClassTemporarily: ->
@removeClass 'idle'
window.clearTimeout(@idleTimeout) if @idleTimeout
@idleTimeout = window.setTimeout (=> @addClass 'idle'), 200
resetCursorAnimation: ->
window.clearTimeout(@idleTimeout) if @idleTimeout
@removeClass 'idle'
_.defer => @addClass 'idle'