Skip to content

Per character filter for TextEdits#23704

Open
ickshonpe wants to merge 11 commits intobevyengine:mainfrom
ickshonpe:editable_text_filter
Open

Per character filter for TextEdits#23704
ickshonpe wants to merge 11 commits intobevyengine:mainfrom
ickshonpe:editable_text_filter

Conversation

@ickshonpe
Copy link
Copy Markdown
Contributor

@ickshonpe ickshonpe commented Apr 7, 2026

Objective

Add an option to reject TextEdits based on a per character filter.

Solution

  • New component: EditableTextFilter. Can be used to set a per character filter for an EditableText entity.
  • TextEdit::insert and TextEdit::Paste edits are ignored unless all their characters pass the filter.

The filter does not apply to characters already within the EditableText's text buffer

Initially 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 TextEditRejected entity event as an alternative (that would also notify when an edit failed due to the max_characters limit being exceeded), but left it out of this PR to keep this one focused on just the filtering.

Testing

New example:

cargo run --example editable_text_filter

@ickshonpe ickshonpe changed the title Per character filter for text insterted with TextEdit Per character filter for TextEdits Apr 7, 2026
@ickshonpe ickshonpe added A-Text Rendering and layout for characters A-UI Graphical user interfaces, styles, layouts, and widgets S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 7, 2026
@github-project-automation github-project-automation bot moved this to Needs SME Triage in UI Apr 7, 2026
@ickshonpe ickshonpe added D-Straightforward Simple bug fixes and API improvements, docs, test and examples C-Feature A new feature, making something new possible and removed D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels Apr 7, 2026
@ickshonpe ickshonpe requested a review from viridia April 7, 2026 14:34
@viridia
Copy link
Copy Markdown
Contributor

viridia commented Apr 7, 2026

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.

@ickshonpe
Copy link
Copy Markdown
Contributor Author

ickshonpe commented Apr 7, 2026

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 TextEdit before application. You need to apply the TextEdit and then validate, which requires some way of rolling back the changes if they fail validation. It'll be easy once we have undo implemented though, and this seems okay until then.

&mut clipboard_text.0,
match filter {
Some(EditableTextFilter(Some(filter))) => filter.as_ref(),
_ => &|_| true,
Copy link
Copy Markdown
Contributor

@Zeophlite Zeophlite Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer this argument to be an Option (not a strong feeling)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it, but it just seemed simpler this way, instead of having to unwrap.

@ickshonpe
Copy link
Copy Markdown
Contributor Author

ickshonpe commented Apr 7, 2026

Another advantage of buffer validation is that we wouldn't have to do these individual checks before TextEdits are applied, we can just have one validation function we call after application, which could handle max_characters as well.

@viridia
Copy link
Copy Markdown
Contributor

viridia commented Apr 7, 2026

Another advantage of buffer validation is that we wouldn't have to do these individual checks before TextEdits are applied, we can just have one validation function we call after application, which could handle max_characters as well.

Instead of using undo, I'd have a second buffer for the proposed change, and then std::mem::swap them when done.

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.)

@Zeophlite Zeophlite added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Text Rendering and layout for characters A-UI Graphical user interfaces, styles, layouts, and widgets C-Feature A new feature, making something new possible S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

3 participants