Skip to content

Commit

Permalink
🔥 Remove DOM polling routine and use atom.views.pollDocument instead
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Feb 28, 2015
1 parent 955881d commit fe451c8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
73 changes: 36 additions & 37 deletions lib/minimap-element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class MinimapElement extends HTMLElement

### Public ###

domPollingInterval: 100
domPollingIntervalId: null
domPollingPaused: false
displayMinimapOnLeft: false

# ## ## ####### ####### ## ## ######
Expand Down Expand Up @@ -63,21 +60,18 @@ class MinimapElement extends HTMLElement
@requestForcedUpdate() if @attached

'minimap.adjustMinimapWidthToSoftWrap': (@adjustToSoftWrap) =>
if @attached
@measureHeightAndWidth()
@requestForcedUpdate()
console.log 'here', @attached
@measureHeightAndWidth() if @attached

'minimap.useHardwareAcceleration': (@useHardwareAcceleration) =>
@requestUpdate() if @attached

attachedCallback: ->
@domPollingIntervalId = setInterval((=> @pollDOM()), @domPollingInterval)
@subscriptions.add atom.views.pollDocument => @pollDOM()
@measureHeightAndWidth()
@requestUpdate()
@attached = true

detachedCallback: ->
clearInterval(@domPollingIntervalId)
@attached = false

attributeChangedCallback: (attrName, oldValue, newValue) ->
Expand Down Expand Up @@ -303,47 +297,52 @@ class MinimapElement extends HTMLElement
setDisplayCodeHighlights: (@displayCodeHighlights) ->
@requestForcedUpdate() if @attached

pauseDOMPolling: ->
@domPollingPaused = true
@resumeDOMPollingAfterDelay ?= debounce(@resumeDOMPolling, 100)
@resumeDOMPollingAfterDelay()

resumeDOMPolling: ->
@domPollingPaused = false

resumeDOMPollingAfterDelay: null

pollDOM: ->
return if @domPollingPaused or @updateRequested
@measureHeightAndWidth() if @isVisible()

if @width isnt @clientWidth or @height isnt @clientHeight
@measureHeightAndWidth()
@requestForcedUpdate()
checkForVisibilityChange: ->
if @isVisible()
if @wasVisible
false
else
@wasVisible = true
else
if @wasVisible
@wasVisible = false
true
else
@wasVisible = false

measureHeightAndWidth: ->
wasResized = true if @width isnt @clientWidth or @height isnt @clientHeight
visibilityChanged = @checkForVisibilityChange()

@height = @clientHeight
@width = @clientWidth
canvasWidth = @width

@requestForcedUpdate() if wasResized or visibilityChanged

return unless @isVisible()

if @adjustToSoftWrap
lineLength = atom.config.get('editor.preferredLineLength')
softWrap = atom.config.get('editor.softWrap')
softWrapAtPreferredLineLength = atom.config.get('editor.softWrapAtPreferredLineLength')
width = lineLength * @minimap.getCharWidth()
if wasResized
if @adjustToSoftWrap
lineLength = atom.config.get('editor.preferredLineLength')
softWrap = atom.config.get('editor.softWrap')
softWrapAtPreferredLineLength = atom.config.get('editor.softWrapAtPreferredLineLength')
width = lineLength * @minimap.getCharWidth()

if softWrap and softWrapAtPreferredLineLength and lineLength and width < @width
@marginRight = width - @width
canvasWidth = width
if softWrap and softWrapAtPreferredLineLength and lineLength and width < @width
@marginRight = width - @width
canvasWidth = width
else
@marginRight = null
else
@marginRight = null
else
delete @marginRight
delete @marginRight

if canvasWidth isnt @canvas.width or @height isnt @canvas.height
@canvas.width = canvasWidth * devicePixelRatio
@canvas.height = (@height + @minimap.getLineHeight()) * devicePixelRatio
if canvasWidth isnt @canvas.width or @height isnt @canvas.height
@canvas.width = canvasWidth * devicePixelRatio
@canvas.height = (@height + @minimap.getLineHeight()) * devicePixelRatio

# ######## ## ## ######## ## ## ######## ######
# ## ## ## ## ### ## ## ## ##
Expand Down
16 changes: 9 additions & 7 deletions spec/minimap-element-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ describe 'MinimapElement', ->
editor.setScrollLeft(200)
minimapElement.attach()

afterEach -> minimap.destroy()

it 'takes the height of the editor', ->
expect(minimapElement.offsetHeight).toEqual(editorElement.clientHeight)

Expand Down Expand Up @@ -204,8 +206,8 @@ describe 'MinimapElement', ->
editorElement.style.width = '800px'
editorElement.style.height = '500px'

sleep(150)
runs -> nextAnimationFrame()
minimapElement.measureHeightAndWidth()
nextAnimationFrame()

it 'detect the resize and adjust itself', ->
expect(minimapElement.offsetWidth).toBeCloseTo(editorElement.offsetWidth / 11, 0)
Expand Down Expand Up @@ -236,12 +238,12 @@ describe 'MinimapElement', ->
canvasWidth = minimapElement.canvas.width
canvasHeight = minimapElement.canvas.height
editorElement.style.display = 'none'
sleep(150)
runs ->
nextAnimationFrame()

expect(minimapElement.canvas.width).toEqual(canvasWidth)
expect(minimapElement.canvas.height).toEqual(canvasHeight)
minimapElement.measureHeightAndWidth()
nextAnimationFrame()

expect(minimapElement.canvas.width).toEqual(canvasWidth)
expect(minimapElement.canvas.height).toEqual(canvasHeight)

# ###### ###### ######## ####### ## ##
# ## ## ## ## ## ## ## ## ## ##
Expand Down

0 comments on commit fe451c8

Please sign in to comment.