diff --git a/src/text-editor-presenter.coffee b/src/text-editor-presenter.coffee index 9e41bf85c..169d95541 100644 --- a/src/text-editor-presenter.coffee +++ b/src/text-editor-presenter.coffee @@ -94,6 +94,8 @@ class TextEditorPresenter @updateCustomGutterDecorationState() if @shouldUpdateCustomGutterDecorationState @updating = false + throw new Error("Reset @shouldUpdateBlaBla variables to `false`!!!") + @state observeModel: -> @@ -110,24 +112,30 @@ class TextEditorPresenter @shouldUpdateLineNumberGutterState = true @shouldUpdateLineNumbersState = true @shouldUpdateGutterOrderState = true - @updateCustomGutterDecorationState() # FIXME: use `shouldUpdate...` + @shouldUpdateCustomGutterDecorationState = true + + @emitDidUpdateState() @disposables.add @model.onDidChangeGrammar(@didChangeGrammar.bind(this)) @disposables.add @model.onDidChangePlaceholderText(@updateContentState.bind(this)) @disposables.add @model.onDidChangeMini => - @shouldUpdateScrollbarDimensions = true + @updateScrollbarDimensions() @shouldUpdateScrollbarsState = true @shouldUpdateContentState = true @shouldUpdateDecorations = true @shouldUpdateLinesState = true @shouldUpdateLineNumberGutterState = true @shouldUpdateLineNumbersState = true + @shouldUpdateGutterOrderState = true + @shouldUpdateCustomGutterDecorationState = true @updateCommonGutterState() # TODO: check out later... - @updateGutterOrderState() # FIXME: use `shouldUpdate...` - @updateCustomGutterDecorationState() # FIXME: use `shouldUpdate...` + + @emitDidUpdateState() @disposables.add @model.onDidChangeLineNumberGutterVisible => @shouldUpdateLineNumberGutterState = true + @shouldUpdateGutterOrderState = true @updateCommonGutterState() # TODO: check out later... - @updateGutterOrderState() # FIXME: use `shouldUpdate...` + + @emitDidUpdateState() @disposables.add @model.onDidAddDecoration(@didAddDecoration.bind(this)) @disposables.add @model.onDidAddCursor(@didAddCursor.bind(this)) @disposables.add @model.onDidChangeScrollTop(@setScrollTop.bind(this)) @@ -153,24 +161,32 @@ class TextEditorPresenter @configDisposables.add atom.config.onDidChange 'editor.showIndentGuide', configParams, ({newValue}) => @showIndentGuide = newValue - @updateContentState() + @shouldUpdateContentState = true + + @emitDidUpdateState() @configDisposables.add atom.config.onDidChange 'editor.scrollPastEnd', configParams, ({newValue}) => @scrollPastEnd = newValue + @shouldUpdateVerticalScrollState = true + @shouldUpdateScrollbarsState = true @updateScrollHeight() - @updateVerticalScrollState() - @updateScrollbarsState() + + @emitDidUpdateState() @configDisposables.add atom.config.onDidChange 'editor.showLineNumbers', configParams, ({newValue}) => @showLineNumbers = newValue - @updateLineNumberGutterState() - @updateCommonGutterState() - @updateGutterOrderState() + @shouldUpdateLineNumberGutterState = true + @shouldUpdateGutterOrderState = true + @updateCommonGutterState() # TODO: check this out + + @emitDidUpdateState() didChangeGrammar: -> @observeConfig() - @updateContentState() - @updateLineNumberGutterState() - @updateCommonGutterState() - @updateGutterOrderState() + @shouldUpdateContentState = true + @shouldUpdateLineNumberGutterState = true + @shouldUpdateGutterOrderState = true + @updateCommonGutterState() # TODO: check this out + + @emitDidUpdateState() buildState: -> @state = @@ -395,18 +411,24 @@ class TextEditorPresenter didAddGutter: (gutter) -> gutterDisposables = new CompositeDisposable gutterDisposables.add gutter.onDidChangeVisible => - @updateGutterOrderState() - @updateCustomGutterDecorationState() + @shouldUpdateGutterOrderState = true + @shouldUpdateCustomGutterDecorationState = true + + @emitDidUpdateState() gutterDisposables.add gutter.onDidDestroy => @disposables.remove(gutterDisposables) gutterDisposables.dispose() - @updateGutterOrderState() + @shouldUpdateGutterOrderState = true + + @emitDidUpdateState() # It is not necessary to @updateCustomGutterDecorationState here. # The destroyed gutter will be removed from the list of gutters in @state, # and thus will be removed from the DOM. @disposables.add(gutterDisposables) - @updateGutterOrderState() - @updateCustomGutterDecorationState() + @shouldUpdateGutterOrderState = true + @shouldUpdateCustomGutterDecorationState = true + + @emitDidUpdateState() updateGutterOrderState: -> @batch "shouldUpdateGutterOrderState", -> @@ -670,8 +692,10 @@ class TextEditorPresenter @startBlinkingCursors() else @stopBlinkingCursors(false) - @updateFocusedState() - @updateHiddenInputState() + @shouldUpdateFocusedState = true + @shouldUpdateHiddenInputState = true + + @emitDidUpdateState() setScrollTop: (scrollTop) -> scrollTop = @constrainScrollTop(scrollTop) @@ -682,14 +706,16 @@ class TextEditorPresenter @updateStartRow() @updateEndRow() @didStartScrolling() - @updateVerticalScrollState() - @updateHiddenInputState() - @updateDecorations() - @updateLinesState() - @updateCursorsState() - @updateLineNumbersState() - @updateCustomGutterDecorationState() - @updateOverlaysState() + @shouldUpdateVerticalScrollState = true + @shouldUpdateHiddenInputState = true + @shouldUpdateDecorations = true + @shouldUpdateLinesState = true + @shouldUpdateCursorsState = true + @shouldUpdateLineNumbersState = true + @shouldUpdateCustomGutterDecorationState = true + @shouldUpdateOverlaysState = true + + @emitDidUpdateState() didStartScrolling: -> if @stoppedScrollingTimeoutId? @@ -703,11 +729,11 @@ class TextEditorPresenter @state.content.scrollingVertically = false if @mouseWheelScreenRow? @mouseWheelScreenRow = null - @updateLinesState() - @updateLineNumbersState() + @shouldUpdateLinesState = true + @shouldUpdateLineNumbersState = true @updateCustomGutterDecorationState() - else - @emitDidUpdateState() + + @emitDidUpdateState() setScrollLeft: (scrollLeft) -> scrollLeft = @constrainScrollLeft(scrollLeft) @@ -715,10 +741,12 @@ class TextEditorPresenter oldScrollLeft = @scrollLeft @scrollLeft = scrollLeft @model.setScrollLeft(scrollLeft) - @updateHorizontalScrollState() - @updateHiddenInputState() - @updateCursorsState() unless oldScrollLeft? - @updateOverlaysState() + @shouldUpdateHorizontalScrollState = true + @shouldUpdateHiddenInputState = true + @shouldUpdateCursorsState = true unless oldScrollLeft? + @shouldUpdateOverlaysState = true + + @emitDidUpdateState() setHorizontalScrollbarHeight: (horizontalScrollbarHeight) -> unless @measuredHorizontalScrollbarHeight is horizontalScrollbarHeight @@ -726,10 +754,12 @@ class TextEditorPresenter @measuredHorizontalScrollbarHeight = horizontalScrollbarHeight @model.setHorizontalScrollbarHeight(horizontalScrollbarHeight) @updateScrollbarDimensions() - @updateScrollbarsState() - @updateVerticalScrollState() - @updateHorizontalScrollState() - @updateCursorsState() unless oldHorizontalScrollbarHeight? + @shouldUpdateScrollbarsState = true + @shouldUpdateVerticalScrollState = true + @shouldUpdateHorizontalScrollState = true + @shouldUpdateCursorsState = true unless oldHorizontalScrollbarHeight? + + @emitDidUpdateState() setVerticalScrollbarWidth: (verticalScrollbarWidth) -> unless @measuredVerticalScrollbarWidth is verticalScrollbarWidth @@ -737,28 +767,34 @@ class TextEditorPresenter @measuredVerticalScrollbarWidth = verticalScrollbarWidth @model.setVerticalScrollbarWidth(verticalScrollbarWidth) @updateScrollbarDimensions() - @updateScrollbarsState() - @updateVerticalScrollState() - @updateHorizontalScrollState() - @updateCursorsState() unless oldVerticalScrollbarWidth? + @shouldUpdateScrollbarsState = true + @shouldUpdateVerticalScrollState = true + @shouldUpdateHorizontalScrollState = true + @shouldUpdateCursorsState = true unless oldVerticalScrollbarWidth? + + @emitDidUpdateState() setAutoHeight: (autoHeight) -> unless @autoHeight is autoHeight @autoHeight = autoHeight - @updateHeightState() + @shouldUpdateHeightState = true + + @emitDidUpdateState() setExplicitHeight: (explicitHeight) -> unless @explicitHeight is explicitHeight @explicitHeight = explicitHeight @model.setHeight(explicitHeight) @updateHeight() - @updateVerticalScrollState() - @updateScrollbarsState() - @updateDecorations() - @updateLinesState() - @updateCursorsState() - @updateLineNumbersState() - @updateCustomGutterDecorationState() + @shouldUpdateVerticalScrollState = true + @shouldUpdateScrollbarsState = true + @shouldUpdateDecorations = true + @shouldUpdateLinesState = true + @shouldUpdateCursorsState = true + @shouldUpdateLineNumbersState = true + @shouldUpdateCustomGutterDecorationState = true + + @emitDidUpdateState() updateHeight: -> height = @explicitHeight ? @contentHeight @@ -776,18 +812,22 @@ class TextEditorPresenter @model.setWidth(contentFrameWidth) @updateScrollbarDimensions() @updateClientWidth() - @updateVerticalScrollState() - @updateHorizontalScrollState() - @updateScrollbarsState() - @updateContentState() - @updateDecorations() - @updateLinesState() - @updateCursorsState() unless oldContentFrameWidth? + @shouldUpdateVerticalScrollState = true + @shouldUpdateHorizontalScrollState = true + @shouldUpdateScrollbarsState = true + @shouldUpdateContentState = true + @shouldUpdateDecorations = true + @shouldUpdateLinesState = true + @shouldUpdateCursorsState = true unless oldContentFrameWidth? + + @emitDidUpdateState() setBoundingClientRect: (boundingClientRect) -> unless @clientRectsEqual(@boundingClientRect, boundingClientRect) @boundingClientRect = boundingClientRect - @updateOverlaysState() + @shouldUpdateOverlaysState = true + + @emitDidUpdateState() clientRectsEqual: (clientRectA, clientRectB) -> clientRectA? and clientRectB? and @@ -800,22 +840,28 @@ class TextEditorPresenter if @windowWidth isnt width or @windowHeight isnt height @windowWidth = width @windowHeight = height - @updateOverlaysState() + @shouldUpdateOverlaysState = true + + @emitDidUpdateState() setBackgroundColor: (backgroundColor) -> unless @backgroundColor is backgroundColor @backgroundColor = backgroundColor - @updateContentState() - @updateLineNumberGutterState() + @shouldUpdateContentState = true + @shouldUpdateLineNumberGutterState = true @updateCommonGutterState() - @updateGutterOrderState() + @shouldUpdateGutterOrderState = true + + @emitDidUpdateState() setGutterBackgroundColor: (gutterBackgroundColor) -> unless @gutterBackgroundColor is gutterBackgroundColor @gutterBackgroundColor = gutterBackgroundColor - @updateLineNumberGutterState() + @shouldUpdateLineNumberGutterState = true @updateCommonGutterState() - @updateGutterOrderState() + @shouldUpdateGutterOrderState = true + + @emitDidUpdateState() setLineHeight: (lineHeight) -> unless @lineHeight is lineHeight @@ -826,17 +872,19 @@ class TextEditorPresenter @updateHeight() @updateStartRow() @updateEndRow() - @updateHeightState() - @updateHorizontalScrollState() - @updateVerticalScrollState() - @updateScrollbarsState() - @updateHiddenInputState() - @updateDecorations() - @updateLinesState() - @updateCursorsState() - @updateLineNumbersState() - @updateCustomGutterDecorationState() - @updateOverlaysState() + @shouldUpdateHeightState = true + @shouldUpdateHorizontalScrollState = true + @shouldUpdateVerticalScrollState = true + @shouldUpdateScrollbarsState = true + @shouldUpdateHiddenInputState = true + @shouldUpdateDecorations = true + @shouldUpdateLinesState = true + @shouldUpdateCursorsState = true + @shouldUpdateLineNumbersState = true + @shouldUpdateCustomGutterDecorationState = true + @shouldUpdateOverlaysState = true + + @emitDidUpdateState() setMouseWheelScreenRow: (mouseWheelScreenRow) -> unless @mouseWheelScreenRow is mouseWheelScreenRow @@ -876,15 +924,17 @@ class TextEditorPresenter characterWidthsChanged: -> @updateContentDimensions() - @updateHorizontalScrollState() - @updateVerticalScrollState() - @updateScrollbarsState() - @updateHiddenInputState() - @updateContentState() - @updateDecorations() - @updateLinesState() - @updateCursorsState() - @updateOverlaysState() + @shouldUpdateHorizontalScrollState = true + @shouldUpdateVerticalScrollState = true + @shouldUpdateScrollbarsState = true + @shouldUpdateHiddenInputState = true + @shouldUpdateContentState = true + @shouldUpdateDecorations = true + @shouldUpdateLinesState = true + @shouldUpdateCursorsState = true + @shouldUpdateOverlaysState = true + + @emitDidUpdateState() clearScopedCharacterWidths: -> @characterWidthsByScope = {} @@ -972,11 +1022,11 @@ class TextEditorPresenter intersectsVisibleRowRange = true if intersectsVisibleRowRange - @updateLinesState() if decoration.isType('line') + @shouldUpdateLinesState = true if decoration.isType('line') if decoration.isType('line-number') - @updateLineNumbersState() + @shouldUpdateLineNumbersState = true else if decoration.isType('gutter') - @updateCustomGutterDecorationState() + @shouldUpdateCustomGutterDecorationState = true if decoration.isType('highlight') return if change.textChanged @@ -984,7 +1034,9 @@ class TextEditorPresenter @updateHighlightState(decoration) if decoration.isType('overlay') - @updateOverlaysState() + @shouldUpdateOverlaysState = true + + @emitDidUpdateState() decorationPropertiesDidChange: (decoration, event) -> {oldProperties} = event @@ -995,29 +1047,33 @@ class TextEditorPresenter decoration.getMarker().getScreenRange()) @addToLineDecorationCaches(decoration, decoration.getMarker().getScreenRange()) if decoration.isType('line') or Decoration.isType(oldProperties, 'line') - @updateLinesState() + @shouldUpdateLinesState = true if decoration.isType('line-number') or Decoration.isType(oldProperties, 'line-number') - @updateLineNumbersState() + @shouldUpdateLineNumbersState = true if (decoration.isType('gutter') and not decoration.isType('line-number')) or (Decoration.isType(oldProperties, 'gutter') and not Decoration.isType(oldProperties, 'line-number')) - @updateCustomGutterDecorationState() + @shouldUpdateCustomGutterDecorationState = true else if decoration.isType('overlay') - @updateOverlaysState() + @shouldUpdateOverlaysState = true else if decoration.isType('highlight') @updateHighlightState(decoration, event) + @emitDidUpdateState() + didDestroyDecoration: (decoration) -> if decoration.isType('line') or decoration.isType('gutter') @removeFromLineDecorationCaches(decoration, decoration.getMarker().getScreenRange()) - @updateLinesState() if decoration.isType('line') + @shouldUpdateLinesState = true if decoration.isType('line') if decoration.isType('line-number') - @updateLineNumbersState() + @shouldUpdateLineNumbersState = true else if decoration.isType('gutter') - @updateCustomGutterDecorationState(decoration.getProperties().gutterName) + @shouldUpdateCustomGutterDecorationState = true if decoration.isType('highlight') @updateHighlightState(decoration) if decoration.isType('overlay') - @updateOverlaysState() + @shouldUpdateOverlaysState = true + + @emitDidUpdateState() highlightDidFlash: (decoration) -> flash = decoration.consumeNextFlash() @@ -1032,15 +1088,17 @@ class TextEditorPresenter if decoration.isType('line') or decoration.isType('gutter') @addToLineDecorationCaches(decoration, decoration.getMarker().getScreenRange()) - @updateLinesState() if decoration.isType('line') + @shouldUpdateLinesState = true if decoration.isType('line') if decoration.isType('line-number') - @updateLineNumbersState() + @shouldUpdateLineNumbersState = true else if decoration.isType('gutter') - @updateCustomGutterDecorationState() + @shouldUpdateCustomGutterDecorationState = true else if decoration.isType('highlight') @updateHighlightState(decoration) else if decoration.isType('overlay') - @updateOverlaysState() + @shouldUpdateOverlaysState = true + + @emitDidUpdateState() updateDecorations: -> @batch "shouldUpdateDecorations", -> @lineDecorationsByScreenRow = {} @@ -1197,14 +1255,18 @@ class TextEditorPresenter overlayState.itemWidth = itemWidth overlayState.itemHeight = itemHeight overlayState.contentMargin = contentMargin - @updateOverlaysState() + @shouldUpdateOverlaysState = true + + @emitDidUpdateState() observeCursor: (cursor) -> didChangePositionDisposable = cursor.onDidChangePosition => - @updateHiddenInputState() if cursor.isLastCursor() + @shouldUpdateHiddenInputState = true if cursor.isLastCursor() @pauseCursorBlinking() @updateCursorState(cursor) + @emitDidUpdateState() + didChangeVisibilityDisposable = cursor.onDidChangeVisibility => @updateCursorState(cursor) @@ -1212,19 +1274,23 @@ class TextEditorPresenter @disposables.remove(didChangePositionDisposable) @disposables.remove(didChangeVisibilityDisposable) @disposables.remove(didDestroyDisposable) - @updateHiddenInputState() + @shouldUpdateHiddenInputState = true @updateCursorState(cursor, true) + @emitDidUpdateState() + @disposables.add(didChangePositionDisposable) @disposables.add(didChangeVisibilityDisposable) @disposables.add(didDestroyDisposable) didAddCursor: (cursor) -> @observeCursor(cursor) - @updateHiddenInputState() + @shouldUpdateHiddenInputState = true @pauseCursorBlinking() @updateCursorState(cursor) + @emitDidUpdateState() + startBlinkingCursors: -> unless @toggleCursorBlinkHandle @state.content.cursorsVisible = true