【问题标题】:How to detect in keybindings if sidebar is open?如果侧边栏打开,如何检测键绑定?
【发布时间】:2018-05-12 04:41:19
【问题描述】:

我希望cmd+1 在侧边栏显示(如果关闭)和侧边栏打开时交替显示。

如果关闭:{ "keys": ["super+1"], "command": "reveal_in_side_bar"}

如果打开:{ "keys": ["super+1"], "command": "toggle_side_bar" }

我不知道怎么做if 部分 .谢谢

【问题讨论】:

    标签: sublimetext3 sublimetext2 sublimetext sublimetext-snippet


    【解决方案1】:

    据我所知,没有内置的键绑定上下文可以用来判断侧边栏是打开还是关闭。但这可以使用Python API 轻松完成,特别是使用window.is_sidebar_visible(),并且还可以创建自定义键绑定上下文。

    从“工具”菜单中,导航至“开发人员”>“新插件”。然后将视图的内容替换为:

    import sublime, sublime_plugin
    
    class SidebarContextListener(sublime_plugin.EventListener):
        def on_query_context(self, view, key, operator, operand, match_all):
            if key != 'sidebar_visible' or not (operand in ('reveal', 'toggle')):
                return None
            visible = view.window().is_sidebar_visible()
            if operand == 'toggle' and visible:
                return True
            if operand == 'reveal' and not visible:
                return True
            return None
    

    并将其保存在 ST 建议的文件夹 (Packages/User) 中,类似于 sidebar_context.py - 扩展名很重要,名称不重要。

    现在,我们可以在您的键绑定中使用它,例如:

    { "keys": ["super+1"], "command": "toggle_side_bar", "context":
        [
            { "key": "sidebar_visible", "operand": "toggle" },
        ],
    },
    
    { "keys": ["super+1"], "command": "reveal_in_side_bar", "context":
        [
            { "key": "sidebar_visible", "operand": "reveal" },
        ],
    },
    

    【讨论】:

    • 免责声明:我是在手机上写的,所以如果有语法错误或其他问题并且它不起作用,请原谅我 - 另外,请随时编辑答案并修复它:)
    • 谢谢你 - 它看起来非常好,但我需要先深入研究 python 文档和 sublime 文档,然后才能先解决这个问题。
    • 通过我在上面所做的(轻微)编辑,它似乎对我来说工作正常。请注意,切换侧边栏的命令不是您想的那样。上面的代码现在对你有用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 2022-07-19
    • 2010-09-23
    相关资源
    最近更新 更多