Skip to content

Commit

Permalink
Implement independent scroll top for stand-alone minimap
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Aug 16, 2015
1 parent 4de0f2a commit 6afdac4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/minimap.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class Minimap
@subscriptions = subs = new CompositeDisposable
@initializeDecorations()

if @standAlone
@scrollTop = 0

subs.add atom.config.observe 'editor.scrollPastEnd', (@scrollPastEnd) =>
@emitter.emit('did-change-config', {
config: 'editor.scrollPastEnd'
Expand All @@ -52,10 +55,10 @@ class Minimap

subs.add @textEditor.onDidChange (changes) =>
@emitChanges(changes)
subs.add @textEditor.onDidChangeScrollTop (scrollTop) =>
@emitter.emit('did-change-scroll-top', scrollTop)
subs.add @textEditor.onDidChangeScrollLeft (scrollLeft) =>
@emitter.emit('did-change-scroll-left', scrollLeft)
subs.add @textEditor.onDidChangeScrollTop =>
@emitter.emit('did-change-scroll-top', this) unless @standAlone
subs.add @textEditor.onDidChangeScrollLeft =>
@emitter.emit('did-change-scroll-left', this) unless @standAlone
subs.add @textEditor.onDidDestroy =>
@destroy()

Expand Down Expand Up @@ -108,7 +111,8 @@ class Minimap
@emitter.on 'did-change-config', callback

# Calls the `callback` when the text editor `scrollTop` value have been
# changed.
# changed or when the minimap scroll top have been changed in stand-alone
# mode.
#
# callback - The callback {Function}. The event the callback will receive
# the new `scrollTop` {Number} value.
Expand Down Expand Up @@ -305,7 +309,16 @@ class Minimap
#
# Returns a {Number}.
getScrollTop: ->
Math.abs(@getCapedTextEditorScrollRatio() * @getMaxScrollTop())
if @standAlone
@scrollTop
else
Math.abs(@getCapedTextEditorScrollRatio() * @getMaxScrollTop())

# Sets the minimap scroll top value when in stand-alone mode.
#
# scrollTop - The {Number} of pixels of vertical scroll.
setScrollTop: (@scrollTop) ->
@emitter.emit('did-change-scroll-top', this) if @standAlone

# Returns the maximum scroll value of the {Minimap}.
#
Expand Down
39 changes: 39 additions & 0 deletions spec/minimap-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,42 @@ describe 'Stand alone minimap', ->

minimap.height = 100
expect(minimap.getLastVisibleScreenRow()).toEqual(20)

it 'does not relay scroll top events from the editor', ->
editor.setText(largeSample)

scrollSpy = jasmine.createSpy('didScroll')
minimap.onDidChangeScrollTop(scrollSpy)

editor.setScrollTop(100)

expect(scrollSpy).not.toHaveBeenCalled()

it 'does not relay scroll left events from the editor', ->
editor.setText(largeSample)

scrollSpy = jasmine.createSpy('didScroll')
minimap.onDidChangeScrollLeft(scrollSpy)

# Seems like text without a view aren't able to scroll horizontally
# even when its width was set.
spyOn(editor.displayBuffer, 'getScrollWidth').andReturn(10000)

editor.setScrollLeft(100)

expect(scrollSpy).not.toHaveBeenCalled()

it 'has a scroll top that is not bound to the text editor', ->
scrollSpy = jasmine.createSpy('didScroll')
minimap.onDidChangeScrollTop(scrollSpy)

editor.setText(largeSample)
editor.setScrollTop(1000)

expect(minimap.getScrollTop()).toEqual(0)
expect(scrollSpy).not.toHaveBeenCalled()

minimap.setScrollTop(10)

expect(minimap.getScrollTop()).toEqual(10)
expect(scrollSpy).toHaveBeenCalled()

0 comments on commit 6afdac4

Please sign in to comment.