using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Xaml.Behaviors;

namespace InABox.WPF;

public class TextBoxEnterAsTabBehavior :
    Behavior<TextBox>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.PreviewKeyDown += AssociatedObjectOnPreviewKeyDown;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.PreviewKeyDown -= AssociatedObjectOnPreviewKeyDown;
        base.OnDetaching();
    }

    private void AssociatedObjectOnPreviewKeyDown(object sender, KeyEventArgs args)
    {
        if (args.Key != Key.Enter) { return; }

        args.Handled = true;
        AssociatedObject.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    }
}