Sublime Forum

How to get visibility and focus of sidebar?

#1

As title say.

Background: the following code does not hide the sidebar because self.view.window().run_command(“focus_side_bar”) is nullified by the fact that sidebar is already visible and focused:

[code]import sublime, sublime_plugin

class toggle_sidebar(sublime_plugin.TextCommand):
def run(self, edit):
self.view.window().run_command(“toggle_side_bar”)
self.view.window().run_command(“focus_side_bar”)[/code]
So, I need to check sidebar visibility & focus status to decide whether to run focus_side_bar command

1 Like

#2

Build 3098 added Window.is_sidebar_visible() and Window.set_sidebar_visible() but as far as I know there is still no way to determine if it has the focus

also related:


1 Like

#3

So could it now be possible to use sidebar_visible in key binding contexts in some way? Obviously I intended to have a key binding to toggle the sidebar if it is visible (to hide it) and one (the same key) to focus the sidebar if it is not visible.

I suppose this is something that would just be much easier to do in a plugin?

And 5 minutes later:

import sublime
import sublime_plugin

class SideBarHopperCommand(sublime_plugin.WindowCommand):
    def run(self):
        if self.window.is_sidebar_visible():
            self.window.set_sidebar_visible(False)
        else:
            self.window.set_sidebar_visible(True)
            self.window.run_command("focus_side_bar")
    {
        "command": "side_bar_hopper",
        "keys": [
            "f1"
        ]
    },

There seems to be something odd though, it will sometimes focus the file in the folder tree instead of the open-files list at the top. Will probably debug this if it happens often enough to annoy me.

2 Likes

#4

Consider adding

self.window.focus_group(self.window.active_group())

above the line:

self.window.set_sidebar_visible(False)

…otherwise the sidebar can retain input focus while it’s invisible.

3 Likes