Home Download Buy Blog Forum Support

Dynamic menu items

Dynamic menu items

Postby avataru on Fri Apr 13, 2012 11:18 am

Hello everyone,

I created a plugin that has a single command which accepts a single argument. The available argument values are stored in a .sublime-settings file and users can add and change them as they wish. Currently I manually add an entry in the context menu for each argument but it would be nice if this was done dynamically. However, I couldn't find any way to do this through the API.

I'd rather not have Sublime edit the plugin's Context.sublime-menu file each time it loads the plugin but at the moment I think this is the only solution. Any ideas how to do this in a nice way (dynamically, through the API)?

To make things clear, here's an example:

my_plugin.py
Code: Select all
import sublime, sublime_plugin

settings = sublime.load_settings('MySettings.sublime-settings')

class MyPluginActionCommand(sublime_plugin.TextCommand):
   def run(self, edit, argument):
      current_argument = settings.get(argument)

      # do stuff based on current_argument

MySettings.sublime-settings
Code: Select all
{
   "first_argument": "this is the first available argument",
   "second_argument": "this is the second available argument"
}


Right now, I have to manually create a Context.sublime-menu file but I want the menu items to automatically appear in the context menu:
Code: Select all
[
   { "id": "end" },
   { "caption": "-" },
   {
      "caption": "My Plugin",
      "id": "my_plugin",
      "children":
      [
         {
            "caption": "First action",
            "command": "my_plugin_action", "args": { "argument" : "first_argument" }
         },
         {
            "caption": "Second action",
            "command": "my_plugin_action", "args": { "argument" : "second_argument" }
         }
      ]
   }
]
avataru
 
Posts: 5
Joined: Fri Apr 13, 2012 10:56 am

Re: Dynamic menu items

Postby jbjornson on Fri Apr 13, 2012 12:46 pm

Maybe this discussion can help?
viewtopic.php?f=4&t=2322&p=10510#p10510
jbjornson
 
Posts: 335
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: Dynamic menu items

Postby avataru on Fri Apr 13, 2012 2:22 pm

So I guess the only solution would be to edit the Context.sublime-menu through python, every time my_script.py is loaded.
avataru
 
Posts: 5
Joined: Fri Apr 13, 2012 10:56 am

Re: Dynamic menu items

Postby avataru on Sun Apr 15, 2012 10:49 pm

Looking at Main.sublime-menu I found the following (note the $ variables which seem to generate multiple menu items):
Code: Select all
            {
                "caption": "Syntax",
                "mnemonic": "S",
                "id": "syntax",
                "children": [ { "command": "$file_types" } ]
            }
Code: Select all
            {
                "caption": "Dictionary",
                "children": [ { "command": "$dictionaries" } ]
            }
Code: Select all
            {
                "caption": "Build System",
                "mnemonic": "u",
                "children":
                [
                    { "command": "set_build_system", "args": { "file": "" }, "caption": "Automatic", "checkbox": true },
                    { "caption": "-" },
                    { "command": "$project_build_systems" },
                    { "command": "$build_systems" },
                    { "caption": "-" },
                    { "command": "new_build_system", "caption": "New Build System…" }
                ]
            }
Code: Select all
            {
                "caption": "Macros",
                "children": [ { "command": "$macros" } ]
            }
Code: Select all
            {
                "caption": "Color Scheme",
                "children": [ { "command": "$color_schemes" } ]
            }

That's exactly what I need. :)
avataru
 
Posts: 5
Joined: Fri Apr 13, 2012 10:56 am

Re: Dynamic menu items

Postby jps on Mon Apr 16, 2012 1:31 am

There's no support for this in the API, unfortunately
jps
Site Admin
 
Posts: 3058
Joined: Wed Mar 19, 2008 12:33 pm

Re: Dynamic menu items

Postby avataru on Mon Apr 16, 2012 4:48 am

Alright, thank you for the reply Jon.
avataru
 
Posts: 5
Joined: Fri Apr 13, 2012 10:56 am


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], valerij_ and 10 guests