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
Conversation
You can test this PR using the following package version. |
cc @Gillibald |
src/Avalonia.Controls/TextBox.cs
Outdated
{ | ||
if (text is null) | ||
return null; | ||
|
||
if (!AcceptsReturn) | ||
{ | ||
var endOfFirstLine = text.IndexOfAny(crlf); |
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.
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;
}
}
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.
This is the way you should look for line breaks
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 will amend my PR.
8bf8316
to
64cf0af
Compare
Co-authored-by: Max Katz <maxkatz6@outlook.com>
The tests fail on Windows because 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 (Notice that in deviation to the suggestion of @Gillibald I am using |
I have adjusted the logic to only look for line-break characters |
You can test this PR using the following package version. |
) * 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
What does the pull request do?
It changes text insertion into
TextBox
withAcceptsReturn==false
so that behavior is the same as in WPF.What is the current behavior?
Upon insertion of multiline text into
TextBox
withAcceptsReturn==false
theTextBox
becomes multiline. It is currently impossible to prevent aTextBox
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
withAcceptsReturn==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 toSanitizeInputText
and extended to handle the multiline scenario.Fixed issues
Fixes #5600