Menus without child menuitems should be hidden
In a couple of situations all child menu-items are not shown due to "is_visible" because simply the context on which these are triggered is not relevant. However the container of these menu-items(the menu) are still shown empty. Which disturbs a bit, and sometimes may cause confusion.
I think it should be disabled instead.
Well, as the child items are invisible, I think the right thing to do here it to hide it. If the menu was created to be show, then the child menu items will not have "is_visible" returning False.
If all child menu items agree to return False for "is_visible" (therefore the menu is empty) then the menu should be hidden too
Good point.
There is no way to set the visible or enabled state of a menu item containing child items, thus you can not tell ST which should be preferred. Usually hiding information should very rarely be done because it confuses users as to why things are "not there". Explicitly disabling something has more feedback.
Maybe a new key for .sublime-menu files could be added that specifies if a menu item without visible children should be hidden or disabled.
I've found a workaround:
EDIT: this doesn't seem to work in Windows. It at least works in Ubuntu 14.04, ST 3114.
Make a command in your plugin whose entire purpose is to check if it should be enabled/visible (either by performing the check directly or by querying all of the children explicitly). For the parent menu item, add a "command" key with a value of your new command.
class CfptTest(sublime_plugin.TextCommand):
"""Return the text in the highlighted region
"""
def gettext(self):
region = self.view.sel()[0]
return self.view.substr(region)
"""Disable command when no text selected
"""
def is_enabled(self):
return self.gettext() is not ""
and
[
{ "caption": "Copy-From" , "command": "cfpt_test", "children":
[
....
]
}
]
@duckythescientist, doesn't work for me. Neither is_enabled nor is_visible get called. Windows 10, ST 3118.
It would be a fine solution if it worked however.
@FichteFoll, interesting. It works with my Ubuntu 14.04, ST 3114, but it doesn't work in my Windows 7 with ST 3114.
Also hidden them when all its children are invalid. It is necessary for the https://github.com/DamnWidget/anaconda/issues/571.
Well, if there is the is_visible for the children, why not for its parent?

Or allow the is_visible command as:
import sublime
import sublime_plugin
class AnacondaContextMenuVisibilityCommand(sublime_plugin.TextCommand):
def run(self, edit):
return False
def is_visible(self):
print( ' Calling AnacondaContextMenuVisibilityCommand is_visible...' )
return False
Which does not work currently work for this menu entry file:
[
{
"caption": "Anaconda",
"command": "anaconda_context_menu_visibility",
"id": "anaconda_context",
"children":
[
{
"command": "anaconda_goto",
"caption": "Goto Definition"
},
]
}
]
Related threads:
- https://github.com/DamnWidget/anaconda/issues/571
- https://forum.sublimetext.com/t/hide-parent-menu-item-in-context-menu/11272
- https://forum.sublimetext.com/t/how-to-hide-parent-menu-entry-using-the-is-visible/23913
I can confirm. I have same issue on OSX.
@FichteFoll, interesting. It works with my Ubuntu 14.04, ST 3114, but it doesn't work in my Windows 7 with ST 3114.
I can confirm that this trick also does not work on Mac OSX.