From e6952dc352a1de9476286a1170ff12462442eee8 Mon Sep 17 00:00:00 2001 From: luthfiampas Date: Wed, 24 Feb 2021 14:46:40 +0700 Subject: [PATCH] rename AutoSelect to TextSearch --- src/Avalonia.Controls/ComboBox.cs | 54 +++++++++---------- .../ComboBoxTests.cs | 4 +- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/Avalonia.Controls/ComboBox.cs b/src/Avalonia.Controls/ComboBox.cs index 95baf8146d..fa467a83ac 100644 --- a/src/Avalonia.Controls/ComboBox.cs +++ b/src/Avalonia.Controls/ComboBox.cs @@ -78,13 +78,13 @@ namespace Avalonia.Controls ContentControl.VerticalContentAlignmentProperty.AddOwner(); /// - /// Defines the property. + /// Defines the property. /// - public static readonly StyledProperty IsAutoSelectEnabledProperty = - AvaloniaProperty.Register(nameof(IsAutoSelectEnabled)); + public static readonly StyledProperty IsTextSearchEnabledProperty = + AvaloniaProperty.Register(nameof(IsTextSearchEnabled)); - private string _autoSelectTerm = string.Empty; - private DispatcherTimer _autoSelectTimer; + private string _textSearchTerm = string.Empty; + private DispatcherTimer _textSearchTimer; private bool _isDropDownOpen; private Popup _popup; private object _selectionBoxItem; @@ -174,14 +174,12 @@ namespace Avalonia.Controls } /// - /// Gets or sets value indicating whether auto-select is currently enabled. - /// If true, the control will try to find then select matched - /// based on current keyboard inputs. + /// Gets or sets a value that specifies whether a user can jump to a value by typing. /// - public bool IsAutoSelectEnabled + public bool IsTextSearchEnabled { - get { return GetValue(IsAutoSelectEnabledProperty); } - set { SetValue(IsAutoSelectEnabledProperty, value); } + get { return GetValue(IsTextSearchEnabledProperty); } + set { SetValue(IsTextSearchEnabledProperty, value); } } /// @@ -252,16 +250,16 @@ namespace Avalonia.Controls /// protected override void OnTextInput(TextInputEventArgs e) { - if (!IsAutoSelectEnabled || e.Handled) + if (!IsTextSearchEnabled || e.Handled) return; - StopAutoSelectTimer(); + StopTextSearchTimer(); - _autoSelectTerm += e.Text; + _textSearchTerm += e.Text; bool match(ItemContainerInfo info) => info.ContainerControl is IContentControl control && - control.Content?.ToString()?.StartsWith(_autoSelectTerm, StringComparison.OrdinalIgnoreCase) == true; + control.Content?.ToString()?.StartsWith(_textSearchTerm, StringComparison.OrdinalIgnoreCase) == true; var info = ItemContainerGenerator.Containers.FirstOrDefault(match); @@ -270,7 +268,7 @@ namespace Avalonia.Controls SelectedIndex = info.Index; } - StartAutoSelectTimer(); + StartTextSearchTimer(); e.Handled = true; } @@ -473,30 +471,30 @@ namespace Avalonia.Controls SelectedIndex = prev; } - private void StartAutoSelectTimer() + private void StartTextSearchTimer() { - _autoSelectTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) }; - _autoSelectTimer.Tick += AutoSelectTimer_Tick; - _autoSelectTimer.Start(); + _textSearchTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) }; + _textSearchTimer.Tick += TextSearchTimer_Tick; + _textSearchTimer.Start(); } - private void StopAutoSelectTimer() + private void StopTextSearchTimer() { - if (_autoSelectTimer == null) + if (_textSearchTimer == null) { return; } - _autoSelectTimer.Stop(); - _autoSelectTimer.Tick -= AutoSelectTimer_Tick; + _textSearchTimer.Stop(); + _textSearchTimer.Tick -= TextSearchTimer_Tick; - _autoSelectTimer = null; + _textSearchTimer = null; } - private void AutoSelectTimer_Tick(object sender, EventArgs e) + private void TextSearchTimer_Tick(object sender, EventArgs e) { - _autoSelectTerm = string.Empty; - StopAutoSelectTimer(); + _textSearchTerm = string.Empty; + StopTextSearchTimer(); } } } diff --git a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs index 218832d988..7cd7e74976 100644 --- a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs @@ -144,7 +144,7 @@ namespace Avalonia.Controls.UnitTests [InlineData(-1, 2, "c", "A item", "B item", "C item")] [InlineData(0, 1, "b", "A item", "B item", "C item")] [InlineData(2, 2, "x", "A item", "B item", "C item")] - public void AutoSelect_Should_Have_Expected_SelectedIndex( + public void TextSearch_Should_Have_Expected_SelectedIndex( int initialSelectedIndex, int expectedSelectedIndex, string searchTerm, @@ -154,7 +154,7 @@ namespace Avalonia.Controls.UnitTests { var target = new ComboBox { - IsAutoSelectEnabled = true, + IsTextSearchEnabled = true, Template = GetTemplate(), Items = items.Select(x => new ComboBoxItem { Content = x }) };