Sublime Forum

How to modify the text in show_input_panel dynamic

#1

Hi, guys:
In my plugin, i call the show_input_panel to show a input panel for me. in this time, i may select a word in main editor, so the panel’s text would be the same as the word I selection.

how do i do this?

thanks.

0 Likes

#2

The input panel doesn’t support updating the text dynamically, but it might be possible to hack around this a little bit.

What you’d do is something like this:

  1. Show input panel with initial selection when command is run

  2. Set some variable indicating that you are “running” (probably class-level or module-level so that it stays after the command has been run)

  3. Have an event listener which reacts to on_selection_modified and calls another input panel with the new word (thus discarding the first panel). Only do this while “running” is True

  4. When the panel is selected or cancelled, set the “running” variable from step 2 to False

I tried implementing this, but it’s pretty nasty. It kind of works, but i would personally stay far away from it, since it violates the way people are accustomed to interacting with the input panel:

[code]from functools import partial
import sublime
from sublime_plugin import WindowCommand, EventListener

class Running(object):
is_running = False

def stop_running(*args, **kwargs):
Running.is_running = False

def noop(*args, **kargs):
pass

class TestSublimeStuffCommand(WindowCommand):

def run(self):
    Running.is_running = True
    self.window.show_input_panel('test', '1', stop_running, noop, stop_running)

class TestEventListener(EventListener):

def on_selection_modified(self, view):
    if Running.is_running:
        sublime.set_timeout(partial(self.update_input_panel, view, view.window()), 50)

def update_input_panel(self, view, window):
    word = view.substr(view.sel()[0])
    if window and word:
        window.show_input_panel('test', word, stop_running, noop, stop_running)

[/code]

Note that you need to do some callbacks and stuff to make sure you manage when it’s running and not, and you need to give the command enough time for the selection to change (i.e. if you double click - thats what the 50 ms are for).

But again… Please don’t use it, at least not without THOROUGH testing.

0 Likes

#3

[quote=“miped”]The input panel doesn’t support updating the text dynamically, but it might be possible to hack around this a little bit.
[/quote]

I think you could modify the input panel dynamically. After all, window.show_input_panel returns a view. Of course, that’s just speculation, haven’t actually tried anything.

0 Likes

#4

Hadn’t seen that. Using the returned view would make it the solution above slightly less hacky. But it’d still break user expectations about how it works, so it’d need to be well-documented.

0 Likes