Browse Source

Merge pull request #6466 from AvaloniaUI/fixes/6439-tab-focus-disabled

Don't focus children of disabled controls when tabbing between controls.
release/0.10.8
Dan Walmsley 5 years ago
committed by Steven Kirk
parent
commit
d272bea541
  1. 11
      src/Avalonia.Input/Navigation/TabNavigation.cs
  2. 27
      tests/Avalonia.Input.UnitTests/KeyboardNavigationTests_Tab.cs

11
src/Avalonia.Input/Navigation/TabNavigation.cs

@ -234,7 +234,7 @@ namespace Avalonia.Input.Navigation
// 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)
{
@ -245,7 +245,7 @@ namespace Avalonia.Input.Navigation
{
if (children[i] is InputElement ie)
{
if (ie.IsVisible)
if (IsVisibleAndEnabled(ie))
return ie;
else
{
@ -270,7 +270,7 @@ namespace Avalonia.Input.Navigation
// 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;
@ -283,7 +283,7 @@ namespace Avalonia.Input.Navigation
{
if (children[i] is InputElement ie)
{
if (ie.IsVisible)
if (IsVisibleAndEnabled(ie))
return ie;
else
{
@ -600,7 +600,7 @@ namespace Avalonia.Input.Navigation
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;
@ -668,5 +668,6 @@ namespace Avalonia.Input.Navigation
}
private static bool IsTabStopOrGroup(IInputElement e) => IsTabStop(e) || IsGroup(e);
private static bool IsVisibleAndEnabled(IInputElement e) => e.IsVisible && e.IsEnabled;
}
}

27
tests/Avalonia.Input.UnitTests/KeyboardNavigationTests_Tab.cs

@ -1225,5 +1225,32 @@ namespace Avalonia.Input.UnitTests
"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);
}
}
}

Loading…
Cancel
Save