refactor: improve AutoCompleteBox behavior and formatting

This commit is contained in:
Dong Bin
2025-06-07 18:29:04 +08:00
parent 7805e04af4
commit 182eb28da5

View File

@@ -1,14 +1,11 @@
using System.Diagnostics;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Input;
using Avalonia.Interactivity;
using Irihi.Avalonia.Shared.Contracts;
namespace Ursa.Controls;
public class AutoCompleteBox: Avalonia.Controls.AutoCompleteBox, IClearControl
public class AutoCompleteBox : Avalonia.Controls.AutoCompleteBox, IClearControl
{
static AutoCompleteBox()
{
@@ -19,24 +16,24 @@ public class AutoCompleteBox: Avalonia.Controls.AutoCompleteBox, IClearControl
{
AddHandler(PointerPressedEvent, OnBoxPointerPressed, RoutingStrategies.Tunnel);
}
private void OnBoxPointerPressed(object? sender, PointerPressedEventArgs e)
{
if (Equals(sender, this) && e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
SetCurrentValue(IsDropDownOpenProperty, true);
}
}
protected override void OnGotFocus(GotFocusEventArgs e)
{
base.OnGotFocus(e);
if (IsDropDownOpen) return;
SetCurrentValue(IsDropDownOpenProperty, true);
}
public void Clear()
{
SetCurrentValue(SelectedItemProperty, null);
}
private void OnBoxPointerPressed(object? sender, PointerPressedEventArgs e)
{
if (Equals(sender, this) && e.GetCurrentPoint(this).Properties.IsLeftButtonPressed && IsDropDownOpen == false)
SetCurrentValue(IsDropDownOpenProperty, true);
}
protected override void OnGotFocus(GotFocusEventArgs e)
{
base.OnGotFocus(e);
if (e.NavigationMethod != NavigationMethod.Directional && e.NavigationMethod != NavigationMethod.Tab) return;
if (!this.GetTemplateChildren().Contains(e.Source)) return;
// If the focus is set by keyboard navigation, open the dropdown.
if (IsDropDownOpen == false) SetCurrentValue(IsDropDownOpenProperty, true);
}
}