From fe451c823c02dc4faceb74614cb93297d280afa4 Mon Sep 17 00:00:00 2001 From: abe33 Date: Sun, 1 Mar 2015 00:54:29 +0100 Subject: [PATCH] :fire: Remove DOM polling routine and use atom.views.pollDocument instead --- lib/minimap-element.coffee | 73 ++++++++++++++++---------------- spec/minimap-element-spec.coffee | 16 ++++--- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/lib/minimap-element.coffee b/lib/minimap-element.coffee index ada74a4f..f5e86a10 100644 --- a/lib/minimap-element.coffee +++ b/lib/minimap-element.coffee @@ -14,9 +14,6 @@ class MinimapElement extends HTMLElement ### Public ### - domPollingInterval: 100 - domPollingIntervalId: null - domPollingPaused: false displayMinimapOnLeft: false # ## ## ####### ####### ## ## ###### @@ -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) -> @@ -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 # ######## ## ## ######## ## ## ######## ###### # ## ## ## ## ### ## ## ## ## diff --git a/spec/minimap-element-spec.coffee b/spec/minimap-element-spec.coffee index 0b4d2108..a4a1ec73 100644 --- a/spec/minimap-element-spec.coffee +++ b/spec/minimap-element-spec.coffee @@ -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) @@ -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) @@ -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) # ###### ###### ######## ####### ## ## # ## ## ## ## ## ## ## ## ## ##