Skip to content

Commit

Permalink
Add scroll animation.
Browse files Browse the repository at this point in the history
  • Loading branch information
xndcn committed Feb 4, 2015
1 parent 6d84692 commit 986c725
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class Main
minimum: 0
maximum: 1
description: "The opacity used to render the line's text in the minimap."
scrollAnimation:
type: 'boolean'
default: false
description: "If this option is enabled then when you click the minimap it will scroll to the destination with animation"

# Internal: The activation state of the minimap package.
active: false
Expand Down
30 changes: 29 additions & 1 deletion lib/minimap-element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ CanvasDrawer = require './mixins/canvas-drawer'

MinimapQuickSettingsView = null

animate = ({from, to, duration, step}) ->
start = new Date()

swing = (progress) ->
return 0.5 - Math.cos( progress * Math.PI ) / 2

update = ->
passed = new Date() - start
if duration == 0
progress = 1
else
progress = passed / duration
progress = 1 if progress > 1
delta = swing(progress)
step(from + (to-from)*delta)
requestAnimationFrame(update) if progress < 1

update()

# Public:
class MinimapElement extends HTMLElement
DOMStylesReader.includeInto(this)
Expand Down Expand Up @@ -353,7 +372,16 @@ class MinimapElement extends HTMLElement

scrollTop = row * @minimap.textEditor.getLineHeightInPixels() - @minimap.textEditor.getHeight() / 2

@minimap.textEditor.setScrollTop(scrollTop)
from = @minimap.textEditor.getScrollTop()
to = scrollTop
step = (now) =>
@minimap.textEditor.setScrollTop(now)
if atom.config.get('minimap.scrollAnimation')
duration = 300
else
duration = 0
animate(from: from, to: to, duration: duration, step: step)

relayMousewheelEvent: (e) =>
editorElement = atom.views.getView(@minimap.textEditor)

Expand Down
23 changes: 21 additions & 2 deletions spec/minimap-element-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe 'MinimapElement', ->
# ## ## ## ## ## ## ###### ## ##

describe 'when attached to the text editor element', ->
[nextAnimationFrame, canvas, visibleArea] = []
[noAnimationFrame, nextAnimationFrame, canvas, visibleArea] = []

beforeEach ->
jasmineContent = document.body.querySelector('#jasmine-content')
Expand Down Expand Up @@ -263,15 +263,34 @@ describe 'MinimapElement', ->
it 'relays the events to the editor view', ->
expect(editor.setScrollTop).toHaveBeenCalled()

describe 'pressing the mouse on the minimap canvas', ->
describe 'pressing the mouse on the minimap canvas (without scroll animation)', ->
beforeEach ->
atom.config.set 'minimap.scrollAnimation', false
canvas = minimapElement.canvas
mousedown(canvas)
nextAnimationFrame()

it 'scrolls the editor to the line below the mouse', ->
expect(editor.getScrollTop()).toEqual(360)

describe 'pressing the mouse on the minimap canvas (with scroll animation)', ->
beforeEach ->
atom.config.set 'minimap.scrollAnimation', true
canvas = minimapElement.canvas
mousedown(canvas)
nextAnimationFrame()

it 'scrolls the editor gradually to the line below the mouse', ->
expect(editor.getScrollTop()).toEqual(0)

#wait until all animations run out
waitsFor ->
nextAnimationFrame()
return nextAnimationFrame == noAnimationFrame

runs ->
expect(editor.getScrollTop()).toEqual(360)

describe 'dragging the visible area', ->
[visibleArea, originalTop] = []

Expand Down

0 comments on commit 986c725

Please sign in to comment.