From b4d43249b0e19d63acb650c70b5db7bece1e99bf Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 1 Oct 2026 15:54:20 +0530 Subject: [PATCH] Fix focus changing when closing non focused window before the focused one --- docs/changelog.rst | 3 +++ kitty/window_list.py | 3 ++- kitty_tests/layout.py | 4 +++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 460c9fec0..845a5c17d 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -206,6 +206,9 @@ Detailed list of changes status bars, file trees, etc. Create them with :code:`launch --type=window-dock` or :code:`launch --type=tab-dock`, see :ref:`docks` +- Fix closing an unfocused window that comes before the focused window in the + layout sometimes moving focus to a different window + 0.49.2 [2026-10-01] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/window_list.py b/kitty/window_list.py index ccc569cc2..d4c6216aa 100644 --- a/kitty/window_list.py +++ b/kitty/window_list.py @@ -656,7 +656,8 @@ class WindowList: self._active_group_idx = self._most_recent_main_group_idx() else: self.make_previous_group_active(notify=False) - elif self.active_group_idx >= len(self.groups): + elif self.active_group_idx > i: + # The active group shifted down by one self._active_group_idx -= 1 if (dock := self.active_dock) is not None and (owner := self.dock_owner(dock)) is not None: # The active normal group must remain the owner of the active dock diff --git a/kitty_tests/layout.py b/kitty_tests/layout.py index 54d8df4af..5849d0dc9 100644 --- a/kitty_tests/layout.py +++ b/kitty_tests/layout.py @@ -186,8 +186,10 @@ class TestLayout(BaseTest): windows.set_active_group_idx(0) to_remove = windows.active_window windows.set_active_group_idx(3) + prev_window = windows.active_window windows.remove_window(to_remove) - self.ae(windows.active_group_idx, 3) + self.ae(windows.active_group_idx, 2) + self.ae(windows.active_window, prev_window) check_visible() expect_ids(2, 3, 5, 6)