Hide cursors with non-empty selection if showCursorsOnSelection is false

Also, remove some barely used public APIs around cursor visibility that
don't make much sense and are not ideal for performance. We don't want
to subscribe to the visibility of each cursor.
This commit is contained in:
Nathan Sobo
2017-04-10 16:10:00 -06:00
committed by Antonio Scandurra
parent bc8b548d1a
commit 8aae3ab1ae
5 changed files with 36 additions and 42 deletions

View File

@@ -254,6 +254,39 @@ describe('TextEditorComponent', () => {
expect(cursorNodes.length).toBe(0)
})
it('hides cursors with non-empty selections when showCursorOnSelection is false', async () => {
const {component, element, editor} = buildComponent()
editor.setSelectedScreenRanges([
[[0, 0], [0, 3]],
[[1, 0], [1, 0]]
])
await component.getNextUpdatePromise()
{
const cursorNodes = Array.from(element.querySelectorAll('.cursor'))
expect(cursorNodes.length).toBe(2)
verifyCursorPosition(component, cursorNodes[0], 0, 3)
verifyCursorPosition(component, cursorNodes[1], 1, 0)
}
editor.update({showCursorOnSelection: false})
await component.getNextUpdatePromise()
{
const cursorNodes = Array.from(element.querySelectorAll('.cursor'))
expect(cursorNodes.length).toBe(1)
verifyCursorPosition(component, cursorNodes[0], 1, 0)
}
editor.setSelectedScreenRanges([
[[0, 0], [0, 3]],
[[1, 0], [1, 4]]
])
await component.getNextUpdatePromise()
{
const cursorNodes = Array.from(element.querySelectorAll('.cursor'))
expect(cursorNodes.length).toBe(0)
}
})
it('blinks cursors when the editor is focused and the cursors are not moving', async () => {
assertDocumentFocused()
const {component, element, editor} = buildComponent()

View File

@@ -12,20 +12,14 @@ EmptyLineRegExp = /(\r\n[\t ]*\r\n)|(\n[\t ]*\n)/g
# of a {DisplayMarker}.
module.exports =
class Cursor extends Model
showCursorOnSelection: null
screenPosition: null
bufferPosition: null
goalColumn: null
visible: true
# Instantiated by a {TextEditor}
constructor: ({@editor, @marker, @showCursorOnSelection, id}) ->
constructor: ({@editor, @marker, id}) ->
@emitter = new Emitter
@showCursorOnSelection ?= true
@assignId(id)
@updateVisibility()
destroy: ->
@marker.destroy()
@@ -57,15 +51,6 @@ class Cursor extends Model
onDidDestroy: (callback) ->
@emitter.on 'did-destroy', callback
# Public: Calls your `callback` when the cursor's visibility has changed
#
# * `callback` {Function}
# * `visibility` {Boolean}
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidChangeVisibility: (callback) ->
@emitter.on 'did-change-visibility', callback
###
Section: Managing Cursor Position
###
@@ -568,21 +553,6 @@ class Cursor extends Model
Section: Visibility
###
# Public: Sets whether the cursor is visible.
setVisible: (visible) ->
if @visible isnt visible
@visible = visible
@emitter.emit 'did-change-visibility', @visible
# Public: Returns the visibility of the cursor.
isVisible: -> @visible
updateVisibility: ->
if @showCursorOnSelection
@setVisible(true)
else
@setVisible(@marker.getBufferRange().isEmpty())
###
Section: Comparing to another cursor
###
@@ -599,9 +569,6 @@ class Cursor extends Model
Section: Utilities
###
# Public: Prevents this cursor from causing scrolling.
clearAutoscroll: ->
# Public: Deselects the current selection.
clearSelection: (options) ->
@selection?.clear(options)
@@ -651,11 +618,6 @@ class Cursor extends Model
Section: Private
###
setShowCursorOnSelection: (value) ->
if value isnt @showCursorOnSelection
@showCursorOnSelection = value
@updateVisibility()
getNonWordCharacters: ->
@editor.getNonWordCharacters(@getScopeDescriptor().getScopesArray())

View File

@@ -769,8 +769,6 @@ class Selection extends Model
{oldHeadScreenPosition, oldTailScreenPosition, newHeadScreenPosition} = e
{textChanged} = e
@cursor.updateVisibility()
unless oldHeadScreenPosition.isEqual(newHeadScreenPosition)
@cursor.goalColumn = null
cursorMovedEvent = {

View File

@@ -920,6 +920,7 @@ class TextEditorComponent {
addCursorDecorationToMeasure (marker, screenRange, reversed) {
const {model} = this.props
if (!model.getShowCursorOnSelection() && !screenRange.isEmpty()) return
const isLastCursor = model.getLastCursor().getMarker() === marker
const screenPosition = reversed ? screenRange.start : screenRange.end
const {row, column} = screenPosition

View File

@@ -379,7 +379,7 @@ class TextEditor extends Model
when 'showCursorOnSelection'
if value isnt @showCursorOnSelection
@showCursorOnSelection = value
cursor.setShowCursorOnSelection(value) for cursor in @getCursors()
@component?.scheduleUpdate()
else
if param isnt 'ref' and param isnt 'key'