How to restore focus to quick panel after focusing some other group to open a file?

不想你离开。 提交于 2019-12-22 12:32:32

问题


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:


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:



来源:https://stackoverflow.com/questions/30626639/how-to-restore-focus-to-quick-panel-after-focusing-some-other-group-to-open-a-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!