cocopon / tweakpane

:control_knobs: Compact GUI for fine-tuning parameters and monitoring value changes
https://tweakpane.github.io/docs/
MIT License
3.41k stars 85 forks source link

exportState/importState should not rely on ordering, but have some versioning or keyname check #615

Closed leonbloy closed 1 week ago

leonbloy commented 2 weeks ago

I have a Pane with several bindings that I continuosly save and retrieve from localstorage via exportState/importState.

If I change the order or some bindings, the import messes all up, because it (apparently) blindy trusts that the order of the saved items will match those of the current pane.

This, I guess, should be also a problem if a new item is added to a Pane (not at the end).

The exportState/importState functions should either have some versioning scheme (and refuse to import if things do not match), or save and retrieve not by order but by key,

cocopon commented 2 weeks ago

It seems like you might be referring to the preset feature in v3 or earlier: https://tweakpane.github.io/docs/v3/misc/#imex

This feature is useful if you primarily use Tweakpane for binding values, but there are many components (including 3rd party ones) that are not meant for binding. There was no way to persist the state including components other than bindings, which has been an issue.

In v4, state represents the state for all components and it should contain all the properties necessary to restore its state. For example, the state of a folder should include ordered children, expanded and title.

If you prefer the classic style, you can use a small utility function to convert it from state: https://tweakpane.github.io/docs/migration/v4/#preset

The exportState/importState functions should either have some versioning scheme

I believe that this kind of complex and niche feature should be implemented by users themselves.

leonbloy commented 2 weeks ago

Thanks for the response. But the issue still exists: the import make a total mess of the states if there has been some alteration of the structure between the export and the import. I think that that should be addressed, but I defer to you judgment, I'm a very recent user of tweakpane.

cocopon commented 2 weeks ago

I think that the order of children is part of the folder's state. The importing/exporting feature is not designed for panes with different structures. As I mentioned before, I believe that such feature (e.g. versioning) should be implemented by users themselves.

The state contains enough data to extract a preset, where the order of container children doesn't matter. you can see the reference implementation here:

cocopon commented 1 week ago

Feel free to reopen the issue if you have any updates.