From 2b0ef682550000045126e393ccffe7f76f5fcb87 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 7 Apr 2014 13:51:25 -0600 Subject: [PATCH] Blink cursors always. Still need to pause blinking when moving. --- spec/editor-component-spec.coffee | 18 ++++++++++++++++++ spec/spec-helper.coffee | 11 +++++++++++ src/cursor-component.coffee | 5 ++++- src/editor-component.coffee | 12 ++++++++++-- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/spec/editor-component-spec.coffee b/spec/editor-component-spec.coffee index 3aa509754..01a92bcd9 100644 --- a/spec/editor-component-spec.coffee +++ b/spec/editor-component-spec.coffee @@ -117,6 +117,24 @@ describe "EditorComponent", -> expect(cursorRect.left).toBe rangeRect.left expect(cursorRect.width).toBe rangeRect.width + it "blinks cursors", -> + editor.addCursorAtScreenPosition([1, 0]) + [cursorNode1, cursorNode2] = node.querySelectorAll('.cursor') + expect(cursorNode1.classList.contains('blink-off')).toBe false + expect(cursorNode2.classList.contains('blink-off')).toBe false + + advanceClock(component.props.cursorBlinkPeriod / 2) + expect(cursorNode1.classList.contains('blink-off')).toBe true + expect(cursorNode2.classList.contains('blink-off')).toBe true + + advanceClock(component.props.cursorBlinkPeriod / 2) + expect(cursorNode1.classList.contains('blink-off')).toBe false + expect(cursorNode2.classList.contains('blink-off')).toBe false + + advanceClock(component.props.cursorBlinkPeriod / 2) + expect(cursorNode1.classList.contains('blink-off')).toBe true + expect(cursorNode2.classList.contains('blink-off')).toBe true + describe "selection rendering", -> it "renders 1 region for 1-line selections", -> # 1-line selection diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 5574abae5..13b4449d4 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -92,6 +92,8 @@ beforeEach -> spyOn(WorkspaceView.prototype, 'setTitle').andCallFake (@title) -> spyOn(window, "setTimeout").andCallFake window.fakeSetTimeout spyOn(window, "clearTimeout").andCallFake window.fakeClearTimeout + spyOn(window, "setInterval").andCallFake window.fakeSetInterval + spyOn(window, "clearInterval").andCallFake window.fakeClearInterval spyOn(pathwatcher.File.prototype, "detectResurrectionAfterDelay").andCallFake -> @detectResurrection() spyOn(Editor.prototype, "shouldPromptToSave").andReturn false @@ -243,6 +245,15 @@ window.fakeSetTimeout = (callback, ms) -> window.fakeClearTimeout = (idToClear) -> window.timeouts = window.timeouts.filter ([id]) -> id != idToClear +window.fakeSetInterval = (callback, ms) -> + action = -> + callback() + window.fakeSetTimeout(action, ms) + window.fakeSetTimeout(action, ms) + +window.fakeClearInterval = (idToClear) -> + window.fakeClearTimeout(idToClear) + window.advanceClock = (delta=1) -> window.now += delta callbacks = [] diff --git a/src/cursor-component.coffee b/src/cursor-component.coffee index 08344a9fe..515341e07 100644 --- a/src/cursor-component.coffee +++ b/src/cursor-component.coffee @@ -8,7 +8,10 @@ CursorComponent = React.createClass render: -> {top, left, height, width} = @props.cursor.getPixelRect() - div className: 'cursor', style: {top, left, height, width} + className = 'cursor' + className += ' blink-off' if @props.blinkOff + + div className: className, style: {top, left, height, width} componentDidMount: -> @subscribe @props.cursor, 'moved', => @forceUpdate() diff --git a/src/editor-component.coffee b/src/editor-component.coffee index 06dbcaf18..387e24724 100644 --- a/src/editor-component.coffee +++ b/src/editor-component.coffee @@ -63,9 +63,10 @@ EditorCompont = React.createClass renderCursors: -> {editor} = @props + {blinkCursorsOff} = @state for selection in editor.getSelections() when editor.selectionIntersectsVisibleRowRange(selection) - CursorComponent(cursor: selection.cursor) + CursorComponent(cursor: selection.cursor, blinkOff: blinkCursorsOff) renderUnderlayer: -> {editor} = @props @@ -83,7 +84,7 @@ EditorCompont = React.createClass getInitialState: -> {} - getDefaultProps: -> updateSync: true + getDefaultProps: -> cursorBlinkPeriod: 800 componentDidMount: -> @measuredLines = new WeakSet @@ -92,12 +93,14 @@ EditorCompont = React.createClass @listenForCustomEvents() @observeEditor() @observeConfig() + @blinkCursors() @updateAllDimensions() @props.editor.setVisible(true) componentWillUnmount: -> @getDOMNode().removeEventListener 'mousewheel', @onMouseWheel + clearInterval(@cursorBlinkIntervalHandle) componentDidUpdate: -> @updateVerticalScrollbar() @@ -233,6 +236,11 @@ EditorCompont = React.createClass observeConfig: -> @subscribe atom.config.observe 'editor.fontFamily', @setFontFamily + blinkCursors: -> + @cursorBlinkIntervalHandle = setInterval(@toggleCursorBlink, @props.cursorBlinkPeriod / 2) + + toggleCursorBlink: -> @setState(blinkCursorsOff: not @state.blinkCursorsOff) + setFontSize: (fontSize) -> @clearScopedCharWidths() @setState({fontSize})