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

Inconsistent centering [BUG] #26

Closed samhaaf closed 2 years ago

samhaaf commented 2 years ago

Hello! Twice in one day. I would make a merge request but there's a choice to make, so I defer your preference and backward compatibility.

Describe the bug Currently, a window is not centered by default but it centers on on terminal.RESIZE. I'm proposing that either windows are centered on manager.add() or not centered on terminal.RESIZE

To Reproduce 1:

with ptg.WindowManager() as manager:
    wm.add(ptg.Window("Hello"))
    wm.run()

2: resize terminal

Possible cause

Containers automatically center on terminal.RESIZE.

pytermgui/widgets/layouts.py:

77        terminal.subscribe(terminal.RESIZE, lambda *_: self.center(self._centered_axis))

Possible solution 1st possibility: disable line 77 above to remove the automatic centering.

2nd possibility: Center new windows by default.

/pytermgui/window_manager.py

576        window.center()

This might be self._centered but I prefer option 2.

bczsalba commented 2 years ago

Thank you for calling me out on this! It has bothered me forever, just never enough to change it. I think I like the second option better, though when a window is set is_static it should not be moved. Since the centering itself takes place within Container, that might require making an override function within Window as well.

bczsalba commented 2 years ago

Whoops, I'm fairly certain I accidentally managed to fix this some time ago, specifically in commit 7143affee. The route I ended up taking was removing Container resize handling.

I considered the second option, but realized that in many cases you don't want to center a window, and turning a window centered from non-centered is simpler (one call to Window.center()), while the opposite is a tiny bit more difficult. When working with only a handful of windows manually calling center is manageable in my experience, and when making a full UI application (like I am doing with Pagoda), you probably already have WindowManager subclassed, so adding a definition of the add method that also centers the given window is not much overhead.

Thank you for the issue!