Skip to content

Commit

Permalink
Implement resize detection with DOM polling
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Dec 15, 2014
1 parent d3edad1 commit a5b888c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/minimap-element.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{debounce} = require 'underscore-plus'
{CompositeDisposable} = require 'event-kit'
DOMStylesReader = require './mixins/dom-styles-reader'
CanvasDrawer = require './mixins/canvas-drawer'
Expand All @@ -6,6 +7,10 @@ class MinimapElement extends HTMLElement
DOMStylesReader.includeInto(this)
CanvasDrawer.includeInto(this)

domPollingInterval: 100
domPollingIntervalId: null
domPollingPaused: false

createdCallback: ->
@subscriptions = new CompositeDisposable
@initializeContent()
Expand All @@ -14,6 +19,7 @@ class MinimapElement extends HTMLElement
@getTextEditorElementRoot().appendChild(this)

attachedCallback: ->
@domPollingIntervalId = setInterval((=> @pollDOM()), @domPollingInterval)
@measureHeightAndWidth()
@requestUpdate()

Expand All @@ -40,6 +46,23 @@ class MinimapElement extends HTMLElement
@visibleArea.classList.add('minimap-visible-area')
@shadowRoot.appendChild(@visibleArea)

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

resumeDOMPolling: ->
@domPollingPaused = false

resumeDOMPollingAfterDelay: null

pollDOM: ->
return if @domPollingPaused or @updateRequested

if @width isnt @clientWidth or @height isnt @clientHeight
@measureHeightAndWidth()
@requestUpdate()

measureHeightAndWidth: ->
@width = @clientWidth
@height = @clientHeight
Expand Down
19 changes: 19 additions & 0 deletions spec/minimap-element-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,22 @@ describe 'MinimapElement', ->
it 'updates the visible area', ->
expect(visibleArea.offsetTop).toBeCloseTo(minimap.getTextEditorScrollTop() - minimap.getMinimapScrollTop(), 0)
expect(visibleArea.offsetLeft).toBeCloseTo(minimap.getTextEditorScrollLeft(), 0)

describe 'when the editor is resized to a greater size', ->
beforeEach ->
height = editor.getHeight()
editorElement.style.width = '300px'
editorElement.style.height = '300px'

waitsFor -> editor.getHeight() isnt height

runs ->
advanceClock(150)
nextAnimationFrame()

it 'detect the resize and adjust itself', ->
expect(minimapElement.offsetWidth).toBeCloseTo(editorElement.offsetWidth / 11, 0)
expect(minimapElement.offsetHeight).toEqual(editorElement.offsetHeight)

expect(canvas.offsetWidth).toEqual(minimapElement.offsetWidth)
expect(canvas.offsetHeight).toEqual(minimapElement.offsetHeight + minimap.getLineHeight())

0 comments on commit a5b888c

Please sign in to comment.