Skip to content

Commit

Permalink
Add more minimap scroll related method
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Dec 12, 2014
1 parent 2e51742 commit 990f29a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
12 changes: 9 additions & 3 deletions lib/minimap.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@ class Minimap

getTextEditorHeight: -> @textEditor.getHeight() * @getScaleFactor()

getTextEditorScroll: -> @textEditor.getScrollTop() * @getScaleFactor()
getTextEditorScrollTop: -> @textEditor.getScrollTop() * @getScaleFactor()

getTextEditorScrollRatio: ->
@textEditor.getScrollTop() / @textEditor.displayBuffer.getMaxScrollTop()

getHeight: -> @textEditor.getLineCount() * @getLineHeight()

getScaleFactor: -> @getLineHeight() / @textEditor.getLineHeightInPixels()

getLineHeight: -> @charHeight + @interline

getMinimapScrollHeight: -> Math.max(0, @getHeight() - @textEditor.getHeight())
getMinimapScrollTop: ->
@getTextEditorScrollRatio() * @getMinimapMaxScrollTop()

getMinimapMaxScrollTop: -> Math.max(0, @getHeight() - @textEditor.getHeight())

canScroll: -> @getMinimapScrollHeight() > 0
canScroll: -> @getMinimapMaxScrollTop() > 0

subscribeToConfig: ->
@subscriptions.add atom.config.observe 'minimap.charHeight', (@charHeight) =>
Expand Down
47 changes: 33 additions & 14 deletions spec/minimap-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ describe 'Minimap', ->
expect(minimap.getHeight()).toEqual(20)

editor.setText(largeSample)
largeLineCount = editor.getLineCount()
expect(minimap.getHeight()).toEqual(largeLineCount * 5)
expect(minimap.getHeight()).toEqual(editor.getLineCount() * 5)

it 'measures the scaling factor between the editor and the minimap', ->
expect(minimap.getScaleFactor()).toEqual(0.5)
Expand All @@ -36,23 +35,43 @@ describe 'Minimap', ->
editor.setText(largeSample)
expect(minimap.getTextEditorHeight()).toEqual(25)

it 'scales the editor scroll based on the minimap scale factor', ->
editor.setText(largeSample)
editor.setScrollTop(1000)

expect(minimap.getTextEditorScroll()).toEqual(500)

it 'measures the available minimap scroll', ->
editor.setText(largeSample)
largeLineCount = editor.getLineCount()

expect(minimap.getMinimapScrollHeight()).toEqual(largeLineCount * 5 - 50)
expect(minimap.getMinimapMaxScrollTop()).toEqual(largeLineCount * 5 - 50)
expect(minimap.canScroll()).toBeTruthy()

describe 'when there is no scrolling needed to display the whole minimap', ->
describe 'measuring the available minimap scroll', ->
it 'returns 0', ->
editor.setText(smallSample)
it 'returns 0 when computing the minimap scroll', ->
console.log minimap.getMinimapScrollTop()
expect(minimap.getMinimapScrollTop()).toBeCloseTo(0)

it 'returns 0 when measuring the available minimap scroll', ->
editor.setText(smallSample)

expect(minimap.getMinimapMaxScrollTop()).toEqual(0)
expect(minimap.canScroll()).toBeFalsy()

describe 'when the editor is scrolled', ->
[largeLineCount, editorHeight, editorScrollRatio] = []

beforeEach ->
editor.setText(largeSample)
editor.setScrollTop(1000)
largeLineCount = editor.getLineCount()
editorHeight = largeLineCount * editor.getLineHeightInPixels()
editorScrollRatio = editor.getScrollTop() / editor.displayBuffer.getMaxScrollTop()

it 'scales the editor scroll based on the minimap scale factor', ->
expect(minimap.getTextEditorScrollTop()).toEqual(500)

it 'computes the offset to apply based on the editor scroll top', ->
expect(minimap.getMinimapScrollTop()).toEqual(editorScrollRatio * minimap.getMinimapMaxScrollTop())

describe 'down to the bottom', ->
it 'computes an offset that scrolls the minimap to the bottom edge', ->
editor.setScrollTop(editor.displayBuffer.getMaxScrollTop())
editorScrollRatio = editor.getScrollTop() / editor.displayBuffer.getMaxScrollTop()

expect(minimap.getMinimapScrollHeight()).toEqual(0)
expect(minimap.canScroll()).toBeFalsy()
expect(minimap.getMinimapScrollTop()).toEqual(minimap.getMinimapMaxScrollTop())

0 comments on commit 990f29a

Please sign in to comment.