From 2e37d7f0cf2a1d6842dae3e3b15fa702b8d862bb Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 17 Aug 2016 16:14:57 +0200 Subject: [PATCH] Change state.content.width based on autoWidth --- spec/text-editor-presenter-spec.coffee | 33 ++++++++++++++++++++++++++ src/text-editor-presenter.coffee | 9 ++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/spec/text-editor-presenter-spec.coffee b/spec/text-editor-presenter-spec.coffee index e04eadaa2..13e6d218e 100644 --- a/spec/text-editor-presenter-spec.coffee +++ b/spec/text-editor-presenter-spec.coffee @@ -2664,6 +2664,39 @@ 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 width of the content + 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 4f5ca74ef..51fb44345 100644 --- a/src/text-editor-presenter.coffee +++ b/src/text-editor-presenter.coffee @@ -269,7 +269,14 @@ class TextEditorPresenter @sharedGutterStyles.maxHeight = @boundingClientRect.height @state.content.maxHeight = @boundingClientRect.height - @state.content.width = Math.max(@contentWidth + @verticalScrollbarWidth, @contentFrameWidth) + verticalScrollbarWidth = @verticalScrollbarWidth ? 0 + contentFrameWidth = @contentFrameWidth ? 0 + contentWidth = @contentWidth ? 0 + if @model.getAutoWidth() + @state.content.width = contentWidth + verticalScrollbarWidth + else + @state.content.width = Math.max(contentWidth + verticalScrollbarWidth, contentFrameWidth) + @state.content.autoWidth = @model.getAutoWidth() @state.content.scrollWidth = @scrollWidth @state.content.scrollLeft = @scrollLeft @state.content.backgroundColor = if @model.isMini() then null else @backgroundColor