From e1e510e4730b7525ef18304e44c3f9ba3af8f6e7 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 7 Jul 2014 15:39:00 -0600 Subject: [PATCH] Re-measure character widths when stylesheets change Fixes #2845 --- spec/editor-component-spec.coffee | 25 +++++++++++++++++++++++++ src/editor-component.coffee | 9 +++++++++ 2 files changed, 34 insertions(+) diff --git a/spec/editor-component-spec.coffee b/spec/editor-component-spec.coffee index 9e698eb8d..91cb630b2 100644 --- a/spec/editor-component-spec.coffee +++ b/spec/editor-component-spec.coffee @@ -587,6 +587,31 @@ describe "EditorComponent", -> expect(cursorRect.left).toBe rangeRect.left expect(cursorRect.width).toBe rangeRect.width + it "positions cursors correctly after character widths are changed via a stylesheet change", -> + atom.config.set('editor.fontFamily', 'sans-serif') + editor.setCursorScreenPosition([0, 16]) + runSetImmediateCallbacks() + + atom.themes.applyStylesheet 'test', """ + .function.js { + font-weight: bold; + } + """ + runSetImmediateCallbacks() # re-measure characters once for a synchronous set of stylesheet changes + runSetImmediateCallbacks() # update based on new measurements + + cursor = node.querySelector('.cursor') + cursorRect = cursor.getBoundingClientRect() + + cursorLocationTextNode = component.lineNodeForScreenRow(0).querySelector('.storage.type.function.js').firstChild + range = document.createRange() + range.setStart(cursorLocationTextNode, 0) + range.setEnd(cursorLocationTextNode, 1) + rangeRect = range.getBoundingClientRect() + + expect(cursorRect.left).toBe rangeRect.left + expect(cursorRect.width).toBe rangeRect.width + it "sets the cursor to the default character width at the end of a line", -> editor.setCursorScreenPosition([0, Infinity]) runSetImmediateCallbacks() diff --git a/src/editor-component.coffee b/src/editor-component.coffee index 53a3b79c9..abafdb308 100644 --- a/src/editor-component.coffee +++ b/src/editor-component.coffee @@ -28,6 +28,7 @@ EditorComponent = React.createClass updateRequested: false updatesPaused: false updateRequestedWhilePaused: false + characterWidthRemeasurementRequested: false cursorsMoved: false selectionChanged: false selectionAdded: false @@ -653,6 +654,7 @@ EditorComponent = React.createClass onStylesheetsChanged: (stylesheet) -> @refreshScrollbars() if @containsScrollbarSelector(stylesheet) + @requestCharacterWidthRemeasurement() onScreenLinesChanged: (change) -> {editor} = @props @@ -793,6 +795,13 @@ EditorComponent = React.createClass else if @remeasureCharacterWidthsWhenShown and @state.visible and not prevState.visible @remeasureCharacterWidths() + requestCharacterWidthRemeasurement: -> + unless @characterWidthRemeasurementRequested + @characterWidthRemeasurementRequested = true + setImmediate => + @characterWidthRemeasurementRequested = false + @remeasureCharacterWidths() + remeasureCharacterWidths: -> @remeasureCharacterWidthsWhenShown = false @refs.lines.remeasureCharacterWidths()