diff --git a/lib/adapters/beta-adapter.coffee b/lib/adapters/beta-adapter.coffee new file mode 100644 index 00000000..9dd7820e --- /dev/null +++ b/lib/adapters/beta-adapter.coffee @@ -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 diff --git a/lib/adapters/legacy-adapter.coffee b/lib/adapters/legacy-adapter.coffee new file mode 100644 index 00000000..68b61c99 --- /dev/null +++ b/lib/adapters/legacy-adapter.coffee @@ -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 diff --git a/lib/minimap-element.coffee b/lib/minimap-element.coffee index 3e4e56e8..a294b57e 100644 --- a/lib/minimap-element.coffee +++ b/lib/minimap-element.coffee @@ -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' @@ -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() @@ -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 @@ -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. # diff --git a/lib/minimap.coffee b/lib/minimap.coffee index f1181285..bb7fccde 100644 --- a/lib/minimap.coffee +++ b/lib/minimap.coffee @@ -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 @@ -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') @@ -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 @@ -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() @@ -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. # @@ -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`. # @@ -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`. # @@ -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. #