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.23k stars 54 forks source link

Non-selectable button [BUG] #24

Closed samhaaf closed 2 years ago

samhaaf commented 2 years ago

Describe the bug When you create a container (only tested on the Window widget) with only one button in it, you can't select the button.

To Reproduce

ptg.Window(ptg.Button("Reload?", onclick=lambda *_: manager.alert("Reloading!"))),

Expected behavior I would expect there to be either:

System information

$ ptg --version
PyTermGUI v2.0.0+
Python: 3.10.2
Platform: Linux-4.14.190-22554634-abT860XXU4CUL2-aarch64-with-libc
...

Possible cause Here, only selecting when selectables_length is 2 or more:

/pytermgui/widgets/layouts.py

761        if self.selectables_length > 1 and _is_nav(key):

Possible solution

Updating to >= 1 would handle this single selectable case, and returning True after the first select will trigger the style update on the button to its 'highlight' class.

/pytermgui/widgets/layouts.py

761         if self.selectables_length >= 1 and _is_nav(key):
762             handled = False
763
764             if self.selected_index is None:
765                 self.select(0)
766                 return True
bczsalba commented 2 years ago

Yup, that code is very weird. Probably had some reason behind it when it was written, but not sure what it does now. For what it's worth it is still selectable using the button, but that's not helping much.

Thank you for the really detailed issue btw, I'll get on fixing it as soon as I have time.

bczsalba commented 2 years ago

Turns out I could use your exact code to fix the issue, so thank you for that. There is no initial selection at the moment for various (mostly legacy at this point) reasons, but now that you mention it there probably should be.

Thank you for the issue!

samhaaf commented 2 years ago

Thank you for this project! It's a perfect abstraction, and I'm going to use it a lot

bczsalba commented 2 years ago

I'd love to see what you come up with! It's lovely to see people enjoying the library.