Per character filter for TextEdits#23704
Conversation
…xtFilter` component.
TextEditTextEdits
|
I'd like to ask, is the plan to have both a "per-character filter" and a "buffer validation" filter? Because the latter could subsume the functionality of the former - if I have a mechanism powerful enough to do phone numbers or email addresses, then restricting to hex characters is just a simple case. |
Yep I'd favour using buffer validation for both, but buffer validation is a bit more complicated as it can't be implemented just by examination of each |
| &mut clipboard_text.0, | ||
| match filter { | ||
| Some(EditableTextFilter(Some(filter))) => filter.as_ref(), | ||
| _ => &|_| true, |
There was a problem hiding this comment.
I'd prefer this argument to be an Option (not a strong feeling)
There was a problem hiding this comment.
I thought about it, but it just seemed simpler this way, instead of having to unwrap.
|
Another advantage of buffer validation is that we wouldn't have to do these individual checks before |
Instead of using undo, I'd have a second buffer for the proposed change, and then However, I realized that perhaps character filtering is not redundant with buffer validation. Take the case we've been discussing with the color picker, where you paste a string like "#ffffff" and you want to strip the "#", but allow the hex digits to be pasted. Buffer validation would reject the whole thing. (Again, this is trivial with controlled widgets.) |
Objective
Add an option to reject
TextEdits based on a per character filter.Solution
EditableTextFilter. Can be used to set a per character filter for anEditableTextentity.TextEdit::insertandTextEdit::Pasteedits are ignored unless all their characters pass the filter.The filter does not apply to characters already within the
EditableText's text bufferInitially I thought it would be better to implement the filter at the widget level, then a key press that results in a rejected edit could be propagated, but the clipboard isn't available to the keyboard observer function so paste edits would still need to be filtered when the input buffer is applied.
I made a branch adding a
TextEditRejectedentity event as an alternative (that would also notify when an edit failed due to themax_characterslimit being exceeded), but left it out of this PR to keep this one focused on just the filtering.Testing
New example: