This commit is contained in:
Antonio Scandurra
2015-09-22 16:20:15 +02:00
parent e8b7a6bc1f
commit b5a59017d5
4 changed files with 129 additions and 11 deletions

View File

@@ -132,6 +132,9 @@ class DisplayBuffer extends Model
onDidChangeScrollLeft: (callback) ->
@emitter.on 'did-change-scroll-left', callback
onDidChangeScrollPosition: (callback) ->
@emitter.on 'did-change-scroll-position', callback
observeScrollTop: (callback) ->
callback(@scrollTop)
@onDidChangeScrollTop(callback)
@@ -348,6 +351,10 @@ class DisplayBuffer extends Model
[startRow, endRow]
getLogicalHeight: ->
[startRow, endRow] = @getVisibleRowRange()
endRow - startRow
intersectsVisibleRowRange: (startRow, endRow) ->
[visibleStart, visibleEnd] = @getVisibleRowRange()
not (endRow <= visibleStart or visibleEnd <= startRow)
@@ -356,7 +363,31 @@ class DisplayBuffer extends Model
{start, end} = selection.getScreenRange()
@intersectsVisibleRowRange(start.row, end.row + 1)
scrollToScreenRangeLogical: (screenRange, options) ->
top = screenRange.start.row
left = screenRange.start.column
bottom = screenRange.end.row
right = screenRange.end.column
if options?.center
center = (top + bottom) / 2
top = center - @getLogicalHeight() / 2
bottom = center + @getLogicalHeight() / 2
else
top -= @getVerticalScrollMargin()
bottom += @getVerticalScrollMargin()
left -= @getHorizontalScrollMargin()
right += @getHorizontalScrollMargin()
screenRange = new Range(new Point(top, left), new Point(bottom, right))
scrollEvent = {screenRange, options}
@emitter.emit "did-change-scroll-position", scrollEvent
scrollToScreenRange: (screenRange, options) ->
@scrollToScreenRangeLogical(screenRange, options)
verticalScrollMarginInPixels = @getVerticalScrollMarginInPixels()
horizontalScrollMarginInPixels = @getHorizontalScrollMarginInPixels()
@@ -377,6 +408,13 @@ class DisplayBuffer extends Model
desiredScrollLeft = left - horizontalScrollMarginInPixels
desiredScrollRight = right + horizontalScrollMarginInPixels
if global.enableLogs
console.log "====== DB ======"
console.log "Client Height: #{@getClientHeight()}"
console.log "#{desiredScrollTop}/#{desiredScrollBottom}"
console.log "#{@getScrollTop()}/#{@getScrollBottom()}"
console.log "================"
if options?.reversed ? true
if desiredScrollBottom > @getScrollBottom()
@setScrollBottom(desiredScrollBottom)

View File

@@ -71,6 +71,7 @@ class TextEditorPresenter
@updateContentDimensions()
@updateScrollbarDimensions()
@updateScrollPosition()
@updateStartRow()
@updateEndRow()
@updateCommonGutterState()
@@ -115,8 +116,6 @@ class TextEditorPresenter
observeModel: ->
@disposables.add @model.onDidChange =>
@updateContentDimensions()
@shouldUpdateHeightState = true
@shouldUpdateVerticalScrollState = true
@shouldUpdateHorizontalScrollState = true
@@ -162,8 +161,9 @@ class TextEditorPresenter
@disposables.add @model.onDidAddDecoration(@didAddDecoration.bind(this))
@disposables.add @model.onDidAddCursor(@didAddCursor.bind(this))
@disposables.add @model.onDidChangeScrollTop(@setScrollTop.bind(this))
@disposables.add @model.onDidChangeScrollLeft(@setScrollLeft.bind(this))
@disposables.add @model.onDidChangeScrollPosition(@didChangeScrollPosition.bind(this))
# @disposables.add @model.onDidChangeScrollTop(@setScrollTop.bind(this))
# @disposables.add @model.onDidChangeScrollLeft(@setScrollLeft.bind(this))
@observeDecoration(decoration) for decoration in @model.getDecorations()
@observeCursor(cursor) for cursor in @model.getCursors()
@disposables.add @model.onDidAddGutter(@didAddGutter.bind(this))
@@ -813,7 +813,7 @@ class TextEditorPresenter
unless @scrollTop is scrollTop or Number.isNaN(scrollTop)
@scrollTop = scrollTop
@model.setScrollTop(scrollTop)
@model.setScrollTop(@scrollTop)
@didStartScrolling()
@shouldUpdateVerticalScrollState = true
@shouldUpdateHiddenInputState = true
@@ -852,7 +852,7 @@ class TextEditorPresenter
unless @scrollLeft is scrollLeft or Number.isNaN(scrollLeft)
oldScrollLeft = @scrollLeft
@scrollLeft = scrollLeft
@model.setScrollLeft(scrollLeft)
@model.setScrollLeft(@scrollLeft)
@shouldUpdateHorizontalScrollState = true
@shouldUpdateHiddenInputState = true
@shouldUpdateCursorsState = true
@@ -865,11 +865,29 @@ class TextEditorPresenter
getScrollLeft: ->
@scrollLeft
getClientHeight: ->
if @clientHeight
@clientHeight
else
@explicitHeight - @horizontalScrollbarHeight
getClientWidth: ->
@clientWidth ? @contentFrameWidth
getScrollBottom: -> @getScrollTop() + @getClientHeight()
setScrollBottom: (scrollBottom) ->
@setScrollTop(scrollBottom - @getClientHeight())
@getScrollBottom()
getScrollRight: -> @getScrollLeft() + @getClientWidth()
setScrollRight: (scrollRight) ->
@setScrollLeft(scrollRight - @getClientWidth())
@getScrollRight()
setHorizontalScrollbarHeight: (horizontalScrollbarHeight) ->
unless @measuredHorizontalScrollbarHeight is horizontalScrollbarHeight
oldHorizontalScrollbarHeight = @measuredHorizontalScrollbarHeight
@measuredHorizontalScrollbarHeight = horizontalScrollbarHeight
@model.setHorizontalScrollbarHeight(horizontalScrollbarHeight)
@shouldUpdateScrollbarsState = true
@shouldUpdateVerticalScrollState = true
@shouldUpdateHorizontalScrollState = true
@@ -881,7 +899,6 @@ class TextEditorPresenter
unless @measuredVerticalScrollbarWidth is verticalScrollbarWidth
oldVerticalScrollbarWidth = @measuredVerticalScrollbarWidth
@measuredVerticalScrollbarWidth = verticalScrollbarWidth
@model.setVerticalScrollbarWidth(verticalScrollbarWidth)
@shouldUpdateScrollbarsState = true
@shouldUpdateVerticalScrollState = true
@shouldUpdateHorizontalScrollState = true
@@ -1438,3 +1455,61 @@ class TextEditorPresenter
@startBlinkingCursorsAfterDelay ?= _.debounce(@startBlinkingCursors, @getCursorBlinkResumeDelay())
@startBlinkingCursorsAfterDelay()
@emitDidUpdateState()
didChangeScrollPosition: (position) ->
@pendingScrollLogicalPosition = position
@shouldUpdateCursorsState = true
@shouldUpdateCustomGutterDecorationState = true
@shouldUpdateDecorations = true
@shouldUpdateHiddenInputState = true
@shouldUpdateHorizontalScrollState = true
@shouldUpdateLinesState = true
@shouldUpdateLineNumbersState = true
@shouldUpdateOverlaysState = true
@shouldUpdateScrollPosition = true
@shouldUpdateVerticalScrollState = true
@emitDidUpdateState()
updateScrollPosition: ->
return unless @pendingScrollLogicalPosition?
{screenRange, options} = @pendingScrollLogicalPosition
console.log screenRange.start.toString()
console.log screenRange.end.toString()
{top, left} = @pixelRectForScreenRange(new Range(screenRange.start, screenRange.start))
{top: endTop, left: endLeft, height: endHeight} = @pixelRectForScreenRange(new Range(screenRange.end, screenRange.end))
bottom = endTop + endHeight
right = endLeft
if global.enableLogs
console.log "====== presenter ======"
console.log "Client Height: #{@getClientHeight()}"
console.log "#{desiredScrollTop}/#{desiredScrollBottom}"
console.log "#{@getScrollTop()}/#{@getScrollBottom()}"
console.log "======================="
if options?.reversed ? true
if desiredScrollBottom > @getScrollBottom()
@setScrollBottom(desiredScrollBottom)
if desiredScrollTop < @getScrollTop()
@setScrollTop(desiredScrollTop)
if desiredScrollRight > @getScrollRight()
@setScrollRight(desiredScrollRight)
if desiredScrollLeft < @getScrollLeft()
@setScrollLeft(desiredScrollLeft)
else
if desiredScrollTop < @getScrollTop()
@setScrollTop(desiredScrollTop)
if desiredScrollBottom > @getScrollBottom()
@setScrollBottom(desiredScrollBottom)
if desiredScrollLeft < @getScrollLeft()
@setScrollLeft(desiredScrollLeft)
if desiredScrollRight > @getScrollRight()
@setScrollRight(desiredScrollRight)
@pendingScrollLogicalPosition = null

View File

@@ -464,6 +464,9 @@ class TextEditor extends Model
onDidChangeScrollLeft: (callback) ->
@emitter.on 'did-change-scroll-left', callback
onDidChangeScrollPosition: (callback) ->
@displayBuffer.onDidChangeScrollPosition(callback)
# TODO Remove once the tabs package no longer uses .on subscriptions
onDidChangeIcon: (callback) ->
@emitter.on 'did-change-icon', callback