sublime_text icon indicating copy to clipboard operation
sublime_text copied to clipboard

Menus without child menuitems should be hidden

Open titoBouzout opened this issue 10 years ago • 9 comments

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.

titoBouzout avatar Feb 10 '16 05:02 titoBouzout

I think it should be disabled instead.

FichteFoll avatar Feb 13 '16 00:02 FichteFoll

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

titoBouzout avatar Feb 13 '16 17:02 titoBouzout

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.

FichteFoll avatar Feb 14 '16 00:02 FichteFoll

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 avatar Jul 08 '16 23:07 duckythescientist

@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 avatar Jul 11 '16 14:07 FichteFoll

@FichteFoll, interesting. It works with my Ubuntu 14.04, ST 3114, but it doesn't work in my Windows 7 with ST 3114.

duckythescientist avatar Jul 11 '16 19:07 duckythescientist

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:

  1. https://github.com/DamnWidget/anaconda/issues/571
  2. https://forum.sublimetext.com/t/hide-parent-menu-item-in-context-menu/11272
  3. https://forum.sublimetext.com/t/how-to-hide-parent-menu-entry-using-the-is-visible/23913

evandrocoan avatar Oct 23 '16 21:10 evandrocoan

I can confirm. I have same issue on OSX.

pioz avatar Aug 22 '25 09:08 pioz

@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.

pioz avatar Aug 22 '25 09:08 pioz