How to restore focus to quick panel after focusing

2019-05-21 13:55发布

Situation is like this: I am writing a plugin that needs to:

  1. Open quick panel
  2. On hover of first item, focus other group
  3. Open in that group
  4. Restore focus to quick panel input so I can move to next item in list and so on...

I did solve 1-3 but the 4th one gives me troubles. Is there a way to do so?

1条回答
走好不送
2楼-- · 2019-05-21 14:01

You need to get the view associated with the quick panel. The method show_quick_panel doesn't returns the view, but you can get it with using the method on_activated with a EventListener plugin.

This method (on_activated) is called when you focus any view (tab, console, quick_panel...). So what this plugin will do is capture the view associated with the quick panel.

Example plugin to get the view:

import sublime, sublime_plugin

class Example(sublime_plugin.EventListener):
    def on_activated(self, view):
        """This method is called whenever a view (tab, quick panel, etc.) gains focus, but we only want to get the quick panel view, so we use a flag"""
        if hasattr(sublime, 'capturingQuickPanelView') and sublime.capturingQuickPanelView == True:
            sublime.capturingQuickPanelView = False
            """View saved as an attribute of the global variable sublime so it can be accesed from your plugin or anywhere"""
            sublime.quickPanelView = view
            print(sublime.quickPanelView)

Now in your plugin you need to tell the eventListener when the actived view corresponds to the quick panel in order to capture it. Example of what you need in your plugin:

import sublime, sublime_plugin

class Sample(sublime_plugin.WindowCommand):
    def restoreQuickPanelFocus(self):
        """Restore focus to quick panel is as easy as focus in the quick panel view, that the eventListener has previously captured and saved"""
        self.window.focus_view(sublime.quickPanelView)

    def on_highlighted(self, index):
        """Open image[index] in group 1"""
        self.window.focus_group(1)
        self.window.open_file(self.items[index])
        """Wait for image to open and restore focus to quick panel"""
        sublime.set_timeout(self.restoreQuickPanelFocus, 100)

    def run(self):
        print('runando')
        """Divide layout (as an example) """
        self.window.set_layout({
            "cols": [0.0, 0.4, 1.0],
            "rows": [0.0, 0.6, 1.0],
            "cells": [[0, 0, 2, 1], [0, 1, 1, 2], [1, 1, 2, 2]]
            })

        """Items=> images paths"""
        self.items = ('C:/images/img1.jpg','C:/images/img2.jpg','C:/images/img3.jpg','C:/images/img4.jpg','C:/images/img5.jpg')

        """Now we are going to show the quick panel, so we set the capturing flag to true as the next activated view will correspond to quick panel"""
        sublime.capturingQuickPanelView = True
        self.window.show_quick_panel(self.items, None, sublime.KEEP_OPEN_ON_FOCUS_LOST , 0, self.on_highlighted)

Result:

Result

查看更多
登录 后发表回答