From 5ddf4e1a0bfb7240022eb05ea7bef974c99ce340 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Mon, 23 Jan 2012 17:15:11 -0800 Subject: [PATCH] Ensure combined scroll margins are smaller than editor height. This prevents jerky scrolling when the window is very short. --- spec/atom/editor-spec.coffee | 45 ++++++++++++++++++++++-------------- spec/spec-helper.coffee | 4 ++-- src/atom/cursor.coffee | 6 ++++- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 90a6e6b84..ba6f150f9 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -44,29 +44,40 @@ describe "Editor", -> expect(editor.getPosition()).toEqual(row: 0, col: 0) describe "vertical movement", -> + describe "scroll margins", -> + beforeEach -> + editor.attachToDom() + editor.focus() + editor.scrollMargin = 3 - fit "scrolls the buffer with the specified scroll margin when cursor approaches the end of the screen", -> - editor.attachToDom() - editor.focus() - editor.scrollMargin = 3 - editor.height(editor.lineHeight * 10) + it "scrolls the buffer with the specified scroll margin when cursor approaches the end of the screen", -> + editor.height(editor.lineHeight * 10) - _.times 6, -> editor.moveDown() - expect(editor.scrollTop()).toBe(0) + _.times 6, -> editor.moveDown() + expect(editor.scrollTop()).toBe(0) - editor.moveDown() - expect(editor.scrollTop()).toBe(editor.lineHeight) - editor.moveDown() - expect(editor.scrollTop()).toBe(editor.lineHeight * 2) + editor.moveDown() + expect(editor.scrollTop()).toBe(editor.lineHeight) + editor.moveDown() + expect(editor.scrollTop()).toBe(editor.lineHeight * 2) - _.times 3, -> editor.moveUp() - expect(editor.scrollTop()).toBe(editor.lineHeight * 2) + _.times 3, -> editor.moveUp() + expect(editor.scrollTop()).toBe(editor.lineHeight * 2) - editor.moveUp() - expect(editor.scrollTop()).toBe(editor.lineHeight) + editor.moveUp() + expect(editor.scrollTop()).toBe(editor.lineHeight) - editor.moveUp() - expect(editor.scrollTop()).toBe(0) + editor.moveUp() + expect(editor.scrollTop()).toBe(0) + + it "sacrifices margins when there isn't enough height", -> + editor.height(editor.lineHeight * 5) + + _.times 3, -> editor.moveDown() + expect(editor.scrollTop()).toBe(editor.lineHeight) + + editor.moveUp() + expect(editor.scrollTop()).toBe(0) describe "when up is pressed on the first line", -> it "moves the cursor to the beginning of the line, but retains the goal column", -> diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index b2288d091..f0f6f1ffe 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -7,8 +7,8 @@ require 'window' afterEach -> (new Native).resetMainMenu() - # atom.globalKeymap.reset() - # $('#jasmine-content').empty() + atom.globalKeymap.reset() + $('#jasmine-content').empty() window.atom = new (require 'app') diff --git a/src/atom/cursor.coffee b/src/atom/cursor.coffee index 943cd02aa..021eaca89 100644 --- a/src/atom/cursor.coffee +++ b/src/atom/cursor.coffee @@ -68,7 +68,11 @@ class Cursor extends Template position = @parentView.pixelPositionFromPoint(@point) @css(position) - margin = @parentView.scrollMargin * @height() + linesInView = @parentView.height() / @height() + + maxScrollMargin = Math.floor((linesInView - 1) / 2) + scrollMargin = Math.min(@parentView.scrollMargin, maxScrollMargin) + margin = scrollMargin * @height() desiredTop = position.top - margin desiredBottom = position.top + @height() + margin