Repository: AvaloniaUI/Avalonia
File: src/Avalonia.Base/Input/InputElement.cs (C#)

Code before:
            get => GetValue(IsTabStopProperty);
            set => SetValue(IsTabStopProperty, value);
        }

        /// <inheritdoc/>
        public bool IsEffectivelyEnabled
        {
            get => _isEffectivelyEnabled;
            private set
            {
                SetAndRaise(IsEffectivelyEnabledProperty, ref _isEffectivelyEnabled, value);
                PseudoClasses.Set(":disabled", !value);

                if (!IsEffectivelyEnabled)
                {
                    if (FocusManager.Instance?.Current == this)
                    {
                        _restoreFocus = true;
                        FocusManager.Instance?.Focus(null);
                    }
                    else
                    {
                        _restoreFocus = false;
                    }
                }
                else if (IsEffectivelyEnabled && _restoreFocus)
                {
                    FocusManager.Instance?.Focus(this);
                }
            }
        }

        /// <summary>
        /// Gets or sets a value that determines the order in which elements receive focus when the
        /// user navigates through controls by pressing the Tab key.
        /// </summary>
        public int TabIndex
        {
            get => GetValue(TabIndexProperty);
            set => SetValue(TabIndexProperty, value);
        }

        public List<KeyBinding> KeyBindings { get; } = new List<KeyBinding>();

        /// <summary>
        /// Allows a derived class to override the enabled state of the control.
        /// </summary>
        /// <remarks>
        /// Derived controls may wish to disable the enabled state of the control without overwriting the
        /// user-supplied <see cref="IsEnabled"/> setting. This can be done by overriding this property
        /// to return the overridden enabled state. If the value returned from <see cref="IsEnabledCore"/>

Review comment (suggestion):
Consider the following situation:

- Two buttons, `A` and `B`
- `A` has focus
- `A` is made disabled, so focus is cleared
- `B` is focused by the user
- `A` is made enabled
- At this point `A._restoreFocus` is still true, focus jumps back to `A`

I'm not sure this is desired?

A repro:

```xml
<Window xmlns="https://github.com/avaloniaui"
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
        x:Class="Sandbox.MainWindow">
  <StackPanel>
    <Button Name="A">A</Button>
    <Button Name="B">B</Button>
  </StackPanel>
</Window>
```

```csharp
        public MainWindow()
        {
            this.InitializeComponent();
            this.AttachDevTools();

            var a = this.FindControl<Button>("A");
            var b = this.FindControl<Button>("B");

            a.Click += (s, e) => a.IsEnabled = false;
            b.Click += (s, e) => a.IsEnabled = true;
        }
```

Code after:
            set => SetValue(IsTabStopProperty, value);
        }

        /// <inheritdoc/>
        public bool IsEffectivelyEnabled
        {
            get => _isEffectivelyEnabled;
            private set
            {
                SetAndRaise(IsEffectivelyEnabledProperty, ref _isEffectivelyEnabled, value);
                PseudoClasses.Set(":disabled", !value);

                if (!IsEffectivelyEnabled && FocusManager.Instance?.Current == this)
                {
                    FocusManager.Instance?.Focus(null);
                }
            }
        }

        /// <summary>
        /// Gets or sets a value that determines the order in which elements receive focus when the
        /// user navigates through controls by pressing the Tab key.
        /// </summary>
        public int TabIndex
        {
            get => GetValue(TabIndexProperty);
            set => SetValue(TabIndexProperty, value);
        }

        public List<KeyBinding> KeyBindings { get; } = new List<KeyBinding>();

        /// <summary>
        /// Allows a derived class to override the enabled state of the control.
        /// </summary>
        /// <remarks>
        /// Derived controls may wish to disable the enabled state of the control without overwriting the
        /// user-supplied <see cref="IsEnabled"/> setting. This can be done by overriding this property
        /// to return the overridden enabled state. If the value returned from <see cref="IsEnabledCore"/>
        /// should change, then the derived control should call <see cref="UpdateIsEffectivelyEnabled()"/>.
        /// </remarks>
        protected virtual bool IsEnabledCore => IsEnabled;

        public GestureRecognizerCollection GestureRecognizers
            => _gestureRecognizers ?? (_gestureRecognizers = new GestureRecognizerCollection(this));

        /// <summary>
        /// Focuses the control.
        /// </summary>
        public void Focus()
        {
            FocusManager.Instance?.Focus(this);