-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Windows] Fix modal page keyboard focus not shifting to newly opened modal #34212
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ea0b1d4
Fix modal page keyboard focus on Windows
jfversluis 11fadb2
Address PR review: improve focus handling and test robustness
jfversluis 3c1f931
Fix potential memory leak and duplicate Loaded subscriptions
jfversluis f0d5519
Enhance focus trapping: TabFocusNavigation + transparent modal support
jfversluis ef36bb5
Use GettingFocus event trap instead of IsEnabled/TabFocusNavigation
jfversluis 7a3f1db
Fix post-pop focus restoration and delegate storage
jfversluis ca178fe
Fix stale focus trap during modal removal + add device test
jfversluis 5640b97
Fix semi-transparent modal focus trap and post-pop interaction
jfversluis 0f5a9bf
Address review: restore TabFocusNavigation on pop, add nested modals …
jfversluis 20faf34
Cache and restore original IsHitTestVisible on modal pop
jfversluis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 22938, "Keyboard focus does not shift to a newly opened modal page", PlatformAffected.All)] | ||
jfversluis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public class Issue22938 : ContentPage | ||
| { | ||
| public Issue22938() | ||
| { | ||
| var clickCountLabel = new Label | ||
| { | ||
| Text = "0", | ||
| AutomationId = "ClickCountLabel", | ||
| FontSize = 24 | ||
| }; | ||
|
|
||
| var mainPageButton = new Button | ||
| { | ||
| Text = "Click Me", | ||
| AutomationId = "MainPageButton", | ||
| Command = new Command(() => | ||
| { | ||
| int count = int.Parse(clickCountLabel.Text); | ||
| clickCountLabel.Text = (count + 1).ToString(); | ||
| }) | ||
| }; | ||
|
|
||
| var openModalButton = new Button | ||
| { | ||
| Text = "Open Modal", | ||
| AutomationId = "OpenModalButton", | ||
| Command = new Command(async () => | ||
| { | ||
| // Use semi-transparent background to match the reproduction scenario. | ||
| // This causes the underlying page to remain in the visual tree | ||
| // (ModalNavigationManager does not call RemovePage for non-default backgrounds). | ||
| var modalPage = new ContentPage | ||
| { | ||
| BackgroundColor = Color.FromArgb("#40808080"), | ||
| Content = new VerticalStackLayout | ||
| { | ||
| Spacing = 20, | ||
| Padding = new Thickness(30), | ||
| VerticalOptions = LayoutOptions.Center, | ||
| Children = | ||
| { | ||
| new Label | ||
| { | ||
| Text = "Modal Page", | ||
| AutomationId = "ModalPageLabel", | ||
| FontSize = 24, | ||
| HorizontalOptions = LayoutOptions.Center | ||
| }, | ||
| new Entry | ||
| { | ||
| Placeholder = "Focus target on modal", | ||
| AutomationId = "ModalEntry" | ||
| }, | ||
| new Button | ||
| { | ||
| Text = "Close Modal", | ||
| AutomationId = "CloseModalButton", | ||
| Command = new Command(async () => | ||
| { | ||
| await Navigation.PopModalAsync(); | ||
| }) | ||
| } | ||
| } | ||
jfversluis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
|
|
||
| await Navigation.PushModalAsync(modalPage); | ||
| }) | ||
| }; | ||
|
|
||
| Content = new VerticalStackLayout | ||
| { | ||
| Spacing = 20, | ||
| Padding = new Thickness(30), | ||
| VerticalOptions = LayoutOptions.Center, | ||
| Children = | ||
| { | ||
| new Label | ||
| { | ||
| Text = "Main Page - Issue 22938", | ||
| FontSize = 24 | ||
| }, | ||
| mainPageButton, | ||
| clickCountLabel, | ||
| openModalButton | ||
| } | ||
| }; | ||
| } | ||
| } | ||
78 changes: 78 additions & 0 deletions
78
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue22938.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| using NUnit.Framework; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
|
|
||
| public class Issue22938 : _IssuesUITest | ||
| { | ||
| public Issue22938(TestDevice device) : base(device) | ||
| { | ||
| } | ||
|
|
||
| public override string Issue => "Keyboard focus does not shift to a newly opened modal page"; | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.Focus)] | ||
| public void ModalPageShouldReceiveKeyboardFocus() | ||
| { | ||
| App.WaitForElement("OpenModalButton"); | ||
|
|
||
| // Open the modal page | ||
| App.Tap("OpenModalButton"); | ||
|
|
||
| // Wait for modal to appear — the Entry is the first focusable element | ||
| App.WaitForElement("ModalEntry"); | ||
|
|
||
| // Press Enter — with the fix, focus is on ModalEntry (an Entry control), | ||
| // so Enter should NOT activate MainPageButton on the page beneath | ||
| App.PressEnter(); | ||
|
|
||
| // Close the modal by tapping the close button explicitly | ||
| App.Tap("CloseModalButton"); | ||
|
|
||
jfversluis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Wait for main page to reappear | ||
| App.WaitForElement("ClickCountLabel"); | ||
|
|
||
| // Verify the main page button was NOT clicked by the Enter key | ||
| var clickCount = App.WaitForElement("ClickCountLabel").GetText(); | ||
| Assert.That(clickCount, Is.EqualTo("0"), | ||
| "Enter key should not activate buttons on the page beneath a modal"); | ||
| } | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.Focus)] | ||
| public void TabShouldNotCycleToBehindModal() | ||
| { | ||
| App.WaitForElement("OpenModalButton"); | ||
|
|
||
| // Open the modal page (uses semi-transparent background so underlying page stays in tree) | ||
| App.Tap("OpenModalButton"); | ||
|
|
||
| // Wait for modal to appear | ||
| App.WaitForElement("ModalEntry"); | ||
|
|
||
| // Tab through many times to attempt cycling past the modal into the underlying page. | ||
| // The modal has 2 focusable elements (Entry + CloseModalButton), so 10 tabs should | ||
| // cycle through them multiple times. If focus escapes to the underlying page, | ||
| // one of the tabs could land on MainPageButton. | ||
| for (int i = 0; i < 10; i++) | ||
| { | ||
| App.SendTabKey(); | ||
| } | ||
|
|
||
| // Now press Enter. If focus leaked to MainPageButton, this would click it. | ||
| App.PressEnter(); | ||
|
|
||
| // Close the modal | ||
| App.Tap("CloseModalButton"); | ||
|
|
||
| // Wait for main page to reappear | ||
| App.WaitForElement("ClickCountLabel"); | ||
|
|
||
| // Verify the main page button was NOT clicked during Tab cycling | ||
| var clickCount = App.WaitForElement("ClickCountLabel").GetText(); | ||
| Assert.That(clickCount, Is.EqualTo("0"), | ||
| "Tab key should not cycle focus to buttons on the page beneath a modal"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.