|
1 | 1 | {Emitter} = require 'emissary' |
2 | | -MinimapView = require './minimap-view' |
3 | 2 | Debug = require 'prolix' |
4 | 3 |
|
| 4 | +ViewManagement = require './mixins/view-management' |
| 5 | +PluginManagement = require './mixins/plugin-management' |
| 6 | + |
5 | 7 | require '../vendor/resizeend' |
6 | 8 |
|
| 9 | +# Public: The `Minimap` package provides an eagle-eye view of text buffers. |
| 10 | +# |
| 11 | +# It also provides API for plugin packages that want to interact with the |
| 12 | +# minimap and be available to the user through the minimap settings. |
| 13 | +# |
| 14 | +# ## Events |
| 15 | +# |
| 16 | +# * `activated` - |
| 17 | +# Emitted synchronously when the package is activated. By suscribing |
| 18 | +# to this event other packages can be notified of the activation of the |
| 19 | +# minimap. At that point the minimap views have been created. |
| 20 | +# |
| 21 | +# * `deactivated` - |
| 22 | +# Emitted synchronously when the package have been deactivated. The views |
| 23 | +# are no longer available at that point. |
| 24 | +# |
| 25 | +# * `minimap-view:created` - |
| 26 | +# Emitted synchronously when a {MinimapView} have been created for |
| 27 | +# an active {PaneView}. |
| 28 | +# Your handler will be called with an object containing the following keys. |
| 29 | +# * `view`: The {MinimapView} that was created |
| 30 | +# |
| 31 | +# * `minimap-view:will-be-destroyed` - |
| 32 | +# Emitted synchronously when a {MinimapView} is about to be destroyed. |
| 33 | +# Your handler will be called with an object containing the following keys. |
| 34 | +# * `view`: The {MinimapView} that was created |
| 35 | +# |
| 36 | +# * `minimap-view:destroyed` - |
| 37 | +# Emitted synchronously when a {MinimapView} was destroyed, at that point |
| 38 | +# the view have been removed from the DOM. |
| 39 | +# Your handler will be called with an object containing the following keys. |
| 40 | +# * `view`: The {MinimapView} that was created |
| 41 | +# |
| 42 | +# * `plugin:added` - |
| 43 | +# Emitted synchronously when a minimap plugin was registered. |
| 44 | +# Your handler will be called with an object containing the following keys. |
| 45 | +# * `name` - The {String} name used to register the plugin |
| 46 | +# * `plugin` - The plugin {Object} that was registered |
| 47 | +# |
| 48 | +# * `plugin:removed` - |
| 49 | +# Emitted synchronously when a minimap plugin was unregistered. |
| 50 | +# Your handler will be called with an object containing the following keys. |
| 51 | +# * `name` - The {String} name used to register the plugin |
| 52 | +# * `plugin` - The plugin {Object} that was unregistered |
| 53 | +# |
| 54 | +# * `plugin:activated` - |
| 55 | +# Emitted synchronously when a minimap plugin was activated. |
| 56 | +# Your handler will be called with an object containing the following keys. |
| 57 | +# * `name` - The {String} name used to register the plugin |
| 58 | +# * `plugin` - The plugin {Object} that was activated |
| 59 | +# |
| 60 | +# * `plugin:deactivated` - |
| 61 | +# Emitted synchronously when a minimap plugin was deactivated. |
| 62 | +# Your handler will be called with an object containing the following keys. |
| 63 | +# * `name` - The {String} name used to register the plugin |
| 64 | +# * `plugin` - The plugin {Object} that was deactivated |
| 65 | +# |
| 66 | +# ## Plugins Interface |
| 67 | +# |
| 68 | +# Plugins should conform to the following interface: |
| 69 | +# |
| 70 | +# ```coffee |
| 71 | +# class Plugin |
| 72 | +# void activatePlugin: -> |
| 73 | +# void deactivatePlugin: -> |
| 74 | +# bool isActive: -> |
| 75 | +# ``` |
7 | 76 | class Minimap |
8 | 77 | Emitter.includeInto(this) |
9 | 78 | Debug('minimap').includeInto(this) |
| 79 | + ViewManagement.includeInto(this) |
| 80 | + PluginManagement.includeInto(this) |
10 | 81 |
|
11 | | - # We'll be storing each MinimapView using the id of their PaneView |
12 | | - minimapViews: {} |
| 82 | + # Public: The default minimap settings |
| 83 | + configDefaults: |
| 84 | + plugins: {} |
| 85 | + autoToggle: false |
13 | 86 |
|
14 | | - # We'll be using this property to store the toggle state as the |
15 | | - # minimapViews object will never be set to null. |
| 87 | + # Internal: The activation state of the minimap package. |
16 | 88 | active: false |
17 | 89 |
|
| 90 | + # Public: Activates the minimap package. |
18 | 91 | activate: -> |
19 | 92 | atom.workspaceView.command 'minimap:toggle', => @toggleNoDebug() |
20 | 93 | atom.workspaceView.command 'minimap:toggle-debug', => @toggleDebug() |
| 94 | + @toggleNoDebug() if atom.config.get 'minimap.autoToggle' |
21 | 95 |
|
| 96 | + # Public: Deactivates the minimap package. |
22 | 97 | deactivate: -> |
23 | | - view.destroy() for id, view of @minimapViews |
24 | | - @eachPaneViewSubscription.off() |
25 | | - @minimapViews = {} |
| 98 | + @destroyViews() |
26 | 99 | @emit('deactivated') |
27 | 100 |
|
28 | | - toggle: () -> |
29 | | - if @active |
30 | | - @active = false |
31 | | - @deactivate() |
32 | | - else |
33 | | - @open() |
34 | | - @active = true |
35 | | - @emit('activated') |
36 | | - |
| 101 | + # Public: Toggles the minimap activation state with debug turned on. |
37 | 102 | toggleDebug: -> |
38 | 103 | @getChannel().activate() |
39 | 104 | @toggle() |
40 | 105 |
|
| 106 | + # Public: Toggles the minimap activation state with debug turned off. |
41 | 107 | toggleNoDebug: -> |
42 | 108 | @getChannel().deactivate() |
43 | 109 | @toggle() |
44 | 110 |
|
45 | | - updateAllViews: -> |
46 | | - view.onScrollViewResized() for id,view of @minimapViews |
47 | | - |
48 | | - minimapForEditorView: (editorView) -> |
49 | | - @minimapForPaneView(editorView.getPane()) |
50 | | - |
51 | | - minimapForPaneView: (paneView) -> @minimapForPane(paneView.model) |
52 | | - minimapForPane: (pane) -> @minimapViews[pane.id] |
53 | | - |
54 | | - open: -> |
55 | | - # When toggled we'll look for each existing and future pane thanks to |
56 | | - # the `eachPaneView` method. It returns a subscription object so we'll |
57 | | - # store it and it will be used in the `deactivate` method to removes |
58 | | - # the callback. |
59 | | - @eachPaneViewSubscription = atom.workspaceView.eachPaneView (paneView) => |
60 | | - paneId = paneView.model.id |
61 | | - view = new MinimapView(paneView) |
62 | | - view.onActiveItemChanged(paneView.getActiveItem()) |
63 | | - @updateAllViews() |
64 | | - |
65 | | - @minimapViews[paneId] = view |
66 | | - @emit('minimap-view:created', view) |
67 | | - |
68 | | - paneView.model.on 'destroyed', => |
69 | | - view = @minimapViews[paneId] |
70 | | - |
71 | | - if view? |
72 | | - @emit('minimap-view:before-destruction', view) |
73 | | - |
74 | | - view.destroy() |
75 | | - delete @minimapViews[paneId] |
76 | | - @updateAllViews() |
77 | | - |
| 111 | + # Public: Returns the char width ratio of the minimap compared to the real |
| 112 | + # editor. **The value is currently hard-coded until we find a good way to |
| 113 | + # compute it from the editor state**. |
| 114 | + getCharWidthRatio: -> 0.8 |
78 | 115 |
|
| 116 | + # Internal: Toggles the minimap activation state. |
| 117 | + toggle: () -> |
| 118 | + if @active |
| 119 | + @active = false |
| 120 | + @deactivate() |
| 121 | + else |
| 122 | + @createViews() |
| 123 | + @active = true |
| 124 | + @emit('activated') |
79 | 125 |
|
| 126 | +# The minimap module is an instance of the {Minimap} class. |
80 | 127 | module.exports = new Minimap() |
0 commit comments