From 48db50fb60f7c32e098d46096aebbd2eaaf8eae2 Mon Sep 17 00:00:00 2001 From: abe33 Date: Tue, 2 Dec 2014 21:43:47 +0100 Subject: [PATCH] Use StyleManager events to invalidate the DOM styles cache --- lib/minimap-render-view.coffee | 36 +++++++++++++++++++---------- lib/mixins/dom-styles-reader.coffee | 15 +++++++++--- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/lib/minimap-render-view.coffee b/lib/minimap-render-view.coffee index e3d4169b..74bfa42e 100644 --- a/lib/minimap-render-view.coffee +++ b/lib/minimap-render-view.coffee @@ -47,7 +47,6 @@ class MinimapRenderView extends ScrollView @tokenColorCache = {} @decorationColorCache = {} @initializeDecorations() - @tokenized = false @offscreenCanvas = document.createElement('canvas') @offscreenCtxt = @offscreenCanvas.getContext('2d') @@ -57,17 +56,33 @@ class MinimapRenderView extends ScrollView initialize: -> @lineCanvas.webkitImageSmoothingEnabled = false - @subscriptions.add atom.config.observe 'minimap.interline', (@interline) => + subs = @subscriptions + + subs.add atom.styles.onDidAddStyleElement => + @invalidateCache() + @forceUpdate() + + subs.add atom.styles.onDidRemoveStyleElement => + @invalidateCache() + @forceUpdate() + + subs.add atom.styles.onDidUpdateStyleElement => + @invalidateCache() + @forceUpdate() + + subs.add atom.config.observe 'minimap.interline', (@interline) => @emitter.emit 'did-change-scale' @forceUpdate() - @subscriptions.add atom.config.observe 'minimap.charWidth', (@charWidth) => + + subs.add atom.config.observe 'minimap.charWidth', (@charWidth) => @emitter.emit 'did-change-scale' @forceUpdate() - @subscriptions.add atom.config.observe 'minimap.charHeight', (@charHeight) => + + subs.add atom.config.observe 'minimap.charHeight', (@charHeight) => @emitter.emit 'did-change-scale' @forceUpdate() - @subscriptions.add atom.config.observe 'minimap.textOpacity', (@textOpacity) => - @invalidateCache() + + subs.add atom.config.observe 'minimap.textOpacity', (@textOpacity) => @forceUpdate() # Destroys the {MinimapRenderView} instance, unsubscribes from the listened @@ -110,12 +125,9 @@ class MinimapRenderView extends ScrollView @subscriptions.add @editor.onDidChange (changes) => @stackChanges(changes) @subscriptions.add @displayBuffer.onDidTokenize => - @tokenized = true - @invalidateCache() + @invalidateIfFirstTokenization() @forceUpdate() - @tokenized = true if @displayBuffer.tokenizedBuffer.fullyTokenized - # ## ## ######## ######## ### ######## ######## # ## ## ## ## ## ## ## ## ## ## # ## ## ## ## ## ## ## ## ## ## @@ -335,7 +347,7 @@ class MinimapRenderView extends ScrollView # # Returns a {String}. getDefaultColor: -> - color = @retrieveStyleFromDom(['.dummy'], 'color') + color = @retrieveStyleFromDom(['.editor'], 'color', false, false) @transparentize(color, @getTextOpacity()) # Returns the text color for the passed-in `token` object. @@ -455,7 +467,7 @@ class MinimapRenderView extends ScrollView for token in line.tokens w = token.screenDelta unless token.isOnlyWhitespace() - color = if displayCodeHighlights and @tokenized + color = if displayCodeHighlights @getTokenColor(token) else @getDefaultColor() diff --git a/lib/mixins/dom-styles-reader.coffee b/lib/mixins/dom-styles-reader.coffee index 64180cf7..5b1d5068 100644 --- a/lib/mixins/dom-styles-reader.coffee +++ b/lib/mixins/dom-styles-reader.coffee @@ -3,19 +3,22 @@ Mixin = require 'mixto' module.exports = class DOMStylesReader extends Mixin @domStylesCache: {} - + # Internal: This function insert a dummy element in the DOM to compute # its style, return the specified property, and remove the element # from the DOM. # # scopes - An {Array} of {String} reprensenting the scope to reproduce. # property - The property {String} name. + # shadowRoot - A {Boolean} of whether to evaluate the styles in the editor + # shadow DOM or not. + # cache - A {Boolean} of whether to use the cache or not. # # Returns a {String} of the property value. - retrieveStyleFromDom: (scopes, property, shadowRoot=true) -> + retrieveStyleFromDom: (scopes, property, shadowRoot=true, cache=true) -> key = scopes.join(' ') - if @constructor.domStylesCache[key]?[property]? + if cache and @constructor.domStylesCache[key]?[property]? return @constructor.domStylesCache[key][property] @ensureDummyNodeExistence(shadowRoot) @@ -48,3 +51,9 @@ class DOMStylesReader extends Mixin invalidateCache: -> @constructor.domStylesCache = {} + + invalidateIfFirstTokenization: -> + return if @constructor.hasTokenizedOnce + + @invalidateCache() + @constructor.hasTokenizedOnce = true