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

GetCharacterIndexFromPoint does not work as intended if HorizontalContentAlignment is set to Center #7651

Closed
rampaa opened this issue Mar 21, 2023 · 8 comments · Fixed by #7730
Assignees
Labels
🚧 work in progress Investigate Requires further investigation by the WPF team.

Comments

@rampaa
Copy link

rampaa commented Mar 21, 2023

Description

If the HorizontalContentAlignment of a TextBox is set to "Center" (or to "Right"), GetCharacterIndexFromPoint does not return the correct character index corresponding to a given point and instead returns -1.

Reproduction Steps

See the following demo:
GetCharacterIndexFromPointBugDemo.zip

Expected behavior

GetCharacterIndexFromPoint should return the correct character index from a given point even if the HorizontalContentAlignment is set to Center.

Actual behavior

GetCharacterIndexFromPoint returns -1 if HorizontalContentAlignment is set to Center/Right

Regression?

No response

Known Workarounds

No response

Impact

No response

Configuration

.NET 6/7
Windows 10 x64

Other information

No response

@huiliu-code
Copy link

Hi,@rampaa. I encountered errors when unzipping your zip file. Could you provide a new zip file that can reproduce the problem?

@rampaa
Copy link
Author

rampaa commented Mar 22, 2023

rampaa added a commit to rampaa/JL that referenced this issue Mar 23, 2023
The workaround itself creates some other problems but it's better than not being able to lookup stuff when the text is centered
@Kuldeep-MS Kuldeep-MS added the Investigate Requires further investigation by the WPF team. label Mar 24, 2023
@Kuldeep-MS Kuldeep-MS self-assigned this Mar 24, 2023
@miloush

This comment was marked as outdated.

@miloush
Copy link
Contributor

miloush commented Mar 28, 2023

OK found it, the important part is that GetCharacterIndexFromPoint is called with snapToText: false.

In that case TextBoxView.GetLineIndexFromPoint cannot find the line:

// Returns the index of a line containing point, or -1 if no such
// line exists. If snapToText is true, the closest match is returned.
//
// Point must be in document space.
private int GetLineIndexFromPoint(Point point, bool snapToText)

and the whole character finding is cancelled.

So a workaround is to use snapToText: true.

I think the fix should be in GetLineIndexFromPoint to take into account line alignment.

@dipeshmsft
Copy link
Member

@rampaa @miloush , closing this issue as the fix has been merged in the main branch

@rampaa
Copy link
Author

rampaa commented Jun 9, 2023

@dipeshmsft will the fix be backported to .NET 6?

@pchaurasia14
Copy link
Member

@rampaa - We'll need to discuss with shiproom if this can be backported to .NET 6/7.

@rampaa
Copy link
Author

rampaa commented Jun 19, 2023

@pchaurasia14 thanks for your reply! I've developed an application that makes extensive use of GetCharacterIndexFromPoint so this bug is a real headache for me and for my user base. Would really appreciate a .NET 6 backport.

@ghost ghost locked as resolved and limited conversation to collaborators Jul 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🚧 work in progress Investigate Requires further investigation by the WPF team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants