From f9c7c08641d4c9c0075c13dff505f7db8c2a8a8c Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 29 Nov 2012 18:52:34 -0700 Subject: [PATCH] Selected results of a find operation are centered in the viewport --- spec/app/editor-spec.coffee | 6 ++++ src/app/editor.coffee | 36 +++++++++++-------- src/app/selection-view.coffee | 7 ++++ src/app/selection.coffee | 10 ++++-- .../src/commands/composite-command.coffee | 2 +- 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index 2ccc12e2c..eeb5f4804 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -940,6 +940,12 @@ describe "Editor", -> editor.moveCursorDown() 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) + editor.setSelectedBufferRange([[6, 0], [8, 0]], autoscroll: true) + expect(editor.scrollTop()).toBe 3 * editor.lineHeight + describe "cursor rendering", -> describe "when the cursor moves", -> charWidth = null diff --git a/src/app/editor.coffee b/src/app/editor.coffee index f9f293b91..be9bb936f 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -495,24 +495,28 @@ class Editor extends View scrollToBottom: -> @scrollBottom(@screenLineCount() * @lineHeight) - scrollTo: (pixelPosition) -> + scrollTo: (pixelPosition, options) -> return unless @attached - @scrollVertically(pixelPosition) + @scrollVertically(pixelPosition, options) @scrollHorizontally(pixelPosition) - scrollVertically: (pixelPosition) -> - linesInView = @scrollView.height() / @lineHeight - maxScrollMargin = Math.floor((linesInView - 1) / 2) - scrollMargin = Math.min(@vScrollMargin, maxScrollMargin) - margin = scrollMargin * @lineHeight - desiredTop = pixelPosition.top - margin - desiredBottom = pixelPosition.top + @lineHeight + margin - + scrollVertically: (pixelPosition, {center}={}) -> scrollViewHeight = @scrollView.height() - if desiredBottom > @scrollTop() + scrollViewHeight - @scrollTop(desiredBottom - scrollViewHeight) - else if desiredTop < @scrollTop() - @scrollTop(desiredTop) + + if center + @scrollTop(pixelPosition.top - (scrollViewHeight / 2)) + else + linesInView = @scrollView.height() / @lineHeight + maxScrollMargin = Math.floor((linesInView - 1) / 2) + scrollMargin = Math.min(@vScrollMargin, maxScrollMargin) + margin = scrollMargin * @lineHeight + desiredTop = pixelPosition.top - margin + desiredBottom = pixelPosition.top + @lineHeight + margin + + if desiredBottom > @scrollTop() + scrollViewHeight + @scrollTop(desiredBottom - scrollViewHeight) + else if desiredTop < @scrollTop() + @scrollTop(desiredTop) scrollHorizontally: (pixelPosition) -> return if @activeEditSession.getSoftWrap() @@ -792,6 +796,10 @@ class Editor extends View @scrollTo(cursorView.getPixelPosition()) unless options.suppressAutoScroll cursorView.needsAutoscroll = false + for selectionView in @getSelectionViews() when selectionView.selection.needsAutoscroll + @scrollTo(selectionView.getCenterPixelPosition(), center: true) + selectionView.selection.needsAutoscroll = false + updateRenderedLines: -> firstVisibleScreenRow = @getFirstVisibleScreenRow() lastVisibleScreenRow = @getLastVisibleScreenRow() diff --git a/src/app/selection-view.coffee b/src/app/selection-view.coffee index 5a4a1d002..3e9a404b8 100644 --- a/src/app/selection-view.coffee +++ b/src/app/selection-view.coffee @@ -49,6 +49,13 @@ class SelectionView extends View @append(region) @regions.push(region) + getCenterPixelPosition: -> + { start, end } = @getScreenRange() + startRow = start.row + endRow = end.row + endRow-- if end.column == 0 + @editor.pixelPositionForScreenPosition([((startRow + endRow + 1) / 2), start.column]) + clearRegions: -> region.remove() for region in @regions @regions = [] diff --git a/src/app/selection.coffee b/src/app/selection.coffee index 9c6948e21..e7a59d077 100644 --- a/src/app/selection.coffee +++ b/src/app/selection.coffee @@ -63,11 +63,13 @@ class Selection { start, end } = bufferRange [start, end] = [end, start] if options.reverse ? @isReversed() + @needsAutoscroll = options.autoscroll + @editSession.destroyFoldsIntersectingBufferRange(bufferRange) unless options.preserveFolds @placeAnchor() unless @anchor @modifySelection => - @anchor.setBufferPosition(start, options) - @cursor.setBufferPosition(end, options) + @anchor.setBufferPosition(start, autoscroll: false) + @cursor.setBufferPosition(end, autoscroll: false) getBufferRowRange: -> range = @getBufferRange() @@ -334,7 +336,11 @@ class Selection modifySelection: (fn) -> @retainSelection = true @placeAnchor() unless @anchor + @anchor.pauseEvents() + @cursor.pauseEvents() fn() + @anchor.resumeEvents() + @cursor.resumeEvents() @retainSelection = false placeAnchor: -> diff --git a/src/extensions/command-panel/src/commands/composite-command.coffee b/src/extensions/command-panel/src/commands/composite-command.coffee index e80f12a1b..f037a4a20 100644 --- a/src/extensions/command-panel/src/commands/composite-command.coffee +++ b/src/extensions/command-panel/src/commands/composite-command.coffee @@ -38,7 +38,7 @@ class CompositeCommand operation.destroy() if bufferRanges.length and not currentCommand.preserveSelections - editSession.setSelectedBufferRanges(bufferRanges) + editSession.setSelectedBufferRanges(bufferRanges, autoscroll: true) deferred.resolve({errorMessages})