Sublime Forum

I need to create a unique popup menu that I can use alongside the default context_menu

#1

2019-05-30T04:00:00Z

I am currently looking for a way to create a unique context_menu that does not rely on any of the default .sublime-menus menu files already in existance.
I would just use the regular context_menu, but I have that pretty well filled up already. And I would like different buttons for different menus.

This is my current right click menu:
context_menu
I would like to create another dynamic menu similar to this menu, but one that is not dependent on the default.sublime-menus, and can be activated by using the key/mouse combonation ctrl+button4.

{
  "button": "button4",
  "count": 1,
  "modifiers": ["ctrl"],
  "command": "quick_click"
}

I have looked at show_popup_menu () as an option and have used the paste_from_history command as a sort of reference.
Class PasteFromHistoryCommand(sublime_plugin.TextCommand):
However, the only thing that menu seems to do, is take the selection which has been highlighted from a list, and pass it to a text variable which is passed to the text command paste.
i.e.

g_clipboard_history.push_text(text)
sublime.set_clipboard(text)
self.view.run_command("paste")

show_popup_menu(items, on_done, flags) has the fields ‘items’, ‘on_done’, ect. And I do not know what these mean or how to use them.

I have tried code like this: (And I get no feedback whatsoever)

class QuickClickCommand(sublime_plugin.TextCommand):
  def run(self, view):
    self.view.show_popup_menu (
      [
        "Select All",
        "Swap Case"
      ],
      [
        self.view.run_command("select_all"),
        self.view.run_command("swap_case")
      ],
      ["1", "2"]  // ← with or without this option
    )

But when I use the following code ►

class QuickClickCommand(sublime_plugin.TextCommand):
  def run(self, view):
    self.view.show_popup_menu (
      ["Item 1", "Item 2"], ["1", "2"]
    )

► I get a context menu with 2 items, neither of which do a single thing when pressed upon.
show_popup
I’ve tried several combinations - nothing seems to work and I cannot find information on this topic.

As you can see I have a plugin:

class QuickClickCommand(sublime_plugin.TextCommand):

And I access that plugin by selecting ctrl+button4

{
    "button": "button4",
    "count": 1,
    "modifiers": ["ctrl"],
    "command": "quick_click"
}

Is it possible to create my own quickclick .sublime-menu file or am I stuck using the default context_menu .sublime-menu?

Any links, suggestions, or explanations of how
show_popup_menu(items, on_done) is used would be appreciated.

Here is what the user docs say about it:
Shows a pop up menu at the caret, to select an item in a list. on_done will be called once, with the index of the selected item. If the pop up menu was cancelled, on_done will be called with an argument of -1. Items is an array of strings.
Flags currently has no option.

When I use this code:I get the following result.

class QuickClickCommand(sublime_plugin.TextCommand):
  def run(self, view):
    self.view.show_popup('Hello, <b>World!</b><br>
      <a href="moo">Click Me</a>', on_navigate=print)
    self.view.show_popup_menu (["Item 1", "Item 2"], ["1", "2"]) 

the%20one
Is there a way to make my own custom menu in sublime aside from adding content to the 11 other possible menus that are available upon instillation?

Thank you for your patients in reading this and any time you might spend on replying to me.

1 Like

#2

See my answer on your Stack Overflow question for an explanation of why this isn’t working for you and a working example.

0 Likes