From ac0e3095ecd9037387c8fd611375bc33f7196661 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Fri, 30 Nov 2012 11:40:16 -0700 Subject: [PATCH] Only autoscroll selections to center if they are offscreen --- spec/app/editor-spec.coffee | 10 +++++++--- src/app/editor.coffee | 12 ++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index eeb5f4804..1d9e2ebd6 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -941,10 +941,14 @@ describe "Editor", -> expect(editor.scrollTop()).toBeGreaterThan(0) describe "when the selected buffer range is assigned with the autoscroll option set to true", -> - it "centers the selection in the viewport if possible", -> - setEditorHeightInLines(editor, 8) + it "centers the selection in the viewport if its vertical center is currently offscreen", -> + setEditorHeightInLines(editor, 4) + + editor.setSelectedBufferRange([[2, 0], [4, 0]], autoscroll: true) + expect(editor.scrollTop()).toBe 0 + editor.setSelectedBufferRange([[6, 0], [8, 0]], autoscroll: true) - expect(editor.scrollTop()).toBe 3 * editor.lineHeight + expect(editor.scrollTop()).toBe 5 * editor.lineHeight describe "cursor rendering", -> describe "when the cursor moves", -> diff --git a/src/app/editor.coffee b/src/app/editor.coffee index be9bb936f..4d257e2f2 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -502,9 +502,14 @@ class Editor extends View scrollVertically: (pixelPosition, {center}={}) -> scrollViewHeight = @scrollView.height() + scrollTop = @scrollTop() + scrollBottom = scrollTop + scrollViewHeight if center - @scrollTop(pixelPosition.top - (scrollViewHeight / 2)) + console.log scrollTop, pixelPosition.top, scrollBottom + + unless scrollTop < pixelPosition.top < scrollBottom + @scrollTop(pixelPosition.top - (scrollViewHeight / 2)) else linesInView = @scrollView.height() / @lineHeight maxScrollMargin = Math.floor((linesInView - 1) / 2) @@ -512,10 +517,9 @@ class Editor extends View margin = scrollMargin * @lineHeight desiredTop = pixelPosition.top - margin desiredBottom = pixelPosition.top + @lineHeight + margin - - if desiredBottom > @scrollTop() + scrollViewHeight + if desiredBottom > scrollBottom @scrollTop(desiredBottom - scrollViewHeight) - else if desiredTop < @scrollTop() + else if desiredTop < scrollTop @scrollTop(desiredTop) scrollHorizontally: (pixelPosition) ->