From a72dc17b87f0dfabdd5db8ff8289104ac019084f Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 31 Jan 2012 11:21:08 -0700 Subject: [PATCH] Move hidden input to follow cursor to stop spurious scrolling Commit 8e77c05c fixed scrolling *up* when clicking the buffer while scrolled down. But it was still scrolling to the far left when typing / clicking on a really long line. This commit solves both issues by always positioning the hidden input to the same location as the cursor, so it doesn't tell webkit to scroll away from the current focus when it is focused or the user types. --- spec/atom/editor-spec.coffee | 7 ++++++- src/atom/editor.coffee | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index c4d6f2790..d28c18521 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -50,13 +50,18 @@ describe "Editor", -> describe "cursor movement", -> describe ".setCursorPosition({row, column})", -> - it "moves the cursor to cover the character at the given row and column", -> + beforeEach -> editor.attachToDom() editor.setCursorPosition(row: 2, column: 2) + it "moves the cursor to cover the character at the given row and column", -> expect(editor.getCursor().position().top).toBe(2 * editor.lineHeight) expect(editor.getCursor().position().left).toBe(2 * editor.charWidth) + it "moves the hidden input element to the position of the cursor to prevent scrolling misbehavior", -> + expect(editor.hiddenInput.position().top).toBe(2 * editor.lineHeight) + expect(editor.hiddenInput.position().left).toBe(2 * editor.charWidth) + describe "when the arrow keys are pressed", -> it "moves the cursor by a single row/column", -> editor.trigger keydownEvent('right') diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index be207d0e9..5ef8f7756 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -66,7 +66,6 @@ class Editor extends Template handleEvents: -> @on 'focus', => - @hiddenInput.css(top: @scrollTop()) @hiddenInput.focus() false @@ -79,8 +78,12 @@ class Editor extends Template @hiddenInput.on "textInput", (e) => @insertText(e.originalEvent.data) + @on 'cursor:position-changed', => + @hiddenInput.css(@pixelPositionFromPoint(@cursor.getPosition())) + @one 'attach', => @calculateDimensions() + @hiddenInput.width(@charWidth) @focus() buildLineElement: (lineText) ->