Skip to content

Commit 63feef3

Browse files
authored
Merge pull request #762 from atom-minimap/decoration-management-class
2 parents 37246d0 + 784732d commit 63feef3

File tree

8 files changed

+262
-202
lines changed

8 files changed

+262
-202
lines changed

docs/Developers Documentation.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,24 @@ While the interface is the same, some details such as the available decorations
6868
A plugin can and should set the plugin origin on the decorations it creates so that the Minimap can easily know which order to apply on the decorations. When not provided, the plugin origin will be inferred from the path of the function invoking the `decorateMarker` method. If the origin can't be inferred the order value will always be `0` for this decoration.
6969

7070
```js
71-
minimapView.decorateMarker(marker, {type: 'line', color: '#ff0000', plugin: 'my-plugin-name'})
71+
const decorationManager = minimapView.getDecorationManagement()
72+
73+
decorationManager.decorateMarker(marker, {type: 'line', color: '#ff0000', plugin: 'my-plugin-name'})
7274
```
7375

7476
#### Scope And Styling
7577

7678
The most important change is that decorations on the Minimap doesn't use a `class`, but rather a `scope`
7779

7880
```js
79-
minimapView.decorateMarker(marker, {type: 'line', scope: '.scope .to .the.marker.style'})
81+
decorationManager.decorateMarker(marker, {type: 'line', scope: '.scope .to .the.marker.style'})
8082
```
8183

8284
It's still possible to pass a class parameter to the decoration:
8385

8486

8587
```js
86-
minimapView.decorateMarker(marker, {type: 'line', class: 'the marker style'})
88+
decorationManager.decorateMarker(marker, {type: 'line', class: 'the marker style'})
8789
```
8890

8991
In that case, when rendering the decoration a scope will be build that will look like `.minimap .editor .the.marker.style`.
@@ -105,7 +107,9 @@ Also note that only the `background` property will be retrieved to style a decor
105107
A last option is to pass a css color directly in a `color` option, such as:
106108

107109
```js
108-
minimapView.decorateMarker(marker, {type: 'line', color: '#ff0000'})
110+
const decorationManager = minimapView.getDecorationManagement()
111+
112+
decorationManager.decorateMarker(marker, {type: 'line', color: '#ff0000'})
109113
```
110114

111115
In that case neither the scope nor the class will be used.

lib/decoration-management.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ export default class DecorationManagement {
1616
/**
1717
* Initializes the decorations related properties.
1818
*/
19-
initializeDecorations () {
19+
initializeDecorations (minimap) {
20+
this.minimap = minimap
21+
2022
if (this.emitter == null) {
2123
/**
2224
* The minimap emitter, lazily created if not created yet.
2325
* @type {Emitter}
2426
* @access private
2527
*/
2628
this.emitter = new Emitter()
29+
} else {
30+
this.emitter = this.minimap.emitter
2731
}
2832

2933
/**
@@ -66,6 +70,9 @@ export default class DecorationManagement {
6670
* @access private
6771
*/
6872
this.decorationDestroyedSubscriptions = new Map()
73+
74+
// is set to true when a minimapElement is destroyed
75+
this.destroyed = false
6976
}
7077

7178
/**
@@ -318,7 +325,7 @@ export default class DecorationManagement {
318325
* @emits {did-change} when the decoration is created successfully
319326
*/
320327
decorateMarker (marker, decorationParams) {
321-
if (this.destroyed || marker == null) { return }
328+
if (this.destroyed || this.minimap.destroyed || marker == null) { return }
322329

323330
const { id } = marker
324331

@@ -431,7 +438,7 @@ export default class DecorationManagement {
431438
* @access private
432439
*/
433440
emitDecorationChanges (type, decoration) {
434-
if (this.editorDestroyed()) { return }
441+
if (this.destroyed || this.minimap.editorDestroyed()) { return }
435442

436443
this.invalidateDecorationForScreenRowsCache()
437444

@@ -453,8 +460,8 @@ export default class DecorationManagement {
453460
emitRangeChanges (type, range, screenDelta) {
454461
const startScreenRow = range.start.row
455462
const endScreenRow = range.end.row
456-
const lastRenderedScreenRow = this.getLastVisibleScreenRow()
457-
const firstRenderedScreenRow = this.getFirstVisibleScreenRow()
463+
const lastRenderedScreenRow = this.minimap.getLastVisibleScreenRow()
464+
const firstRenderedScreenRow = this.minimap.getFirstVisibleScreenRow()
458465

459466
if (screenDelta == null) {
460467
screenDelta = (lastRenderedScreenRow - firstRenderedScreenRow) -
@@ -531,7 +538,7 @@ export default class DecorationManagement {
531538
for (let i = 0, len = decorations.length; i < len; i++) {
532539
const decoration = decorations[i]
533540

534-
if (!this.adapter.editorDestroyed()) {
541+
if (!this.destroyed && !this.minimap.editorDestroyed()) {
535542
this.emitDecorationChanges(decoration.getProperties().type, decoration)
536543
}
537544
this.emitter.emit('did-remove-decoration', {

lib/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function activate () {
102102
/**
103103
* Returns a {MinimapElement} for the passed-in model if it's a {Minimap}.
104104
*
105-
* @param {*} model the model for which returning a view
105+
* @param {Minimap} model the model for which returning a view
106106
* @return {MinimapElement}
107107
*/
108108
export function minimapViewProvider (model) {

lib/minimap-element.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { CompositeDisposable, Disposable } from 'atom'
44
import { EventsDelegation, AncestorsMethods } from 'atom-utils-plus'
55
import elementResizeDetectorImport from 'element-resize-detector'
6+
import DecorationManagement from './decoration-management'
67

78
import * as Main from './main'
89
import CanvasDrawer from './mixins/canvas-drawer'
@@ -177,6 +178,8 @@ class MinimapElement {
177178
*/
178179
this.quickSettingsElement = undefined
179180

181+
this.DecorationManagement = new DecorationManagement()
182+
180183
// States
181184

182185
/**
@@ -452,7 +455,10 @@ class MinimapElement {
452455
*/
453456
destroy () {
454457
this.subscriptions.dispose()
458+
this.DecorationManagement.removeAllDecorations()
459+
this.DecorationManagement.destroyed = true
455460
this.detach()
461+
this.minimap.minimapElement = null
456462
this.minimap = null
457463
}
458464

@@ -645,6 +651,14 @@ class MinimapElement {
645651
delete this.openQuickSettings
646652
}
647653

654+
/**
655+
* get the DecorationManagement API for minimapElement
656+
* @return {DecorationManagement}
657+
*/
658+
getDecorationManagement () {
659+
return this.DecorationManagement
660+
}
661+
648662
// ## ## ####### ######## ######## ##
649663
// ### ### ## ## ## ## ## ##
650664
// #### #### ## ## ## ## ## ##
@@ -669,6 +683,11 @@ class MinimapElement {
669683
setModel (minimap) {
670684
this.minimap = minimap
671685

686+
// set minimapElement for Minimap
687+
this.minimap.minimapElement = this
688+
689+
this.DecorationManagement.initializeDecorations(this.minimap)
690+
672691
this.subscriptions.add(
673692

674693
this.minimap.onDidChangeScrollTop(() => {
@@ -697,7 +716,7 @@ class MinimapElement {
697716
this.requestUpdate()
698717
}),
699718

700-
this.minimap.onDidChangeDecorationRange((change) => {
719+
this.DecorationManagement.onDidChangeDecorationRange((change) => {
701720
const { type } = change
702721
if (type === 'line' ||
703722
type === 'highlight-under' ||

lib/minimap.js

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
'use strict'
22

3-
import include from './decorators/include'
4-
import DecorationManagement from './decoration-management'
5-
63
import { Emitter, CompositeDisposable } from 'atom'
74
import StableAdapter from './adapters/stable-adapter'
85

@@ -17,11 +14,6 @@ let nextModelId = 1
1714
* destroyed whenever their `TextEditor` is destroyed.
1815
*/
1916
export default class Minimap {
20-
static initClass () {
21-
include(this, DecorationManagement)
22-
return this
23-
}
24-
2517
/**
2618
* Creates a new Minimap instance for the given `TextEditor`.
2719
*
@@ -39,6 +31,17 @@ export default class Minimap {
3931
throw new Error('Cannot create a minimap without an editor')
4032
}
4133

34+
/**
35+
* The Minimap's minimapElement.
36+
*
37+
* @type {MinimapElement}
38+
* @access private
39+
*/
40+
this.minimapElement = undefined
41+
42+
// local cache of this.minimapElement.DecorationManagement
43+
this.DecorationManagement = undefined
44+
4245
/**
4346
* The Minimap's text editor.
4447
*
@@ -210,8 +213,6 @@ export default class Minimap {
210213
*/
211214
this.flushChangesTimer = null
212215

213-
this.initializeDecorations()
214-
215216
if (atom.views.getView(this.textEditor).getScrollTop != null) {
216217
this.adapter = new StableAdapter(this.textEditor)
217218
} else {
@@ -287,7 +288,6 @@ export default class Minimap {
287288
clearTimeout(this.flushChangesTimer)
288289
this.flushChangesTimer = null
289290
this.pendingChangeEvents = []
290-
this.removeAllDecorations()
291291
this.subscriptions.dispose()
292292
this.subscriptions = null
293293
this.textEditor = null
@@ -525,6 +525,13 @@ export default class Minimap {
525525
}
526526
}
527527

528+
/**
529+
* @return {MinimapElement} returns the current minimapElement
530+
*/
531+
getMinimapElement () {
532+
return this.minimapElement
533+
}
534+
528535
/**
529536
* Returns the `TextEditor` that this minimap represents.
530537
*
@@ -1056,6 +1063,29 @@ export default class Minimap {
10561063
clearCache () { this.adapter.clearCache() }
10571064

10581065
editorDestroyed () { this.adapter.editorDestroyed() }
1059-
}
10601066

1061-
Minimap.initClass()
1067+
/**
1068+
* get the DecorationManagement API for the current minimapElement
1069+
* @return {DecorationManagement}
1070+
*/
1071+
getDecorationManagement () {
1072+
if (this.DecorationManagement === undefined) {
1073+
this.DecorationManagement = this.minimapElement.DecorationManagement
1074+
}
1075+
return this.DecorationManagement
1076+
}
1077+
1078+
// Decoration API duplicated for backward compatibility in the service
1079+
getDecorations () { return this.getDecorationManagement().getDecorations() }
1080+
onDidAddDecoration (...args) { return this.getDecorationManagement().onDidAddDecoration(...args) }
1081+
onDidRemoveDecoration (...args) { return this.getDecorationManagement().onDidRemoveDecoration(...args) }
1082+
onDidChangeDecorationRange (...args) { return this.getDecorationManagement().onDidChangeDecorationRange(...args) }
1083+
onDidUpdateDecoration (...args) { return this.getDecorationManagement().onDidUpdateDecoration(...args) }
1084+
decorationForId (...args) { return this.getDecorationManagement().decorationForId(...args) }
1085+
decorationsForScreenRowRange (...args) { return this.getDecorationManagement().decorationsForScreenRowRange(...args) }
1086+
decorationsByTypeThenRows () { return this.getDecorationManagement().decorationsByTypeThenRows() }
1087+
decorateMarker (...args) { return this.getDecorationManagement().decorateMarker(...args) }
1088+
removeDecoration (...args) { return this.getDecorationManagement().removeDecoration(...args) }
1089+
removeAllDecorationsForMarker (...args) { return this.getDecorationManagement().removeAllDecorationsForMarker(...args) }
1090+
removeAllDecorations () { return this.getDecorationManagement().removeAllDecorations() }
1091+
}

lib/mixins/canvas-drawer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export default class CanvasDrawer extends Mixin {
140140
this.drawLines(firstRow, lastRow)
141141
}
142142

143-
const decorations = this.minimap.decorationsByTypeThenRows(firstRow, lastRow)
143+
const decorations = this.DecorationManagement.decorationsByTypeThenRows(firstRow, lastRow)
144144

145145
const renderData = {
146146
context: this.backLayer.context,

0 commit comments

Comments
 (0)