Skip to content

Keep track of selected TabItem for focus#8300

Merged
anjaligupta-dev merged 1 commit into
dotnet:mainfrom
miloush:TabControlKeepFocus
Jun 24, 2024
Merged

Keep track of selected TabItem for focus#8300
anjaligupta-dev merged 1 commit into
dotnet:mainfrom
miloush:TabControlKeepFocus

Conversation

@miloush

@miloush miloush commented Oct 13, 2023

Copy link
Copy Markdown
Contributor

Fixes #8293

Description

When a TabItem is selected in TabControl, it sets focus to its content (unless the TabItem itself is focused). If the content gets succesfully focused, then the TabItem supresses receiving focus to not steal it from the content back.

When the content is a Button in its own focus scope (such as in a ToolBar), the Button will try to switch back to the main focus scope after pressed (on losing mouse capture).

Finally, when a TabItem is focused, the TabControl will switch to that tab.

As a result, if a TabItem contains ToolBar with a Button, that Button will try to re-focus the previously focused element. However, if the TabItem suppressed receiving focus, then the previously selected tab will still be remembered as the last focused element, and re-focusing it will select it and thus unexpectedly change the displayed tab.

Customer Impact

Customers not taking this fix will experience undesirable UI behaviour with buttons in their own focus scope inside a TabItem. By default that is only the case for buttons inside ToolBar, although developers can change that.

Workarounds of the original issue exist, as described in #8293 (comment)

Regression

No. Well technically the Switch.System.Windows.Controls.TabControl.SelectionPropertiesCanLagBehindSelectionChangedEvent compatibility switch introduced this issue in .NET 4.7.1. Before that, the TabItem would not opt out of receiving focus, so this issue did not happen, but there were even worse problems.

Testing

Compiled and tested with the repro app in #8293 on .NET 8.0.100-preview.7.23376.3. Verified the issue repros without the fix and does not repro after the fix.

Risk

This is potentially a higher impact change (focusing behaviour changes) and there is a lot of corner cases that might be difficult to pick up. To minimize the risk as much as possible, the new behavior is executed in a very specific case only:

  1. Selecting the tab item did not change the focused element.
  2. The focused element is another tab item of the same control. (in that case the code does not run, so issue does not exist)
  3. The tab item succeeded focusing its content and suppressed receiving focus.
  4. The newly focused element is in a different focus scope.
Microsoft Reviewers: Open in CodeFlow

@miloush miloush requested a review from a team as a code owner October 13, 2023 16:18
@ghost ghost assigned miloush Oct 13, 2023
@ghost ghost added the PR metadata: Label to tag PRs, to facilitate with triage label Oct 13, 2023
@ghost ghost requested review from dipeshmsft and singhashish-wpf October 13, 2023 16:18
@ghost ghost added the Community Contribution A label for all community Contributions label Oct 13, 2023
@miloush miloush force-pushed the TabControlKeepFocus branch from ff1bd78 to 3abaf72 Compare October 13, 2023 17:45
@miloush miloush marked this pull request as draft October 13, 2023 20:21
@miloush miloush force-pushed the TabControlKeepFocus branch from 3abaf72 to 2bf59ad Compare October 13, 2023 22:40
@miloush miloush marked this pull request as ready for review October 13, 2023 23:07

@anjaligupta-dev anjaligupta-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM, thanks for your contribution.

@anjaligupta-dev anjaligupta-dev merged commit baffbb6 into dotnet:main Jun 24, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TabControl selection changes triggers with no reason

3 participants