From 57a006d19b0a58a50e8222f8ad631f25e48fce0c Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 23 Sep 2015 09:39:33 +0200 Subject: [PATCH] Start porting scroll-related specs --- spec/text-editor-component-spec.coffee | 166 +++++++++++++++++++++++++ spec/text-editor-spec.coffee | 112 ----------------- src/text-editor-component.coffee | 6 + src/text-editor-element.coffee | 6 + src/text-editor-presenter.coffee | 11 +- 5 files changed, 188 insertions(+), 113 deletions(-) diff --git a/spec/text-editor-component-spec.coffee b/spec/text-editor-component-spec.coffee index 374fab15f..455d2cca1 100644 --- a/spec/text-editor-component-spec.coffee +++ b/spec/text-editor-component-spec.coffee @@ -3371,6 +3371,172 @@ describe "TextEditorComponent", -> expect(line1LeafNodes[0].classList.contains('indent-guide')).toBe false expect(line1LeafNodes[1].classList.contains('indent-guide')).toBe false + fffdescribe "autoscroll", -> + beforeEach -> + editor.setVerticalScrollMargin(2) + editor.setHorizontalScrollMargin(2) + component.setLineHeight("10px") + component.setFontSize(17) + component.measureDimensions() + nextAnimationFrame() + + # Why does this set an incorrect width? :confused: + wrapperNode.style.width = "108px" + wrapperNode.style.height = 5.5 * 10 + "px" + component.measureDimensions() + nextAnimationFrame() + + component.presenter.setHorizontalScrollbarHeight(0) + component.presenter.setVerticalScrollbarWidth(0) + nextAnimationFrame() # perform requested update + + afterEach -> + atom.themes.removeStylesheet("test") + + describe "moving cursors", -> + it "scrolls down when the last cursor gets closer than ::verticalScrollMargin to the bottom of the editor", -> + expect(wrapperNode.getScrollTop()).toBe 0 + expect(wrapperNode.getScrollBottom()).toBe 5.5 * 10 + + editor.setCursorScreenPosition([2, 0]) + nextAnimationFrame() + expect(wrapperNode.getScrollBottom()).toBe 5.5 * 10 + + editor.moveDown() + nextAnimationFrame() + expect(wrapperNode.getScrollBottom()).toBe 6 * 10 + + editor.moveDown() + nextAnimationFrame() + expect(wrapperNode.getScrollBottom()).toBe 7 * 10 + + it "scrolls up when the last cursor gets closer than ::verticalScrollMargin to the top of the editor", -> + editor.setCursorScreenPosition([11, 0]) + nextAnimationFrame() + wrapperNode.setScrollBottom(wrapperNode.getScrollHeight()) + nextAnimationFrame() + + editor.moveUp() + nextAnimationFrame() + expect(wrapperNode.getScrollBottom()).toBe wrapperNode.getScrollHeight() + + editor.moveUp() + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 7 * 10 + + editor.moveUp() + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 6 * 10 + + it "scrolls right when the last cursor gets closer than ::horizontalScrollMargin to the right of the editor", -> + expect(wrapperNode.getScrollLeft()).toBe 0 + expect(wrapperNode.getScrollRight()).toBe 5.5 * 10 + + editor.setCursorScreenPosition([0, 2]) + nextAnimationFrame() + expect(wrapperNode.getScrollRight()).toBe 5.5 * 10 + + editor.moveRight() + nextAnimationFrame() + expect(wrapperNode.getScrollRight()).toBe 6 * 10 + + editor.moveRight() + nextAnimationFrame() + expect(wrapperNode.getScrollRight()).toBe 7 * 10 + + it "scrolls left when the last cursor gets closer than ::horizontalScrollMargin to the left of the editor", -> + wrapperNode.setScrollRight(wrapperNode.getScrollWidth()) + nextAnimationFrame() + expect(wrapperNode.getScrollRight()).toBe wrapperNode.getScrollWidth() + editor.setCursorScreenPosition([6, 62], autoscroll: false) + nextAnimationFrame() + + editor.moveLeft() + nextAnimationFrame() + expect(wrapperNode.getScrollLeft()).toBe 59 * 10 + + editor.moveLeft() + nextAnimationFrame() + expect(wrapperNode.getScrollLeft()).toBe 58 * 10 + + it "scrolls down when inserting lines makes the document longer than the editor's height", -> + editor.setCursorScreenPosition([13, Infinity]) + editor.insertNewline() + nextAnimationFrame() + + expect(wrapperNode.getScrollBottom()).toBe 14 * 10 + editor.insertNewline() + nextAnimationFrame() + expect(wrapperNode.getScrollBottom()).toBe 15 * 10 + + it "autoscrolls to the cursor when it moves due to undo", -> + editor.insertText('abc') + wrapperNode.setScrollTop(Infinity) + nextAnimationFrame() + + editor.undo() + nextAnimationFrame() + + expect(wrapperNode.getScrollTop()).toBe 0 + + it "doesn't scroll when the cursor moves into the visible area", -> + editor.setCursorBufferPosition([0, 0]) + nextAnimationFrame() + + wrapperNode.setScrollTop(40) + nextAnimationFrame() + + editor.setCursorBufferPosition([6, 0]) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 40 + + it "honors the autoscroll option on cursor and selection manipulation methods", -> + expect(wrapperNode.getScrollTop()).toBe 0 + editor.addCursorAtScreenPosition([11, 11], autoscroll: false) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 0 + editor.addCursorAtBufferPosition([11, 11], autoscroll: false) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 0 + editor.setCursorScreenPosition([11, 11], autoscroll: false) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 0 + editor.setCursorBufferPosition([11, 11], autoscroll: false) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 0 + editor.addSelectionForBufferRange([[11, 11], [11, 11]], autoscroll: false) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 0 + editor.addSelectionForScreenRange([[11, 11], [11, 12]], autoscroll: false) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 0 + editor.setSelectedBufferRange([[11, 0], [11, 1]], autoscroll: false) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 0 + editor.setSelectedScreenRange([[11, 0], [11, 6]], autoscroll: false) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 0 + editor.clearSelections(autoscroll: false) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 0 + + editor.addSelectionForScreenRange([[0, 0], [0, 4]]) + nextAnimationFrame() + + editor.getCursors()[0].setScreenPosition([11, 11], autoscroll: true) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBeGreaterThan 0 + editor.getCursors()[0].setBufferPosition([0, 0], autoscroll: true) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 0 + editor.getSelections()[0].setScreenRange([[11, 0], [11, 4]], autoscroll: true) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBeGreaterThan 0 + editor.getSelections()[0].setBufferRange([[0, 0], [0, 4]], autoscroll: true) + nextAnimationFrame() + expect(wrapperNode.getScrollTop()).toBe 0 + + describe "middle mouse paste on Linux", -> originalPlatform = null diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index 203b138cd..c73926b41 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -909,118 +909,6 @@ describe "TextEditor", -> cursor2 = editor.addCursorAtBufferPosition([1, 4]) expect(cursor2.marker).toBe cursor1.marker - describe "autoscroll", -> - beforeEach -> - editor.setVerticalScrollMargin(2) - editor.setHorizontalScrollMargin(2) - editor.setLineHeightInPixels(10) - editor.setDefaultCharWidth(10) - editor.setHorizontalScrollbarHeight(0) - editor.setHeight(5.5 * 10) - editor.setWidth(5.5 * 10) - - it "scrolls down when the last cursor gets closer than ::verticalScrollMargin to the bottom of the editor", -> - expect(editor.getScrollTop()).toBe 0 - expect(editor.getScrollBottom()).toBe 5.5 * 10 - - editor.setCursorScreenPosition([2, 0]) - expect(editor.getScrollBottom()).toBe 5.5 * 10 - - editor.moveDown() - expect(editor.getScrollBottom()).toBe 6 * 10 - - editor.moveDown() - expect(editor.getScrollBottom()).toBe 7 * 10 - - it "scrolls up when the last cursor gets closer than ::verticalScrollMargin to the top of the editor", -> - editor.setCursorScreenPosition([11, 0]) - editor.setScrollBottom(editor.getScrollHeight()) - - editor.moveUp() - expect(editor.getScrollBottom()).toBe editor.getScrollHeight() - - editor.moveUp() - expect(editor.getScrollTop()).toBe 7 * 10 - - editor.moveUp() - expect(editor.getScrollTop()).toBe 6 * 10 - - it "scrolls right when the last cursor gets closer than ::horizontalScrollMargin to the right of the editor", -> - expect(editor.getScrollLeft()).toBe 0 - expect(editor.getScrollRight()).toBe 5.5 * 10 - - editor.setCursorScreenPosition([0, 2]) - expect(editor.getScrollRight()).toBe 5.5 * 10 - - editor.moveRight() - expect(editor.getScrollRight()).toBe 6 * 10 - - editor.moveRight() - expect(editor.getScrollRight()).toBe 7 * 10 - - it "scrolls left when the last cursor gets closer than ::horizontalScrollMargin to the left of the editor", -> - editor.setScrollRight(editor.getScrollWidth()) - expect(editor.getScrollRight()).toBe editor.getScrollWidth() - editor.setCursorScreenPosition([6, 62], autoscroll: false) - - editor.moveLeft() - expect(editor.getScrollLeft()).toBe 59 * 10 - - editor.moveLeft() - expect(editor.getScrollLeft()).toBe 58 * 10 - - it "scrolls down when inserting lines makes the document longer than the editor's height", -> - editor.setCursorScreenPosition([13, Infinity]) - editor.insertNewline() - expect(editor.getScrollBottom()).toBe 14 * 10 - editor.insertNewline() - expect(editor.getScrollBottom()).toBe 15 * 10 - - it "autoscrolls to the cursor when it moves due to undo", -> - editor.insertText('abc') - editor.setScrollTop(Infinity) - editor.undo() - expect(editor.getScrollTop()).toBe 0 - - it "doesn't scroll when the cursor moves into the visible area", -> - editor.setCursorBufferPosition([0, 0]) - editor.setScrollTop(40) - expect(editor.getVisibleRowRange()).toEqual([4, 9]) - editor.setCursorBufferPosition([6, 0]) - expect(editor.getScrollTop()).toBe 40 - - it "honors the autoscroll option on cursor and selection manipulation methods", -> - expect(editor.getScrollTop()).toBe 0 - editor.addCursorAtScreenPosition([11, 11], autoscroll: false) - expect(editor.getScrollTop()).toBe 0 - editor.addCursorAtBufferPosition([11, 11], autoscroll: false) - expect(editor.getScrollTop()).toBe 0 - editor.setCursorScreenPosition([11, 11], autoscroll: false) - expect(editor.getScrollTop()).toBe 0 - editor.setCursorBufferPosition([11, 11], autoscroll: false) - expect(editor.getScrollTop()).toBe 0 - editor.addSelectionForBufferRange([[11, 11], [11, 11]], autoscroll: false) - expect(editor.getScrollTop()).toBe 0 - editor.addSelectionForScreenRange([[11, 11], [11, 12]], autoscroll: false) - expect(editor.getScrollTop()).toBe 0 - editor.setSelectedBufferRange([[11, 0], [11, 1]], autoscroll: false) - expect(editor.getScrollTop()).toBe 0 - editor.setSelectedScreenRange([[11, 0], [11, 6]], autoscroll: false) - expect(editor.getScrollTop()).toBe 0 - editor.clearSelections(autoscroll: false) - expect(editor.getScrollTop()).toBe 0 - - editor.addSelectionForScreenRange([[0, 0], [0, 4]]) - - editor.getCursors()[0].setScreenPosition([11, 11], autoscroll: true) - expect(editor.getScrollTop()).toBeGreaterThan 0 - editor.getCursors()[0].setBufferPosition([0, 0], autoscroll: true) - expect(editor.getScrollTop()).toBe 0 - editor.getSelections()[0].setScreenRange([[11, 0], [11, 4]], autoscroll: true) - expect(editor.getScrollTop()).toBeGreaterThan 0 - editor.getSelections()[0].setBufferRange([[0, 0], [0, 4]], autoscroll: true) - expect(editor.getScrollTop()).toBe 0 - describe '.logCursorScope()', -> beforeEach -> spyOn(atom.notifications, 'addInfo') diff --git a/src/text-editor-component.coffee b/src/text-editor-component.coffee index f06e0c339..cb6329f05 100644 --- a/src/text-editor-component.coffee +++ b/src/text-editor-component.coffee @@ -382,6 +382,12 @@ class TextEditorComponent getScrollBottom: -> @presenter.getScrollBottom() + getScrollHeight: -> + @presenter.getScrollHeight() + + getScrollWidth: -> + @presenter.getScrollWidth() + onMouseDown: (event) => unless event.button is 0 or (event.button is 1 and process.platform is 'linux') # Only handle mouse down events for left mouse button on all platforms diff --git a/src/text-editor-element.coffee b/src/text-editor-element.coffee index c2adc83dc..96e1d3969 100644 --- a/src/text-editor-element.coffee +++ b/src/text-editor-element.coffee @@ -243,6 +243,12 @@ class TextEditorElement extends HTMLElement getScrollBottom: -> @component.getScrollBottom() + getScrollHeight: -> + @component.getScrollHeight() + + getScrollWidth: -> + @component.getScrollWidth() + stopEventPropagation = (commandListeners) -> newCommandListeners = {} for commandName, commandListener of commandListeners diff --git a/src/text-editor-presenter.coffee b/src/text-editor-presenter.coffee index 58caaa6e5..6c6f56881 100644 --- a/src/text-editor-presenter.coffee +++ b/src/text-editor-presenter.coffee @@ -870,7 +870,10 @@ class TextEditorPresenter @explicitHeight - @horizontalScrollbarHeight getClientWidth: -> - @clientWidth ? @contentFrameWidth + if @clientWidth + @clientWidth + else + @contentFrameWidth - @verticalScrollbarWidth getScrollBottom: -> @getScrollTop() + @getClientHeight() setScrollBottom: (scrollBottom) -> @@ -882,6 +885,12 @@ class TextEditorPresenter @setScrollLeft(scrollRight - @getClientWidth()) @getScrollRight() + getScrollHeight: -> + @scrollHeight + + getScrollWidth: -> + @scrollWidth + setHorizontalScrollbarHeight: (horizontalScrollbarHeight) -> unless @measuredHorizontalScrollbarHeight is horizontalScrollbarHeight oldHorizontalScrollbarHeight = @measuredHorizontalScrollbarHeight