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

[Windows] Fix select-all Entry select logic #23329

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

Foda
Copy link
Member

@Foda Foda commented Jun 27, 2024

Description of Change

Consider that you want to implement a "select all text on focus" function for an Entry field:

private void OnEntryFocused(object? sender, FocusEventArgs e)
{
    if (TextBox.Text != null)
    {
        TextBox.CursorPosition = 0;
        TextBox.SelectionLength = entry.Text.Length;
    }
}

Currently, this will work the first time the Entry is focused, but it won't work the second time.

The problem is caused by the following:

  • Entry gains focus
  • Cursor position and selection length are set, values propagate to WinUI TextBox
  • Entry is unfocused
  • Entry gains focus again
  • Cursor position and selection length are set, but the values are the same, so we don't set the values again on the WinUI TextBox
  • WinUI TextBox handles input event, moves cursor to where you click (note: the native WinUI TextBox seems to have special logic around canceling this logic if setting selection during an OnFocus event? this is where the issue comes from!)
  • WinUI control then propagates cursor position and selection length values to the Maui Entry, text is now unselected

The fix here is to just always ensure setting cursor position and selection length flows to the native WinUI TextBox control. A better solution might be adding a Select and SelectAll method to Entry, but for now this will work.

Mike Corsaro added 2 commits June 27, 2024 10:42
…ts even if the value is the same

This allows "select all text on focus" logic to work on WinUI
@Foda Foda requested a review from PureWeen June 27, 2024 22:19
@Foda Foda requested a review from a team as a code owner June 27, 2024 22:19
@Foda Foda requested a review from Eilon June 27, 2024 22:19
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.

None yet

1 participant