forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu-manager.coffee
213 lines (183 loc) · 6.57 KB
/
menu-manager.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
path = require 'path'
_ = require 'underscore-plus'
{ipcRenderer} = require 'electron'
CSON = require 'season'
fs = require 'fs-plus'
{Disposable} = require 'event-kit'
MenuHelpers = require './menu-helpers'
platformMenu = require('../package.json')?._atomMenu?.menu
# Extended: Provides a registry for menu items that you'd like to appear in the
# application menu.
#
# An instance of this class is always available as the `atom.menu` global.
#
# ## Menu CSON Format
#
# Here is an example from the [tree-view](https://github.com/atom/tree-view/blob/master/menus/tree-view.cson):
#
# ```coffee
# [
# {
# 'label': 'View'
# 'submenu': [
# { 'label': 'Toggle Tree View', 'command': 'tree-view:toggle' }
# ]
# }
# {
# 'label': 'Packages'
# 'submenu': [
# 'label': 'Tree View'
# 'submenu': [
# { 'label': 'Focus', 'command': 'tree-view:toggle-focus' }
# { 'label': 'Toggle', 'command': 'tree-view:toggle' }
# { 'label': 'Reveal Active File', 'command': 'tree-view:reveal-active-file' }
# { 'label': 'Toggle Tree Side', 'command': 'tree-view:toggle-side' }
# ]
# ]
# }
# ]
# ```
#
# Use in your package's menu `.cson` file requires that you place your menu
# structure under a `menu` key.
#
# ```coffee
# 'menu': [
# {
# 'label': 'View'
# 'submenu': [
# { 'label': 'Toggle Tree View', 'command': 'tree-view:toggle' }
# ]
# }
# ]
# ```
#
# See {::add} for more info about adding menu's directly.
module.exports =
class MenuManager
constructor: ({@resourcePath, @keymapManager, @packageManager}) ->
@initialized = false
@pendingUpdateOperation = null
@template = []
@keymapManager.onDidLoadBundledKeymaps => @loadPlatformItems()
@packageManager.onDidActivateInitialPackages => @sortPackagesMenu()
initialize: ({@resourcePath}) ->
@keymapManager.onDidReloadKeymap => @update()
@update()
@initialized = true
# Public: Adds the given items to the application menu.
#
# ## Examples
# ```coffee
# atom.menu.add [
# {
# label: 'Hello'
# submenu : [{label: 'World!', id: 'World!', command: 'hello:world'}]
# }
# ]
# ```
#
# * `items` An {Array} of menu item {Object}s containing the keys:
# * `label` The {String} menu label.
# * `submenu` An optional {Array} of sub menu items.
# * `command` An optional {String} command to trigger when the item is
# clicked.
#
# * `id` (internal) A {String} containing the menu item's id.
# Returns a {Disposable} on which `.dispose()` can be called to remove the
# added menu items.
add: (items) ->
items = _.deepClone(items)
for item in items
continue unless item.label? # TODO: Should we emit a warning here?
@merge(@template, item)
@update()
new Disposable => @remove(items)
remove: (items) ->
@unmerge(@template, item) for item in items
@update()
clear: ->
@template = []
@update()
# Should the binding for the given selector be included in the menu
# commands.
#
# * `selector` A {String} selector to check.
#
# Returns a {Boolean}, true to include the selector, false otherwise.
includeSelector: (selector) ->
try
return true if document.body.webkitMatchesSelector(selector)
catch error
# Selector isn't valid
return false
# Simulate an atom-text-editor element attached to a atom-workspace element attached
# to a body element that has the same classes as the current body element.
unless @testEditor?
# Use new document so that custom elements don't actually get created
testDocument = document.implementation.createDocument(document.namespaceURI, 'html')
testBody = testDocument.createElement('body')
testBody.classList.add(@classesForElement(document.body)...)
testWorkspace = testDocument.createElement('atom-workspace')
workspaceClasses = @classesForElement(document.body.querySelector('atom-workspace'))
workspaceClasses = ['workspace'] if workspaceClasses.length is 0
testWorkspace.classList.add(workspaceClasses...)
testBody.appendChild(testWorkspace)
@testEditor = testDocument.createElement('atom-text-editor')
@testEditor.classList.add('editor')
testWorkspace.appendChild(@testEditor)
element = @testEditor
while element
return true if element.webkitMatchesSelector(selector)
element = element.parentElement
false
# Public: Refreshes the currently visible menu.
update: ->
return unless @initialized
clearTimeout(@pendingUpdateOperation) if @pendingUpdateOperation?
@pendingUpdateOperation = setTimeout(=>
unsetKeystrokes = new Set
for binding in @keymapManager.getKeyBindings()
if binding.command is 'unset!'
unsetKeystrokes.add(binding.keystrokes)
keystrokesByCommand = {}
for binding in @keymapManager.getKeyBindings()
continue unless @includeSelector(binding.selector)
continue if unsetKeystrokes.has(binding.keystrokes)
continue if process.platform is 'darwin' and /^alt-(shift-)?.$/.test(binding.keystrokes)
continue if process.platform is 'win32' and /^ctrl-alt-(shift-)?.$/.test(binding.keystrokes)
keystrokesByCommand[binding.command] ?= []
keystrokesByCommand[binding.command].unshift binding.keystrokes
@sendToBrowserProcess(@template, keystrokesByCommand)
, 1)
loadPlatformItems: ->
if platformMenu?
@add(platformMenu)
else
menusDirPath = path.join(@resourcePath, 'menus')
platformMenuPath = fs.resolve(menusDirPath, process.platform, ['cson', 'json'])
{menu} = CSON.readFileSync(platformMenuPath)
@add(menu)
# Merges an item in a submenu aware way such that new items are always
# appended to the bottom of existing menus where possible.
merge: (menu, item) ->
MenuHelpers.merge(menu, item)
unmerge: (menu, item) ->
MenuHelpers.unmerge(menu, item)
sendToBrowserProcess: (template, keystrokesByCommand) ->
ipcRenderer.send 'update-application-menu', template, keystrokesByCommand
# Get an {Array} of {String} classes for the given element.
classesForElement: (element) ->
if classList = element?.classList
Array::slice.apply(classList)
else
[]
sortPackagesMenu: ->
packagesMenu = _.find @template, ({id}) -> MenuHelpers.normalizeLabel(id) is 'Packages'
return unless packagesMenu?.submenu?
packagesMenu.submenu.sort (item1, item2) ->
if item1.label and item2.label
MenuHelpers.normalizeLabel(item1.label).localeCompare(MenuHelpers.normalizeLabel(item2.label))
else
0
@update()