Sublime Forum

[SOLVED] Is it possible to detect if panels are visible AND have focus?

#1

#Issue:

I have a plugin that was crashing when hotkey commands were executed while a panel was focused.
EG: find, console, quick panel, etc.

In order to resolve the issue, I had to figure out a way to differentiate between focused panels & active views.

This method works, but will also disable the plugin for unsaved views:

fileName = self.view.file_name()

if not fileName:
	return

This method works, and will also function on unsaved views IF they have a non-Plain Text syntax:

fileName         = self.view.file_name()
viewSyntax       = self.view.settings().get ( "syntax" )
plainText_Syntax = "Packages/Text/Plain text.tmLanguage"

if not fileName \
and viewSyntax == plainText_Syntax:
	return

Is there a better way to avoid executing a command at a focused panel, that will also include unsaved documents with a Plain Text syntax?



#Attempted Solutions:

These are the other things I’ve tried, which aside from the above examples, proved to be unhelpful:

window.active_panel()

active_panel is not a valid solution because it only establishes that a panel is visible.
It does include information as to whether or not the panel has focus.


KEYS = [ file | file_base_name | file_extension | file_name | file_path | folder | packages | platform | project | project_base_name | project_extension | project_name | project_path ]
window().extract_variables()[ "KEY" ]


KEYS = [ em_width | encoding | file_name | id | line_height | name | scope_name(point) | syntax | viewport_position | visible_region ]
view.settings().get( "KEY" )

Most of these variables & settings will return the same values for focused panels & unsaved views.

1 Like

#2

Why don’t you push your code on Github, or at least give us a complete working code showing your issue ?
The most important part of your issue is why your plugin crash and you tell nothing about it.

Anyway, I guess it’s because your code must not be executed when the current view is a widget.
You can add a context to your keybinding:

    { "keys": ["up"], "command": "smart_cursor", "args": {"cmd": "move", "by": "lines", "forward": false}, "context":
        [
            { "key": "setting.command_mode", "operand": false },
            { "key": "auto_complete_visible", "operator": "equal", "operand": false },
            { "key": "setting.is_widget", "operator": "equal", "operand": false },
            { "key": "panel_has_focus", "operator": "equal", "operand": false }
        ]
    }

Or check in the code of your plugin:
self.view.settings().get('is_widget')

4 Likes

#3

That works perfectly. Thanks!

Just saw that it’s listed on the unofficial docs. Didn’t think to look there as I figured that sort of thing would be on the official API reference.



The code in question is like 5k lines worth of my first [ Python | SublimeText ] code. It works, but I had no idea what I was doing so I’m sure a lot of things could have been done better. I’ll probably end up redoing it from scratch before releasing it, got a few newer ST projects that have priority though.

Screencast demos here & here if you’re interested.

0 Likes