Sublime Forum

Checking selected menu items

#1

How do we make a menu item “checkable,” as in, selected, and keep the state of that selection?

0 Likes

#2

You’ll need 2185 or later for this to work, although it’s not out yet. When it is, you’ll need to:

  • Implement is_checked in your plugin
  • Add “checkbox”: true to the corresponding entry in the .sublime-menu file
0 Likes

#3

Oh Jon, you’re such a tease.

0 Likes

#4

hi,

how exactly do I need to implement is_checked? I’ve looked around the docs, but it appears nowhere.

Thanks, mates

0 Likes

#5

class MyCheckableCommand(sublime_plugin.WindowCommand):
def run(self):
pass # do something

def is_checked(self):
    return True # or False
0 Likes

#6

I couldn’t figure out how to get the current value from the menu entry! Really, really thank you! My problem was I tried that entry without a command, and it was behaving like a checkbox, and I expected I should grab the value, not override it with is_checked!

Thank you again =)

0 Likes

#7

Just wanted to add that if the command requires an argument, is_checked takes the same argument with the same name. This is how you can know whether or not to return True or False.

For example if one of your menu entries look like this:

{
    "command": "some_command",
    "caption": "Display Text",
    "checkbox": true,
    "args": {
        "arg_name": "hello"
    }
},

Then your is_checked can look like this, taking the same argument

def is_checked(self, arg_name=None):
    return arg_name is None
0 Likes