Skip to content

Commit d272bea

Browse files
danwalmsleygrokys
authored andcommitted
Merge pull request #6466 from AvaloniaUI/fixes/6439-tab-focus-disabled
Don't focus children of disabled controls when tabbing between controls.
1 parent cb1b2b0 commit d272bea

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/Avalonia.Input/Navigation/TabNavigation.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ internal static class TabNavigation
234234
// Return the first visible element.
235235
var uiElement = e as InputElement;
236236

237-
if (uiElement is null || uiElement.IsVisible)
237+
if (uiElement is null || IsVisibleAndEnabled(uiElement))
238238
{
239239
if (e is IVisual elementAsVisual)
240240
{
@@ -245,7 +245,7 @@ internal static class TabNavigation
245245
{
246246
if (children[i] is InputElement ie)
247247
{
248-
if (ie.IsVisible)
248+
if (IsVisibleAndEnabled(ie))
249249
return ie;
250250
else
251251
{
@@ -270,7 +270,7 @@ internal static class TabNavigation
270270
// Return the last visible element.
271271
var uiElement = e as InputElement;
272272

273-
if (uiElement == null || uiElement.IsVisible)
273+
if (uiElement == null || IsVisibleAndEnabled(uiElement))
274274
{
275275
var elementAsVisual = e as IVisual;
276276

@@ -283,7 +283,7 @@ internal static class TabNavigation
283283
{
284284
if (children[i] is InputElement ie)
285285
{
286-
if (ie.IsVisible)
286+
if (IsVisibleAndEnabled(ie))
287287
return ie;
288288
else
289289
{
@@ -600,7 +600,7 @@ internal static class TabNavigation
600600
var vchild = children[i];
601601
if (vchild == elementAsVisual)
602602
break;
603-
if (vchild.IsVisible == true && vchild is IInputElement ie)
603+
if (vchild is IInputElement ie && IsVisibleAndEnabled(ie))
604604
prev = ie;
605605
}
606606
return prev;
@@ -668,5 +668,6 @@ private static bool IsTabStop(IInputElement e)
668668
}
669669

670670
private static bool IsTabStopOrGroup(IInputElement e) => IsTabStop(e) || IsGroup(e);
671+
private static bool IsVisibleAndEnabled(IInputElement e) => e.IsVisible && e.IsEnabled;
671672
}
672673
}

tests/Avalonia.Input.UnitTests/KeyboardNavigationTests_Tab.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,5 +1225,32 @@ public void Respects_TabIndex_Moving_Backwards()
12251225
"Button2", "Button3", "Button5", "Button1", "Button6", "Button4"
12261226
}, result);
12271227
}
1228+
1229+
[Fact]
1230+
public void Cannot_Focus_Child_Of_Disabled_Control()
1231+
{
1232+
Button start;
1233+
Button expected;
1234+
1235+
var top = new StackPanel
1236+
{
1237+
[KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
1238+
Children =
1239+
{
1240+
(start = new Button { Name = "Button1" }),
1241+
new Border
1242+
{
1243+
IsEnabled = false,
1244+
Child = new Button { Name = "Button2" },
1245+
},
1246+
(expected = new Button { Name = "Button3" }),
1247+
}
1248+
};
1249+
1250+
var current = (IInputElement)start;
1251+
var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
1252+
1253+
Assert.Same(expected, result);
1254+
}
12281255
}
12291256
}

0 commit comments

Comments
 (0)