【问题标题】:How to restore focus to quick panel after focusing some other group to open a file?聚焦其他组以打开文件后如何将焦点恢复到快速面板?
【发布时间】:2015-06-03 17:17:24
【问题描述】:

情况是这样的:我正在写一个插件,需要:

  1. 打开快速面板
  2. 悬停第一项时,关注其他组
  3. 在该组中打开
  4. 将焦点恢复到快速面板输入,以便我可以移至列表中的下一项,依此类推...

我确实解决了 1-3,但第 4 个给我带来了麻烦。有办法吗?

【问题讨论】:

    标签: sublimetext3 sublimetext sublime-text-plugin


    【解决方案1】:

    您需要获取与快速面板关联的视图show_quick_panel 方法不会返回 view,但您可以通过 on_activated 方法和 EventListener 来获得它> 插件。

    当您聚焦任何视图(选项卡、控制台、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)
    

    现在在您的插件中,您需要告诉 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)
    

    结果:

    【讨论】:

      【解决方案2】:

      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)
      

      【讨论】:

        猜你喜欢
        • 2015-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多