Skip to content

Commit 1af0158

Browse files
authored
Merge pull request #755 from atom-minimap/reuse-DOMStylesReader
2 parents 7fb05bd + c6c7d91 commit 1af0158

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

lib/dom-styles-reader.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export default class DOMStylesReader {
2121
*/
2222
this.dummyNode = undefined
2323

24+
// used to check if the dummyNode is on the current targetNode
25+
this.targetNode = undefined
26+
2427
/**
2528
* Set to true once tokenized
2629
* @access private
@@ -40,16 +43,17 @@ export default class DOMStylesReader {
4043
* to build
4144
* @param {string} property the name of the style property to compute
4245
* @param {Node} targetNode
43-
* @param {boolean} cache whether to cache the computed value or not
46+
* @param {boolean} getFromCache whether to cache the computed value or not
4447
* @return {string} the computed property's value
4548
* used in CanvasDrawer
4649
*/
47-
retrieveStyleFromDom (scopes, property, targetNode, cache) {
50+
retrieveStyleFromDom (scopes, property, targetNode, getFromCache) {
51+
if (!scopes.length) { return '' } // no scopes
4852
const key = scopes.join(' ')
4953
let cachedData = this.domStylesCache.get(key)
5054

5155
if (cachedData !== undefined) {
52-
if (cache) { // if should get the value from the cache
56+
if (getFromCache) { // if should get the value from the cache
5357
const value = cachedData[property]
5458
if (value !== undefined) {
5559
// value exists
@@ -59,7 +63,6 @@ export default class DOMStylesReader {
5963
} else {
6064
// key did not exist. create it
6165
cachedData = {}
62-
this.domStylesCache.set(key, cachedData)
6366
}
6467

6568
this.ensureDummyNodeExistence(targetNode)
@@ -99,12 +102,13 @@ export default class DOMStylesReader {
99102
* @access private
100103
*/
101104
ensureDummyNodeExistence (targetNode) {
102-
if (this.dummyNode === undefined) {
105+
if (this.targetNode !== targetNode || this.dummyNode === undefined) {
103106
this.dummyNode = document.createElement('span')
104107
this.dummyNode.style.visibility = 'hidden'
105108

106109
// attach to the target node
107110
targetNode.appendChild(this.dummyNode)
111+
this.targetNode = targetNode
108112
}
109113
}
110114

lib/main.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Minimap from './minimap'
66
import config from './config.json'
77
import * as PluginManagement from './plugin-management'
88
import { treeSitterWarning } from './performance-monitor'
9+
import DOMStylesReader from './dom-styles-reader'
910

1011
export { default as config } from './config.json'
1112
export * from './plugin-management'
@@ -63,6 +64,12 @@ let subscriptionsOfCommands = null
6364
*/
6465
export const emitter = new Emitter()
6566

67+
68+
/**
69+
DOMStylesReader cache used for storing token colors
70+
*/
71+
let DOMStylesReaderInstance = null
72+
6673
/**
6774
* Activates the minimap package.
6875
*/
@@ -85,6 +92,8 @@ export function activate () {
8592
})
8693

8794
editorsMinimaps = new Map()
95+
DOMStylesReaderInstance = new DOMStylesReader()
96+
8897
subscriptions = new CompositeDisposable()
8998
active = true
9099

@@ -100,6 +109,8 @@ export function activate () {
100109
export function minimapViewProvider (model) {
101110
if (model instanceof Minimap) {
102111
const element = new MinimapElement()
112+
// add DOMStylesReaderInstance
113+
element.DOMStylesReader = DOMStylesReaderInstance;
103114
element.setModel(model)
104115
return element
105116
}
@@ -125,6 +136,7 @@ export function deactivate () {
125136
subscriptionsOfCommands.dispose()
126137
subscriptionsOfCommands = null
127138
editorsMinimaps = undefined
139+
DOMStylesReaderInstance.invalidateDOMStylesCache()
128140
toggled = false
129141
active = false
130142
}
@@ -153,6 +165,7 @@ export function toggle () {
153165
toggled = true
154166
initSubscriptions()
155167
}
168+
DOMStylesReaderInstance.invalidateDOMStylesCache()
156169
}
157170

158171
/**
@@ -358,6 +371,11 @@ function initSubscriptions () {
358371
emitter.emit('did-create-minimap', minimap)
359372
minimapElement.attach()
360373
}),
374+
// empty color cache if the theme changes
375+
atom.themes.onDidChangeActiveThemes(() => {
376+
DOMStylesReaderInstance.invalidateDOMStylesCache()
377+
editorsMinimaps.forEach((minimap) => { atom.views.getView(minimap).requestForcedUpdate() })
378+
}),
361379
treeSitterWarning()
362380
)
363381
}

lib/minimap-element.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -377,18 +377,6 @@ class MinimapElement {
377377
}
378378

379379
this.subscriptions.add(
380-
381-
/*
382-
We use `atom.styles.onDidAddStyleElement` instead of
383-
`atom.themes.onDidChangeActiveThemes`.
384-
Why? Currently, The style element will be removed first, and then re-added
385-
and the `change` event has not be triggered in the process.
386-
*/
387-
atom.styles.onDidAddStyleElement(() => {
388-
this.DOMStylesReader.invalidateDOMStylesCache()
389-
this.requestForcedUpdate()
390-
}),
391-
392380
this.subscribeToMediaQuery()
393381
)
394382
}

lib/mixins/canvas-drawer.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Mixin from 'mixto'
55

66
import * as Main from '../main'
77
import CanvasLayer from '../canvas-layer'
8-
import DOMStylesReader from '../dom-styles-reader'
98

109
const SPEC_MODE = atom.inSpecMode()
1110

@@ -76,11 +75,6 @@ export default class CanvasDrawer extends Mixin {
7675

7776
// the maximum number of tokens to render in one line
7877
this.maxTokensInOneLine = atom.config.get('minimap.maxTokensInOneLine')
79-
80-
/**
81-
* This MinimapElement's DOMStylesReader
82-
*/
83-
this.DOMStylesReader = new DOMStylesReader()
8478
}
8579

8680
/**
@@ -695,7 +689,7 @@ function drawLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWi
695689
for (let iToken = 0; iToken < numTokenToRender; iToken++) {
696690
const token = editorTokensForScreenRow[iToken]
697691
const tokenText = token.text.replace(invisibleRegExp, ' ')
698-
const tokenScopes = token.scopeDescriptor || token.scopes
692+
const tokenScopes = token.scopes
699693

700694
if (lastLine !== line) {
701695
x = 0

spec/minimap-element-spec.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ describe('MinimapElement', () => {
837837
// wait until all animations run out
838838
waitsFor(() => {
839839
nextAnimationFrame !== noAnimationFrame && nextAnimationFrame()
840-
return editorElement.getScrollTop() >= 480
840+
return editorElement.getScrollTop() >= 470 // flaky
841841
})
842842
})
843843

@@ -870,7 +870,7 @@ describe('MinimapElement', () => {
870870
// wait until all animations run out
871871
waitsFor(() => {
872872
nextAnimationFrame !== noAnimationFrame && nextAnimationFrame()
873-
return editorElement.getScrollTop() >= 480
873+
return editorElement.getScrollTop() >= 470 //flaky
874874
})
875875
})
876876

@@ -1198,9 +1198,7 @@ describe('MinimapElement', () => {
11981198
spyOn(minimapElement, 'requestForcedUpdate').andCallThrough()
11991199
spyOn(minimapElement.DOMStylesReader, 'invalidateDOMStylesCache').andCallThrough()
12001200

1201-
const styleNode = document.createElement('style')
1202-
styleNode.textContent = 'body{ color: #233 }'
1203-
atom.styles.emitter.emit('did-add-style-element', styleNode)
1201+
atom.themes.emitter.emit('did-change-active-themes')
12041202
})
12051203

12061204
waitsFor('minimap frame requested', () => {

0 commit comments

Comments
 (0)