From 2489e19bb7173b1b1c30f8db8831f341b079d799 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 4 Apr 2012 14:08:21 -0700 Subject: [PATCH 1/2] CompositeCursor.addCursor takes an optional screenPosition argument --- spec/app/editor-spec.coffee | 6 ++++++ src/app/anchor.coffee | 10 +++++++--- src/app/composite-cursor.coffee | 11 +++++------ src/app/cursor.coffee | 5 +++-- src/app/editor.coffee | 6 +++--- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index 6b417f8bf..b1c159bed 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -1002,8 +1002,14 @@ describe "Editor", -> describe "multiple cursors", -> it "places multiple cursor with meta-click", -> editor.attachToDom() + setEditorHeightInChars(editor, 5) editor.lines.trigger mousedownEvent(editor: editor, point: [3, 0]) + editor.scroller.scrollTop(editor.lineHeight * 6) + + spyOn(editor, "scrollTo").andCallThrough() + editor.lines.trigger mousedownEvent(editor: editor, point: [6, 0], metaKey: true) + expect(editor.scrollTo.callCount).toBe 1 [cursor1, cursor2] = editor.find('.cursor').map -> $(this).view() expect(cursor1.position()).toEqual(top: 3 * editor.lineHeight, left: 0) diff --git a/src/app/anchor.coffee b/src/app/anchor.coffee index d1eeb105f..ccc625004 100644 --- a/src/app/anchor.coffee +++ b/src/app/anchor.coffee @@ -6,10 +6,14 @@ class Anchor bufferPosition: null screenPosition: null - constructor: (editor) -> + constructor: (editor, screenPosition) -> @editor = editor - @bufferPosition = new Point(0, 0) - @screenPosition = new Point(0, 0) + + if screenPosition + @setScreenPosition(screenPosition) + else + @bufferPosition = new Point(0,0) + @screenPosition = new Point(0,0) handleBufferChange: (e) -> { oldRange, newRange } = e diff --git a/src/app/composite-cursor.coffee b/src/app/composite-cursor.coffee index ac3166557..e4399b4ac 100644 --- a/src/app/composite-cursor.coffee +++ b/src/app/composite-cursor.coffee @@ -17,19 +17,18 @@ class CompositeCursor getCursors: -> @cursors - addCursor: -> - cursor = new Cursor(@editor) + addCursor: (screenPosition=null) -> + cursor = new Cursor({@editor, screenPosition}) @cursors.push(cursor) @editor.lines.append(cursor) cursor addCursorAtScreenPosition: (screenPosition) -> - cursor = @addCursor() - cursor.setScreenPosition(screenPosition) + cursor = @addCursor(screenPosition) addCursorAtBufferPosition: (bufferPosition) -> - cursor = @addCursor() - cursor.setBufferPosition(bufferPosition) + screenPosition = @editor.screenPositionForBufferPosition(bufferPosition) + cursor = @addCursor(screenPosition) removeCursor: (cursor) -> _.remove(@cursors, cursor) diff --git a/src/app/cursor.coffee b/src/app/cursor.coffee index deb545d7c..6b48c488a 100644 --- a/src/app/cursor.coffee +++ b/src/app/cursor.coffee @@ -12,8 +12,9 @@ class Cursor extends View editor: null wordRegex: /(\w+)|([^\w\s]+)/g - initialize: (@editor) -> - @anchor = new Anchor(@editor) + initialize: ({editor, screenPosition}) -> + @editor = editor + @anchor = new Anchor(@editor, screenPosition) @selection = @editor.compositeSelection.addSelectionForCursor(this) @one 'attach', => @updateAppearance() diff --git a/src/app/editor.coffee b/src/app/editor.coffee index a9fb0af98..3d6de5c69 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -482,9 +482,9 @@ class Editor extends View @buffer.getMode() scrollTo: (pixelPosition) -> - _.defer => - @scrollVertically(pixelPosition) - @scrollHorizontally(pixelPosition) + _.defer => # Optimization + @scrollVertically(pixelPosition) + @scrollHorizontally(pixelPosition) scrollVertically: (pixelPosition) -> linesInView = @scroller.height() / @lineHeight From fb02290972b4a33191314641118c5fd849a91f6f Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 4 Apr 2012 15:01:38 -0700 Subject: [PATCH 2/2] Sync multiple cursor animations --- src/app/cursor.coffee | 9 ++++++++- src/app/editor.coffee | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/app/cursor.coffee b/src/app/cursor.coffee index 6b48c488a..37d4d795a 100644 --- a/src/app/cursor.coffee +++ b/src/app/cursor.coffee @@ -16,7 +16,9 @@ class Cursor extends View @editor = editor @anchor = new Anchor(@editor, screenPosition) @selection = @editor.compositeSelection.addSelectionForCursor(this) - @one 'attach', => @updateAppearance() + @one 'attach', => + @updateAppearance() + @editor.syncCursorAnimations() handleBufferChange: (e) -> @anchor.handleBufferChange(e) @@ -52,6 +54,11 @@ class Cursor extends View window.clearTimeout(@idleTimeout) if @idleTimeout @idleTimeout = window.setTimeout (=> @addClass 'idle'), 200 + resetCursorAnimation: -> + window.clearTimeout(@idleTimeout) if @idleTimeout + @removeClass 'idle' + _.defer => @addClass 'idle' + clearSelection: -> @selection.clearSelection() unless @selection.retainSelection diff --git a/src/app/editor.coffee b/src/app/editor.coffee index 3d6de5c69..9ba80fe3a 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -186,6 +186,7 @@ class Editor extends View @off 'mousemove', moveHandler reverse = @compositeSelection.getLastSelection().isReversed() @compositeSelection.mergeIntersectingSelections({reverse}) + @syncCursorAnimations() renderLines: -> @lineCache = [] @@ -514,5 +515,9 @@ class Editor extends View else if desiredLeft < @scroller.scrollLeft() @scroller.scrollLeft(desiredLeft) + syncCursorAnimations: -> + for cursor in @getCursors() + do (cursor) -> cursor.resetCursorAnimation() + logLines: -> @renderer.logLines()