bczsalba / pytermgui

Python TUI framework with mouse support, modular widget system, customizable and rapid terminal markup language and more!
https://ptg.bczsalba.com
MIT License
2.21k stars 54 forks source link

[BUG] Focus next doesn't work #54

Closed jonnieey closed 1 year ago

jonnieey commented 2 years ago

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
bczsalba commented 2 years ago

Should be fixed. Thank you for the report!

jonnieey commented 2 years ago

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.

bczsalba commented 2 years ago

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.

jonnieey commented 2 years ago

Sure, thanks.

bczsalba commented 1 year ago

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