2014-05-26 04:00:20 +00:00
|
|
|
v8Util = process.atomBinding 'v8_util'
|
2013-10-05 06:31:30 +00:00
|
|
|
|
2013-05-16 11:34:23 +00:00
|
|
|
nextCommandId = 0
|
|
|
|
|
2016-01-12 02:03:02 +00:00
|
|
|
### Maps role to methods of webContents ###
|
2015-09-01 15:34:32 +00:00
|
|
|
rolesMap =
|
|
|
|
undo: 'undo'
|
|
|
|
redo: 'redo'
|
|
|
|
cut: 'cut'
|
|
|
|
copy: 'copy'
|
|
|
|
paste: 'paste'
|
|
|
|
selectall: 'selectAll'
|
|
|
|
minimize: 'minimize'
|
|
|
|
close: 'close'
|
|
|
|
|
2016-01-12 02:03:02 +00:00
|
|
|
### Maps methods that should be called directly on the BrowserWindow instance ###
|
2015-11-21 23:13:57 +00:00
|
|
|
methodInBrowserWindow =
|
|
|
|
minimize: true
|
|
|
|
close: true
|
|
|
|
|
2013-05-16 11:34:23 +00:00
|
|
|
class MenuItem
|
|
|
|
@types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
|
|
|
|
|
|
|
|
constructor: (options) ->
|
2015-11-12 10:28:04 +00:00
|
|
|
{Menu} = require 'electron'
|
2013-05-16 14:22:33 +00:00
|
|
|
|
2015-12-07 19:20:15 +00:00
|
|
|
{click, @selector, @type, @role, @label, @sublabel, @accelerator, @icon, @enabled, @visible, @checked, @submenu} = options
|
2013-05-16 11:34:23 +00:00
|
|
|
|
2015-12-07 19:20:15 +00:00
|
|
|
if @submenu? and @submenu.constructor isnt Menu
|
|
|
|
@submenu = Menu.buildFromTemplate @submenu
|
2013-05-16 14:22:33 +00:00
|
|
|
@type = 'submenu' if not @type? and @submenu?
|
|
|
|
throw new Error('Invalid submenu') if @type is 'submenu' and @submenu?.constructor isnt Menu
|
|
|
|
|
2014-05-26 04:40:21 +00:00
|
|
|
@overrideReadOnlyProperty 'type', 'normal'
|
2015-09-01 11:48:11 +00:00
|
|
|
@overrideReadOnlyProperty 'role'
|
2014-05-26 04:40:21 +00:00
|
|
|
@overrideReadOnlyProperty 'accelerator'
|
2015-02-13 04:11:50 +00:00
|
|
|
@overrideReadOnlyProperty 'icon'
|
2014-05-26 04:40:21 +00:00
|
|
|
@overrideReadOnlyProperty 'submenu'
|
|
|
|
@overrideProperty 'label', ''
|
|
|
|
@overrideProperty 'sublabel', ''
|
|
|
|
@overrideProperty 'enabled', true
|
|
|
|
@overrideProperty 'visible', true
|
|
|
|
@overrideProperty 'checked', false
|
2013-05-16 11:34:23 +00:00
|
|
|
|
2014-05-25 07:25:36 +00:00
|
|
|
throw new Error("Unknown menu type #{@type}") if MenuItem.types.indexOf(@type) is -1
|
2013-05-16 11:34:23 +00:00
|
|
|
|
|
|
|
@commandId = ++nextCommandId
|
2015-09-01 15:34:32 +00:00
|
|
|
@click = (focusedWindow) =>
|
2016-01-12 02:03:02 +00:00
|
|
|
### Manually flip the checked flags when clicked. ###
|
2014-05-26 04:40:21 +00:00
|
|
|
@checked = !@checked if @type in ['checkbox', 'radio']
|
|
|
|
|
2015-11-21 23:13:57 +00:00
|
|
|
if @role and rolesMap[@role] and process.platform isnt 'darwin' and focusedWindow?
|
|
|
|
methodName = rolesMap[@role]
|
|
|
|
if methodInBrowserWindow[methodName]
|
|
|
|
focusedWindow[methodName]()
|
|
|
|
else
|
|
|
|
focusedWindow.webContents?[methodName]()
|
2015-09-01 15:34:32 +00:00
|
|
|
else if typeof click is 'function'
|
|
|
|
click this, focusedWindow
|
2013-08-15 09:50:30 +00:00
|
|
|
else if typeof @selector is 'string'
|
|
|
|
Menu.sendActionToFirstResponder @selector
|
2013-05-16 11:34:23 +00:00
|
|
|
|
2014-05-26 04:40:21 +00:00
|
|
|
overrideProperty: (name, defaultValue=null) ->
|
|
|
|
this[name] ?= defaultValue
|
|
|
|
|
|
|
|
overrideReadOnlyProperty: (name, defaultValue=null) ->
|
|
|
|
this[name] ?= defaultValue
|
|
|
|
Object.defineProperty this, name,
|
|
|
|
enumerable: true
|
|
|
|
writable: false
|
|
|
|
value: this[name]
|
|
|
|
|
2013-05-16 11:34:23 +00:00
|
|
|
module.exports = MenuItem
|