Skip to content

Adjust handle anchor offset for collapsed text selection for Android #169715

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

muhammadkamel
Copy link
Contributor

@muhammadkamel muhammadkamel commented May 29, 2025

The text selection anchor/handle in InputField was slightly misaligned, shifting slightly to the left. This PR fixes the handle positioning to ensure proper alignment with the text cursor.

Before:
baloon_before

After fix:
baloon_after_fixed

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@github-actions github-actions bot added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels May 29, 2025
@muhammadkamel
Copy link
Contributor Author

muhammadkamel commented May 30, 2025

@justinmc
@Piinks
@chunhtai

Appreciate your support here

@muhammadkamel
Copy link
Contributor Author

@kenzieschmoll
Appreciate your support to check this :)

@Piinks
Copy link
Contributor

Piinks commented Jun 3, 2025

@muhammadkamel code reviewers are assigned during weekly triage, please give us time to do so instead of pinging maintainers. They may not have any knowledge of this part of the codebase.
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#who

@muhammadkamel
Copy link
Contributor Author

@muhammadkamel code reviewers are assigned during weekly triage, please give us time to do so instead of pinging maintainers. They may not have any knowledge of this part of the codebase. https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#who

Hi @Piinks

Got it!
Thanks for your support!

@dkwingsmt dkwingsmt requested a review from justinmc June 4, 2025 18:33
Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

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

Thank you for fixing this! A one line fix but I think it makes it look a lot better.

Can you add a test? Probably something in text_selection_test.dart that shows the handle, gets the handle's Rect and the cursor's Rect, and then verifies the position of the handle in relation to the cursor.

I took a screenshot of my Pixel 9 Android 16 running the Google Keep app and zoomed in to confirm that the handle position matches what is shown in this PR 👍 .

@@ -112,7 +112,7 @@ class MaterialTextSelectionControls extends TextSelectionControls {
@override
Offset getHandleAnchor(TextSelectionHandleType type, double textLineHeight) {
return switch (type) {
TextSelectionHandleType.collapsed => const Offset(_kHandleSize / 2, -4),
TextSelectionHandleType.collapsed => const Offset(_kHandleSize / 2.18, -4.5),
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a comment explaining how you came up with these values? Something like:

// These values were eyeballed to match a physical Pixel 9 Running Android 16.

@justinmc
Copy link
Contributor

justinmc commented Jun 4, 2025

Also, are you aware of an issue for this?

Updated the calculation of the anchor point for TextSelectionHandleType.collapsed to properly account for the cursor width.

Changed from:
Offset(_kHandleSize / 2, -4.6)
to:
Offset((_kHandleSize - cursorWidth) / 2, -4.6)

This ensures that the anchor aligns with the actual visual center of the cursor.
This change is necessary because the total handle size is 48x48 pixels, which is larger than the actual cursor width (2px), so centering must be adjusted accordingly.
@muhammadkamel
Copy link
Contributor Author

Hi @justinmc

I have updated the calculation of the anchor point for TextSelectionHandleType.collapsed to properly account for the cursor width.

Changed from:
Offset(_kHandleSize / 2, -4.6)
to:
Offset((_kHandleSize - cursorWidth) / 2, -4.6)

This ensures that the anchor aligns with the actual visual center of the cursor.
This change is necessary because the total handle size is 48x48 pixels, which is larger than the actual cursor width (2px), so centering must be adjusted accordingly.

@muhammadkamel
Copy link
Contributor Author

@justinmc

Also, this is a new screenshot after applying this calculation:
fix_ancher

@muhammadkamel
Copy link
Contributor Author

muhammadkamel commented Jun 10, 2025

@justinmc
@Piinks
Please check the new updates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: text input Entering text in a text field or keyboard related problems f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants