-
Notifications
You must be signed in to change notification settings - Fork 569
Widget adaptor for string parsing #346
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
Conversation
It's tempting to try to derive a |
I love this API, seems especially slick for Label, but I'm not sure if it's quite functional enough to be good for numeric textboxes. The feedback loop of parsed data modifying the state of the textbox that's trying to modify the state just doesn't quite work in practice. Perhaps additional functionality here, or additional functionality in textbox, could correct for that? |
I actually built this for, and am using it with, text boxes, but on closer examination I see that you get a panic if the text box becomes empty. Maybe adapt |
One problem with this approach is that it's impossible to input values for which there isn't some edit path from the current value to the desired value where every step is syntactically valid. Instantly rejecting invalid inputs is cute for things like integers, but e.g. inputting 1e10 for a float would be impossible. I think I'll reprise this to preserve invalid values internally. |
yea, you beat me to it there. I don't think |
So I'm thinking of So I think we want some sort of 'validator' type. There should be two validation steps: during editing, we should check for the validity of a partial string, and then when editing ends we should check for the validity of the final string. there's lots of subtlety, here. is |
Wasn't able to reproduce the panic. For posterity:
I went ahead and reworked this with the This is obviously not touching at all on the questions you're raising regarding rich formatting, internationalization, and dynamic user-visible validation. I think those are important things to have, but maybe this very simple approach is still useful in the meantime/for simple cases? |
@Ralith can you use this widget in one of the examples, so it's easier to play around with/test? |
c9dd358
to
d927640
Compare
Done, along with a tweak to fix some poor behavior thereby identified. |
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.
Okay, I can see this having some value, although I suspect we will want something more flexible before too long? In particular I think the fact that a parse failure deletes the existing data might be a liability. In any case, not hard to deprecate it down the road.
BaseState, BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, PaintCtx, UpdateCtx, Widget, | ||
}; | ||
|
||
/// Converts a `Widget<String>` to a `Widget<Option<T>>`, mapping parse errors to None |
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 would explicitly document that this works with the FromStr
trait, and that T
has to be a type that implements FromStr
.
@Ralith this looks ready to go? |
Yeah; looking forward to a better solution, but in lieu of infinite free time there's no sense blocking a simple one. |
Useful for e.g. text boxes intended to contain integers.