From 8cd2a9191dc1c61ad941e82ef9382a7bc4313ef4 Mon Sep 17 00:00:00 2001 From: 0x5bfa <62196528+0x5bfa@users.noreply.github.com> Date: Tue, 11 Jun 2024 00:57:25 +0900 Subject: [PATCH 1/7] Init --- .../Data/Commands/Manager/CommandManager.cs | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Files.App/Data/Commands/Manager/CommandManager.cs b/src/Files.App/Data/Commands/Manager/CommandManager.cs index 39924064c860..2728cc412e0e 100644 --- a/src/Files.App/Data/Commands/Manager/CommandManager.cs +++ b/src/Files.App/Data/Commands/Manager/CommandManager.cs @@ -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 { @@ -420,9 +421,43 @@ private void OverwriteKeyBindings() } } - _allKeyBindings = commands.Values - .SelectMany(command => command.HotKeys, (command, hotKey) => (Command: command, HotKey: hotKey)) - .ToImmutableDictionary(item => item.HotKey, item => item.Command); + try + { + _allKeyBindings = commands.Values + .SelectMany(command => command.HotKeys, (command, hotKey) => (Command: command, HotKey: hotKey)) + .ToImmutableDictionary(item => item.HotKey, item => item.Command); + } + catch (ArgumentException) + { + // 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 raw = commands.Values.SelectMany(x => x.HotKeys).Select(x => x.LocalizedLabel); + var excepts = raw.Except(raw.Distinct()).Distinct(); + + foreach (var duplicatedKey in excepts) + { + if (!string.IsNullOrEmpty(duplicatedKey)) + { + var occurrences = commands.Values.Where(x => x.HotKeyText?.Contains(duplicatedKey) ?? false).OfType().ToList(); + + // Leave the first occurrence + for (int index = 1; index < occurrences.Count; index++) + occurrences[index].RestoreKeyBindings(); + } + } + } + catch (Exception ex) + { + App.Logger.LogError(ex, "The app is using default key bindings because of a serious error."); + + foreach (var command in commands.Values.OfType()) + command.RestoreKeyBindings(); + + _allKeyBindings = commands.Values + .SelectMany(command => command.HotKeys, (command, hotKey) => (Command: command, HotKey: hotKey)) + .ToImmutableDictionary(item => item.HotKey, item => item.Command); + } } public static HotKeyCollection GetDefaultKeyBindings(IAction action) From 0992b17bc8e3b8698094f67357625640b1d4a8d7 Mon Sep 17 00:00:00 2001 From: 0x5bfa <62196528+0x5bfa@users.noreply.github.com> Date: Tue, 11 Jun 2024 02:23:10 +0900 Subject: [PATCH 2/7] Tested --- .../Data/Commands/Manager/CommandManager.cs | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Files.App/Data/Commands/Manager/CommandManager.cs b/src/Files.App/Data/Commands/Manager/CommandManager.cs index 2728cc412e0e..b2fd11224108 100644 --- a/src/Files.App/Data/Commands/Manager/CommandManager.cs +++ b/src/Files.App/Data/Commands/Manager/CommandManager.cs @@ -392,16 +392,18 @@ public IEnumerator GetEnumerator() => /// private void OverwriteKeyBindings() { + var allCommands = commands.Values.OfType(); + if (ActionsSettingsService.ActionsV2 is null) { - foreach (var command in commands.Values.OfType()) + foreach (var command in allCommands) { command.RestoreKeyBindings(); } } else { - foreach (var command in commands.Values.OfType()) + foreach (var command in allCommands) { var customizedKeyBindings = ActionsSettingsService.ActionsV2.FindAll(x => x.CommandCode == command.Code.ToString()); @@ -432,20 +434,26 @@ private void OverwriteKeyBindings() // 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 raw = commands.Values.SelectMany(x => x.HotKeys).Select(x => x.LocalizedLabel); - var excepts = raw.Except(raw.Distinct()).Distinct(); + 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 duplicatedKey in excepts) + foreach (var item in duplicates) { - if (!string.IsNullOrEmpty(duplicatedKey)) + if (!string.IsNullOrEmpty(item)) { - var occurrences = commands.Values.Where(x => x.HotKeyText?.Contains(duplicatedKey) ?? false).OfType().ToList(); + var occurrences = allCommands.Where(x => x.HotKeys.Select(x => x.LocalizedLabel).Contains(item)); - // Leave the first occurrence - for (int index = 1; index < occurrences.Count; index++) - occurrences[index].RestoreKeyBindings(); + // Restore the defaults for all occurrences + for (int index = 0; index < occurrences.Count(); index++) + { + occurrences.ElementAt(index).RestoreKeyBindings(); + } } } + + _allKeyBindings = commands.Values + .SelectMany(command => command.HotKeys, (command, hotKey) => (Command: command, HotKey: hotKey)) + .ToImmutableDictionary(item => item.HotKey, item => item.Command); } catch (Exception ex) { From baa780320c83d419539d7efaf7f6b1fe6505c54a Mon Sep 17 00:00:00 2001 From: 0x5bfa <62196528+0x5bfa@users.noreply.github.com> Date: Tue, 11 Jun 2024 02:25:07 +0900 Subject: [PATCH 3/7] Improve error message --- src/Files.App/Data/Commands/Manager/CommandManager.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Files.App/Data/Commands/Manager/CommandManager.cs b/src/Files.App/Data/Commands/Manager/CommandManager.cs index b2fd11224108..02f88b21e203 100644 --- a/src/Files.App/Data/Commands/Manager/CommandManager.cs +++ b/src/Files.App/Data/Commands/Manager/CommandManager.cs @@ -454,17 +454,19 @@ private void OverwriteKeyBindings() _allKeyBindings = commands.Values .SelectMany(command => command.HotKeys, (command, hotKey) => (Command: command, HotKey: hotKey)) .ToImmutableDictionary(item => item.HotKey, item => item.Command); + + App.Logger.LogError(ex, "The app is temporarily using default key for some commands bindings because of a serious error of assigning custom keys."); } catch (Exception ex) { - App.Logger.LogError(ex, "The app is using default key bindings because of a serious error."); - foreach (var command in commands.Values.OfType()) command.RestoreKeyBindings(); _allKeyBindings = commands.Values .SelectMany(command => command.HotKeys, (command, hotKey) => (Command: command, HotKey: hotKey)) .ToImmutableDictionary(item => item.HotKey, item => item.Command); + + App.Logger.LogError(ex, "The app is temporarily using default key bindings for all because of a serious error of assigning custom keys."); } } From 35f865280ec7f611757e052f346759002c00bbea Mon Sep 17 00:00:00 2001 From: 0x5bfa <62196528+0x5bfa@users.noreply.github.com> Date: Tue, 11 Jun 2024 02:45:20 +0900 Subject: [PATCH 4/7] Added ex --- src/Files.App/Data/Commands/Manager/CommandManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Files.App/Data/Commands/Manager/CommandManager.cs b/src/Files.App/Data/Commands/Manager/CommandManager.cs index 02f88b21e203..74074e107409 100644 --- a/src/Files.App/Data/Commands/Manager/CommandManager.cs +++ b/src/Files.App/Data/Commands/Manager/CommandManager.cs @@ -429,7 +429,7 @@ private void OverwriteKeyBindings() .SelectMany(command => command.HotKeys, (command, hotKey) => (Command: command, HotKey: hotKey)) .ToImmutableDictionary(item => item.HotKey, item => item.Command); } - catch (ArgumentException) + 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 From 2a87bc7a1fd972f7b645ba0aedbcc22c023d6e0d Mon Sep 17 00:00:00 2001 From: 0x5bfa <62196528+0x5bfa@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:34:53 +0900 Subject: [PATCH 5/7] Req --- .../Data/Commands/Manager/CommandManager.cs | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Files.App/Data/Commands/Manager/CommandManager.cs b/src/Files.App/Data/Commands/Manager/CommandManager.cs index 74074e107409..846dee2d46bb 100644 --- a/src/Files.App/Data/Commands/Manager/CommandManager.cs +++ b/src/Files.App/Data/Commands/Manager/CommandManager.cs @@ -396,10 +396,7 @@ private void OverwriteKeyBindings() if (ActionsSettingsService.ActionsV2 is null) { - foreach (var command in allCommands) - { - command.RestoreKeyBindings(); - } + allCommands.ForEach(x => x.RestoreKeyBindings()); } else { @@ -409,10 +406,12 @@ private void OverwriteKeyBindings() 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 @@ -425,6 +424,7 @@ private void OverwriteKeyBindings() 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); @@ -444,29 +444,27 @@ private void OverwriteKeyBindings() var occurrences = allCommands.Where(x => x.HotKeys.Select(x => x.LocalizedLabel).Contains(item)); // Restore the defaults for all occurrences - for (int index = 0; index < occurrences.Count(); index++) - { - occurrences.ElementAt(index).RestoreKeyBindings(); - } + occurrences.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.LogError(ex, "The app is temporarily using default key for some commands bindings because of a serious error of assigning custom keys."); + 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) { - foreach (var command in commands.Values.OfType()) - command.RestoreKeyBindings(); + 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.LogError(ex, "The app is temporarily using default key bindings for all because of a serious error of assigning custom keys."); + App.Logger.LogWarning(ex, "The app is temporarily using default key bindings for all because of a serious error of assigning custom keys."); } } From a18525ef82efd255d77ab84aad302f24d32e9967 Mon Sep 17 00:00:00 2001 From: 0x5bfa <62196528+0x5bfa@users.noreply.github.com> Date: Sun, 16 Jun 2024 00:27:19 +0900 Subject: [PATCH 6/7] Reset in json only for duplication to avoid unable to overwrite --- .../Data/Commands/Manager/CommandManager.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Files.App/Data/Commands/Manager/CommandManager.cs b/src/Files.App/Data/Commands/Manager/CommandManager.cs index 846dee2d46bb..d7884f20a41d 100644 --- a/src/Files.App/Data/Commands/Manager/CommandManager.cs +++ b/src/Files.App/Data/Commands/Manager/CommandManager.cs @@ -443,8 +443,21 @@ private void OverwriteKeyBindings() { var occurrences = allCommands.Where(x => x.HotKeys.Select(x => x.LocalizedLabel).Contains(item)); - // Restore the defaults for all occurrences + // 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(ActionsSettingsService.ActionsV2) + : []; + + // Remove the duplicated key binding from user settings JSON file + var occurrencesInJson = actions.Where(x => x.KeyBinding.Contains(item)); + occurrencesInJson.ForEach(x => actions.Remove(x)); + + // Reset + ActionsSettingsService.ActionsV2 = actions; } } From 5a7e565a8ba74437a11e20110a665ce5150744b3 Mon Sep 17 00:00:00 2001 From: 0x5bfa <62196528+0x5bfa@users.noreply.github.com> Date: Mon, 17 Jun 2024 23:57:45 +0900 Subject: [PATCH 7/7] Fixed --- src/Files.App/Data/Commands/Manager/CommandManager.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Files.App/Data/Commands/Manager/CommandManager.cs b/src/Files.App/Data/Commands/Manager/CommandManager.cs index d7884f20a41d..0fcf0c7c0966 100644 --- a/src/Files.App/Data/Commands/Manager/CommandManager.cs +++ b/src/Files.App/Data/Commands/Manager/CommandManager.cs @@ -453,8 +453,7 @@ ActionsSettingsService.ActionsV2 is not null : []; // Remove the duplicated key binding from user settings JSON file - var occurrencesInJson = actions.Where(x => x.KeyBinding.Contains(item)); - occurrencesInJson.ForEach(x => actions.Remove(x)); + actions.RemoveAll(x => x.KeyBinding.Contains(item)); // Reset ActionsSettingsService.ActionsV2 = actions;