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

Improve NumericUpDown and AutoCompleteBox focusability #13376

Merged
merged 4 commits into from
Feb 3, 2024

Conversation

mgnslndh
Copy link
Contributor

@mgnslndh mgnslndh commented Oct 24, 2023

What does the pull request do?

Makes the NumericUpDown control focusable. Focus is delegated its child TextBox.

What is the current behavior?

NumericUpDown is not focusable.

What is the updated/expected behavior with this PR?

Makes it possible to focus the NumericUpDown control and when that happens focus is delegated to the containing TextBox control.

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

  • Override Focusable property default value to true
  • Focus the TextBox when NumericTextBox gets focus.

Checklist

I tried adding unit tests for this behavior but failed. It boils down to the visual tree not being setup correctly during test so the calls to TextBox.Focus() will fail under test, but works in a normal app scenario.

If it is possible to make a working test then please advise me how to proceed. The RealFocus test service is not enough for this test case.

Breaking changes

Don't think so.

Fixed issues

Fixes #8069
Fixes #13339
Fixes #11152
Fixes #5016
Fixes #12921

@maxkatz6
Copy link
Member

Does it work with Shift+Tab or any other way of back-focus to previous focusable element?

@mgnslndh
Copy link
Contributor Author

@maxkatz6

Does it work with Shift+Tab or any other way of back-focus to previous focusable element?

No, the PR does not address that scenario. I'll look into it.

@IanRawley
Copy link
Contributor

I've used the ideas in this as a base to manage something for a basic control I'm working on, and the back navigation aspect can be handled to an extent with

if (e.Source == this && e.KeyModifiers.HasFlag(KeyModifiers.Shift))
{
    KeyboardNavigationHandler.GetNext(this, NavigationDirection.Previous)?.Focus(NavigationMethod.Tab);
}

in the OnGotFocus override.

@IanRawley
Copy link
Contributor

I've refactored my version to account for a few oversights, and sometimes overthinking things, and come to this version.

protected override void OnGotFocus(GotFocusEventArgs e)
{
    base.OnGotFocus(e);
    if (e.Source == this)
    {
        if (e.KeyModifiers.HasFlag(KeyModifiers.Shift))
            KeyboardNavigationHandler.GetNext(this, NavigationDirection.Previous)?.Focus(NavigationMethod.Tab, e.KeyModifiers);
        else
            TextBox?.Focus(NavigationMethod.Tab);

        e.Handled = true;
    }
}

I'm not sure what you'd need to change to account for the ButtonSpinner that wraps the TextBox in the Simple Theme. I suspect nothing actually, but haven't looked into it.

@maxkatz6 maxkatz6 added the backport-candidate-11.0.x Consider this PR for backporting to 11.0 branch label Jan 12, 2024
@maxkatz6 maxkatz6 changed the title Make NumericUpDown focusable Improve NumericUpDown and AutoCompleteBox focusability Feb 3, 2024
@maxkatz6 maxkatz6 merged commit c2798ce into AvaloniaUI:master Feb 3, 2024
2 of 6 checks passed
maxkatz6 added a commit that referenced this pull request Feb 8, 2024
* Delegate focus to TextBox

* Port FocusChanged from AutoCompleteBox to NumericUpDown

* Improve focus with NumericUpDown and ButtonSpinner bindings

---------

Co-authored-by: Max Katz <[email protected]>
@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 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants