-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Allow TextEdit to represent and validate generic types #7621
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
base: main
Are you sure you want to change the base?
Conversation
Any type implementing the correct trait can be displayed and validated by TextEdit.
Preview is being built... Preview will be available at https://egui-pr-preview.github.io/pr/7621-textedit View snapshot changes at kitdiff |
This is to easily allow people to preview the change. It's not intended to be permanent (though this is up for discussion).
Without criticizing the approach itself, I would like to note that in order to solve the original problem with |
If you mean in regards to current system, to implement that I could change the E.G. |
I mean, the typed characters usually appended to the text input, which creates difficulties for the |
This allows proper parsing of the char datatype, as you can identify which char is new and which is old.
I've updated the |
Demonstrate custom parsing rules with a newtype
Regarding the todo of Changing it to a newtype will prevent the current implementations from being applied to all instances of Leaving it as a trait that is implemented by |
Whilst working on "Allowing Invalid States", I came encountered the following issue and its corresponding PR:
As suggested by willbicks in his response to the issue, combining |
This makes it significantly easier for users to edit values.
The value is updated whilst the user is editing the TextEdit if the TextEdit contains a valid value.
This prevents breaking changes.
This is provided as a named argument to clarify the purpose of the parameter. Consequently, is_mutable has to change to a manual implementation.
This includes references to the types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this approach and it would solve issues like i had previously. And I like the example since I have some newtypes that wrap a string and enforce a specific format, this would be covered.
A mutable borrow is used instead.
Allow any type implementing the required trait (
TextType
) to be displayed and validated by TextEdit.This is still a work in progress.
I am opening this as a draft to get feedback on:
Closes:
TextBuffer
forchar
#7564Example
I have added an example at
./examples/text_validation/
. This gives a brief showcase towards what the functionality and API of this more genericTextEdit
might look like.Implementation Details
TextEdit
has been changed to take a generic parameter that is bounded byTextType
.TextType
requires two functions, one to convert the value into a string, the other to convert the value from a string. I could not use the existing ToString and FromString becauseFromString
is not required to parse the output fromToString
.Users can define custom validation rules by implementing
TextType
on new types.Allowing Invalid States (Implemented)
Currently the
TextEdit
can only exist in valid states which is not desirable. A simple example of this is thechar
data type. The only way for a user to edit it currently is for the user to write the new char elsewhere, and then paste it over the existing character. This occurs because achar
can only contain one character, so if the user attempts to add their new desired character theTextEdit
becomes invalid and the new character is removed, since there was more than one character. The same sequence occurs if the user attempts to delete the character to add a new one.My idea to resolve this problem is to allow invalid states while a
TextEdit
is focused, which will be implemented by storing the string representation of theTextEdit
value using TextEditState (this will require adding a new attribute toTextEditState
).While Focused: any valid changes will be written to both the string and the represented value.
While Focused: any invalid changes will be written only to the string.
If the
TextEdit
looses focus while in an invalid state, a valid string representation will be set from the represented value.This implementation will require all
TextEdit
s that are mutable to use a persistent Id to store the string representation between frames (certain datatypes could be made exempt, See String Optimisations).String Optimisations
I have not conducted research into which types will be used most often, but I am going to guess that the most common datatype will be
&mut String
(and other equivalents). Without any optimisation for the raw string datatypes, theTextEdit
will be storing two copies, one as the display string and one as the represented value. This is wasteful.The idea that I have to combat this issue (if it is needed) is to skip using the display string for unvalidated string data types. This can be accomplished in multiple ways. The two that come to mind are:
impl TextEdit<String>
) to have string basedTextEdit
s not execute any display string code.TextType
that specifies whether a given datatype should use a display string .TODO
Clean upTextBuffer
implementations to apply directly toString
type.TextType
attribute inTextEdit
should use generics or dynamic dispatch.TextType
.TextType
as both mutable and immutable for core datatypes, such as strings and integers.All todo goals are ones that I can achieve without external help, I just want to know whether any of this is desired before I dedicate more time.