Skip to content

Commit

Permalink
Merge pull request #6466 from AvaloniaUI/fixes/6439-tab-focus-disabled
Browse files Browse the repository at this point in the history
Don't focus children of disabled controls when tabbing between controls.
  • Loading branch information
Dan Walmsley authored and grokys committed Sep 29, 2021
1 parent cb1b2b0 commit d272bea
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
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);
}
}
}

0 comments on commit d272bea

Please sign in to comment.