Make cursor blink by changing CSS visibility with setInterval

This is actually more efficient than the CSS animation we were doing previously, because it doesn't force the cursor to be sampled at 60 FPS for something that changes around twice a second.
This commit is contained in:
Nathan Sobo
2012-11-14 20:43:34 -07:00
parent 5dea68bb4c
commit 185ca22488
3 changed files with 35 additions and 8 deletions

View File

@@ -935,11 +935,11 @@ describe "Editor", ->
editor.setSelectedBufferRange([[0, 0], [3, 0]])
expect(editor.getSelection().isEmpty()).toBeFalsy()
expect(cursorView).not.toBeVisible()
expect(cursorView.css('visibility')).toBe 'hidden'
editor.setCursorBufferPosition([1, 3])
expect(editor.getSelection().isEmpty()).toBeTruthy()
expect(cursorView).toBeVisible()
expect(cursorView.css('visibility')).toBe 'visible'
describe "auto-scrolling", ->
it "only auto-scrolls when the last cursor is moved", ->
@@ -1794,10 +1794,10 @@ describe "Editor", ->
editor.setCursorScreenPosition([2,0])
expect(editor.lineElementForScreenRow(2)).toMatchSelector('.fold.selected')
expect(editor.find('.cursor').css('display')).toBe 'none'
expect(editor.find('.cursor').css('visibility')).toBe 'hidden'
editor.setCursorScreenPosition([3,0])
expect(editor.find('.cursor').css('display')).toBe 'block'
expect(editor.find('.cursor').css('visibility')).toBe 'visible'
describe "when a selected fold is scrolled into view (and the fold line was not previously rendered)", ->
it "renders the fold's line element with the 'selected' class", ->

View File

@@ -9,16 +9,19 @@ 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, { bufferChange, autoscroll }) =>
@cursor.on 'change-screen-position.cursor-view', (screenPosition, { autoscroll }) =>
@needsUpdate = true
@shouldPauseBlinking = true
@needsAutoscroll = (autoscroll ? true) and @cursor?.isLastCursor()
@editor.requestDisplayUpdate()
@@ -43,9 +46,13 @@ class CursorView extends View
unless _.isEqual(@lastPixelPosition, pixelPosition)
changedPosition = true
@css(pixelPosition)
# @removeIdleClassTemporarily() unless bufferChange
@trigger 'cursor-move'
if @shouldPauseBlinking
@resetBlinking()
else if !@startBlinkingTimeout
@startBlinking()
@setVisible(@cursor.isVisible() and not @editor.isFoldedAtScreenRow(screenPosition.row))
getPixelPosition: ->
@@ -54,7 +61,27 @@ class CursorView extends View
setVisible: (visible) ->
unless @visible == visible
@visible = visible
@toggle(@visible)
if @visible
@css('visibility', '')
else
@css('visibility', 'hidden')
toggleVisible: ->
@setVisible(not @visible) if @cursor.isVisible
stopBlinking: ->
clearInterval(@blinkInterval) if @blinkInterval
@blinkInterval = null
@setVisible(true) if @cursor.isVisible
startBlinking: ->
return if @blinkInterval?
blink = => @toggleVisible()
@blinkInterval = setInterval(blink, @blinkPeriod / 2)
resetBlinking: ->
@stopBlinking()
@startBlinking()
getBufferPosition: ->
@cursor.getBufferPosition()

View File

@@ -768,7 +768,7 @@ class Editor extends View
syncCursorAnimations: ->
for cursorView in @getCursorViews()
do (cursorView) -> cursorView.resetCursorAnimation()
do (cursorView) -> cursorView.resetBlinking()
autoscroll: (options={}) ->
for cursorView in @getCursorViews() when cursorView.needsAutoscroll