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

Transfer information between scenes #218

Closed egakupi closed 4 years ago

egakupi commented 5 years ago

Hi,

Is it possible to transfer information between two scenes? What is the best practice for it? I have some scenes and I need to raise next scene with data from previous scene.

Thanks for your time, Dmitriy

peterbrittain commented 5 years ago

Yeah - we need to do that on a regular basis in the widgets sub-package. If you look at the contact list example, you'll see that it uses a separate data model class to do that (otherwise you end up with all sorts of problems when the screen resizes).

egakupi commented 5 years ago

Thanks for the quick response. I checked example with contact list. Class for sending string is not what I wanted, but have to.

Also I was reading documentation, but I didn't find how I should to reset a scene and to draw all layouts on it after each raising this scene?

peterbrittain commented 5 years ago

The model is up to you. You just need to create your own version of the Scene class (or indeed Effects) that is aware of the model object that you want to pass in (like Frames handle the model parameter). A more detailed explanation of the logic is available here: https://asciimatics.readthedocs.io/en/stable/widgets.html#model-view-design

Not sure what you're asking in the second part... A Layout is part of a Frame, which is just one Effect in a Scene. To display all the Effects in a Scene, you just need to transition to it. This is done by raising a NextScene exception - as covered here: https://asciimatics.readthedocs.io/en/stable/widgets.html#exceptions. Under the covers asciimatics will exit() the current scene, reset() the new one and clear() the Screen (if requested by the new Scene).

egakupi commented 5 years ago

Maybe I did not clearly ask it. For example, I have 3 scenes: main-sc0-sc1. In main I raise sc0, in sc1 – sc1, after it returns the main scene. After this road if I raise sc0 in main, sc0 will be in the same condition it was before. At sc0 I check available devices and I want re-do every time. But sc0 shows devices which was find before. I need the thing like reset scene or something like that. Thanks

peterbrittain commented 5 years ago

Erm... But that's what already happens. When you move to the new Scene, asciimatics will call Scene.reset() which will call reset() on each Effect in the Scene. If you have some existing data that isn't being correctly cleaned up, it is because you need extra logic in the reset for your Scene or associated Effects.

The contact list sample code achieves this by updating the reset methods as needed. For example, look at https://github.com/peterbrittain/asciimatics/blob/master/samples/contact_list.py#L157-L160

egakupi commented 5 years ago

Thank you very much for your patience and time. Have a good day!

peterbrittain commented 5 years ago

No problems. I think the fact that you needed to ask these questions means that the docs aren't quite sufficient yet. I'll have a think about what needs adding and update them accordingly.

jcarayan commented 4 years ago

In the documentation you say:

Note that you don’t have to be this heavy-weight with the data storage; a simple class to wrap a list of dictionaries would also suffice

I really think actually showing a quick example of this would be very valuable for a lot of users.

peterbrittain commented 4 years ago

Ok. Given further questions on the topic, I agree. Got any particular demo in mind?

jcarayan42 commented 4 years ago

I am trying to make a simple example that has 2 scenes. One has a ListBox, the other has a scene that just displays the selection. I think using python's shelve module is a good simple use case.