Allow tracking focus change events in watchers

Fixes #2918
This commit is contained in:
Kovid Goyal 2020-08-23 08:51:04 +05:30
parent db64aef666
commit f4ddaacb3c
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 10 additions and 0 deletions

View File

@ -19,6 +19,8 @@ To update |kitty|, :doc:`follow the instructions <binary>`.
- Add a new extensible escape code to allow terminal programs to trigger - Add a new extensible escape code to allow terminal programs to trigger
desktop notifications. See :ref:`desktop_notifications` (:iss:`1474`) desktop notifications. See :ref:`desktop_notifications` (:iss:`1474`)
- Allow tracking focus change events in watchers (:iss:`2918`)
0.18.3 [2020-08-11] 0.18.3 [2020-08-11]
------------------- -------------------

View File

@ -74,6 +74,9 @@ functions for the events you are interested in, for example:
def on_resize(boss, window, data): def on_resize(boss, window, data):
# Here data will contain old_geometry and new_geometry # Here data will contain old_geometry and new_geometry
def on_focus_change(boss, window, data):
# Here data kill contain focused
def on_close(boss, window, data): def on_close(boss, window, data):
# called when window is closed, typically when the program running in # called when window is closed, typically when the program running in
# it exits. # it exits.

View File

@ -216,6 +216,9 @@ def load_watch_modules(opts: LaunchCLIOptions) -> Optional[Watchers]:
w = m.get('on_resize') w = m.get('on_resize')
if callable(w): if callable(w):
ans.on_resize.append(w) ans.on_resize.append(w)
w = m.get('on_focus_change')
if callable(w):
ans.on_focus_change.append(w)
return ans return ans

View File

@ -90,6 +90,7 @@ class Watchers:
def __init__(self) -> None: def __init__(self) -> None:
self.on_resize: List[Watcher] = [] self.on_resize: List[Watcher] = []
self.on_close: List[Watcher] = [] self.on_close: List[Watcher] = []
self.on_focus_change: List[Watcher] = []
def call_watchers(windowref: Callable[[], Optional['Window']], which: str, data: Dict[str, Any]) -> None: def call_watchers(windowref: Callable[[], Optional['Window']], which: str, data: Dict[str, Any]) -> None:
@ -459,6 +460,7 @@ class Window:
def focus_changed(self, focused: bool) -> None: def focus_changed(self, focused: bool) -> None:
if self.destroyed: if self.destroyed:
return return
call_watchers(weakref.ref(self), 'on_focus_change', {'focused': focused})
if focused: if focused:
self.needs_attention = False self.needs_attention = False
if self.screen.focus_tracking_enabled: if self.screen.focus_tracking_enabled: