Skip to content

Commit

Permalink
Use StyleManager events to invalidate the DOM styles cache
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Dec 2, 2014
1 parent 6fd00fa commit 48db50f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
36 changes: 24 additions & 12 deletions lib/minimap-render-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class MinimapRenderView extends ScrollView
@tokenColorCache = {}
@decorationColorCache = {}
@initializeDecorations()
@tokenized = false

@offscreenCanvas = document.createElement('canvas')
@offscreenCtxt = @offscreenCanvas.getContext('2d')
Expand All @@ -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
Expand Down Expand Up @@ -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

# ## ## ######## ######## ### ######## ########
# ## ## ## ## ## ## ## ## ## ##
# ## ## ## ## ## ## ## ## ## ##
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
15 changes: 12 additions & 3 deletions lib/mixins/dom-styles-reader.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -48,3 +51,9 @@ class DOMStylesReader extends Mixin

invalidateCache: ->
@constructor.domStylesCache = {}

invalidateIfFirstTokenization: ->
return if @constructor.hasTokenizedOnce

@invalidateCache()
@constructor.hasTokenizedOnce = true

0 comments on commit 48db50f

Please sign in to comment.