Sublime Forum

Enable/Disable Parent menu item?

#1

I’ve in my menu an item with children.
Example:

[code]{
“caption”: “Parent Item”,
“children”:

    {"caption": "Child Item 1", "command": "child", "args": {"sub": "one"}},
    {"caption": "Child Item 2", "command": "child", "args": {"sub": "two"}},
    {"caption": "Child Item 3", "command": "child", "args": {"sub": "three"}}
]

},
[/code]
I can enable/disable all the child item with “is_enabled” in command “child”. But also if the childs are disabled - the parent item is enabled. Exists a way to enable/disable the parent item too?

0 Likes

#2

I’d try to provide “command” and “args” keys to the parent menu where you do is_enabled. If it doesn’t work than there’s probably no way to do that.

0 Likes

#3

Now I’ve tried this:
commands:

class ParentItemCommand(sublime_plugin.TextCommand):
    parent_item_enable = False
    def run(self, edit, sub):
        if sub == 'one':
            print('do stuff: "one"')
        elif sub == 'two':
            print('do stuff: "two"')
        elif sub == 'three':
            print('do stuff: "three"')

    def is_enabled(self):
        return self.parent_item_enable


class EnDisableParentCommand(sublime_plugin.TextCommand):
    def run(self, edit, enable=False):
        ParentItemCommand.parent_item_enable = enable

menu:

{"caption": "Parent Item",
    "children":
    
        {"caption": "Child One", "command": "parent_item", "args": {"sub": "one"}},
        {"caption": "Child Two", "command": "parent_item", "args": {"sub": "two"}},
        {"caption": "Child Three", "command": "parent_item", "args": {"sub": "three"}}
    ]},

But unfortunately the parent item provides no disabling. I think only item, that using commands, can be disbled in the used command itself.
The parent item uses only the internal command “show-your-children” (I dont know which name is it really). And so it ignores the disable setting.

I’ll play around this problem more. May be, I find out a workaround to solve this.

0 Likes

#4

This was my only idea, but you’re right, it doesn’t seem to work.

Main.sublime-menu


  { "id": "help" ,
    "children": 
      { "caption": "-"
      },
      { "caption": "Parent Item",
        "command": "parent_item",
        "args": {"sub": "parent"},
        "children":
        
            { "caption": "Child One", "command": "parent_item", "args": {"sub": "one"}
            },
            { "caption": "Child Two", "command": "parent_item", "args": {"sub": "two"}
            },
            { "caption": "Child Three", "command": "parent_item", "args": {"sub": "three"}
          }
        ]
      },
      { "caption": "Enable",
        "command": "en_disable_parent", "args": {"enable": true}
      },
      { "caption": "Disable",
        "command": "en_disable_parent", "args": {"enable": false}
      }
    ]
  }
]

Python

import sublime_plugin


class ParentItemCommand(sublime_plugin.TextCommand):
    parent_item_enable = False

    def run(self, edit, sub=None):
        print('do stuff: "%s"' % sub)

    def is_enabled(self, sub=None):
        return self.__class__.parent_item_enable


class EnDisableParentCommand(sublime_plugin.TextCommand):
    def run(self, edit, enable=False):
        ParentItemCommand.parent_item_enable = enable
0 Likes