From e71027ed36ceb37cf5dc59438c31b502c50c4ae8 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 17 Aug 2016 17:58:36 +0200 Subject: [PATCH] Ensure editors don't scroll or show scrollbars when autoWidth is enabled --- spec/text-editor-presenter-spec.coffee | 88 ++++++++++++++++---------- src/text-editor-presenter.coffee | 18 ++++-- 2 files changed, 67 insertions(+), 39 deletions(-) diff --git a/spec/text-editor-presenter-spec.coffee b/spec/text-editor-presenter-spec.coffee index efa1c3393..784e18e1b 100644 --- a/spec/text-editor-presenter-spec.coffee +++ b/spec/text-editor-presenter-spec.coffee @@ -425,6 +425,13 @@ describe "TextEditorPresenter", -> editor.setMini(false) expect(getState(presenter).horizontalScrollbar.visible).toBe true + it "is false when `editor.autoWidth` is true", -> + editor.update({autoWidth: true}) + presenter = buildPresenter(explicitHeight: 10, contentFrameWidth: 30, verticalScrollbarWidth: 7, baseCharacterWidth: 10) + getState(presenter) # trigger a state update to store state in the presenter + editor.setText('abcdefghijklm') + expect(getState(presenter).horizontalScrollbar.visible).toBe(false) + describe ".height", -> it "tracks the value of ::horizontalScrollbarHeight", -> presenter = buildPresenter(horizontalScrollbarHeight: 10) @@ -538,6 +545,21 @@ describe "TextEditorPresenter", -> presenter.setScrollLeft(10) expect(getState(presenter).content.scrollLeft).toBe 0 + it "is always 0 when `editor.autoWidth` is true", -> + editor.update({autoWidth: true}) + editor.setText('abcdefghijklm') + presenter = buildPresenter(explicitHeight: 10, contentFrameWidth: 30, verticalScrollbarWidth: 15, baseCharacterWidth: 10) + getState(presenter) # trigger a state update to store state in the presenter + + editor.setCursorBufferPosition([0, Infinity]) + editor.insertText('n') + expect(getState(presenter).content.scrollLeft).toBe(0) + + editor.setText('abcdefghijklm\nnopqrstuvwxy') # make the vertical scrollbar appear + editor.setCursorBufferPosition([1, Infinity]) + editor.insertText('z') + expect(getState(presenter).content.scrollLeft).toBe(0) + describe ".verticalScrollbar", -> describe ".visible", -> it "is true if the scrollHeight exceeds the computed client height", -> @@ -755,6 +777,39 @@ describe "TextEditorPresenter", -> expect(getState(presenter).hiddenInput.width).toBe 2 describe ".content", -> + describe '.width', -> + describe "when `editor.autoWidth` is false (the default)", -> + it "equals to the max width between the content frame width and the content width + the vertical scrollbar width", -> + editor.setText('abc\ndef\nghi\njkl') + presenter = buildPresenter(explicitHeight: 10, contentFrameWidth: 33, verticalScrollbarWidth: 7, baseCharacterWidth: 10) + expect(getState(presenter).content.width).toBe(3 * 10 + 7 + 1) + presenter.setContentFrameWidth(50) + expect(getState(presenter).content.width).toBe(50) + presenter.setVerticalScrollbarWidth(27) + expect(getState(presenter).content.width).toBe(3 * 10 + 27 + 1) + + describe "when `editor.autoWidth` is true", -> + it "equals to the content width + the vertical scrollbar width", -> + editor.setText('abc\ndef\nghi\njkl') + presenter = buildPresenter(explicitHeight: 10, contentFrameWidth: 300, verticalScrollbarWidth: 7, baseCharacterWidth: 10) + expectStateUpdate presenter, -> editor.update({autoWidth: true}) + expect(getState(presenter).content.width).toBe(3 * 10 + 7 + 1) + editor.setText('abcdefghi\n') + expect(getState(presenter).content.width).toBe(9 * 10 + 7 + 1) + + it "ignores the vertical scrollbar width when it is unset", -> + editor.setText('abcdef\nghijkl') + presenter = buildPresenter(explicitHeight: 10, contentFrameWidth: 33, verticalScrollbarWidth: 7, baseCharacterWidth: 10) + presenter.setVerticalScrollbarWidth(null) + expect(getState(presenter).content.width).toBe(6 * 10 + 1) + + it "ignores the content frame width when it is unset", -> + editor.setText('abc\ndef\nghi\njkl') + presenter = buildPresenter(explicitHeight: 10, contentFrameWidth: 33, verticalScrollbarWidth: 7, baseCharacterWidth: 10) + getState(presenter) # trigger a state update, causing verticalScrollbarWidth to be stored in the presenter + presenter.setContentFrameWidth(null) + expect(getState(presenter).content.width).toBe(3 * 10 + 7 + 1) + describe ".maxHeight", -> it "changes based on boundingClientRect", -> presenter = buildPresenter(scrollTop: 0, lineHeight: 10) @@ -2664,39 +2719,6 @@ describe "TextEditorPresenter", -> pixelPosition: {top: 10, left: 0} } - describe ".width", -> - describe "when `editor.autoWidth` is false (the default)", -> - it "equals to the max width between the content frame width and the content width + the vertical scrollbar width", -> - editor.setText('abc\ndef\nghi\njkl') - presenter = buildPresenter(explicitHeight: 10, contentFrameWidth: 33, verticalScrollbarWidth: 7, baseCharacterWidth: 10) - expect(getState(presenter).content.width).toBe(3 * 10 + 7 + 1) - presenter.setContentFrameWidth(50) - expect(getState(presenter).content.width).toBe(50) - presenter.setVerticalScrollbarWidth(27) - expect(getState(presenter).content.width).toBe(3 * 10 + 27 + 1) - - describe "when `editor.autoWidth` is true", -> - it "equals to the content width + the vertical scrollbar width", -> - editor.setText('abc\ndef\nghi\njkl') - presenter = buildPresenter(explicitHeight: 10, contentFrameWidth: 300, verticalScrollbarWidth: 7, baseCharacterWidth: 10) - expectStateUpdate presenter, -> editor.update({autoWidth: true}) - expect(getState(presenter).content.width).toBe(3 * 10 + 7 + 1) - editor.setText('abcdefghi\n') - expect(getState(presenter).content.width).toBe(9 * 10 + 7 + 1) - - it "ignores the vertical scrollbar width when it is unset", -> - editor.setText('abcdef\nghijkl') - presenter = buildPresenter(explicitHeight: 10, contentFrameWidth: 33, verticalScrollbarWidth: 7, baseCharacterWidth: 10) - presenter.setVerticalScrollbarWidth(null) - expect(getState(presenter).content.width).toBe(6 * 10 + 1) - - it "ignores the content frame width when it is unset", -> - editor.setText('abc\ndef\nghi\njkl') - presenter = buildPresenter(explicitHeight: 10, contentFrameWidth: 33, verticalScrollbarWidth: 7, baseCharacterWidth: 10) - getState(presenter) # trigger a state update, causing verticalScrollbarWidth to be stored in the presenter - presenter.setContentFrameWidth(null) - expect(getState(presenter).content.width).toBe(3 * 10 + 7 + 1) - describe ".height", -> it "updates model's rows per page when it changes", -> presenter = buildPresenter(explicitHeight: 50, lineHeightInPixels: 10, horizontalScrollbarHeight: 10) diff --git a/src/text-editor-presenter.coffee b/src/text-editor-presenter.coffee index e60b1fc33..c65a735d8 100644 --- a/src/text-editor-presenter.coffee +++ b/src/text-editor-presenter.coffee @@ -669,6 +669,7 @@ class TextEditorPresenter if @contentWidth isnt oldContentWidth @updateScrollbarDimensions() + @updateClientWidth() @updateScrollWidth() updateClientHeight: -> @@ -685,7 +686,11 @@ class TextEditorPresenter updateClientWidth: -> return unless @contentFrameWidth? and @verticalScrollbarWidth? - clientWidth = @contentFrameWidth - @verticalScrollbarWidth + if @model.getAutoWidth() + clientWidth = @contentWidth + else + clientWidth = @contentFrameWidth - @verticalScrollbarWidth + @model.setWidth(clientWidth, true) unless @editorWidthInChars unless @clientWidth is clientWidth @@ -727,29 +732,30 @@ class TextEditorPresenter return unless @measuredVerticalScrollbarWidth? and @measuredHorizontalScrollbarHeight? return unless @contentWidth? and @contentHeight? - clientWidthWithVerticalScrollbar = @contentFrameWidth + if @model.getAutoWidth() + clientWidthWithVerticalScrollbar = @contentWidth + @measuredVerticalScrollbarWidth + else + clientWidthWithVerticalScrollbar = @contentFrameWidth clientWidthWithoutVerticalScrollbar = clientWidthWithVerticalScrollbar - @measuredVerticalScrollbarWidth clientHeightWithHorizontalScrollbar = @height clientHeightWithoutHorizontalScrollbar = clientHeightWithHorizontalScrollbar - @measuredHorizontalScrollbarHeight horizontalScrollbarVisible = - not @model.isMini() and (@contentWidth > clientWidthWithVerticalScrollbar or @contentWidth > clientWidthWithoutVerticalScrollbar and @contentHeight > clientHeightWithHorizontalScrollbar) verticalScrollbarVisible = - not @model.isMini() and (@contentHeight > clientHeightWithHorizontalScrollbar or @contentHeight > clientHeightWithoutHorizontalScrollbar and @contentWidth > clientWidthWithVerticalScrollbar) horizontalScrollbarHeight = - if horizontalScrollbarVisible + if horizontalScrollbarVisible and not @model.isMini() @measuredHorizontalScrollbarHeight else 0 verticalScrollbarWidth = - if verticalScrollbarVisible + if verticalScrollbarVisible and not @model.isMini() @measuredVerticalScrollbarWidth else 0