Sublime Forum

Intercept "open_project" input panel context

#1

When pressing ctrl+alt+p a (quick?) input panel is open, allowing me to switch to another project.
I would like to be able to press ctrl+alt+p again and automatically switch to the previously opened project. As such I was looking for a way to intercept that specific “open_project” input panel context, but apparently it seems it’s a just normal quick input panel with no identifier. The best I could come up with is this, but it’s too generic:

{ "keys": ["ctrl+alt+p"], "command": "switch_to_previous_project", "context":
    [
        { "key": "overlay_visible", "operator": "equal", "operand": true },
        { "key": "panel_has_focus", "operator": "equal", "operand": false },
    ]
}

Another idea I had in mind was to override the original command class / cmd (that is "project_manager", args={"action": "open_project"}) and attach an identifier to its settings, but I was wondering if perhaps there is an easier way.

Thanks in advance.

0 Likes

#2

OK, I managed to implement this by using an event listener which guesses when a project_manager input panel view shows up, and attaches an identifier to it. It requires https://github.com/randy3k/ProjectManager package. In case somebody will ever need this:

import sublime
import sublime_plugin


LAST_WINDOW_CMD = None
CURR_FOCUSED_VIEWS = {}


def current_focused_view(window=None):
    window = window or sublime.active_window()
    return CURR_FOCUSED_VIEWS.get(window, None)


class SmartProjectManagerCommand(sublime_plugin.WindowCommand):
    """Override "open project" input panel command. If the panel is
    already open, and I press ctrl+alt+p again, this will automatically
    switch to the previously opened project. Kind of similar to
    desktop's "alt+tab", but for ST projects. See:
    https://forum.sublimetext.com/t/66568"""

    def run(self):
        view = current_focused_view()
        if view.settings().get("is_panel_project_manager"):
            items = sublime.project_history()
            if len(items) > 1:
                self.window.run_command("close_workspace")
                self.window.run_command(
                    "open_project_or_workspace", {"file": items[1]}
                )
                self.window.run_command("hide_overlay")
        else:
            self.window.run_command(
                "project_manager", {'action': 'open_project'}
            )


class Events(sublime_plugin.EventListener):
    def on_activated(self, view):
        CURR_FOCUSED_VIEWS[view.window()] = view
        if (
            LAST_WINDOW_CMD == "project_manager"
            and view.element() == "quick_panel:input"
        ):
            view.settings().set("is_panel_project_manager", True)

    def on_window_command(self, view, cmd_name, args):
        global LAST_WINDOW_CMD
        LAST_WINDOW_CMD = cmd_name

…and in the sublime-keymap file:

{ "keys": ["ctrl+alt+p"], "command": "smart_project_manager", },
0 Likes