Only one visible cursor causes the editor to scroll

This commit is contained in:
Corey Johnson 2012-04-03 15:36:10 -07:00
parent a372a2b411
commit bd20d34132
4 changed files with 66 additions and 12 deletions

View File

@ -12,7 +12,7 @@ describe "Cursor", ->
editor = new Editor
editor.enableKeymap()
editor.setBuffer(buffer)
cursor = editor.find('.cursor').view()
cursor = editor.getCursors()[0]
describe "adding and removing of the idle class", ->
it "removes the idle class while moving, then adds it back when it stops", ->
@ -43,3 +43,45 @@ describe "Cursor", ->
cursor.setScreenPosition([1,30])
expect(cursor.isOnEOL()).toBeTruthy()
describe "vertical auto scroll", ->
beforeEach ->
editor.attachToDom()
editor.focus()
editor.vScrollMargin = 2
it "only attempts to scroll when a cursor is visible", ->
setEditorWidthInChars(editor, 20)
setEditorHeightInChars(editor, 10)
editor.setCursorBufferPosition([11,0])
editor.addCursorAtBufferPosition([0,0])
editor.addCursorAtBufferPosition([6,50])
window.advanceClock()
offscreenScrollHandler = spyOn(editor.getCursors()[0], 'autoScrollVertically')
onscreenScrollHandler = spyOn(editor.getCursors()[1], 'autoScrollVertically')
anotherOffscreenScrollHandler = spyOn(editor.getCursors()[2], 'autoScrollVertically')
editor.moveCursorRight()
window.advanceClock()
expect(offscreenScrollHandler).not.toHaveBeenCalled()
expect(onscreenScrollHandler).toHaveBeenCalled()
expect(anotherOffscreenScrollHandler).not.toHaveBeenCalled()
it "only attempts to scroll once when multiple cursors are visible", ->
setEditorWidthInChars(editor, 20)
setEditorHeightInChars(editor, 10)
editor.setCursorBufferPosition([11,0])
editor.addCursorAtBufferPosition([0,0])
editor.addCursorAtBufferPosition([6,0])
window.advanceClock()
offscreenScrollHandler = spyOn(editor.getCursors()[0], 'autoScrollVertically')
onscreenScrollHandler = spyOn(editor.getCursors()[1], 'autoScrollVertically')
anotherOnscreenScrollHandler = spyOn(editor.getCursors()[2], 'autoScrollVertically')
editor.moveCursorRight()
window.advanceClock()
expect(offscreenScrollHandler).not.toHaveBeenCalled()
expect(onscreenScrollHandler).toHaveBeenCalled()
expect(anotherOnscreenScrollHandler).not.toHaveBeenCalled()

View File

@ -262,10 +262,12 @@ describe "Editor", ->
expect(editor.scroller.scrollTop()).toBe(0)
it "reduces scroll margins when there isn't enough height to maintain them and scroll smoothly", ->
editor.height(editor.lineHeight * 5)
setEditorHeightInChars(editor, 5)
_.times 3, ->
editor.moveCursorDown()
window.advanceClock()
_.times 3, -> editor.moveCursorDown()
window.advanceClock()
expect(editor.scroller.scrollTop()).toBe(editor.lineHeight)
editor.moveCursorUp()
@ -1920,8 +1922,8 @@ describe "Editor", ->
it "returns correct bounds based on scroll position", ->
expect(editor.bounds()).toEqual [[0,0], [10, 10]]
editor.scrollTop(editor.lineHeight * 1)
editor.horizontalScroller.scrollLeft(editor.charWidth * 1)
editor.scroller.scrollTop(editor.lineHeight * 1)
editor.scroller.scrollLeft(editor.charWidth * 1)
expect(editor.bounds()).toEqual [[1,1], [11, 11]]
describe "screenPositionInBounds(screenPosition)", ->

View File

@ -132,11 +132,21 @@ class Cursor extends View
@setBufferPosition @editor.getEofPosition()
updateAppearance: ->
position = @editor.pixelPositionForScreenPosition(@getScreenPosition())
screenPosition = @getScreenPosition()
position = @editor.pixelPositionForScreenPosition(screenPosition)
@css(position)
if @editor.getCursors().length == 1 or @editor.screenPositionInBounds(screenPosition)
@autoScroll(position)
autoScroll: (position) ->
return if @editor._autoScrolling
@editor._autoScrolling = true
_.defer =>
@autoScrollVertically(position)
@autoScrollHorizontally(position)
@editor._autoScrolling = false
@autoScrollVertically(position)
@autoScrollHorizontally(position)
autoScrollVertically: (position) ->
linesInView = @editor.scroller.height() / @editor.lineHeight

View File

@ -163,10 +163,10 @@ class Editor extends View
@parents('#root-view').view()
bounds: ->
rows = @height() / @lineHeight
columns = @horizontalScroller.width() / @charWidth
rows = @scroller.height() / @lineHeight
columns = @scroller.width() / @charWidth
start = new Point(@scrollTop() / @lineHeight, @horizontalScroller.scrollLeft() / @charWidth)
start = new Point(@scroller.scrollTop() / @lineHeight, @scroller.scrollLeft() / @charWidth)
end = new Point(start.row + rows, start.column + columns)
new Range(start, end)