From 182eb28da5548b017db6033c405a20df80c40e45 Mon Sep 17 00:00:00 2001 From: Dong Bin Date: Sat, 7 Jun 2025 18:29:04 +0800 Subject: [PATCH 1/2] refactor: improve AutoCompleteBox behavior and formatting --- .../AutoCompleteBox/AutoCompleteBox.cs | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/Ursa/Controls/AutoCompleteBox/AutoCompleteBox.cs b/src/Ursa/Controls/AutoCompleteBox/AutoCompleteBox.cs index 423c840..06c8da6 100644 --- a/src/Ursa/Controls/AutoCompleteBox/AutoCompleteBox.cs +++ b/src/Ursa/Controls/AutoCompleteBox/AutoCompleteBox.cs @@ -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); + } } \ No newline at end of file From b6dfaa1b5603ef46b5230b7c3fb14551d0784982 Mon Sep 17 00:00:00 2001 From: Dong Bin Date: Sat, 7 Jun 2025 18:36:40 +0800 Subject: [PATCH 2/2] test: fix test failure. --- src/Ursa/Controls/AutoCompleteBox/AutoCompleteBox.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Ursa/Controls/AutoCompleteBox/AutoCompleteBox.cs b/src/Ursa/Controls/AutoCompleteBox/AutoCompleteBox.cs index 06c8da6..17e7d13 100644 --- a/src/Ursa/Controls/AutoCompleteBox/AutoCompleteBox.cs +++ b/src/Ursa/Controls/AutoCompleteBox/AutoCompleteBox.cs @@ -31,7 +31,8 @@ public class AutoCompleteBox : Avalonia.Controls.AutoCompleteBox, IClearControl protected override void OnGotFocus(GotFocusEventArgs e) { base.OnGotFocus(e); - if (e.NavigationMethod != NavigationMethod.Directional && e.NavigationMethod != NavigationMethod.Tab) return; + // If the focus is set by pointer navigation, it is handled by PointerPressed, do not open the dropdown. + if (e.NavigationMethod == NavigationMethod.Pointer) 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);