pytermgui
pytermgui copied to clipboard
[BUG] Focus next doesn't work
focus_next method doesn't work Cycles on the first and last windows.
To Reproduce Steps to reproduce the behavior:
- Create layout with 3 windows
- Bind key
to manager to self.focus_next() - run program
- press tab
Should cycle through all windows
System information
PyTermGUI version 6.0.0
System details:
Python version: 3.10.4
$TERM: st-256color
$COLORTERM: None
Color support: ColorSystem.STANDARD
OS Platform: Linux-5.17.5-arch1-1-x86_64-with-glibc2.35
Possible solution
def focus_next(self):
if self.focused is None:
self.focus(self._windows[0])
return self.focused
self._windows = self._windows[1:] + [self._windows[0]]
self.focus(self._windows[0])
return self.focused
Should be fixed. Thank you for the report!
Just tested, doesn't work as expected. If you only have two windows it doesn't work. Also if you use many windows eg. sand box/layouts.py it doesn't work as it should.
Yup, just remembered figuring this one out ages ago. Basically the problem is this:
# Windows shown as indices for simplicity's sake
self._windows = [0, 1, 2, 3]
self.focus_next()
self._windows = [1, 0, 2, 3]
self.focus_next()
self._windows = [0, 1, 2, 3]
Not sure how well that explains it. The point is, when a window is focused it is pushed a slot down the "stack", so when the next window is focused over and over it ends up alternating between 2 windows. The way this was resolved back in the day was by tracking the currently focused window's index as an instance attribute, and referring to it in this call. I really didn't like that solution, which is why it no longer exists.
I'll try to come up with something that fixes it.
Sure, thanks.
Sorry about the wait, not sure how I forgot about this one.