2013-05-30 12:16:54 +00:00
|
|
|
BrowserWindow = require 'browser-window'
|
2013-08-14 04:03:37 +00:00
|
|
|
EventEmitter = require('events').EventEmitter
|
2013-05-30 12:16:54 +00:00
|
|
|
MenuItem = require 'menu-item'
|
2014-05-25 04:47:38 +00:00
|
|
|
v8Util = process.atomBinding 'v8_util'
|
2013-05-14 11:24:52 +00:00
|
|
|
|
2013-05-16 02:54:37 +00:00
|
|
|
bindings = process.atomBinding 'menu'
|
2013-05-16 11:34:23 +00:00
|
|
|
|
2014-05-25 04:32:47 +00:00
|
|
|
# Automatically generated radio menu item's group id.
|
|
|
|
nextGroupId = 0
|
|
|
|
|
|
|
|
# Search between seperators to find a radio menu item and return its group id,
|
|
|
|
# otherwise generate a group id.
|
|
|
|
generateGroupId = (items, pos) ->
|
|
|
|
if pos > 0
|
|
|
|
for i in [pos - 1..0]
|
|
|
|
item = items[i]
|
|
|
|
return item.groupId if item.type is 'radio'
|
|
|
|
break if item.type is 'separator'
|
|
|
|
else if pos < items.length
|
|
|
|
for i in [pos..items.length - 1]
|
|
|
|
item = items[i]
|
|
|
|
return item.groupId if item.type is 'radio'
|
|
|
|
break if item.type is 'separator'
|
|
|
|
++nextGroupId
|
|
|
|
|
2015-04-13 03:22:33 +00:00
|
|
|
# Returns the index of item according to |id|.
|
|
|
|
indexOfItemById = (items, id) ->
|
|
|
|
return i for item, i in items when item.id is id
|
|
|
|
-1
|
|
|
|
|
|
|
|
# Returns the index of where to insert the item according to |position|.
|
|
|
|
indexToInsertByPosition = (items, position) ->
|
|
|
|
return items.length unless position
|
|
|
|
|
|
|
|
[query, id] = position.split '='
|
|
|
|
insertIndex = indexOfItemById items, id
|
|
|
|
if insertIndex is -1 and query isnt 'endof'
|
|
|
|
console.warn "Item with id '#{id}' is not found"
|
|
|
|
return items.length
|
|
|
|
|
|
|
|
switch query
|
|
|
|
when 'after'
|
|
|
|
insertIndex++
|
|
|
|
when 'endof'
|
|
|
|
# If the |id| doesn't exist, then create a new group with the |id|.
|
|
|
|
if insertIndex is -1
|
|
|
|
items.push id: id, type: 'separator'
|
|
|
|
insertIndex = items.length - 1
|
|
|
|
|
|
|
|
# Find the end of the group.
|
|
|
|
insertIndex++
|
|
|
|
while insertIndex < items.length and items[insertIndex].type isnt 'separator'
|
|
|
|
insertIndex++
|
|
|
|
|
|
|
|
insertIndex
|
|
|
|
|
2013-05-16 02:54:37 +00:00
|
|
|
Menu = bindings.Menu
|
2013-05-14 11:24:52 +00:00
|
|
|
Menu::__proto__ = EventEmitter.prototype
|
|
|
|
|
2014-05-27 01:10:54 +00:00
|
|
|
Menu::_init = ->
|
|
|
|
@commandsMap = {}
|
|
|
|
@groupsMap = {}
|
|
|
|
@items = []
|
|
|
|
@delegate =
|
|
|
|
isCommandIdChecked: (commandId) => @commandsMap[commandId]?.checked
|
|
|
|
isCommandIdEnabled: (commandId) => @commandsMap[commandId]?.enabled
|
|
|
|
isCommandIdVisible: (commandId) => @commandsMap[commandId]?.visible
|
|
|
|
getAcceleratorForCommandId: (commandId) => @commandsMap[commandId]?.accelerator
|
2015-02-13 04:11:50 +00:00
|
|
|
getIconForCommandId: (commandId) => @commandsMap[commandId]?.icon
|
2015-09-01 15:34:32 +00:00
|
|
|
executeCommand: (commandId) =>
|
|
|
|
@commandsMap[commandId]?.click BrowserWindow.getFocusedWindow()
|
2014-05-27 01:10:54 +00:00
|
|
|
menuWillShow: =>
|
|
|
|
# Make sure radio groups have at least one menu item seleted.
|
|
|
|
for id, group of @groupsMap
|
|
|
|
checked = false
|
|
|
|
for radioItem in group when radioItem.checked
|
|
|
|
checked = true
|
|
|
|
break
|
|
|
|
v8Util.setHiddenValue group[0], 'checked', true unless checked
|
|
|
|
|
2014-11-25 15:47:41 +00:00
|
|
|
Menu::popup = (window, x, y) ->
|
2013-05-15 11:19:35 +00:00
|
|
|
throw new TypeError('Invalid window') unless window?.constructor is BrowserWindow
|
2014-11-25 15:47:41 +00:00
|
|
|
if x? and y?
|
|
|
|
@_popupAt(window, x, y)
|
|
|
|
else
|
|
|
|
@_popup window
|
2013-05-14 11:24:52 +00:00
|
|
|
|
2013-05-16 12:06:25 +00:00
|
|
|
Menu::append = (item) ->
|
|
|
|
@insert @getItemCount(), item
|
|
|
|
|
2013-05-16 11:34:23 +00:00
|
|
|
Menu::insert = (pos, item) ->
|
|
|
|
throw new TypeError('Invalid item') unless item?.constructor is MenuItem
|
|
|
|
|
2014-05-25 04:32:47 +00:00
|
|
|
switch item.type
|
|
|
|
when 'normal' then @insertItem pos, item.commandId, item.label
|
2014-05-26 04:00:20 +00:00
|
|
|
when 'checkbox' then @insertCheckItem pos, item.commandId, item.label
|
2014-05-25 04:32:47 +00:00
|
|
|
when 'separator' then @insertSeparator pos
|
|
|
|
when 'submenu' then @insertSubMenu pos, item.commandId, item.label, item.submenu
|
|
|
|
when 'radio'
|
|
|
|
# Grouping radio menu items.
|
2014-05-26 04:40:21 +00:00
|
|
|
item.overrideReadOnlyProperty 'groupId', generateGroupId(@items, pos)
|
2014-05-25 04:32:47 +00:00
|
|
|
@groupsMap[item.groupId] ?= []
|
|
|
|
@groupsMap[item.groupId].push item
|
2014-05-25 04:47:38 +00:00
|
|
|
|
|
|
|
# Setting a radio menu item should flip other items in the group.
|
2014-05-26 05:01:26 +00:00
|
|
|
v8Util.setHiddenValue item, 'checked', item.checked
|
2014-05-25 04:47:38 +00:00
|
|
|
Object.defineProperty item, 'checked',
|
|
|
|
enumerable: true
|
|
|
|
get: -> v8Util.getHiddenValue item, 'checked'
|
|
|
|
set: (val) =>
|
2014-05-26 04:40:21 +00:00
|
|
|
for otherItem in @groupsMap[item.groupId] when otherItem isnt item
|
2014-05-25 04:47:38 +00:00
|
|
|
v8Util.setHiddenValue otherItem, 'checked', false
|
|
|
|
v8Util.setHiddenValue item, 'checked', true
|
|
|
|
|
2014-05-25 04:32:47 +00:00
|
|
|
@insertRadioItem pos, item.commandId, item.label, item.groupId
|
|
|
|
|
|
|
|
@setSublabel pos, item.sublabel if item.sublabel?
|
2015-02-13 04:11:50 +00:00
|
|
|
@setIcon pos, item.icon if item.icon?
|
2015-09-01 11:48:11 +00:00
|
|
|
@setRole pos, item.role if item.role?
|
2014-05-25 04:32:47 +00:00
|
|
|
|
2014-05-26 04:00:20 +00:00
|
|
|
# Make menu accessable to items.
|
2014-05-26 04:40:21 +00:00
|
|
|
item.overrideReadOnlyProperty 'menu', this
|
2014-05-26 04:00:20 +00:00
|
|
|
|
2014-05-25 04:32:47 +00:00
|
|
|
# Remember the items.
|
2013-08-14 04:03:37 +00:00
|
|
|
@items.splice pos, 0, item
|
|
|
|
@commandsMap[item.commandId] = item
|
2013-05-16 11:34:23 +00:00
|
|
|
|
2014-05-26 01:38:04 +00:00
|
|
|
# Force menuWillShow to be called
|
|
|
|
Menu::_callMenuWillShow = ->
|
|
|
|
@delegate?.menuWillShow()
|
|
|
|
item.submenu._callMenuWillShow() for item in @items when item.submenu?
|
|
|
|
|
2013-10-05 04:56:30 +00:00
|
|
|
applicationMenu = null
|
2013-05-16 02:54:37 +00:00
|
|
|
Menu.setApplicationMenu = (menu) ->
|
2015-06-04 07:14:43 +00:00
|
|
|
throw new TypeError('Invalid menu') unless menu is null or menu.constructor is Menu
|
2013-10-05 04:56:30 +00:00
|
|
|
applicationMenu = menu # Keep a reference.
|
2013-10-05 06:31:30 +00:00
|
|
|
|
|
|
|
if process.platform is 'darwin'
|
2015-06-04 08:14:19 +00:00
|
|
|
return if menu is null
|
2014-05-26 01:38:04 +00:00
|
|
|
menu._callMenuWillShow()
|
2013-10-05 06:31:30 +00:00
|
|
|
bindings.setApplicationMenu menu
|
|
|
|
else
|
2013-12-26 10:41:21 +00:00
|
|
|
windows = BrowserWindow.getAllWindows()
|
2013-10-05 06:31:30 +00:00
|
|
|
w.setMenu menu for w in windows
|
2013-05-16 11:34:23 +00:00
|
|
|
|
2013-10-05 13:05:59 +00:00
|
|
|
Menu.getApplicationMenu = -> applicationMenu
|
|
|
|
|
2013-05-16 09:25:02 +00:00
|
|
|
Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder
|
2013-05-16 02:54:37 +00:00
|
|
|
|
2013-05-16 14:43:58 +00:00
|
|
|
Menu.buildFromTemplate = (template) ->
|
|
|
|
throw new TypeError('Invalid template for Menu') unless Array.isArray template
|
|
|
|
|
2015-04-07 15:14:28 +00:00
|
|
|
positionedTemplate = []
|
|
|
|
insertIndex = 0
|
|
|
|
|
2013-05-16 14:43:58 +00:00
|
|
|
for item in template
|
2015-04-13 03:22:33 +00:00
|
|
|
if item.position
|
|
|
|
insertIndex = indexToInsertByPosition positionedTemplate, item.position
|
|
|
|
else
|
|
|
|
# If no |position| is specified, insert after last item.
|
|
|
|
insertIndex++
|
2015-04-07 15:14:28 +00:00
|
|
|
positionedTemplate.splice insertIndex, 0, item
|
|
|
|
|
|
|
|
menu = new Menu
|
|
|
|
|
|
|
|
for item in positionedTemplate
|
2013-05-16 14:43:58 +00:00
|
|
|
throw new TypeError('Invalid template for MenuItem') unless typeof item is 'object'
|
|
|
|
|
|
|
|
item.submenu = Menu.buildFromTemplate item.submenu if item.submenu?
|
2013-08-14 03:00:08 +00:00
|
|
|
menuItem = new MenuItem(item)
|
2013-08-15 08:09:34 +00:00
|
|
|
menuItem[key] = value for key, value of item when not menuItem[key]?
|
2013-08-14 03:00:08 +00:00
|
|
|
menu.append menuItem
|
2013-05-16 14:43:58 +00:00
|
|
|
|
|
|
|
menu
|
|
|
|
|
2013-05-14 11:24:52 +00:00
|
|
|
module.exports = Menu
|