Move autoScrolling methods from cursor to editor

Scroll methods are now Editor.scrollTo(), Editor.scrollHorizontally() and Editor.scrollVertically()

Editor.scrollTo() can only be called once per runloop
This commit is contained in:
Corey Johnson
2012-04-03 16:12:04 -07:00
parent bd20d34132
commit d016cc27d3
4 changed files with 206 additions and 213 deletions

View File

@@ -137,43 +137,4 @@ class Cursor extends View
@css(position)
if @editor.getCursors().length == 1 or @editor.screenPositionInBounds(screenPosition)
@autoScroll(position)
autoScroll: (position) ->
return if @editor._autoScrolling
@editor._autoScrolling = true
_.defer =>
@editor._autoScrolling = false
@autoScrollVertically(position)
@autoScrollHorizontally(position)
autoScrollVertically: (position) ->
linesInView = @editor.scroller.height() / @editor.lineHeight
maxScrollMargin = Math.floor((linesInView - 1) / 2)
scrollMargin = Math.min(@editor.vScrollMargin, maxScrollMargin)
margin = scrollMargin * @height()
desiredTop = position.top - margin
desiredBottom = position.top + @height() + margin
if desiredBottom > @editor.scroller.scrollBottom()
@editor.scroller.scrollBottom(desiredBottom)
else if desiredTop < @editor.scroller.scrollTop()
@editor.scroller.scrollTop(desiredTop)
autoScrollHorizontally: (position) ->
return if @editor.softWrap
charWidth = @editor.charWidth
charsInView = @editor.scroller.width() / charWidth
maxScrollMargin = Math.floor((charsInView - 1) / 2)
scrollMargin = Math.min(@editor.hScrollMargin, maxScrollMargin)
margin = scrollMargin * charWidth
desiredRight = position.left + charWidth + margin
desiredLeft = position.left - margin
if desiredRight > @editor.scroller.scrollRight()
@editor.scroller.scrollRight(desiredRight)
else if desiredLeft < @editor.scroller.scrollLeft()
@editor.scroller.scrollLeft(desiredLeft)
@editor.scrollTo(position)

View File

@@ -37,6 +37,7 @@ class Editor extends View
autoIndent: null
lineCache: null
isFocused: false
isScrolling: false
initialize: ({buffer}) ->
requireStylesheet 'editor.css'
@@ -328,8 +329,9 @@ class Editor extends View
clipScreenPosition: (screenPosition, options={}) ->
@renderer.clipScreenPosition(screenPosition, options)
pixelPositionForScreenPosition: ({row, column}) ->
{ top: row * @lineHeight, left: column * @charWidth }
pixelPositionForScreenPosition: (position) ->
position = Point.fromObject(position)
{ top: position.row * @lineHeight, left: position.column * @charWidth }
screenPositionFromPixelPosition: ({top, left}) ->
screenPosition = new Point(Math.floor(top / @lineHeight), Math.floor(left / @charWidth))
@@ -478,5 +480,41 @@ class Editor extends View
getCurrentMode: ->
@buffer.getMode()
scrollTo: (position) ->
return if @isScrolling
@isScrolling = true
_.defer =>
@isScrolling = false
@scrollVertically(position)
@scrollHorizontally(position)
scrollVertically: (position) ->
linesInView = @scroller.height() / @lineHeight
maxScrollMargin = Math.floor((linesInView - 1) / 2)
scrollMargin = Math.min(@vScrollMargin, maxScrollMargin)
margin = scrollMargin * @lineHeight
desiredTop = position.top - margin
desiredBottom = position.top + @lineHeight + margin
if desiredBottom > @scroller.scrollBottom()
@scroller.scrollBottom(desiredBottom)
else if desiredTop < @scroller.scrollTop()
@scroller.scrollTop(desiredTop)
scrollHorizontally: (position) ->
return if @softWrap
charsInView = @scroller.width() / @charWidth
maxScrollMargin = Math.floor((charsInView - 1) / 2)
scrollMargin = Math.min(@hScrollMargin, maxScrollMargin)
margin = scrollMargin * @charWidth
desiredRight = position.left + @charWidth + margin
desiredLeft = position.left - margin
if desiredRight > @scroller.scrollRight()
@scroller.scrollRight(desiredRight)
else if desiredLeft < @scroller.scrollLeft()
@scroller.scrollLeft(desiredLeft)
logLines: ->
@renderer.logLines()