-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Let Memory
store arbitrary data for custom widgets.
#255
Comments
So, I tried to implement described idea with Current observations:
So, the idea with any-map is failed, so I suggest it's better to provide the user a way to store their data using simple Rust enums, maybe also introducing helper functions/macros to get/set data from that enums, or parsing using |
I tried to sleep, but this idea came: deserialization can be deferred, and can be performed when we call get function with specialized type. |
So, I created a draft PR where you can view how the approach with Pros:
Cons:
Another approach is introduce something like this type: enum DataStorage {
None,
Bool(bool),
Int(i64),
Float(f32),
String(String),
Tuple(Box<DataStorage>, Box<DataStorage>)
} Pros:
Cons:
Now I see that |
I said:
But this is not quite true. Why user should care if some internal temporary UI data is not saved correctly between runs of different versions of their program? I think a user should use only functions But this is just an idea. And possibly this can create some obscure and hard-to-find errors for complicated interfaces. |
I believe this feature can be done in two PRs:
Default widgets should be rewritten to use this API because users may rely on the source code of these widgets. (I still want to implement this if you give approval) |
Memory
store arbitrary data for custom widgets.
The downcast error may occur when one |
Wow! I went through exactly this thought process many months ago, but I never managed to crack the nut - but it seems you did! Postponing deserialization until we know what type to deserialize to is really clever. As for errors: this storage should only be used for things that are non-critical ("the selected tab" etc), so I think silently ignoring errors is fine (*). Of course we need to be very clear documenting this. (*) it may also be about time that |
As for JSON: I use it elsewhere, but I am considering switching it out for something that supports |
Using Also, it's worth pointing out that |
I started to implement an example widget about how to use Now I need to write a bunch of comments and wait for #257 to be merged. |
I just used this for the first time in daf2e13 - I think this is a good example of something that does not belong in the user state, and so egui can help you keep track of instead. |
Is your feature request related to a problem? Please describe.
![image](https://user-images.githubusercontent.com/18114381/112620358-00b59380-8e5b-11eb-96f4-8ccd1581e46a.png)
I have this widget:
This is the editor where the result can be previewed. And this widget has two states:
View
orEdit
, this can be represented asbool
. I believe this is more convenient to store this data inegui::Memory
because this whole thing looks like some complicated widget that receives&mut String
, and using this we can have the same benefits as other widgets have (able to serialize UI memory between runs). Also, creating custom memory and drag it into every function is quite inconvenient.I looking for the code of
CollapsingHeader
, because it has some bool state too (opened or not). And found thatMemory
hard-coded all data for collapsing headers and collapsing header uses this private access. And, as I see there is no public interface to store data for a custom widget.Describe the solution you'd like
First, we can provide an interface to store just a single
bool
,i32
,String
using enums for our id.But a more scalable approach is using
HashMap
withBox<dyn Any + ...>
. Using it, we can store arbitrary types for ourid
. And default widgets can be painlessly rewritten using it.We also can require
Serialize
+Deserialize
traits for all objects that should be stored in this type-map (under feature, of course).But this type-map can possibly add visible runtime overhead comparing to current hard-coded maps.
Describe alternatives you've considered
Not allowing a user to store data in
egui::Memory
.Additional context
I want to try to implement this feature by myself and open a Pull Request. What do you think?
The text was updated successfully, but these errors were encountered: