Contextmenu and Atom menu


#1

It looks like Atom’s main menu and contextmenu aren’t using the same API. For example it looks like the main menu API has more features (like radio, checkbox and sublabel support) then the contextmenu API. Can anyone confirm and tell me why…

Menu code:
https://github.com/atom/electron/blob/master/atom/browser/api/lib/menu.coffee
https://github.com/atom/electron/blob/master/atom/browser/api/lib/menu-item.coffee

Menu docs:



Contextmenu code:



https://github.com/atom/atom/blob/master/src/browser/context-menu.coffee

Contextmenu docs:

I was looking for a way to implement a radio-submenu in my contextmenu. Apparently Atom supports radios/checkboxes in their main menu, but not in the contextmenu. However when trying the radio menu-items (not the checkbox menu-items), the icons do show up, but I can’t do anything with them (no checked/unchecked options).

I also tried to add my own contextmenu, but that resulted in two contextmenu’s.


How to remove items from Menus of Existing packages
#2

The most obvious difference I can see is that context menus must provide a selector to indicate in which parts of the application the item should show when the context menu opens. If I recall correctly, the Electron API for menus is the same as that for context menus, so it should be possible to get their APIs in line with each other.

It never hurts to open a feature/pull request :slight_smile:


#3

I was able to get both the main menu and context menu to dynamically update to show if a setting was enabled or disabled. In the main menu you can change the template then issue a atom.menu.update(). In the context menu you have to manually add the context menu saving the disposable it returns when you do so, then when you want to update it you dispose() that disposable and re-add it again with whatever changes you want. There’s a small issue with this approach: Sometimes the position of the context menu will change when you remove and re-add it. So in my package I added a setTimeout() on the initial insertion to make sure it happened last. It seems to work pretty well so far. One thing I haven’t figured out is how to best assure that dispatching a menu command that would update the menus only does so once. Right now I filter on atom.workspace.getActiveTextEditor() is @editor but that means the menu option won’t work unless you’ve got an active text editor (not the case when you have the settings panels open for example). Anyway, commit is here: https://github.com/lexicalunit/multi-wrap-guide/commit/93822035661566c291bca4c3d2c5164fe93a49cb