1
0
Эх сурвалжийг харах

Rework focus so we don't return focus to the lastly enabled element

Daniil Pavliuchyk 2 жил өмнө
parent
commit
6b8b5d78f4

+ 2 - 15
src/Avalonia.Base/Input/InputElement.cs

@@ -199,7 +199,6 @@ namespace Avalonia.Input
         private bool _isFocusVisible;
         private bool _isPointerOver;
         private GestureRecognizerCollection? _gestureRecognizers;
-        private bool _restoreFocus;
 
         /// <summary>
         /// Initializes static members of the <see cref="InputElement"/> class.
@@ -444,21 +443,9 @@ namespace Avalonia.Input
                 SetAndRaise(IsEffectivelyEnabledProperty, ref _isEffectivelyEnabled, value);
                 PseudoClasses.Set(":disabled", !value);
 
-                if (!IsEffectivelyEnabled)
+                if (!IsEffectivelyEnabled && FocusManager.Instance?.Current == this)
                 {
-                    if (FocusManager.Instance?.Current == this)
-                    {
-                        _restoreFocus = true;
-                        FocusManager.Instance?.Focus(null);
-                    }
-                    else
-                    {
-                        _restoreFocus = false;
-                    }
-                }
-                else if (IsEffectivelyEnabled && _restoreFocus)
-                {
-                    FocusManager.Instance?.Focus(this);
+                    FocusManager.Instance?.Focus(null);
                 }
             }
         }

+ 29 - 0
tests/Avalonia.Base.UnitTests/Input/InputElement_Focus.cs

@@ -1,5 +1,7 @@
+using System;
 using Avalonia.Controls;
 using Avalonia.Input;
+using Avalonia.Interactivity;
 using Avalonia.UnitTests;
 using Xunit;
 
@@ -25,6 +27,33 @@ namespace Avalonia.Base.UnitTests.Input
             }
         }
 
+        [Fact]
+        public void Focus_Should_Not_Get_Restored_To_Enabled_Control()
+        {
+            using (UnitTestApplication.Start(TestServices.RealFocus))
+            {
+                var sp = new StackPanel();
+                Button target = new Button();
+                Button target1 = new Button();
+                target.Click += (s, e) => target.IsEnabled = false;
+                target1.Click += (s, e) => target.IsEnabled = true;
+                sp.Children.Add(target);
+                sp.Children.Add(target1);
+                var root = new TestRoot
+                {
+                    Child = sp
+                };
+
+                target.Focus();
+                target.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
+                Assert.False(target.IsEnabled);
+                Assert.False(target.IsFocused);
+                target1.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
+                Assert.True(target.IsEnabled);
+                Assert.False(target.IsFocused);
+            }
+        }
+
         [Fact]
         public void Focus_Should_Be_Cleared_When_Control_Is_Removed_From_VisualTree()
         {