Skip to content

Commit

Permalink
🐛 Change minimap width adjustments to avoid update on every DOM poll
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Dec 17, 2014
1 parent fceb40b commit cca596c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
25 changes: 16 additions & 9 deletions lib/minimap-element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class MinimapElement extends HTMLElement
else if @scrollIndicator?
@disposeScrollIndicator()

@requestUpdate() if @attached

'minimap.textOpacity': (@textOpacity) =>
@requestForcedUpdate() if @attached

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()

Expand Down
26 changes: 23 additions & 3 deletions spec/minimap-element-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -292,22 +292,42 @@ 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

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 ->
Expand Down

0 comments on commit cca596c

Please sign in to comment.