From 72e7a90e9ec18b78acc7a5ca96ac27298008b4e1 Mon Sep 17 00:00:00 2001 From: abe33 Date: Fri, 30 Oct 2015 19:17:50 +0100 Subject: [PATCH] Implement removing unused elements in stand-alone minimap --- lib/minimap-element.coffee | 92 +++++++++++++++++++++++--------- spec/minimap-element-spec.coffee | 52 ++++++++++-------- 2 files changed, 98 insertions(+), 46 deletions(-) diff --git a/lib/minimap-element.coffee b/lib/minimap-element.coffee index a294b57e..ca0c0ba4 100644 --- a/lib/minimap-element.coffee +++ b/lib/minimap-element.coffee @@ -49,7 +49,7 @@ class MinimapElement extends HTMLElement @updateMinimapFlexPosition() 'minimap.minimapScrollIndicator': (@minimapScrollIndicator) => - if @minimapScrollIndicator and not @scrollIndicator? + if @minimapScrollIndicator and not @scrollIndicator? and not @standAlone @initializeScrollIndicator() else if @scrollIndicator? @disposeScrollIndicator() @@ -57,7 +57,7 @@ class MinimapElement extends HTMLElement @requestUpdate() if @attached 'minimap.displayPluginsControls': (@displayPluginsControls) => - if @displayPluginsControls and not @openQuickSettings? + if @displayPluginsControls and not @openQuickSettings? and not @standAlone @initializeOpenQuickSettings() else if @openQuickSettings? @disposeOpenQuickSettings() @@ -162,32 +162,57 @@ class MinimapElement extends HTMLElement @shadowRoot.appendChild(@canvas) + @createVisibleArea() + @createControls() + + @subscriptions.add @subscribeTo this, + 'mousewheel': (e) => @relayMousewheelEvent(e) + + @subscriptions.add @subscribeTo @canvas, + 'mousedown': (e) => @mousePressedOverCanvas(e) + + # Initializes the visible area div. + createVisibleArea: -> + return if @visibleArea? + @visibleArea = document.createElement('div') @visibleArea.classList.add('minimap-visible-area') @shadowRoot.appendChild(@visibleArea) + @visibleAreaSubscription = @subscribeTo @visibleArea, + 'mousedown': (e) => @startDrag(e) + 'touchstart': (e) => @startDrag(e) + + @subscriptions.add(@visibleAreaSubscription) + + # Removes the visible area div. + removeVisibleArea: -> + return unless @visibleArea? + + @subscriptions.remove(@visibleAreaSubscription) + @visibleAreaSubscription.dispose() + @shadowRoot.removeChild(@visibleArea) + delete @visibleArea + + # Creates the controls container div. + createControls: -> + return if @controls? or @standAlone + @controls = document.createElement('div') @controls.classList.add('minimap-controls') @shadowRoot.appendChild(@controls) - elementMousewheel = (e) => @relayMousewheelEvent(e) - canvasMousedown = (e) => @mousePressedOverCanvas(e) - visibleAreaMousedown = (e) => @startDrag(e) + removeControls: -> + return unless @controls? - @addEventListener 'mousewheel', elementMousewheel - @canvas.addEventListener 'mousedown', canvasMousedown - @visibleArea.addEventListener 'mousedown', visibleAreaMousedown - @visibleArea.addEventListener 'touchstart', visibleAreaMousedown - - @subscriptions.add new Disposable => - @removeEventListener 'mousewheel', elementMousewheel - @canvas.removeEventListener 'mousedown', canvasMousedown - @visibleArea.removeEventListener 'mousedown', visibleAreaMousedown - @visibleArea.removeEventListener 'touchstart', visibleAreaMousedown + @shadowRoot.removeChild(@controls) + delete @controls # Initializes the scroll indicator div when the `minimapScrollIndicator` # settings is enabled. initializeScrollIndicator: -> + return if @scrollIndicator? or @standAlone + @scrollIndicator = document.createElement('div') @scrollIndicator.classList.add 'minimap-scroll-indicator' @controls.appendChild(@scrollIndicator) @@ -195,13 +220,15 @@ class MinimapElement extends HTMLElement # Disposes the scroll indicator div when the `minimapScrollIndicator` # settings is disabled. disposeScrollIndicator: -> + return unless @scrollIndicator? + @controls.removeChild(@scrollIndicator) - @scrollIndicator = undefined + delete @scrollIndicator # Initializes the quick settings openener div when the # `displayPluginsControls` setting is enabled. initializeOpenQuickSettings: -> - return if @openQuickSettings? + return if @openQuickSettings? or @standAlone @openQuickSettings = document.createElement('div') @openQuickSettings.classList.add 'open-minimap-quick-settings' @@ -291,21 +318,36 @@ class MinimapElement extends HTMLElement @subscriptions.add @minimap.onDidDestroy => @destroy() @subscriptions.add @minimap.onDidChangeConfig => @requestForcedUpdate() if @attached + @subscriptions.add @minimap.onDidChangeStandAlone => - if @minimap.isStandAlone() - @setAttribute('stand-alone', true) - else - @removeAttribute('stand-alone') + @setStandAlone(@minimap.isStandAlone()) @requestUpdate() + @subscriptions.add @minimap.onDidChange (change) => @pendingChanges.push(change) @requestUpdate() - @setAttribute('stand-alone', true) if @minimap.isStandAlone() + @setStandAlone(@minimap.isStandAlone()) + @minimap.setScreenHeightAndWidth(@height, @width) if @width? and @height? @minimap + setStandAlone: (@standAlone) -> + if @standAlone + @setAttribute('stand-alone', true) + @disposeScrollIndicator() + @disposeOpenQuickSettings() + @removeControls() + @removeVisibleArea() + + else + @removeAttribute('stand-alone') + @createVisibleArea() + @createControls() + @initializeScrollIndicator() if @minimapScrollIndicator + @initializeOpenQuickSettings() if @displayPluginsControls + # ## ## ######## ######## ### ######## ######## # ## ## ## ## ## ## ## ## ## ## # ## ## ## ## ## ## ## ## ## ## @@ -596,10 +638,10 @@ class MinimapElement extends HTMLElement # styles - An {Object} where the keys are the properties name and the values # are the CSS values for theses properties. applyStyles: (element, styles) -> - cssText = '' + return unless element? - for property,value of styles - cssText += "#{property}: #{value}; " + cssText = '' + cssText += "#{property}: #{value}; " for property,value of styles element.style.cssText = cssText diff --git a/spec/minimap-element-spec.coffee b/spec/minimap-element-spec.coffee index bec5ed9a..a5c4fb74 100644 --- a/spec/minimap-element-spec.coffee +++ b/spec/minimap-element-spec.coffee @@ -658,36 +658,33 @@ describe 'MinimapElement', -> expect(minimap.width).toEqual(minimapElement.clientWidth) expect(minimap.height).toEqual(minimapElement.clientHeight) - it 'does not display the visible area', -> - waitsFor -> nextAnimationFrame isnt noAnimationFrame - runs -> - nextAnimationFrame() - expect(isVisible(minimapElement.visibleArea)).toBeFalsy() + it 'removes the controls div', -> + expect(minimapElement.shadowRoot.querySelector('.minimap-controls')).toBeNull() + + it 'removes the visible area', -> + expect(minimapElement.visibleArea).toBeUndefined() - it 'does not display the quick settings button', -> + it 'removes the quick settings button', -> atom.config.set 'minimap.displayPluginsControls', true waitsFor -> nextAnimationFrame isnt noAnimationFrame runs -> nextAnimationFrame() - expect(isVisible(minimapElement.openQuickSettings)).toBeFalsy() - - describe 'when minimap.minimapScrollIndicator setting is true', -> - beforeEach -> - editor.setText(mediumSample) - editorElement.setScrollTop(50) + expect(minimapElement.openQuickSettings).toBeUndefined() - waitsFor -> minimapElement.frameRequested - runs -> - nextAnimationFrame() - atom.config.set 'minimap.minimapScrollIndicator', true + it 'removes the scroll indicator', -> + editor.setText(mediumSample) + editorElement.setScrollTop(50) - waitsFor -> minimapElement.frameRequested - runs -> nextAnimationFrame() + waitsFor -> minimapElement.frameRequested + runs -> + nextAnimationFrame() + atom.config.set 'minimap.minimapScrollIndicator', true - it 'offsets the scroll indicator by the difference', -> - indicator = minimapElement.shadowRoot.querySelector('.minimap-scroll-indicator') - expect(realOffsetLeft(indicator)).toBeCloseTo(16, -1) + waitsFor -> minimapElement.frameRequested + runs -> + nextAnimationFrame() + expect(minimapElement.shadowRoot.querySelector('.minimap-scroll-indicator')).toBeNull() describe 'pressing the mouse on the minimap canvas', -> beforeEach -> @@ -705,6 +702,19 @@ describe 'MinimapElement', -> it 'does not scroll the editor to the line below the mouse', -> expect(editorElement.getScrollTop()).toEqual(1000) + describe 'and is changed to be a classical minimap again', -> + beforeEach -> + atom.config.set 'minimap.displayPluginsControls', true + atom.config.set 'minimap.minimapScrollIndicator', true + + minimap.setStandAlone(false) + + it 'recreates the destroyed elements', -> + expect(minimapElement.shadowRoot.querySelector('.minimap-controls')).toExist() + expect(minimapElement.shadowRoot.querySelector('.minimap-visible-area')).toExist() + expect(minimapElement.shadowRoot.querySelector('.minimap-scroll-indicator')).toExist() + expect(minimapElement.shadowRoot.querySelector('.open-minimap-quick-settings')).toExist() + # ######## ######## ###### ######## ######## ####### ## ## # ## ## ## ## ## ## ## ## ## ## ## ## # ## ## ## ## ## ## ## ## ## ####