Ensure combined scroll margins are smaller than editor height.

This prevents jerky scrolling when the window is very short.
This commit is contained in:
Corey Johnson & Nathan Sobo
2012-01-23 17:15:11 -08:00
parent 1a52890d19
commit 5ddf4e1a0b
3 changed files with 35 additions and 20 deletions

View File

@@ -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", ->

View File

@@ -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')

View File

@@ -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