Skip to content

Commit

Permalink
Implement a new adapter layer to handle differences between stable an…
Browse files Browse the repository at this point in the history
…d beta
  • Loading branch information
abe33 committed Oct 22, 2015
1 parent 0d7f096 commit 0d93666
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 21 deletions.
32 changes: 32 additions & 0 deletions lib/adapters/beta-adapter.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports =
class BetaAdater
constructor: (@textEditor) ->
@textEditorElement = atom.views.getView(@textEditor)

onDidChangeScrollTop: (callback) ->
@textEditorElement.onDidChangeScrollTop(callback)

onDidChangeScrollLeft: (callback) ->
@textEditorElement.onDidChangeScrollLeft(callback)

getHeight: ->
@textEditorElement.getHeight()

getScrollTop: ->
@textEditorElement.getScrollTop()

setScrollTop: (scrollTop) ->
@textEditorElement.setScrollTop(scrollTop)

getScrollLeft: ->
@textEditorElement.getScrollLeft()

getHeightWithoutScrollPastEnd: ->
@textEditor.displayBuffer.getLineHeightInPixels()

getMaxScrollTop: ->
maxScrollTop = @textEditorElement.getScrollHeight() - @getHeight()
lineHeight = @textEditor.getLineHeightInPixels()

maxScrollTop -= @getHeight() - 3 * lineHeight if @scrollPastEnd
maxScrollTop
31 changes: 31 additions & 0 deletions lib/adapters/legacy-adapter.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports =
class LegacyAdater
constructor: (@textEditor) ->

onDidChangeScrollTop: (callback) ->
@textEditor.onDidChangeScrollTop(callback)

onDidChangeScrollLeft: (callback) ->
@textEditor.onDidChangeScrollLeft(callback)

getHeight: ->
@textEditor.getHeight()

getScrollTop: ->
@textEditor.getScrollTop()

setScrollTop: (scrollTop) ->
@textEditor.setScrollTop(scrollTop)

getScrollLeft: ->
@textEditor.getScrollLeft()

getHeightWithoutScrollPastEnd: ->
@textEditor.displayBuffer.getLineHeightInPixels()

getMaxScrollTop: ->
maxScrollTop = @textEditor.displayBuffer.getMaxScrollTop()
lineHeight = @textEditor.getLineHeightInPixels()

maxScrollTop -= @getHeight() - 3 * lineHeight if @scrollPastEnd
maxScrollTop
14 changes: 7 additions & 7 deletions lib/minimap-element.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{debounce} = require 'underscore-plus'
{CompositeDisposable, Disposable} = require 'event-kit'
{CompositeDisposable, Disposable} = require 'atom'
{EventsDelegation, AncestorsMethods} = require 'atom-utils'
DOMStylesReader = require './mixins/dom-styles-reader'
CanvasDrawer = require './mixins/canvas-drawer'
Expand Down Expand Up @@ -481,16 +481,16 @@ class MinimapElement extends HTMLElement

textEditor = @minimap.getTextEditor()

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

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

middleMousePressedOverCanvas: ({pageY}) ->
{top: offsetTop} = @getBoundingClientRect()
Expand All @@ -499,7 +499,7 @@ class MinimapElement extends HTMLElement
ratio = y /
(@minimap.getVisibleHeight() - @minimap.getTextEditorScaledHeight())

@minimap.textEditor.setScrollTop(
@minimap.setTextEditorScrollTop(
ratio * @minimap.getTextEditorMaxScrollTop())

# Internal: A method that relays the `mousewheel` events received by
Expand Down Expand Up @@ -568,7 +568,7 @@ class MinimapElement extends HTMLElement

ratio = y / (@minimap.getVisibleHeight() - @minimap.getTextEditorScaledHeight())

@minimap.textEditor.setScrollTop(ratio * @minimap.getTextEditorMaxScrollTop())
@minimap.setTextEditorScrollTop(ratio * @minimap.getTextEditorMaxScrollTop())

# Internal: The method that ends the drag gesture.
#
Expand Down
41 changes: 27 additions & 14 deletions lib/minimap.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{Emitter, CompositeDisposable} = require 'event-kit'
DecorationManagement = require './mixins/decoration-management'
LegacyAdater = require './adapters/legacy-adapter'
BetaAdater = require './adapters/beta-adapter'

nextModelId = 1

Expand All @@ -21,6 +23,7 @@ class Minimap
# :textEditor - A `TextEditor` instance.
constructor: (options={}) ->
{@textEditor, @standAlone, @width, @height} = options

unless @textEditor?
throw new Error('Cannot create a minimap without an editor')

Expand All @@ -29,10 +32,16 @@ class Minimap
@subscriptions = subs = new CompositeDisposable
@initializeDecorations()

if atom.views.getView(@textEditor).getScrollTop?
@adapter = new BetaAdater(@textEditor)
else
@adapter = new LegacyAdater(@textEditor)

if @standAlone
@scrollTop = 0

subs.add atom.config.observe 'editor.scrollPastEnd', (@scrollPastEnd) =>
@adapter.scrollPastEnd = @scrollPastEnd
@emitter.emit('did-change-config', {
config: 'editor.scrollPastEnd'
value: @scrollPastEnd
Expand All @@ -53,12 +62,13 @@ class Minimap
value: @interline
})

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

subs.add @textEditor.onDidChange (changes) =>
@emitChanges(changes)
subs.add @textEditor.onDidDestroy =>
@destroy()

Expand Down Expand Up @@ -175,19 +185,19 @@ class Minimap
#
# Returns a {Number}.
getTextEditorScaledHeight: ->
@textEditor.getHeight() * @getVerticalScaleFactor()
@adapter.getHeight() * @getVerticalScaleFactor()

# Returns the `TextEditor::getScrollTop` value at the {Minimap} scale.
#
# Returns a {Number}.
getTextEditorScaledScrollTop: ->
@textEditor.getScrollTop() * @getVerticalScaleFactor()
@adapter.getScrollTop() * @getVerticalScaleFactor()

# Returns the `TextEditor::getScrollLeft` value at the {Minimap} scale.
#
# Returns a {Number}.
getTextEditorScaledScrollLeft: ->
@textEditor.getScrollLeft() * @getHorizontalScaleFactor()
@adapter.getScrollLeft() * @getHorizontalScaleFactor()

# Returns the maximum scroll the `TextEditor` can perform.
#
Expand All @@ -196,12 +206,15 @@ class Minimap
# final value.
#
# Returns a {Number}.
getTextEditorMaxScrollTop: ->
maxScrollTop = @textEditor.displayBuffer.getMaxScrollTop()
lineHeight = @textEditor.displayBuffer.getLineHeightInPixels()
getTextEditorMaxScrollTop: -> @adapter.getMaxScrollTop()

getTextEditorScrollTop: -> @adapter.getScrollTop()

setTextEditorScrollTop: (scrollTop) -> @adapter.setScrollTop(scrollTop)

getTextEditorScrollLeft: -> @adapter.getScrollLeft()

maxScrollTop -= @textEditor.getHeight() - 3 * lineHeight if @scrollPastEnd
maxScrollTop
getTextEditorHeight: -> @adapter.getHeight()

# Returns the `TextEditor` scroll as a value normalized between `0` and `1`.
#
Expand All @@ -213,7 +226,7 @@ class Minimap
# Returns a {Number}.
getTextEditorScrollRatio: ->
# Because `0/0 = NaN`, so make sure that the denominator does not equal `0`.
@textEditor.getScrollTop() / (@getTextEditorMaxScrollTop() || 1)
@adapter.getScrollTop() / (@getTextEditorMaxScrollTop() || 1)

# Returns the `TextEditor` scroll as a value normalized between `0` and `1`.
#
Expand Down Expand Up @@ -250,7 +263,7 @@ class Minimap
if @isStandAlone()
if @height? then @height else @getHeight()
else
@textEditor.getHeight()
@adapter.getHeight()

# Returns the width the whole {Minimap} will take on screen.
#
Expand Down

0 comments on commit 0d93666

Please sign in to comment.