Skip to content

EditableText::new method to set initial text#23702

Open
ickshonpe wants to merge 6 commits intobevyengine:mainfrom
ickshonpe:editabletext_new
Open

EditableText::new method to set initial text#23702
ickshonpe wants to merge 6 commits intobevyengine:mainfrom
ickshonpe:editabletext_new

Conversation

@ickshonpe
Copy link
Copy Markdown
Contributor

@ickshonpe ickshonpe commented Apr 7, 2026

Objective

It should be simple to set an initial value for EditableText's text buffer.

Otherwise I can see users struggling with this and using some ghastly construction like a run once system in Update to set initial text.

Solution

New EditableText::new method. The value of the text buffer is set immediately and a TextEdit::TextEnd(false) edit is queued to move the cursor to the end of the text.

Testing

Added some initial text to the multiple text example:

cargo run --example multiple_text_inputs

@ickshonpe ickshonpe requested review from Zeophlite and viridia April 7, 2026 10:09
@ickshonpe ickshonpe added A-Text Rendering and layout for characters D-Trivial Nice and easy! A great choice to get started with Bevy S-Needs-Review Needs reviewer attention (from anyone!) to move forward C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Apr 7, 2026
@Zeophlite
Copy link
Copy Markdown
Contributor

Might be worth adding a comment that it sets the cursor to start of text

@ickshonpe
Copy link
Copy Markdown
Contributor Author

ickshonpe commented Apr 7, 2026

Might be worth adding a comment that it sets the cursor to start of text

Ah yeah that's a good point, I hadn't thought about the initial cursor position. I think that for new it's more natural for the cursor to be set to the end. That distinguishes it from placeholder text (when that is implemented) as well, where the cursor would be at the start position.

@ickshonpe ickshonpe changed the title Add an EditableText new method to make it easier to construct text inputs with an initial value. EditableText::new method to set initial text Apr 7, 2026
@viridia
Copy link
Copy Markdown
Contributor

viridia commented Apr 7, 2026

Another option is to initially have all the text selected. You often see this in prompt dialogs, where the text input is filled with some default string, but as soon as the user starts typing, the text is replaced.

Of course, the reason we even need any of this is because our text input is an uncontrolled widget. With controlled widgets, the buffer state is external and supplied by the user, so they can initialize it to whatever they want. For example, in the feathers color picker, the source of truth is not the text buffer, but the rgb value in the resource, which updates the buffer via change detection. And since change detection runs on initial creation, the text input gets initialized as we would want it to.

The downside of uncontrolled widgets is twofold:

  • Every update has to emit an event which contains a complete copy of the pending changes. This is trivial for checkboxes and sliders, not so much for text inputs.
  • The user has to handle this event, or the widget doesn't update.

In the documentation for headless widgets, it says that widgets are controlled, but I specifically call out an exception for text inputs, because I know that making them controlled is a problem.

@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
@ickshonpe
Copy link
Copy Markdown
Contributor Author

Yes Parley's PlainEditor is inherently stateful, so I guess we can only really work around it for the moment.

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 C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Trivial Nice and easy! A great choice to get started with Bevy S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants