2024-04-02 01:23:47 -07:00
|
|
|
|
using Avalonia;
|
2024-04-17 02:13:28 -07:00
|
|
|
|
using Avalonia.Controls;
|
2024-04-02 01:23:47 -07:00
|
|
|
|
using Avalonia.Input;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.Views
|
|
|
|
|
{
|
|
|
|
|
public class AutoFocusBehaviour : AvaloniaObject
|
|
|
|
|
{
|
|
|
|
|
public static readonly AttachedProperty<bool> IsEnabledProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
|
AvaloniaProperty.RegisterAttached<AutoFocusBehaviour, TextBox, bool>("IsEnabled");
|
2024-04-02 01:23:47 -07:00
|
|
|
|
|
|
|
|
|
static AutoFocusBehaviour()
|
|
|
|
|
{
|
2024-04-17 02:13:28 -07:00
|
|
|
|
IsEnabledProperty.Changed.AddClassHandler<TextBox>(OnIsEnabledChanged);
|
2024-04-02 01:23:47 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool GetIsEnabled(AvaloniaObject elem)
|
|
|
|
|
{
|
|
|
|
|
return elem.GetValue(IsEnabledProperty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetIsEnabled(AvaloniaObject elem, bool value)
|
|
|
|
|
{
|
|
|
|
|
elem.SetValue(IsEnabledProperty, value);
|
|
|
|
|
}
|
2024-04-17 02:13:28 -07:00
|
|
|
|
|
|
|
|
|
private static void OnIsEnabledChanged(TextBox elem, AvaloniaPropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (GetIsEnabled(elem))
|
|
|
|
|
{
|
|
|
|
|
elem.AttachedToVisualTree += (o, _) =>
|
|
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
|
if (o is TextBox box)
|
|
|
|
|
{
|
|
|
|
|
box.Focus(NavigationMethod.Directional);
|
|
|
|
|
box.CaretIndex = box.Text?.Length ?? 0;
|
|
|
|
|
}
|
2024-04-17 02:13:28 -07:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-02 01:23:47 -07:00
|
|
|
|
}
|
|
|
|
|
}
|