Home Download Buy Blog Forum Support

Menu question

Menu question

Postby rbabiak on Fri Feb 18, 2011 9:15 am

I have been trying to make a menu for my plug in and am finding there is a lack of documentation on the menu structure.

For instance the word wrap menu entry is
<item caption="&amp;Word Wrap" command="toggle wordWrap"/>

What does toggle do, and how can my plugin find out about the state of the check?
There is a lot of other commands that are kind of cryptic.
toggleApp

this is intresting also
<item caption="&amp;%I %s" command="openRecentProject %i" repeat="8"/>
how does the repeat work?

For instance can i make a menu option that pops up a single line edit window and allow the user to enter a name? Then have that menu option be "Server: xxxxx" and change when the user enters a new name?
rbabiak
 
Posts: 37
Joined: Sun Oct 25, 2009 12:33 pm

Re: Menu question

Postby jps on Fri Feb 18, 2011 9:38 am

(for other readers: this is relevant to Sublime Text 1 only)

The 'toggle' command toggles the value of an option. You can get the current value of an option via:

Code: Select all
view.options().get('wordWrap')


'repeat' is just shorthand for repeating the item entry in the XML that many times, each time replacing %i with an incrementing number, e.g.,

Code: Select all
<item caption="&amp;%I %s" command="openRecentProject 0"/>
<item caption="&amp;%I %s" command="openRecentProject 1"/>
...


With regards to adding your own menu entries, the general way is to make a plugin that does want you want, and then create a menu item with the corresponding command. It's not possible to for Python commands to customize the text shown for their menu item, however.
jps
Site Admin
 
Posts: 3067
Joined: Wed Mar 19, 2008 12:33 pm

Re: Menu question

Postby rbabiak on Mon Feb 21, 2011 8:25 am

Added this to the Main.sublime-menu file

<item caption="&amp;Run Client" command="toggle runClient"/>

The item appears in my menu and is checkable, and uncheckable.

Using the console all I get returned is None

>>> print view.options().get("runClient")
None

Do i have to set the name into a config file or something to make it work?
rbabiak
 
Posts: 37
Joined: Sun Oct 25, 2009 12:33 pm

Re: Menu question

Postby jps on Mon Feb 21, 2011 9:33 am

The option won't exist until you toggle it the first time:

Code: Select all
>>> print view.options().get('foo')
None
>>> view.runCommand('toggle foo')
>>> view.options().get('foo')
True
jps
Site Admin
 
Posts: 3067
Joined: Wed Mar 19, 2008 12:33 pm

Re: Menu question

Postby rbabiak on Tue Feb 22, 2011 10:27 am

The example you give works, but

>>> view.runCommand("toggle runClient")
>>> print view.options().get("runClient")
False

I check the menu at this point and it is checked.

>>> view.runCommand("toggle runClient")
>>> print view.options().get("runClient")
True

Again checked the menu and it is still checked, many repeats don't change the setting.

Also changing the menu so it is checked or unchecked still produces the same result as the last run command. I am adding this to Main.sublime-menu if that makes a difference. There appears to be a non obvious disconnect between the view.options() and the main menu. Below is the menu XML I am using and the console commands that I am using to try and poll the options.

Code: Select all
   <menu caption="&amp;CCP">
      <item caption="&amp;Launch" command="slipwayCmd"/>
      <menu caption="&amp;Client">
         <item caption="&amp;Run Client" command="toggle runClient"/>
         <item caption="&amp;Debug" command="toggle debugClient"/>
         <item caption="&amp;Jessica" command="toggle jessicaClient"/>
         <item caption="&amp;x64" command="toggle x64Client"/>
         <item caption="Debugger" command="toggle debuggerClient"/>
      </menu>
   </menu>

>>> view.runCommand("toggle runClient")
>>> print view.options().get("runClient")
Last edited by rbabiak on Tue Feb 22, 2011 10:29 am, edited 1 time in total.
rbabiak
 
Posts: 37
Joined: Sun Oct 25, 2009 12:33 pm

Re: Menu question

Postby rbabiak on Tue Feb 22, 2011 10:28 am

also here is my command

Code: Select all
import sublime, sublimeplugin

class slipwayCmd(sublimeplugin.ApplicationCommand):
    # -----------------------------------------------------------------------------------
    def run(self, args):
        o = sublime.options()
        a = ["runClient", "debugClient", "jessicaClient", "x64Client", "debuggerClient",]
        for each in a:
            print each, o.get(each)
        print "-------------------------------------"
Last edited by rbabiak on Tue Feb 22, 2011 10:54 am, edited 1 time in total.
rbabiak
 
Posts: 37
Joined: Sun Oct 25, 2009 12:33 pm

Re: Menu question

Postby jps on Tue Feb 22, 2011 10:35 am

Your command is asking for the global options, but the toggle command works on the view options. Use view.options().
jps
Site Admin
 
Posts: 3067
Joined: Wed Mar 19, 2008 12:33 pm

Re: Menu question

Postby rbabiak on Tue Feb 22, 2011 12:26 pm

yes but as i said in the first post, the menu doesn't change the value even when i use the view.options().

The root problem is still if i toggle a option in the menu, none of the ways presented in this thread return the current state of the check on the menu.
rbabiak
 
Posts: 37
Joined: Sun Oct 25, 2009 12:33 pm

Re: Menu question

Postby jps on Tue Feb 22, 2011 12:49 pm

Ah, this ones a bit tricky. view.options() will return the options for the current file being edited. The menu, on the other hand, will display the value of the option for the widget that currently has input focus. This may well mean that it's displaying the value of the option from within the console view, rather than the view associated with the file you were last editing.

To ensure that the menu is always displaying the the option from the currently edited file, check that the file itself has input focus before showing the menu, and not the console panel.
jps
Site Admin
 
Posts: 3067
Joined: Wed Mar 19, 2008 12:33 pm

Re: Menu question

Postby rbabiak on Tue Feb 22, 2011 1:08 pm

yep that did it.
using the following to get the options and they mirror the menu exactly.

Code: Select all
sublime.activeWindow().activeView().options()



Thanks for the help.
rbabiak
 
Posts: 37
Joined: Sun Oct 25, 2009 12:33 pm

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 6 guests