Skip to content

Commit 9334b75

Browse files
authored
Merge pull request #743 from atom-minimap/canvas-drawer
2 parents 76f5cbf + 4e753ad commit 9334b75

File tree

7 files changed

+63
-45
lines changed

7 files changed

+63
-45
lines changed

lib/mixins/decoration-management.js renamed to lib/decoration-management.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { Emitter } from 'atom'
44
import { escapeRegExp } from 'underscore-plus'
55
import path from 'path'
6-
import Decoration from '../decoration'
6+
import Decoration from './decoration'
77

88
/**
99
* The mixin that provides the decorations API to the minimap editor

lib/mixins/dom-styles-reader.js renamed to lib/dom-styles-reader.js

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
'use strict'
22

3-
import Mixin from 'mixto'
4-
53
/**
6-
* This mixin is used by the `CanvasDrawer` in `MinimapElement` to
4+
* This class is used by the `CanvasDrawer` in `MinimapElement` to
75
* read the styles informations from the DOM to use when rendering
86
* the `Minimap`.
97
*/
10-
export default class DOMStylesReader extends Mixin {
8+
export default class DOMStylesReader {
9+
constructor () {
10+
/**
11+
* The cache object
12+
* @access private
13+
*/
14+
this.domStylesCache = new Map()
15+
16+
/**
17+
* Set to true once tokenized
18+
* @access private
19+
* unused
20+
*/
21+
// this.hasTokenizedOnce = false
22+
}
23+
1124
/**
1225
* Returns the computed values for the given property and scope in the DOM.
1326
*
@@ -18,23 +31,27 @@ export default class DOMStylesReader extends Mixin {
1831
* @param {Array<string>} scopes a list of classes reprensenting the scope
1932
* to build
2033
* @param {string} property the name of the style property to compute
34+
* @param {Node} targetNode
2135
* @param {boolean} [cache=true] whether to cache the computed value or not
2236
* @return {string} the computed property's value
37+
* used in CanvasDrawer
2338
*/
24-
retrieveStyleFromDom (scopes, property, cache = true) {
25-
this.ensureCache()
26-
39+
retrieveStyleFromDom (scopes, property, targetNode, cache = true) {
2740
const key = scopes.join(' ')
28-
let cachedData = this.constructor.domStylesCache[key]
41+
let cachedData = this.domStylesCache.get(key)
2942

30-
if (cache && (cachedData ? cachedData[property] : undefined) != null) {
31-
return cachedData[property]
43+
if (cache && cachedData !== undefined) {
44+
const value = cachedData[property]
45+
if (value != null) {
46+
return value
47+
}
3248
}
3349

34-
this.ensureDummyNodeExistence()
50+
this.ensureDummyNodeExistence(targetNode)
3551

36-
if (!cachedData) {
37-
this.constructor.domStylesCache[key] = cachedData = {}
52+
if (cachedData === undefined) {
53+
cachedData = {}
54+
this.domStylesCache.set(key, cachedData)
3855
}
3956

4057
let parent = this.dummyNode
@@ -56,7 +73,10 @@ export default class DOMStylesReader extends Mixin {
5673
value = rotateHue(value, filter)
5774
}
5875

59-
if (value !== '') { cachedData[property] = value }
76+
if (value !== '') {
77+
cachedData[property] = value
78+
this.domStylesCache.set(key, cachedData)
79+
}
6080

6181
this.dummyNode.innerHTML = ''
6282
return value
@@ -65,10 +85,11 @@ export default class DOMStylesReader extends Mixin {
6585
/**
6686
* Creates a DOM node container for all the operations that need to read
6787
* styles properties from DOM.
88+
* @param {Node} targetNode
6889
*
6990
* @access private
7091
*/
71-
ensureDummyNodeExistence () {
92+
ensureDummyNodeExistence (targetNode) {
7293
if (this.dummyNode == null) {
7394
/**
7495
* @access private
@@ -77,38 +98,30 @@ export default class DOMStylesReader extends Mixin {
7798
this.dummyNode.style.visibility = 'hidden'
7899
}
79100

80-
this.getTextEditorElement().appendChild(this.dummyNode)
81-
}
82-
83-
/**
84-
* Ensures the presence of the cache object in the class that received
85-
* this mixin.
86-
*
87-
* @access private
88-
*/
89-
ensureCache () {
90-
if (!this.constructor.domStylesCache) {
91-
this.constructor.domStylesCache = {}
92-
}
101+
targetNode.appendChild(this.dummyNode)
93102
}
94103

95104
/**
96105
* Invalidates the cache by emptying the cache object.
106+
* used in MinimapElement
97107
*/
98108
invalidateDOMStylesCache () {
99-
this.constructor.domStylesCache = {}
109+
this.domStylesCache.clear()
100110
}
101111

102112
/**
103113
* Invalidates the cache only for the first tokenization event.
104114
*
105115
* @access private
116+
* unused
106117
*/
118+
/*
107119
invalidateIfFirstTokenization () {
108-
if (this.constructor.hasTokenizedOnce) { return }
120+
if (this.hasTokenizedOnce) { return }
109121
this.invalidateDOMStylesCache()
110-
this.constructor.hasTokenizedOnce = true
122+
this.hasTokenizedOnce = true
111123
}
124+
*/
112125
}
113126

114127
// ## ## ######## ## ######## ######## ######## ######

lib/minimap-element.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { EventsDelegation, AncestorsMethods } from 'atom-utils-plus'
55
import elementResizeDetectorImport from 'element-resize-detector'
66

77
import * as Main from './main'
8-
import DOMStylesReader from './mixins/dom-styles-reader'
8+
import DOMStylesReader from './dom-styles-reader'
99
import CanvasDrawer from './mixins/canvas-drawer'
1010
import include from './decorators/include'
1111
import element from './decorators/element'
@@ -59,7 +59,7 @@ const SPEC_MODE = atom.inSpecMode()
5959
*/
6060
class MinimapElement {
6161
static initClass () {
62-
include(this, DOMStylesReader, CanvasDrawer, EventsDelegation, AncestorsMethods)
62+
include(this, CanvasDrawer, EventsDelegation, AncestorsMethods)
6363
return element(this, 'atom-text-editor-minimap')
6464
}
6565

@@ -181,6 +181,11 @@ class MinimapElement {
181181
*/
182182
this.quickSettingsElement = undefined
183183

184+
/**
185+
* This MinimapElement's DOMStylesReader
186+
*/
187+
this.DOMStylesReader = new DOMStylesReader()
188+
184189
// States
185190

186191
/**
@@ -389,7 +394,7 @@ class MinimapElement {
389394
and the `change` event has not be triggered in the process.
390395
*/
391396
atom.styles.onDidAddStyleElement(() => {
392-
this.invalidateDOMStylesCache()
397+
this.DOMStylesReader.invalidateDOMStylesCache()
393398
this.requestForcedUpdate()
394399
}),
395400

lib/minimap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
import include from './decorators/include'
4-
import DecorationManagement from './mixins/decoration-management'
4+
import DecorationManagement from './decoration-management'
55

66
import { Emitter, CompositeDisposable } from 'atom'
77
import StableAdapter from './adapters/stable-adapter'

lib/mixins/canvas-drawer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export default class CanvasDrawer extends Mixin {
191191
* @return {string} a CSS color
192192
*/
193193
getDefaultColor () {
194-
const color = this.retrieveStyleFromDom(['.editor'], 'color', true)
194+
const color = this.DOMStylesReader.retrieveStyleFromDom(['.editor'], 'color', this.getTextEditorElement(), true)
195195
return transparentize(color, this.getTextOpacity())
196196
}
197197

@@ -206,7 +206,7 @@ export default class CanvasDrawer extends Mixin {
206206
*/
207207
getTokenColor (token) {
208208
const scopes = token.scopeDescriptor || token.scopes
209-
const color = this.retrieveStyleFromDom(scopes, 'color')
209+
const color = this.DOMStylesReader.retrieveStyleFromDom(scopes, 'color', this.getTextEditorElement(), true)
210210

211211
return transparentize(color, this.getTextOpacity())
212212
}
@@ -227,7 +227,7 @@ export default class CanvasDrawer extends Mixin {
227227

228228
if (properties.scope) {
229229
const scopeString = properties.scope.split(/\s+/)
230-
return this.retrieveStyleFromDom(scopeString, 'background-color')
230+
return this.DOMStylesReader.retrieveStyleFromDom(scopeString, 'background-color', this.getTextEditorElement(), true)
231231
} else {
232232
return this.getDefaultColor()
233233
}

spec/fixtures/very-large-file.zip

25.9 KB
Binary file not shown.

spec/minimap-element-spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ describe('MinimapElement', () => {
213213
describe('when a hue-rotate filter is applied to a rgb color', () => {
214214
let [additionnalStyleNode] = []
215215
beforeEach(() => {
216-
minimapElement.invalidateDOMStylesCache()
216+
minimapElement.DOMStylesReader.invalidateDOMStylesCache()
217217

218218
additionnalStyleNode = document.createElement('style')
219219
additionnalStyleNode.textContent = `
@@ -232,7 +232,7 @@ describe('MinimapElement', () => {
232232
})
233233
runs(() => {
234234
nextAnimationFrame()
235-
expect(minimapElement.retrieveStyleFromDom(['.editor'], 'color')).toEqual(`rgb(0, ${0x6d}, ${0x6d})`)
235+
expect(minimapElement.DOMStylesReader.retrieveStyleFromDom(['.editor'], 'color'), minimapElement.getTextEditorElement()).toEqual(`rgb(0, ${0x6d}, ${0x6d})`)
236236
})
237237
})
238238
})
@@ -241,7 +241,7 @@ describe('MinimapElement', () => {
241241
let [additionnalStyleNode] = []
242242

243243
beforeEach(() => {
244-
minimapElement.invalidateDOMStylesCache()
244+
minimapElement.DOMStylesReader.invalidateDOMStylesCache()
245245

246246
additionnalStyleNode = document.createElement('style')
247247
additionnalStyleNode.textContent = `
@@ -260,7 +260,7 @@ describe('MinimapElement', () => {
260260
})
261261
runs(() => {
262262
nextAnimationFrame()
263-
expect(minimapElement.retrieveStyleFromDom(['.editor'], 'color')).toEqual(`rgba(0, ${0x6d}, ${0x6d}, 0)`)
263+
expect(minimapElement.DOMStylesReader.retrieveStyleFromDom(['.editor'], 'color'), minimapElement.getTextEditorElement()).toEqual(`rgba(0, ${0x6d}, ${0x6d}, 0)`)
264264
})
265265
})
266266
})
@@ -1195,7 +1195,7 @@ describe('MinimapElement', () => {
11951195
runs(() => {
11961196
nextAnimationFrame()
11971197
spyOn(minimapElement, 'requestForcedUpdate').andCallThrough()
1198-
spyOn(minimapElement, 'invalidateDOMStylesCache').andCallThrough()
1198+
spyOn(minimapElement.DOMStylesReader, 'invalidateDOMStylesCache').andCallThrough()
11991199

12001200
const styleNode = document.createElement('style')
12011201
styleNode.textContent = 'body{ color: #233 }'
@@ -1209,7 +1209,7 @@ describe('MinimapElement', () => {
12091209

12101210
it('forces a refresh with cache invalidation', () => {
12111211
expect(minimapElement.requestForcedUpdate).toHaveBeenCalled()
1212-
expect(minimapElement.invalidateDOMStylesCache).toHaveBeenCalled()
1212+
expect(minimapElement.DOMStylesReader.invalidateDOMStylesCache).toHaveBeenCalled()
12131213
})
12141214
})
12151215

0 commit comments

Comments
 (0)