Skip to content
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

Discard additional lines upon inserting when AcceptsReturn=false #14173

Merged
merged 5 commits into from Feb 9, 2024

Conversation

markl65536
Copy link
Sponsor Contributor

What does the pull request do?

It changes text insertion into TextBox with AcceptsReturn==false so that behavior is the same as in WPF.

What is the current behavior?

Upon insertion of multiline text into TextBox with AcceptsReturn==false the TextBox becomes multiline. It is currently impossible to prevent a TextBox becoming multiline by user input (e.g. by pasting from clipboard).

What is the updated/expected behavior with this PR?

Upon insertion of multiline text into TextBox with AcceptsReturn==false, only the first line will be inserted and any additional lines will be discarded.

How was the solution implemented (if it's not obvious)?

TextBox.RemoveInvalidCharacters was renamed to SanitizeInputText and extended to handle the multiline scenario.

Fixed issues

Fixes #5600

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.1.999-cibuild0043496-beta. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@maxkatz6 maxkatz6 added the backport-candidate-11.0.x Consider this PR for backporting to 11.0 branch label Feb 8, 2024
@maxkatz6
Copy link
Member

maxkatz6 commented Feb 8, 2024

cc @Gillibald

{
if (text is null)
return null;

if (!AcceptsReturn)
{
var endOfFirstLine = text.IndexOfAny(crlf);
Copy link
Contributor

Choose a reason for hiding this comment

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

 var linebreakEnumerator = new LineBreakEnumerator(text.AsSpan());

 while(linebreakEnumerator.MoveNext(out var linebreak))
 {
     if (linebreak.Required)
     {
         text = text.Substring(0, Math.Max(0, linebreak.PositionMeasure - 1));

         break;
     }
 }

Copy link
Contributor

Choose a reason for hiding this comment

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

This is the way you should look for line breaks

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

I will amend my PR.

Co-authored-by: Max Katz <maxkatz6@outlook.com>
@markl65536
Copy link
Sponsor Contributor Author

The tests fail on Windows because LineBreakEnumerator does not handle the line breaks as expected on Windows (I develop on Linux and tests pass fine there). Windows uses CRLF as line terminator but LineBreakEnumerator leaves a trailing CR. Referring to Unicode Documentation this may be according to spec, because a CR does not cause a line break when followed by a LF, but it is probably not what we want for TextBox.

I am at a loss here. Should I add additional code for "cleanup" of extraneous trailing CRs? This is kinda ugly but I do not see a better way when using LineBreakEnumerator. Any suggestions?

(Notice that in deviation to the suggestion of @Gillibald I am using PositionWrap instead of PositionMeasure. PositionMeasure would trim all trailing whitespace. That would solve the CR-problem but it would kill trailing space.)

@Gillibald
Copy link
Contributor

I have adjusted the logic to only look for line-break characters

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.1.999-cibuild0044651-beta. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@maxkatz6 maxkatz6 merged commit 33d95c6 into AvaloniaUI:master Feb 9, 2024
6 checks passed
maxkatz6 pushed a commit that referenced this pull request Feb 9, 2024
)

* Discard additional lines upon inserting when AcceptReturn=false

* Use LineBreakEnumerator for detecting line breaks

* Remove unused member

Co-authored-by: Max Katz <maxkatz6@outlook.com>

* Use Grapheme enumerator

---------

Co-authored-by: Markus <markus@mlet.at>
Co-authored-by: Max Katz <maxkatz6@outlook.com>
Co-authored-by: Benedikt Stebner <Gillibald@users.noreply.github.com>
#Conflicts:
#	src/Avalonia.Controls/TextBox.cs
@maxkatz6 maxkatz6 added backported-11.0.x and removed backport-candidate-11.0.x Consider this PR for backporting to 11.0 branch labels Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EditBox with AcceptReturn="false" accepts pasting text with CR(LF) characters.
4 participants