情况是这样的:我正在编写一个插件,需要:
- 打开快速面板
- 将鼠标悬停在第一项上时,聚焦其他组
- 在该群组中打开
- 将焦点恢复到快速面板输入,以便我可以移至列表中的下一项,依此类推...
我确实解决了 1-3,但第四个给了我麻烦。有没有办法做到这一点?
最佳答案
您需要获取与快速面板关联的 View 。 show_quick_panel 方法不会返回 View ,但您可以通过使用 on_activated 方法和 EventListener 来获取它> 插件。
当您聚焦任何 View (选项卡、控制台、quick_panel...)时,会调用此方法 (on_activated)。所以这个插件要做的就是捕获与快速面板关联的 View 。
获取 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)
现在,在您的插件中,您需要告诉 eventListener 事件 View 何时对应于快速面板才能捕获它。您的插件中需要的示例:
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)
结果:
关于sublimetext3 - 在聚焦其他组打开文件后如何将焦点恢复到快速面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30626639/