情况是这样的:我正在编写一个需要的插件:
我确实解决了1-3,但第四个给了我麻烦。有办法吗?
您需要获取与快速面板关联的视图。方法show_quick_panel不返回视图,但您可以使用带有EventListener插件的on_activated方法来获取它。
当您聚焦任何视图(选项卡,控制台,quick_panel ...)时,将调用此方法(on_activated)。因此,此插件将执行的操作是捕获与快速面板关联的视图。
获取视图的示例插件:
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)
现在,在插件中,当actived视图对应于快速面板以捕获它时,您需要告诉eventListener。插件中需要的示例:
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)
结果: