Add a new `nth_window action that can be used to go to the nth window and also previously active windows, using negative numbers. Similarly, goto_tab` now accepts negative numbers to go to previous windows

Fix #1040
This commit is contained in:
Kovid Goyal
2018-10-21 10:59:06 +05:30
parent 691286f376
commit 097d11819b
5 changed files with 51 additions and 2 deletions

View File

@@ -34,6 +34,7 @@ class Tab: # {{{
self.tab_manager_ref = weakref.ref(tab_manager)
self.os_window_id = tab_manager.os_window_id
self.id = add_tab(self.os_window_id)
self.active_window_history = deque()
if not self.id:
raise Exception('No OS window with id {} found, or tab counter has wrapped'.format(self.os_window_id))
self.opts, self.args = tab_manager.opts, tab_manager.args
@@ -82,6 +83,9 @@ class Tab: # {{{
def active_window_idx(self, val):
try:
old_active_window = self.windows[self._active_window_idx]
self.active_window_history.append(old_active_window.id)
if len(self.active_window_history) > 64:
self.active_window_history.popleft()
except Exception:
old_active_window = None
self._active_window_idx = max(0, min(val, len(self.windows) - 1))
@@ -255,7 +259,19 @@ class Tab: # {{{
def nth_window(self, num=0):
if self.windows:
self.active_window_idx = self.current_layout.nth_window(self.windows, num)
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:
return
else:
self.active_window_idx = self.current_layout.nth_window(self.windows, num)
self.relayout_borders()
glfw_post_empty_event()
@@ -330,6 +346,7 @@ class TabManager: # {{{
def __init__(self, os_window_id, opts, args, startup_session):
self.os_window_id = os_window_id
self.last_active_tab_id = None
self.opts, self.args = opts, args
self.tabs = []
self.active_tab_history = deque()
@@ -426,6 +443,15 @@ class TabManager: # {{{
def goto_tab(self, tab_num):
if tab_num < len(self.tabs) and 0 <= tab_num:
self.set_active_tab_idx(tab_num)
elif tab_num < 0:
try:
old_active_tab_id = self.active_tab_history[tab_num]
except IndexError:
return
for idx, tab in enumerate(self.tabs):
if tab.id == old_active_tab_id:
self.set_active_tab_idx(idx)
break
def __iter__(self):
return iter(self.tabs)