Home Download Buy Blog Forum Support

how to modify the text in show_input_panel dynamic

how to modify the text in show_input_panel dynamic

Postby fjjohnchen on Fri Jul 05, 2013 3:23 am

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.
fjjohnchen
 
Posts: 1
Joined: Fri Jul 05, 2013 3:19 am

Re: how to modify the text in show_input_panel dynamic

Postby miped on Mon Jul 08, 2013 9:22 pm

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: Select all
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)


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.
SublimeGit: Git integration for Sublime Text 2 and 3
https://sublimegit.net
miped
 
Posts: 16
Joined: Mon Jul 08, 2013 6:30 pm

Re: how to modify the text in show_input_panel dynamic

Postby skuroda on Tue Jul 09, 2013 2:09 am

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


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.
skuroda
 
Posts: 676
Joined: Fri Oct 05, 2012 8:11 am

Re: how to modify the text in show_input_panel dynamic

Postby miped on Tue Jul 09, 2013 5:24 am

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.
SublimeGit: Git integration for Sublime Text 2 and 3
https://sublimegit.net
miped
 
Posts: 16
Joined: Mon Jul 08, 2013 6:30 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 9 guests