mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-11-28 07:39:49 +08:00
Fix autocomplete text issue on query change (#7392)
* Fix autocomplete text issue on query change * Update from invariant to ordinal case for exact byte to byte matching * Add tests for checking when autocomplete should be empty
This commit is contained in:
parent
0314b570cd
commit
466ed10f3d
@ -336,8 +336,9 @@ namespace PowerLauncher
|
||||
{
|
||||
var textBox = (TextBox)sender;
|
||||
var text = textBox.Text;
|
||||
var autoCompleteText = SearchBox.AutoCompleteTextBlock.Text;
|
||||
|
||||
if (string.IsNullOrEmpty(text))
|
||||
if (MainViewModel.ShouldAutoCompleteTextBeEmpty(text, autoCompleteText))
|
||||
{
|
||||
SearchBox.AutoCompleteTextBlock.Text = string.Empty;
|
||||
}
|
||||
|
@ -871,13 +871,25 @@ namespace PowerLauncher.ViewModel
|
||||
}
|
||||
}
|
||||
|
||||
public static bool ShouldAutoCompleteTextBeEmpty(string queryText, string autoCompleteText)
|
||||
{
|
||||
if (string.IsNullOrEmpty(autoCompleteText))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.IsNullOrEmpty(queryText) || autoCompleteText.IndexOf(queryText, StringComparison.Ordinal) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetAutoCompleteText(int index, string input, string query)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(input) && !string.IsNullOrEmpty(query))
|
||||
{
|
||||
if (index == 0)
|
||||
{
|
||||
if (input.IndexOf(query, StringComparison.InvariantCultureIgnoreCase) == 0)
|
||||
if (input.IndexOf(query, StringComparison.OrdinalIgnoreCase) == 0)
|
||||
{
|
||||
// Use the same case as the input query for the matched portion of the string
|
||||
return query + input.Substring(query.Length);
|
||||
@ -894,7 +906,7 @@ namespace PowerLauncher.ViewModel
|
||||
{
|
||||
if (index == 0 && !string.IsNullOrEmpty(query))
|
||||
{
|
||||
if (input.IndexOf(query, StringComparison.InvariantCultureIgnoreCase) == 0)
|
||||
if (input.IndexOf(query, StringComparison.OrdinalIgnoreCase) == 0)
|
||||
{
|
||||
return query + input.Substring(query.Length);
|
||||
}
|
||||
|
@ -207,5 +207,61 @@ namespace Wox.Test
|
||||
// Assert
|
||||
Assert.AreEqual(input, autoCompleteText);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShouldAutoCompleteTextBeEmpty_ShouldReturnFalse_WhenAutoCompleteTextIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
string queryText = "Te";
|
||||
string autoCompleteText = string.Empty;
|
||||
|
||||
// Act
|
||||
bool result = MainViewModel.ShouldAutoCompleteTextBeEmpty(queryText, autoCompleteText);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShouldAutoCompleteTextBeEmpty_ShouldReturnTrue_WhenQueryTextMatchAutoCompleteText()
|
||||
{
|
||||
// Arrange
|
||||
string queryText = "Te";
|
||||
string autoCompleteText = "Teams";
|
||||
|
||||
// Act
|
||||
bool result = MainViewModel.ShouldAutoCompleteTextBeEmpty(queryText, autoCompleteText);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShouldAutoCompleteTextBeEmpty_ShouldReturnTrue_WhenQueryTextIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
string queryText = string.Empty;
|
||||
string autoCompleteText = "Teams";
|
||||
|
||||
// Act
|
||||
bool result = MainViewModel.ShouldAutoCompleteTextBeEmpty(queryText, autoCompleteText);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(true, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShouldAutoCompleteTextBeEmpty_ShouldReturnTrue_WhenQueryTextDoesNotMatchAutoCompleteText()
|
||||
{
|
||||
// Arrange
|
||||
string queryText = "TE";
|
||||
string autoCompleteText = "Teams";
|
||||
|
||||
// Act
|
||||
bool result = MainViewModel.ShouldAutoCompleteTextBeEmpty(queryText, autoCompleteText);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(true, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user