如何在聚焦其他组打开文件后将焦点恢复到快速面板?
How to restore focus to quick panel after focusing some other group to open a file?
情况是这样的:我正在写一个插件需要:
- 打开快捷面板
- 悬停第一个项目时,关注其他组
- 在该组中打开
- 将焦点恢复到快速面板输入,这样我就可以移动到列表中的下一个项目等等...
我确实解决了 1-3 但第 4 个给我带来了麻烦。有办法吗?
您需要获取与快速面板关联的视图。方法 show_quick_panel 没有 returns view,但是你可以使用方法 [=25] =]on_activated 带有 EventListener 插件。
此方法 (on_activated) 在您聚焦任何视图(选项卡、控制台、quick_panel...)时调用。所以这个插件要做的是捕获与 quick panel.
关联的视图
获取视图的示例插件:
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 活动视图何时对应于快速面板,以便捕获它。您在插件中需要的示例:
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)
结果:
sergioFC 的回答对我不起作用 (sublime 3 3210) - 文件视图获得焦点而不是快速面板。
一些选择https://sublimetext.userecho.com/communities/1/topics/2482-open-file-in-current-group-from-api
作为保持快速面板聚焦的一种方式(文件在当前组中打开)
self.window.open_file(items[index][2], 8|sublime.TRANSIENT)
情况是这样的:我正在写一个插件需要:
- 打开快捷面板
- 悬停第一个项目时,关注其他组
- 在该组中打开
- 将焦点恢复到快速面板输入,这样我就可以移动到列表中的下一个项目等等...
我确实解决了 1-3 但第 4 个给我带来了麻烦。有办法吗?
您需要获取与快速面板关联的视图。方法 show_quick_panel 没有 returns view,但是你可以使用方法 [=25] =]on_activated 带有 EventListener 插件。
此方法 (on_activated) 在您聚焦任何视图(选项卡、控制台、quick_panel...)时调用。所以这个插件要做的是捕获与 quick panel.
关联的视图获取视图的示例插件:
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 活动视图何时对应于快速面板,以便捕获它。您在插件中需要的示例:
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)
结果:
sergioFC 的回答对我不起作用 (sublime 3 3210) - 文件视图获得焦点而不是快速面板。
一些选择https://sublimetext.userecho.com/communities/1/topics/2482-open-file-in-current-group-from-api 作为保持快速面板聚焦的一种方式(文件在当前组中打开)
self.window.open_file(items[index][2], 8|sublime.TRANSIENT)