Skip to content
Merged
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
71 changes: 63 additions & 8 deletions src/Files.App/Data/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Frozen;
using System.Collections.Immutable;
using Files.App.Actions;
using Microsoft.Extensions.Logging;

namespace Files.App.Data.Commands
{
Expand Down Expand Up @@ -391,25 +392,26 @@ public IEnumerator<IRichCommand> GetEnumerator() =>
/// </summary>
private void OverwriteKeyBindings()
{
var allCommands = commands.Values.OfType<ActionCommand>();

if (ActionsSettingsService.ActionsV2 is null)
{
foreach (var command in commands.Values.OfType<ActionCommand>())
{
command.RestoreKeyBindings();
}
allCommands.ForEach(x => x.RestoreKeyBindings());
}
else
{
foreach (var command in commands.Values.OfType<ActionCommand>())
foreach (var command in allCommands)
{
var customizedKeyBindings = ActionsSettingsService.ActionsV2.FindAll(x => x.CommandCode == command.Code.ToString());

if (customizedKeyBindings.IsEmpty())
{
// Could not find customized key bindings for the command
command.RestoreKeyBindings();
}
else if (customizedKeyBindings.Count == 1 && customizedKeyBindings[0].KeyBinding == string.Empty)
{
// Do not assign any key binding even though there're default keys pre-defined
command.OverwriteKeyBindings(HotKeyCollection.Empty);
}
else
Expand All @@ -420,9 +422,62 @@ private void OverwriteKeyBindings()
}
}

_allKeyBindings = commands.Values
.SelectMany(command => command.HotKeys, (command, hotKey) => (Command: command, HotKey: hotKey))
.ToImmutableDictionary(item => item.HotKey, item => item.Command);
try
{
// Set collection of a set of command code and key bindings to dictionary
_allKeyBindings = commands.Values
.SelectMany(command => command.HotKeys, (command, hotKey) => (Command: command, HotKey: hotKey))
.ToImmutableDictionary(item => item.HotKey, item => item.Command);
}
catch (ArgumentException ex)
{
// The keys are not necessarily all different because they can be set manually in text editor
// ISSUE: https://github.com/files-community/Files/issues/15331

var flat = commands.Values.SelectMany(x => x.HotKeys).Select(x => x.LocalizedLabel);
var duplicates = flat.GroupBy(x => x).Where(x => x.Count() > 1).Select(group => group.Key);

foreach (var item in duplicates)
{
if (!string.IsNullOrEmpty(item))
{
var occurrences = allCommands.Where(x => x.HotKeys.Select(x => x.LocalizedLabel).Contains(item));

// Restore the defaults for all occurrences in our cache
occurrences.ForEach(x => x.RestoreKeyBindings());

// Get all customized key bindings from user settings json
var actions =
ActionsSettingsService.ActionsV2 is not null
? new List<ActionWithParameterItem>(ActionsSettingsService.ActionsV2)
: [];

// Remove the duplicated key binding from user settings JSON file
actions.RemoveAll(x => x.KeyBinding.Contains(item));

// Reset
ActionsSettingsService.ActionsV2 = actions;
}
}

// Set collection of a set of command code and key bindings to dictionary
_allKeyBindings = commands.Values
.SelectMany(command => command.HotKeys, (command, hotKey) => (Command: command, HotKey: hotKey))
.ToImmutableDictionary(item => item.HotKey, item => item.Command);

App.Logger.LogWarning(ex, "The app found some keys in different commands are duplicated and are using default key bindings for those commands.");
}
catch (Exception ex)
{
allCommands.ForEach(x => x.RestoreKeyBindings());

// Set collection of a set of command code and key bindings to dictionary
_allKeyBindings = commands.Values
.SelectMany(command => command.HotKeys, (command, hotKey) => (Command: command, HotKey: hotKey))
.ToImmutableDictionary(item => item.HotKey, item => item.Command);

App.Logger.LogWarning(ex, "The app is temporarily using default key bindings for all because of a serious error of assigning custom keys.");
}
}

public static HotKeyCollection GetDefaultKeyBindings(IAction action)
Expand Down