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

Add ability to cancel focus change #18373

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

Add ability to cancel focus change #18373

wants to merge 4 commits into from

Conversation

emmauss
Copy link
Contributor

@emmauss emmauss commented Mar 5, 2025

What does the pull request do?

Adds the following api;

public class FocusChangingEventArgs : RoutedEventArgs
    {
        /// <summary>
        /// Gets or sets the element that focus has moved to.
        /// </summary>
        public IInputElement? NewFocusedElement { get;  }

        /// <summary>
        /// Gets or sets the element that previously had focus.
        /// </summary>
        public IInputElement? OldFocusedElement { get; }

        /// <summary>
        /// Gets or sets a value indicating how the change in focus occurred.
        /// </summary>
        public NavigationMethod NavigationMethod { get; }

        /// <summary>
        /// Gets or sets any key modifiers active at the time of focus.
        /// </summary>
        public KeyModifiers KeyModifiers { get; }

        /// <summary>
        /// Gets whether focus change is canceled.
        /// </summary>
        public bool Cancelled { get; }

        /// <summary>
        /// Attempts to cancel the current focus change
        /// </summary>
        /// <returns>true if focus change was cancelled; otherwise, false</returns>
        public bool TryCancel();

        /// <summary>
        /// Attempts to redirect focus from the targeted element to the specified element.
        /// </summary>
        public bool TrySetNewFocusedElement(IInputElement? inputElement);
    }

Adds the following events and callbacks to InputElement

public static readonly RoutedEvent<FocusChangingEventArgs> GettingFocusEvent;
public static readonly RoutedEvent<FocusChangingEventArgs> LosingFocusEvent;
protected virtual void OnGettingFocus(FocusChangingEventArgs e);
protected virtual void OnLosingFocus(FocusChangingEventArgs e);

These api allow controls to cancel any focus change that affects them, as long as the toplevel allows. There are situations where cancelling is not allowed, like when the toplevel loses focus.

What is the current behavior?

Focus change can't be cancelled currently. There's no api to lock focus on a control. There are cases where you want to keep keyboard focus on a control while still being able to interact with other controls, like the case of a textbox in a chat app.

What is the updated/expected behavior with this PR?

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

Checklist

Breaking changes

Obsoletions / Deprecations

Fixed issues

@emmauss emmauss requested review from grokys and maxkatz6 March 5, 2025 12:00
@@ -159,5 +159,31 @@ public void Control_Focus_Should_Be_Set_Before_FocusedElement_Raises_PropertyCha

Assert.Equal(1, propertyChangedRaised);
}

[Fact]
public void Cancelled_Focus_Change_Should_Not_Send_Got_Focus_Event()
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a unit test with focus redirection?
Is it possible by a direct focus in this PR? Or do we need something like this https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.input.gettingfocuseventargs.trysetnewfocusedelement?view=windows-app-sdk-1.6 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

@MrJul MrJul added feature needs-api-review The PR adds new public APIs that should be reviewed. labels Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature needs-api-review The PR adds new public APIs that should be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants