From 56864cdfb7c53736536a0ecb42f10e105145c476 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 9 Mar 2019 10:42:55 +0530 Subject: [PATCH] When a window is closed, switch focus to the previously active window (if any) instead of picking the previous window in the layout Fixes #1450 --- docs/changelog.rst | 3 +++ kitty/tabs.py | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index a2a96e05c..b736b2f47 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -26,6 +26,9 @@ To update |kitty|, :doc:`follow the instructions `. - Make live resizing of OS windows smoother and show the size in cells while the resize is in progress. +- When a window is closed, switch focus to the previously active window (if + any) instead of picking the previous window in the layout (:iss:`1450`) + - icat kitten: Add support for displaying images at http(s) URLs (:iss:`1340`) - A new option :opt:`strip_trailing_spaces` to optionally remove trailing diff --git a/kitty/tabs.py b/kitty/tabs.py index e030aa5fd..5be321cbf 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -253,9 +253,33 @@ class Tab: # {{{ if self.windows: self.remove_window(self.windows[self.active_window_idx]) + def previous_active_window_idx(self, num): + try: + old_window_id = self.active_window_history[-num] + except IndexError: + return + for idx, w in enumerate(self.windows): + if w.id == old_window_id: + return idx + def remove_window(self, window): - self.active_window_idx = self.current_layout.remove_window(self.windows, window, self.active_window_idx) + idx = self.previous_active_window_idx(1) + next_window_id = None + if idx is not None: + next_window_id = self.windows[idx].id + active_window_idx = self.current_layout.remove_window(self.windows, window, self.active_window_idx) remove_window(self.os_window_id, self.id, window.id) + if next_window_id is None and active_window_idx is not None and len(self.windows) > active_window_idx: + next_window_id = self.windows[active_window_idx].id + if next_window_id is not None: + for idx, window in enumerate(self.windows): + if window.id == next_window_id: + self.active_window_idx = self.current_layout.set_active_window(self.windows, idx) + break + else: + self.active_window_idx = active_window_idx + else: + self.active_window_idx = active_window_idx self.relayout_borders() def set_active_window_idx(self, idx): @@ -277,16 +301,10 @@ class Tab: # {{{ def nth_window(self, num=0): if self.windows: if num < 0: - try: - old_window_id = self.active_window_history[num] - except IndexError: - return - for idx, w in enumerate(self.windows): - if w.id == old_window_id: - self.active_window_idx = self.current_layout.set_active_window(self.windows, idx) - break - else: + idx = self.previous_active_window_idx(-num) + if idx is None: return + self.active_window_idx = self.current_layout.set_active_window(self.windows, idx) else: self.active_window_idx = self.current_layout.nth_window(self.windows, num) self.relayout_borders()