Files
atom/src/app/cursor-view.coffee
Nathan Sobo a1cff240b6 Use 'blink-off' css class to blink the cursor when editor is focused
Rather than managing the visibility directly, a class makes it easy. The cursors will always be toggling this class on and off in the background, but only on the focused editor will it actually have an effect.
2012-11-14 21:43:06 -07:00

95 lines
2.4 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
needsAutoscroll: true
needsRemoval: false
shouldPauseBlinking: false
initialize: (@cursor, @editor) ->
@cursor.on 'change-screen-position.cursor-view', (screenPosition, { autoscroll }) =>
@needsUpdate = true
@shouldPauseBlinking = true
@needsAutoscroll = (autoscroll ? true) and @cursor?.isLastCursor()
@editor.requestDisplayUpdate()
@cursor.on 'change-visibility.cursor-view', (visible) =>
@needsUpdate = true
@needsAutoscroll = visible and @cursor.isLastCursor()
@editor.requestDisplayUpdate()
@cursor.on 'destroy.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-move'
if @shouldPauseBlinking
@resetBlinking()
else if !@startBlinkingTimeout
@startBlinking()
@setVisible(@cursor.isVisible() and not @editor.isFoldedAtScreenRow(screenPosition.row))
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'