diff --git a/src/Files.App/UserControls/SearchBox.xaml b/src/Files.App/UserControls/SearchBox.xaml index b1ab9af999e7..2e86ec023d90 100644 --- a/src/Files.App/UserControls/SearchBox.xaml +++ b/src/Files.App/UserControls/SearchBox.xaml @@ -75,6 +75,7 @@ x:Name="SearchRegion" ItemTemplate="{StaticResource SuggestionTemplate}" ItemsSource="{x:Bind SearchBoxViewModel.Suggestions, Mode=OneWay}" + KeyDown="SearchRegion_KeyDown" PlaceholderText="{helpers:ResourceString Name=NavigationToolbarSearchRegion/PlaceholderText}" QuerySubmitted="SearchRegion_QuerySubmitted" Text="{x:Bind SearchBoxViewModel.Query, Mode=TwoWay}" diff --git a/src/Files.App/UserControls/SearchBox.xaml.cs b/src/Files.App/UserControls/SearchBox.xaml.cs index 01edc002ae1a..11fb6f7e32c7 100644 --- a/src/Files.App/UserControls/SearchBox.xaml.cs +++ b/src/Files.App/UserControls/SearchBox.xaml.cs @@ -24,5 +24,7 @@ private void SearchRegion_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQu => SearchBoxViewModel.SearchRegion_QuerySubmitted(sender, e); private void SearchRegion_Escaped(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs e) => SearchBoxViewModel.SearchRegion_Escaped(sender, e); + private void SearchRegion_KeyDown(object sender, KeyRoutedEventArgs e) + => SearchBoxViewModel.SearchRegion_KeyDown(sender, e); } } diff --git a/src/Files.App/ViewModels/SearchBoxViewModel.cs b/src/Files.App/ViewModels/SearchBoxViewModel.cs index 114d40e7e1c7..34510a9f36b5 100644 --- a/src/Files.App/ViewModels/SearchBoxViewModel.cs +++ b/src/Files.App/ViewModels/SearchBoxViewModel.cs @@ -8,6 +8,7 @@ using System.Collections.ObjectModel; using System.Linq; using Windows.Foundation; +using Windows.System; namespace Files.App.ViewModels { @@ -44,9 +45,7 @@ public void SetSuggestions(IEnumerable suggestions) var oldSuggestions = Suggestions.Except(items, suggestionComparer).ToList(); foreach (var oldSuggestion in oldSuggestions) - { Suggestions.Remove(oldSuggestion); - } var newSuggestions = items.Except(Suggestions, suggestionComparer).ToList(); foreach (var newSuggestion in newSuggestions) @@ -95,9 +94,7 @@ public void SearchRegion_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQue // Limit to last 5 queries to improve performance if (oldQueries.Count > 5) - { oldQueries.RemoveAt(5); - } } } @@ -109,9 +106,14 @@ public void SearchRegion_Escaped(KeyboardAccelerator sender, KeyboardAccelerator public void SearchRegion_GotFocus(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(query)) - { AddRecentQueries(); - } + } + + public void SearchRegion_KeyDown(object sender, KeyRoutedEventArgs e) + { + e.Handled = e.Key == VirtualKey.Left || + e.Key == VirtualKey.Right || + ((e.Key == VirtualKey.Up || e.Key == VirtualKey.Down) && Suggestions.Count == 0); } public void AddRecentQueries() diff --git a/src/Files.App/Views/LayoutModes/ColumnViewBase.xaml.cs b/src/Files.App/Views/LayoutModes/ColumnViewBase.xaml.cs index 581e07a08c29..3a883228969c 100644 --- a/src/Files.App/Views/LayoutModes/ColumnViewBase.xaml.cs +++ b/src/Files.App/Views/LayoutModes/ColumnViewBase.xaml.cs @@ -262,20 +262,28 @@ private void ItemNameTextBox_BeforeTextChanging(TextBox textBox, TextBoxBeforeTe private void RenameTextBox_KeyDown(object sender, KeyRoutedEventArgs e) { - if (e.Key == VirtualKey.Escape) + var textBox = (TextBox)sender; + switch (e.Key) { - TextBox textBox = (TextBox)sender; - textBox.LostFocus -= RenameTextBox_LostFocus; - textBox.Text = OldItemName; - EndRename(textBox); - e.Handled = true; - } - else if (e.Key == VirtualKey.Enter) - { - TextBox textBox = (TextBox)sender; - textBox.LostFocus -= RenameTextBox_LostFocus; - CommitRename(textBox); - e.Handled = true; + case VirtualKey.Escape: + textBox.LostFocus -= RenameTextBox_LostFocus; + textBox.Text = OldItemName; + EndRename(textBox); + e.Handled = true; + break; + case VirtualKey.Enter: + textBox.LostFocus -= RenameTextBox_LostFocus; + CommitRename(textBox); + e.Handled = true; + break; + case VirtualKey.Up: + textBox.SelectionStart = 0; + e.Handled = true; + break; + case VirtualKey.Down: + textBox.SelectionStart = textBox.Text.Length; + e.Handled = true; + break; } } diff --git a/src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs b/src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs index 0b0f6610183c..965e1f5de65d 100644 --- a/src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs +++ b/src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs @@ -381,22 +381,28 @@ private void ItemNameTextBox_BeforeTextChanging(TextBox textBox, TextBoxBeforeTe private void RenameTextBox_KeyDown(object sender, KeyRoutedEventArgs e) { - if (e.Key == VirtualKey.Escape) + var textBox = (TextBox)sender; + switch (e.Key) { - TextBox? textBox = sender as TextBox; - textBox!.LostFocus -= RenameTextBox_LostFocus; - textBox.Text = OldItemName; - EndRename(textBox); - e.Handled = true; - } - else if (e.Key == VirtualKey.Enter) - { - TextBox? textBox = sender as TextBox; - if (textBox is null) - return; - textBox.LostFocus -= RenameTextBox_LostFocus; - CommitRename(textBox); - e.Handled = true; + case VirtualKey.Escape: + textBox.LostFocus -= RenameTextBox_LostFocus; + textBox.Text = OldItemName; + EndRename(textBox); + e.Handled = true; + break; + case VirtualKey.Enter: + textBox.LostFocus -= RenameTextBox_LostFocus; + CommitRename(textBox); + e.Handled = true; + break; + case VirtualKey.Up: + textBox.SelectionStart = 0; + e.Handled = true; + break; + case VirtualKey.Down: + textBox.SelectionStart = textBox.Text.Length; + e.Handled = true; + break; } } diff --git a/src/Files.App/Views/LayoutModes/GridViewBrowser.xaml.cs b/src/Files.App/Views/LayoutModes/GridViewBrowser.xaml.cs index e1f671140e42..8c99f166420c 100644 --- a/src/Files.App/Views/LayoutModes/GridViewBrowser.xaml.cs +++ b/src/Files.App/Views/LayoutModes/GridViewBrowser.xaml.cs @@ -321,9 +321,9 @@ private void ItemNameTextBox_BeforeTextChanging(TextBox textBox, TextBoxBeforeTe private void RenameTextBox_KeyDown(object sender, KeyRoutedEventArgs e) { + var textBox = (TextBox)sender; if (e.Key == VirtualKey.Escape) { - TextBox textBox = (TextBox)sender; textBox.LostFocus -= RenameTextBox_LostFocus; textBox.Text = OldItemName; EndRename(textBox); @@ -331,11 +331,15 @@ private void RenameTextBox_KeyDown(object sender, KeyRoutedEventArgs e) } else if (e.Key == VirtualKey.Enter) { - TextBox textBox = (TextBox)sender; textBox.LostFocus -= RenameTextBox_LostFocus; CommitRename(textBox); e.Handled = true; } + else if ((e.Key == VirtualKey.Left && textBox.SelectionStart == 0) || + (e.Key == VirtualKey.Right && (textBox.SelectionStart + textBox.SelectionLength) == textBox.Text.Length)) + { + e.Handled = true; + } } private void RenameTextBox_LostFocus(object sender, RoutedEventArgs e)