peterbrittain / asciimatics

A cross platform package to do curses-like operations, plus higher level APIs and widgets to create text UIs and ASCII art animations
Apache License 2.0
3.64k stars 238 forks source link

Are there anyway to hide/show a widget from a screen layout? #315

Closed jarodtang closed 3 years ago

jarodtang commented 3 years ago

Hi There,

I'm trying to add some dynamics to my app, such as, click a button, display a widget 1 on the layout, and click another button, hide widget 1 and display widget 2.

Are the ways to do this?

Regards, Jarod

peterbrittain commented 3 years ago

Yes, but it is cumbersome...

You can use https://asciimatics.readthedocs.io/en/stable/asciimatics.widgets.html#asciimatics.widgets.layout.Layout.clear_widgets to empty a layout, then re-add all the widgets you really want in the layout, then fix the whole Frame again when you're done.

jarodtang commented 3 years ago

Yes, it's cumbersome, :)

peterbrittain commented 3 years ago

If you're trying to replace one widget with another, the full Frame layout needs to be recalculated, so that call to fix is always going to be required.

However, it may be possible to enhance the layout a bit to provide a slicker way to remove a specific widget and insert a new one. For example, there is already a way to find a widget by name. It would be relatively easy to provide a remove_widget method that uses similar logic to remove one from the layout. You'd then need a way to insert your new widget at the desired location... Maybe an insert_widget method that takes a name of the widget that you want to insert the new one before? Again, much the same logic to find the right location by name and then just update the relevant column data (ensuring that you register the widget as per the existing add_widget code).

I think that would be a reasonable addition to the API.

peterbrittain commented 3 years ago

I'm assuming that answered the question and so am closing. If you made the change for remove/insert, I'd happily take a PR for the additions....