Skip to content

Don't focus children of disabled controls when tabbing between controls. #6466

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 2 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Avalonia.Input/Navigation/TabNavigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ internal static class TabNavigation
// Return the first visible element.
var uiElement = e as InputElement;

if (uiElement is null || uiElement.IsVisible)
if (uiElement is null || IsVisibleAndEnabled(uiElement))
{
if (e is IVisual elementAsVisual)
{
Expand All @@ -245,7 +245,7 @@ internal static class TabNavigation
{
if (children[i] is InputElement ie)
{
if (ie.IsVisible)
if (IsVisibleAndEnabled(ie))
return ie;
else
{
Expand All @@ -270,7 +270,7 @@ internal static class TabNavigation
// Return the last visible element.
var uiElement = e as InputElement;

if (uiElement == null || uiElement.IsVisible)
if (uiElement == null || IsVisibleAndEnabled(uiElement))
{
var elementAsVisual = e as IVisual;

Expand All @@ -283,7 +283,7 @@ internal static class TabNavigation
{
if (children[i] is InputElement ie)
{
if (ie.IsVisible)
if (IsVisibleAndEnabled(ie))
return ie;
else
{
Expand Down Expand Up @@ -600,7 +600,7 @@ internal static class TabNavigation
var vchild = children[i];
if (vchild == elementAsVisual)
break;
if (vchild.IsVisible == true && vchild is IInputElement ie)
if (vchild is IInputElement ie && IsVisibleAndEnabled(ie))
prev = ie;
}
return prev;
Expand Down Expand Up @@ -668,5 +668,6 @@ private static bool IsTabStop(IInputElement e)
}

private static bool IsTabStopOrGroup(IInputElement e) => IsTabStop(e) || IsGroup(e);
private static bool IsVisibleAndEnabled(IInputElement e) => e.IsVisible && e.IsEnabled;
}
}
27 changes: 27 additions & 0 deletions tests/Avalonia.Input.UnitTests/KeyboardNavigationTests_Tab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,5 +1225,32 @@ public void Respects_TabIndex_Moving_Backwards()
"Button2", "Button3", "Button5", "Button1", "Button6", "Button4"
}, result);
}

[Fact]
public void Cannot_Focus_Child_Of_Disabled_Control()
{
Button start;
Button expected;

var top = new StackPanel
{
[KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
Children =
{
(start = new Button { Name = "Button1" }),
new Border
{
IsEnabled = false,
Child = new Button { Name = "Button2" },
},
(expected = new Button { Name = "Button3" }),
}
};

var current = (IInputElement)start;
var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);

Assert.Same(expected, result);
}
}
}