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