ux: auto focus the first input element in popup widget (#49)

This commit is contained in:
leo 2024-04-02 16:23:47 +08:00
parent ddd152df59
commit 96e60da7ad
45 changed files with 401 additions and 333 deletions

View file

@ -0,0 +1,32 @@
using Avalonia;
using Avalonia.Input;
namespace SourceGit.Views
{
public class AutoFocusBehaviour : AvaloniaObject
{
public static readonly AttachedProperty<bool> IsEnabledProperty =
AvaloniaProperty.RegisterAttached<AutoFocusBehaviour, InputElement, bool>("IsEnabled", false, false);
static AutoFocusBehaviour()
{
IsEnabledProperty.Changed.AddClassHandler<InputElement>((input, e) =>
{
if (input.GetValue(IsEnabledProperty))
{
input.AttachedToVisualTree += (o, _) => (o as InputElement).Focus(NavigationMethod.Directional);
}
});
}
public static bool GetIsEnabled(AvaloniaObject elem)
{
return elem.GetValue(IsEnabledProperty);
}
public static void SetIsEnabled(AvaloniaObject elem, bool value)
{
elem.SetValue(IsEnabledProperty, value);
}
}
}