Skip to content

Fix: Fixed issue with using arrow keys while renaming a file #11028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Files.App/UserControls/SearchBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/UserControls/SearchBox.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 8 additions & 6 deletions src/Files.App/ViewModels/SearchBoxViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.ObjectModel;
using System.Linq;
using Windows.Foundation;
using Windows.System;

namespace Files.App.ViewModels
{
Expand Down Expand Up @@ -44,9 +45,7 @@ public void SetSuggestions(IEnumerable<SuggestionModel> 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)
Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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()
Expand Down
34 changes: 21 additions & 13 deletions src/Files.App/Views/LayoutModes/ColumnViewBase.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
36 changes: 21 additions & 15 deletions src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/Files.App/Views/LayoutModes/GridViewBrowser.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,21 +321,25 @@ 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);
e.Handled = true;
}
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)
Expand Down