pytermgui icon indicating copy to clipboard operation
pytermgui copied to clipboard

[BUG] Focus next doesn't work

Open jonnieey opened this issue 3 years ago • 4 comments

focus_next method doesn't work Cycles on the first and last windows.

To Reproduce Steps to reproduce the behavior:

  1. Create layout with 3 windows
  2. Bind key to manager to self.focus_next()
  3. run program
  4. 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

jonnieey avatar May 14 '22 02:05 jonnieey

Should be fixed. Thank you for the report!

bczsalba avatar May 14 '22 10:05 bczsalba

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.

jonnieey avatar May 14 '22 10:05 jonnieey

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.

bczsalba avatar May 14 '22 10:05 bczsalba

Sure, thanks.

jonnieey avatar May 14 '22 10:05 jonnieey

Sorry about the wait, not sure how I forgot about this one.

bczsalba avatar Apr 18 '23 13:04 bczsalba