From cca596c77c6eb647666a2a70646ba2c31c33da33 Mon Sep 17 00:00:00 2001 From: abe33 Date: Wed, 17 Dec 2014 12:50:21 +0100 Subject: [PATCH] :bug: Change minimap width adjustments to avoid update on every DOM poll --- lib/minimap-element.coffee | 25 ++++++++++++++++--------- spec/minimap-element-spec.coffee | 26 +++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/lib/minimap-element.coffee b/lib/minimap-element.coffee index e23e7de8..e29ac3b3 100644 --- a/lib/minimap-element.coffee +++ b/lib/minimap-element.coffee @@ -29,6 +29,8 @@ class MinimapElement extends HTMLElement else if @scrollIndicator? @disposeScrollIndicator() + @requestUpdate() if @attached + 'minimap.textOpacity': (@textOpacity) => @requestForcedUpdate() if @attached @@ -134,20 +136,23 @@ class MinimapElement extends HTMLElement measureHeightAndWidth: -> @height = @clientHeight + @width = @clientWidth + canvasWidth = @width + + return unless @isVisible() if @adjustToSoftWrap lineLength = atom.config.get('editor.preferredLineLength') softWrap = atom.config.get('editor.softWrap') width = lineLength * @minimap.getCharWidth() - @width = width if softWrap and lineLength and (not @width? or width < @width) + @marginRight = width - @width if softWrap and lineLength and width < @width + canvasWidth = width else - @width = @clientWidth - - return unless @isVisible() + delete @marginRight - if @width isnt @canvas.width or @height isnt @canvas.height - @canvas.width = @width * devicePixelRatio + if canvasWidth isnt @canvas.width or @height isnt @canvas.height + @canvas.width = canvasWidth * devicePixelRatio @canvas.height = (@height + @minimap.getLineHeight()) * devicePixelRatio getTextEditor: -> @minimap.getTextEditor() @@ -183,9 +188,9 @@ class MinimapElement extends HTMLElement return unless @attached and @isVisible() if @adjustToSoftWrap - @style.width = @width + 'px' + @style.marginRight = @marginRight + 'px' else - @style.width = null + @style.marginRight = null visibleAreaLeft = @minimap.getTextEditorScrollLeft() visibleAreaTop = @minimap.getTextEditorScrollTop() - @minimap.getMinimapScrollTop() @@ -205,9 +210,11 @@ class MinimapElement extends HTMLElement editorHeight = @getTextEditor().getHeight() indicatorHeight = editorHeight * (editorHeight / @minimap.getHeight()) indicatorScroll = (editorHeight - indicatorHeight) * @minimap.getTextEditorScrollRatio() + indicatorOffset = 0 + indicatorOffset = @marginRight if @adjustToSoftWrap @scrollIndicator.style.height = indicatorHeight + 'px' - @transformElement @scrollIndicator, @makeTranslate(0, indicatorScroll) + @transformElement @scrollIndicator, @makeTranslate(indicatorOffset, indicatorScroll) @disposeScrollIndicator() if not @minimap.canScroll() diff --git a/spec/minimap-element-spec.coffee b/spec/minimap-element-spec.coffee index 3a512a42..c5fb39f8 100644 --- a/spec/minimap-element-spec.coffee +++ b/spec/minimap-element-spec.coffee @@ -292,7 +292,10 @@ describe 'MinimapElement', -> expect(Array::indexOf.call(editorElement.shadowRoot.children, minimapElement)).toEqual(0) describe 'when minimap.adjustMinimapWidthToSoftWrap is true', -> + [minimapWidth] = [] beforeEach -> + minimapWidth = minimapElement.offsetWidth + atom.config.set 'editor.softWrap', true atom.config.set 'editor.softWrapAtPreferredLineLength', true atom.config.set 'editor.preferredLineLength', 2 @@ -300,14 +303,31 @@ describe 'MinimapElement', -> atom.config.set 'minimap.adjustMinimapWidthToSoftWrap', true nextAnimationFrame() - it 'adjusts the width of the minimap', -> - expect(minimapElement.offsetWidth).toEqual(4) + it 'adjusts the width of the minimap canvas', -> + expect(minimapElement.canvas.width).toEqual(4) + + it 'offsets the minimap by the difference', -> + expect(realOffsetLeft(minimapElement)).toBeCloseTo(editorElement.clientWidth - 4, -1) + expect(minimapElement.clientWidth).toBeCloseTo(minimapWidth, -1) describe 'the dom polling routine', -> it 'does not change the value', -> advanceClock(150) nextAnimationFrame() - expect(minimapElement.offsetWidth).toEqual(4) + expect(minimapElement.canvas.width).toEqual(4) + + describe 'and when minimap.minimapScrollIndicator setting is true', -> + beforeEach -> + editor.setText(mediumSample) + editor.setScrollTop(50) + nextAnimationFrame() + + atom.config.set 'minimap.minimapScrollIndicator', true + nextAnimationFrame() + + it 'offsets the scroll indicator by the difference', -> + indicator = minimapElement.shadowRoot.querySelector('.minimap-scroll-indicator') + expect(realOffsetLeft(indicator)).toBeCloseTo(2, -1) describe 'and then disabled', -> beforeEach ->