如何在键盘绑定中检测侧边栏是否打开?



我想cmd+1在侧边栏中显示(如果关闭(和如果侧边栏打开,请关闭它之间交替。

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

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

我不知道如何做if部分 .谢谢

据我所知,没有内置的键绑定上下文可用于判断侧边栏是打开还是关闭。但这可以使用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" },
],
},

相关内容

最新更新