Skip to content
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

Feature: Added support to switch between items using arrow keys in the conflicts dialog #13357

Merged
Merged
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/Dialogs/FilesystemOperationDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
x:Load="{x:Bind IsTextBoxVisible, Mode=OneWay}"
Loaded="NameEdit_Loaded"
LostFocus="NameEdit_LostFocus"
PreviewKeyDown="NameEdit_PreviewKeyDown"
Text="{x:Bind CustomName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>

Expand Down
95 changes: 66 additions & 29 deletions src/Files.App/Dialogs/FilesystemOperationDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.WinUI.UI;
using Files.App.Utils;
using Files.Core.ViewModels.Dialogs;
using Files.Core.ViewModels.Dialogs.FileSystemDialog;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using System;
using System.Threading.Tasks;

namespace Files.App.Dialogs
{
Expand Down Expand Up @@ -72,7 +67,8 @@ protected override void OnApplyTemplate()

private void PrimaryButton_GotFocus(object sender, RoutedEventArgs e)
{
(sender as Button).GotFocus -= PrimaryButton_GotFocus;
if (sender is Button btn)
btn.GotFocus -= PrimaryButton_GotFocus;

if (chkPermanentlyDelete is not null)
chkPermanentlyDelete.IsEnabled = ViewModel.IsDeletePermanentlyEnabled;
Expand All @@ -95,33 +91,14 @@ private void NameStackPanel_Tapped(object sender, Microsoft.UI.Xaml.Input.Tapped
element.DataContext is FileSystemDialogConflictItemViewModel conflictItem &&
conflictItem.ConflictResolveOption == FileNameConflictResolveOptionType.GenerateNewName)
{
conflictItem.IsTextBoxVisible = conflictItem.ConflictResolveOption == FileNameConflictResolveOptionType.GenerateNewName;
conflictItem.CustomName = conflictItem.DestinationDisplayName;
StartRename(conflictItem);
}
}

private void NameEdit_LostFocus(object sender, RoutedEventArgs e)
{
if ((sender as FrameworkElement)?.DataContext is FileSystemDialogConflictItemViewModel conflictItem)
{
conflictItem.CustomName = FilesystemHelpers.FilterRestrictedCharacters(conflictItem.CustomName);

if (ViewModel.IsNameAvailableForItem(conflictItem, conflictItem.CustomName!))
{
conflictItem.IsTextBoxVisible = false;
}
else
{
ViewModel.PrimaryButtonEnabled = false;
}

if (conflictItem.CustomName.Equals(conflictItem.DisplayName))
{
var savedName = conflictItem.DestinationDisplayName;
conflictItem.CustomName = string.Empty;
conflictItem.DestinationDisplayName = savedName;
}
}
EndRename(conflictItem);
}

private void NameEdit_Loaded(object sender, RoutedEventArgs e)
Expand All @@ -145,11 +122,71 @@ private void ConflictOptions_Loaded(object sender, RoutedEventArgs e)
private void FilesystemOperationDialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args)
{
if (ViewModel.FileSystemDialogMode.IsInDeleteMode)
{
DescriptionText.Foreground = App.Current.Resources["TextControlForeground"] as SolidColorBrush;
}

UpdateDialogLayout();
}

private void NameEdit_PreviewKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (sender is not FrameworkElement element || element.DataContext is not FileSystemDialogConflictItemViewModel currentItem)
return;

if (e.Key is Windows.System.VirtualKey.Down)
{
var index = ViewModel.Items.IndexOf(currentItem);
if (index == -1 || index == ViewModel.Items.Count - 1)
return;

var nextItem = ViewModel.Items.Skip(index + 1)
.FirstOrDefault(i => i is FileSystemDialogConflictItemViewModel { ConflictResolveOption: FileNameConflictResolveOptionType.GenerateNewName });

if (nextItem is null)
return;

EndRename(currentItem);
StartRename((FileSystemDialogConflictItemViewModel)nextItem);

e.Handled = true;
}
else if (e.Key is Windows.System.VirtualKey.Up)
{
var index = ViewModel.Items.IndexOf(currentItem);
if (index == -1 || index == 0)
return;

var prevItem = ViewModel.Items.Take(index)
.LastOrDefault(i => i is FileSystemDialogConflictItemViewModel { ConflictResolveOption: FileNameConflictResolveOptionType.GenerateNewName });
if (prevItem is null)
return;

EndRename(currentItem);
StartRename((FileSystemDialogConflictItemViewModel)prevItem);

e.Handled = true;
}
}

private void StartRename(FileSystemDialogConflictItemViewModel conflictItem)
{
conflictItem.IsTextBoxVisible = conflictItem.ConflictResolveOption == FileNameConflictResolveOptionType.GenerateNewName;
conflictItem.CustomName = conflictItem.DestinationDisplayName;
}

private void EndRename(FileSystemDialogConflictItemViewModel conflictItem)
{
conflictItem.CustomName = FilesystemHelpers.FilterRestrictedCharacters(conflictItem.CustomName);

if (ViewModel.IsNameAvailableForItem(conflictItem, conflictItem.CustomName!))
conflictItem.IsTextBoxVisible = false;
else
ViewModel.PrimaryButtonEnabled = false;

if (conflictItem.CustomName.Equals(conflictItem.DisplayName))
{
var savedName = conflictItem.DestinationDisplayName;
conflictItem.DestinationDisplayName = savedName;
}
}
}
}