From ee9d4ab70eeb1663798eb8cd7f9dab9db940f5f9 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki & Nathan Sobo Date: Wed, 21 May 2014 17:15:47 -0600 Subject: [PATCH] Don't measure lineHeight/defaultCharWidth when editor is hidden --- spec/editor-component-spec.coffee | 21 +++++++++++++++++++++ src/editor-component.coffee | 13 ++++++++++--- src/editor-scroll-view-component.coffee | 5 +++-- src/lines-component.coffee | 6 ++++-- src/react-editor-view.coffee | 8 ++++++++ 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/spec/editor-component-spec.coffee b/spec/editor-component-spec.coffee index d6bddd95d..d9f9e7681 100644 --- a/spec/editor-component-spec.coffee +++ b/spec/editor-component-spec.coffee @@ -764,3 +764,24 @@ describe "EditorComponent", -> expect(editor.consolidateSelections).toHaveBeenCalled() expect(event.abortKeyBinding).toHaveBeenCalled() + + describe "hiding and showing the editor", -> + describe "when fontSize, fontFamily, or lineHeight changes while the editor is hidden", -> + it "does not attempt to measure the lineHeight and defaultCharWidth until the editor becomes visible again", -> + wrapperView.hide() + initialLineHeight = editor.getLineHeight() + initialCharWidth = editor.getDefaultCharWidth() + + component.setLineHeight(2) + expect(editor.getLineHeight()).toBe initialLineHeight + expect(editor.getDefaultCharWidth()).toBe initialCharWidth + component.setFontSize(22) + expect(editor.getLineHeight()).toBe initialLineHeight + expect(editor.getDefaultCharWidth()).toBe initialCharWidth + component.setFontFamily('monospace') + expect(editor.getLineHeight()).toBe initialLineHeight + expect(editor.getDefaultCharWidth()).toBe initialCharWidth + + wrapperView.show() + expect(editor.getLineHeight()).not.toBe initialLineHeight + expect(editor.getDefaultCharWidth()).not.toBe initialCharWidth diff --git a/src/editor-component.coffee b/src/editor-component.coffee index bf5904d53..30824de44 100644 --- a/src/editor-component.coffee +++ b/src/editor-component.coffee @@ -31,7 +31,7 @@ EditorComponent = React.createClass mouseWheelScreenRow: null render: -> - {focused, fontSize, lineHeight, fontFamily, showIndentGuide, showInvisibles} = @state + {focused, fontSize, lineHeight, fontFamily, showIndentGuide, showInvisibles, visible} = @state {editor, cursorBlinkPeriod, cursorBlinkResumeDelay} = @props maxLineNumberDigits = editor.getScreenLineCount().toString().length invisibles = if showInvisibles then @state.invisibles else {} @@ -64,7 +64,7 @@ EditorComponent = React.createClass scrollTop, scrollLeft, scrollHeight, scrollWidth, @scrollingVertically, @cursorsMoved, @selectionChanged, @selectionAdded, cursorBlinkPeriod, cursorBlinkResumeDelay, @onInputFocused, @onInputBlurred, @mouseWheelScreenRow, - invisibles + invisibles, visible } ScrollbarComponent @@ -106,7 +106,8 @@ EditorComponent = React.createClass renderedEndRow = Math.min(editor.getScreenLineCount(), visibleEndRow + lineOverdrawMargin) [renderedStartRow, renderedEndRow] - getInitialState: -> {} + getInitialState: -> + visible: true getDefaultProps: -> cursorBlinkPeriod: 800 @@ -467,3 +468,9 @@ EditorComponent = React.createClass lineNodeForScreenRow: (screenRow) -> @refs.scrollView.lineNodeForScreenRow(screenRow) lineNumberNodeForScreenRow: (screenRow) -> @refs.gutter.lineNumberNodeForScreenRow(screenRow) + + hide: -> + @setState(visible: false) + + show: -> + @setState(visible: true) diff --git a/src/editor-scroll-view-component.coffee b/src/editor-scroll-view-component.coffee index 81b37ce8b..daba02c92 100644 --- a/src/editor-scroll-view-component.coffee +++ b/src/editor-scroll-view-component.coffee @@ -16,7 +16,7 @@ EditorScrollViewComponent = React.createClass overflowChangedWhilePaused: false render: -> - {editor, fontSize, fontFamily, lineHeight, showIndentGuide, invisibles} = @props + {editor, fontSize, fontFamily, lineHeight, showIndentGuide, invisibles, visible} = @props {renderedRowRange, pendingChanges, scrollTop, scrollLeft, scrollHeight, scrollWidth, scrollingVertically, mouseWheelScreenRow} = @props {selectionChanged, selectionAdded, cursorBlinkPeriod, cursorBlinkResumeDelay, cursorsMoved, onInputFocused, onInputBlurred} = @props @@ -37,7 +37,8 @@ EditorScrollViewComponent = React.createClass LinesComponent { ref: 'lines', editor, fontSize, fontFamily, lineHeight, showIndentGuide, renderedRowRange, pendingChanges, scrollTop, scrollLeft, scrollingVertically, - selectionChanged, scrollHeight, scrollWidth, mouseWheelScreenRow, invisibles + selectionChanged, scrollHeight, scrollWidth, mouseWheelScreenRow, invisibles, + visible } componentDidMount: -> diff --git a/src/lines-component.coffee b/src/lines-component.coffee index 8b4dc29fa..6b6d37f7b 100644 --- a/src/lines-component.coffee +++ b/src/lines-component.coffee @@ -35,7 +35,7 @@ LinesComponent = React.createClass shouldComponentUpdate: (newProps) -> return true if newProps.selectionChanged - return true unless isEqualForProperties(newProps, @props, 'renderedRowRange', 'fontSize', 'fontFamily', 'lineHeight', 'scrollTop', 'scrollLeft', 'showIndentGuide', 'scrollingVertically', 'invisibles') + return true unless isEqualForProperties(newProps, @props, 'renderedRowRange', 'fontSize', 'fontFamily', 'lineHeight', 'scrollTop', 'scrollLeft', 'showIndentGuide', 'scrollingVertically', 'invisibles', 'visible') {renderedRowRange, pendingChanges} = newProps for change in pendingChanges @@ -44,7 +44,9 @@ LinesComponent = React.createClass false componentDidUpdate: (prevProps) -> - @measureLineHeightAndCharWidth() unless isEqualForProperties(prevProps, @props, 'fontSize', 'fontFamily', 'lineHeight') + {visible} = @props + if visible and not isEqualForProperties(prevProps, @props, 'fontSize', 'fontFamily', 'lineHeight', 'visible') + @measureLineHeightAndCharWidth() @clearScreenRowCaches() unless prevProps.lineHeight is @props.lineHeight @removeLineNodes() unless isEqualForProperties(prevProps, @props, 'showIndentGuide', 'invisibles') @updateLines() diff --git a/src/react-editor-view.coffee b/src/react-editor-view.coffee index 87e5d0693..c301c6f40 100644 --- a/src/react-editor-view.coffee +++ b/src/react-editor-view.coffee @@ -82,3 +82,11 @@ class ReactEditorView extends View @component.onFocus() else @focusOnAttach = true + + hide: -> + super + @component.hide() + + show: -> + super + @component.show()