diff --git a/Terminal.Gui/Input/Keyboard/KeyBindings.cs b/Terminal.Gui/Input/Keyboard/KeyBindings.cs index 5efe34f24a..e24914f80c 100644 --- a/Terminal.Gui/Input/Keyboard/KeyBindings.cs +++ b/Terminal.Gui/Input/Keyboard/KeyBindings.cs @@ -45,7 +45,7 @@ public void Add (Key key, KeyBinding binding) // IMPORTANT: update the memory referenced by the key, and Dictionary uses caching for performance, and thus // IMPORTANT: Apply will update the Dictionary with the new key, but the old key will still be in the dictionary. // IMPORTANT: See the ConfigurationManager.Illustrate_DeepMemberWiseCopy_Breaks_Dictionary test for details. - Bindings.Add (new (key), binding); + _bindings.Add (new (key), binding); } #pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved @@ -102,26 +102,27 @@ public void Add (Key key, View? target, params Command [] commands) Add (key, binding); } - // TODO: Add a dictionary comparer that ignores Scope - // TODO: This should not be public! - /// The collection of objects. - public Dictionary Bindings { get; } = new (new KeyEqualityComparer ()); + private readonly Dictionary _bindings = new (new KeyEqualityComparer ()); /// /// Gets the bindings bound to . /// /// /// - public IEnumerable> GetBindings (Key key) + public IEnumerable> GetBindings (Key? key = null) { - return Bindings.Where (b => b.Key == key.KeyCode); + if (key is null) + { + return _bindings; + } + return _bindings.Where (b => b.Key == key.KeyCode); } /// /// Gets the keys that are bound. /// /// - public IEnumerable GetBoundKeys () { return Bindings.Keys; } + public IEnumerable GetBoundKeys () { return _bindings.Keys; } /// /// The view that the are bound to. @@ -132,7 +133,7 @@ public IEnumerable> GetBindings (Key key) public View? Target { get; init; } /// Removes all objects from the collection. - public void Clear () { Bindings.Clear (); } + public void Clear () { _bindings.Clear (); } /// /// Removes all key bindings that trigger the given command set. Views can have multiple different keys bound to @@ -141,7 +142,7 @@ public IEnumerable> GetBindings (Key key) /// public void Clear (params Command [] command) { - KeyValuePair [] kvps = Bindings + KeyValuePair [] kvps = _bindings .Where (kvp => kvp.Value.Commands.SequenceEqual (command)) .ToArray (); @@ -186,7 +187,7 @@ public Command [] GetCommands (Key key) /// The first bound to the set of commands specified by . /// if the set of caommands was not found. /// - public Key? GetKeyFromCommands (params Command [] commands) { return Bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key; } + public Key? GetKeyFromCommands (params Command [] commands) { return _bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key; } /// Gets Keys bound to the set of commands specified by . /// The set of commands to search. @@ -196,7 +197,7 @@ public Command [] GetCommands (Key key) /// public IEnumerable GetKeysFromCommands (params Command [] commands) { - return Bindings.Where (a => a.Value.Commands.SequenceEqual (commands)).Select (a => a.Key); + return _bindings.Where (a => a.Value.Commands.SequenceEqual (commands)).Select (a => a.Key); } /// Removes a from the collection. @@ -208,7 +209,7 @@ public void Remove (Key key) return; } - Bindings.Remove (key); + _bindings.Remove (key); } /// Replaces the commands already bound to a key. @@ -275,7 +276,7 @@ public bool TryGet (Key key, out KeyBinding binding) if (key.IsValid) { - return Bindings.TryGetValue (key, out binding); + return _bindings.TryGetValue (key, out binding); } return false; diff --git a/UICatalog/Scenarios/KeyBindings.cs b/UICatalog/Scenarios/KeyBindings.cs index d09c8850c0..54862b8c76 100644 --- a/UICatalog/Scenarios/KeyBindings.cs +++ b/UICatalog/Scenarios/KeyBindings.cs @@ -80,7 +80,7 @@ Pressing Esc or {Application.QuitKey} will cause it to quit the app. }; appWindow.Add (appBindingsListView); - foreach (var key in Application.KeyBindings.GetBoundKeys()) + foreach (var key in Application.KeyBindings.GetBoundKeys ()) { var binding = Application.KeyBindings.Get (key); appBindings.Add ($"{key} -> {binding.Target?.GetType ().Name} - {binding.Commands [0]}"); @@ -104,7 +104,7 @@ Pressing Esc or {Application.QuitKey} will cause it to quit the app. foreach (var subview in appWindow.Subviews) { - foreach (var binding in subview.HotKeyBindings.Bindings) + foreach (KeyValuePair binding in subview.HotKeyBindings.GetBindings ()) { hotkeyBindings.Add ($"{binding.Key} -> {subview.GetType ().Name} - {binding.Value.Commands [0]}"); } @@ -148,8 +148,8 @@ private void Application_HasFocusChanged (object sender, EventArgs e) _focusedBindingsListView.Title = $"_Focused ({focused?.GetType ().Name}) Bindings"; - _focusedBindings.Clear(); - foreach (var binding in focused?.KeyBindings!.Bindings) + _focusedBindings.Clear (); + foreach (var binding in focused?.KeyBindings!.GetBindings ()) { _focusedBindings.Add ($"{binding.Key} -> {binding.Value.Commands [0]}"); } diff --git a/UnitTests/Input/Keyboard/KeyBindingsTests.cs b/UnitTests/Input/Keyboard/KeyBindingsTests.cs index cce65c6d55..d2bdd64850 100644 --- a/UnitTests/Input/Keyboard/KeyBindingsTests.cs +++ b/UnitTests/Input/Keyboard/KeyBindingsTests.cs @@ -103,7 +103,7 @@ public void Clear_Clears () public void Defaults () { var keyBindings = new KeyBindings (new ()); - Assert.Empty (keyBindings.Bindings); + Assert.Empty (keyBindings.GetBindings ()); Assert.Null (keyBindings.GetKeyFromCommands (Command.Accept)); Assert.NotNull (keyBindings.Target); } @@ -305,9 +305,6 @@ public void TryGet_Succeeds () var key = new Key (Key.Q.WithCtrl); bool result = keyBindings.TryGet (key, out KeyBinding _); Assert.True (result); ; - - result = keyBindings.Bindings.TryGetValue (key, out KeyBinding _); - Assert.True (result); } [Fact] diff --git a/UnitTests/View/Keyboard/HotKeyTests.cs b/UnitTests/View/Keyboard/HotKeyTests.cs index 4a70ff3487..1c7a34e85b 100644 --- a/UnitTests/View/Keyboard/HotKeyTests.cs +++ b/UnitTests/View/Keyboard/HotKeyTests.cs @@ -72,7 +72,8 @@ public void AddKeyBindingsForHotKey_SetsBinding_Key () Assert.Equal (KeyCode.Z, view.HotKey); view.AddKeyBindingsForHotKey (view.HotKey, Key.A); - Assert.Equal (Key.A, view.HotKeyBindings.Bindings [Key.A].Key); + view.HotKeyBindings.TryGet (Key.A, out var binding); + Assert.Equal (Key.A, binding.Key); } [Fact] @@ -83,7 +84,8 @@ public void AddKeyBindingsForHotKey_SetsBinding_Data () Assert.Equal (KeyCode.Z, view.HotKey); view.AddKeyBindingsForHotKey (view.HotKey, Key.A, "data"); - Assert.Equal ("data", view.HotKeyBindings.Bindings [Key.A].Data); + view.HotKeyBindings.TryGet (Key.A, out var binding); + Assert.Equal ("data", binding.Data); } [Fact] diff --git a/UnitTests/Views/ContextMenuTests.cs b/UnitTests/Views/ContextMenuTests.cs index 066848a501..8fa24b729d 100644 --- a/UnitTests/Views/ContextMenuTests.cs +++ b/UnitTests/Views/ContextMenuTests.cs @@ -1483,9 +1483,9 @@ public void KeyBindings_Removed_On_Close_ContextMenu () Assert.False (deleteFile); cm.Show (menuItems); - Assert.True (cm.MenuBar!.HotKeyBindings.Bindings.ContainsKey (Key.N.WithCtrl)); - Assert.True (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.WithCtrl)); - Assert.True (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.D.WithCtrl)); + Assert.True (cm.MenuBar!.HotKeyBindings.TryGet (Key.N.WithCtrl, out _)); + Assert.True (cm.MenuBar.HotKeyBindings.TryGet (Key.R.WithCtrl, out _)); + Assert.True (cm.MenuBar.HotKeyBindings.TryGet (Key.D.WithCtrl, out _)); Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl)); Application.MainLoop!.RunIteration (); @@ -1502,9 +1502,9 @@ public void KeyBindings_Removed_On_Close_ContextMenu () Assert.True (deleteFile); Assert.False (cm.MenuBar.IsMenuOpen); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.WithCtrl)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.WithCtrl)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.D.WithCtrl)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.N.WithCtrl, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.R.WithCtrl, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.D.WithCtrl, out _)); newFile = false; renameFile = false; @@ -1555,8 +1555,8 @@ public void KeyBindings_With_ContextMenu_And_MenuBar () top.Add (menuBar); Application.Begin (top); - Assert.True (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.WithCtrl)); - Assert.False (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.WithCtrl)); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.N.WithCtrl, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.R.WithCtrl, out _)); Assert.Null (cm.MenuBar); Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl)); @@ -1568,10 +1568,10 @@ public void KeyBindings_With_ContextMenu_And_MenuBar () newFile = false; cm.Show (menuItems); - Assert.True (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.WithCtrl)); - Assert.False (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.WithCtrl)); - Assert.False (cm.MenuBar!.HotKeyBindings.Bindings.ContainsKey (Key.N.WithCtrl)); - Assert.True (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.WithCtrl)); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.N.WithCtrl, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.R.WithCtrl, out _)); + Assert.False (cm.MenuBar!.HotKeyBindings.TryGet (Key.N.WithCtrl, out _)); + Assert.True (cm.MenuBar.HotKeyBindings.TryGet (Key.R.WithCtrl, out _)); Assert.True (cm.MenuBar.IsMenuOpen); Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl)); @@ -1584,10 +1584,10 @@ public void KeyBindings_With_ContextMenu_And_MenuBar () Assert.True (renameFile); Assert.False (cm.MenuBar.IsMenuOpen); - Assert.True (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.WithCtrl)); - Assert.False (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.WithCtrl)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.WithCtrl)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.WithCtrl)); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.N.WithCtrl, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.R.WithCtrl, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.N.WithCtrl, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.R.WithCtrl, out _)); newFile = false; renameFile = false; @@ -1634,7 +1634,7 @@ public void KeyBindings_With_Same_Shortcut_ContextMenu_And_MenuBar () top.Add (menuBar); Application.Begin (top); - Assert.True (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.WithCtrl)); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.N.WithCtrl, out _)); Assert.Null (cm.MenuBar); Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl)); @@ -1645,8 +1645,8 @@ public void KeyBindings_With_Same_Shortcut_ContextMenu_And_MenuBar () newMenuBar = false; cm.Show (menuItems); - Assert.True (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.WithCtrl)); - Assert.True (cm.MenuBar!.HotKeyBindings.Bindings.ContainsKey (Key.N.WithCtrl)); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.N.WithCtrl, out _)); + Assert.True (cm.MenuBar!.HotKeyBindings.TryGet (Key.N.WithCtrl, out _)); Assert.True (cm.MenuBar.IsMenuOpen); Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl)); @@ -1657,8 +1657,8 @@ public void KeyBindings_With_Same_Shortcut_ContextMenu_And_MenuBar () Assert.True (newContextMenu); Assert.False (cm.MenuBar!.IsMenuOpen); - Assert.True (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.WithCtrl)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.WithCtrl)); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.N.WithCtrl, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.N.WithCtrl, out _)); newMenuBar = false; newContextMenu = false; @@ -1704,20 +1704,20 @@ public void HotKeys_Removed_On_Close_ContextMenu () cm.Show (menuItems); Assert.True (cm.MenuBar!.IsMenuOpen); - Assert.False (cm.MenuBar!.HotKeyBindings.Bindings.ContainsKey (Key.N.WithAlt)); - Assert.False (cm.MenuBar!.HotKeyBindings.Bindings.ContainsKey (Key.N.NoShift)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.WithAlt)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.NoShift)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.D.WithAlt)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.D.NoShift)); + Assert.False (cm.MenuBar!.HotKeyBindings.TryGet (Key.N.WithAlt, out _)); + Assert.False (cm.MenuBar!.HotKeyBindings.TryGet (Key.N.NoShift, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.R.WithAlt, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.R.NoShift, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.D.WithAlt, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.D.NoShift, out _)); Assert.Single (Application.Top!.Subviews); View [] menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == cm.MenuBar).ToArray (); - Assert.True (menus [0].HotKeyBindings.Bindings.ContainsKey (Key.N.WithAlt)); - Assert.True (menus [0].HotKeyBindings.Bindings.ContainsKey (Key.N.NoShift)); - Assert.True (menus [0].HotKeyBindings.Bindings.ContainsKey (Key.R.WithAlt)); - Assert.True (menus [0].HotKeyBindings.Bindings.ContainsKey (Key.R.NoShift)); - Assert.True (menus [0].HotKeyBindings.Bindings.ContainsKey (Key.D.WithAlt)); - Assert.True (menus [0].HotKeyBindings.Bindings.ContainsKey (Key.D.NoShift)); + Assert.True (menus [0].HotKeyBindings.TryGet (Key.N.WithAlt, out _)); + Assert.True (menus [0].HotKeyBindings.TryGet (Key.N.NoShift, out _)); + Assert.True (menus [0].HotKeyBindings.TryGet (Key.R.WithAlt, out _)); + Assert.True (menus [0].HotKeyBindings.TryGet (Key.R.NoShift, out _)); + Assert.True (menus [0].HotKeyBindings.TryGet (Key.D.WithAlt, out _)); + Assert.True (menus [0].HotKeyBindings.TryGet (Key.D.NoShift, out _)); Assert.True (Application.RaiseKeyDownEvent (Key.N.WithAlt)); Assert.False (cm.MenuBar!.IsMenuOpen); @@ -1734,12 +1734,12 @@ public void HotKeys_Removed_On_Close_ContextMenu () Application.MainLoop!.RunIteration (); Assert.True (deleteFile); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.WithAlt)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.NoShift)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.WithAlt)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.NoShift)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.D.WithAlt)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.D.NoShift)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.N.WithAlt, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.N.NoShift, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.R.WithAlt, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.R.NoShift, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.D.WithAlt, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.D.NoShift, out _)); newFile = false; renameFile = false; @@ -1796,9 +1796,9 @@ public void HotKeys_With_ContextMenu_And_MenuBar () top.Add (menuBar); Application.Begin (top); - Assert.True (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.F.WithAlt)); - Assert.False (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.WithAlt)); - Assert.False (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.WithAlt)); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.F.WithAlt, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.N.WithAlt, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.R.WithAlt, out _)); View [] menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == menuBar).ToArray (); Assert.Empty (menus); Assert.Null (cm.MenuBar); @@ -1807,7 +1807,7 @@ public void HotKeys_With_ContextMenu_And_MenuBar () Assert.True (menuBar.IsMenuOpen); Assert.Equal (2, Application.Top!.Subviews.Count); menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == menuBar).ToArray (); - Assert.True (menus [0].HotKeyBindings.Bindings.ContainsKey (Key.N.WithAlt)); + Assert.True (menus [0].HotKeyBindings.TryGet (Key.N.WithAlt, out _)); Assert.True (Application.RaiseKeyDownEvent (Key.N.WithAlt)); Assert.False (menuBar.IsMenuOpen); Assert.False (Application.RaiseKeyDownEvent (Key.R.WithAlt)); @@ -1818,29 +1818,29 @@ public void HotKeys_With_ContextMenu_And_MenuBar () newFile = false; cm.Show (menuItems); - Assert.True (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.F.WithAlt)); - Assert.True (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.F.NoShift)); - Assert.False (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.WithAlt)); - Assert.False (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.NoShift)); - Assert.False (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.E.WithAlt)); - Assert.False (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.E.NoShift)); - Assert.False (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.WithAlt)); - Assert.False (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.NoShift)); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.F.WithAlt, out _)); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.F.NoShift, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.N.WithAlt, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.N.NoShift, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.E.WithAlt, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.E.NoShift, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.R.WithAlt, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.R.NoShift, out _)); Assert.True (cm.MenuBar!.IsMenuOpen); - Assert.False (cm.MenuBar!.HotKeyBindings.Bindings.ContainsKey (Key.F.WithAlt)); - Assert.False (cm.MenuBar!.HotKeyBindings.Bindings.ContainsKey (Key.F.NoShift)); - Assert.False (cm.MenuBar!.HotKeyBindings.Bindings.ContainsKey (Key.N.WithAlt)); - Assert.False (cm.MenuBar!.HotKeyBindings.Bindings.ContainsKey (Key.N.NoShift)); - Assert.False (cm.MenuBar!.HotKeyBindings.Bindings.ContainsKey (Key.E.WithAlt)); - Assert.False (cm.MenuBar!.HotKeyBindings.Bindings.ContainsKey (Key.E.NoShift)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.WithAlt)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.NoShift)); + Assert.False (cm.MenuBar!.HotKeyBindings.TryGet (Key.F.WithAlt, out _)); + Assert.False (cm.MenuBar!.HotKeyBindings.TryGet (Key.F.NoShift, out _)); + Assert.False (cm.MenuBar!.HotKeyBindings.TryGet (Key.N.WithAlt, out _)); + Assert.False (cm.MenuBar!.HotKeyBindings.TryGet (Key.N.NoShift, out _)); + Assert.False (cm.MenuBar!.HotKeyBindings.TryGet (Key.E.WithAlt, out _)); + Assert.False (cm.MenuBar!.HotKeyBindings.TryGet (Key.E.NoShift, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.R.WithAlt, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.R.NoShift, out _)); Assert.Equal (3, Application.Top!.Subviews.Count); menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == cm.MenuBar).ToArray (); - Assert.True (menus [0].HotKeyBindings.Bindings.ContainsKey (Key.E.WithAlt)); - Assert.True (menus [0].HotKeyBindings.Bindings.ContainsKey (Key.E.NoShift)); - Assert.True (menus [1].HotKeyBindings.Bindings.ContainsKey (Key.R.WithAlt)); - Assert.True (menus [1].HotKeyBindings.Bindings.ContainsKey (Key.R.NoShift)); + Assert.True (menus [0].HotKeyBindings.TryGet (Key.E.WithAlt, out _)); + Assert.True (menus [0].HotKeyBindings.TryGet (Key.E.NoShift, out _)); + Assert.True (menus [1].HotKeyBindings.TryGet (Key.R.WithAlt, out _)); + Assert.True (menus [1].HotKeyBindings.TryGet (Key.R.NoShift, out _)); Assert.True (cm.MenuBar.IsMenuOpen); Assert.True (Application.RaiseKeyDownEvent (Key.F.WithAlt)); Assert.False (cm.MenuBar.IsMenuOpen); @@ -1852,14 +1852,14 @@ public void HotKeys_With_ContextMenu_And_MenuBar () Assert.True (cm.MenuBar.IsMenuOpen); Assert.Equal (3, Application.Top!.Subviews.Count); menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == cm.MenuBar).ToArray (); - Assert.True (menus [0].HotKeyBindings.Bindings.ContainsKey (Key.E.WithAlt)); - Assert.True (menus [0].HotKeyBindings.Bindings.ContainsKey (Key.E.NoShift)); - Assert.False (menus [0].HotKeyBindings.Bindings.ContainsKey (Key.R.WithAlt)); - Assert.False (menus [0].HotKeyBindings.Bindings.ContainsKey (Key.R.NoShift)); - Assert.False (menus [1].HotKeyBindings.Bindings.ContainsKey (Key.E.WithAlt)); - Assert.False (menus [1].HotKeyBindings.Bindings.ContainsKey (Key.E.NoShift)); - Assert.True (menus [1].HotKeyBindings.Bindings.ContainsKey (Key.R.WithAlt)); - Assert.True (menus [1].HotKeyBindings.Bindings.ContainsKey (Key.R.NoShift)); + Assert.True (menus [0].HotKeyBindings.TryGet (Key.E.WithAlt, out _)); + Assert.True (menus [0].HotKeyBindings.TryGet (Key.E.NoShift, out _)); + Assert.False (menus [0].HotKeyBindings.TryGet (Key.R.WithAlt, out _)); + Assert.False (menus [0].HotKeyBindings.TryGet (Key.R.NoShift, out _)); + Assert.False (menus [1].HotKeyBindings.TryGet (Key.E.WithAlt, out _)); + Assert.False (menus [1].HotKeyBindings.TryGet (Key.E.NoShift, out _)); + Assert.True (menus [1].HotKeyBindings.TryGet (Key.R.WithAlt, out _)); + Assert.True (menus [1].HotKeyBindings.TryGet (Key.R.NoShift, out _)); Assert.True (Application.RaiseKeyDownEvent (Key.E.NoShift)); Assert.True (Application.RaiseKeyDownEvent (Key.R.WithAlt)); Assert.False (cm.MenuBar.IsMenuOpen); @@ -1867,14 +1867,14 @@ public void HotKeys_With_ContextMenu_And_MenuBar () Assert.True (renameFile); Assert.Single (Application.Top!.Subviews); - Assert.True (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.F.WithAlt)); - Assert.True (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.F.NoShift)); - Assert.False (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.WithAlt)); - Assert.False (menuBar.HotKeyBindings.Bindings.ContainsKey (Key.N.NoShift)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.E.WithAlt)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.E.NoShift)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.WithAlt)); - Assert.False (cm.MenuBar.HotKeyBindings.Bindings.ContainsKey (Key.R.NoShift)); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.F.WithAlt, out _)); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.F.NoShift, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.N.WithAlt, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.N.NoShift, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.E.WithAlt, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.E.NoShift, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.R.WithAlt, out _)); + Assert.False (cm.MenuBar.HotKeyBindings.TryGet (Key.R.NoShift, out _)); newFile = false; renameFile = false; diff --git a/UnitTests/Views/MenuBarTests.cs b/UnitTests/Views/MenuBarTests.cs index a332492e8d..34fac896e8 100644 --- a/UnitTests/Views/MenuBarTests.cs +++ b/UnitTests/Views/MenuBarTests.cs @@ -19,8 +19,9 @@ public void AddMenuBarItem_RemoveMenuItem_Dynamically () menuBar.Menus = [menuBarItem]; Assert.Single (menuBar.Menus); Assert.Single (menuBar.Menus [0].Children!); - Assert.Contains (Key.N.WithAlt, menuBar.HotKeyBindings.Bindings); - Assert.DoesNotContain (Key.I, menuBar.HotKeyBindings.Bindings); + + Assert.True (menuBar.HotKeyBindings.TryGet (Key.N.WithAlt, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.I, out _)); var top = new Toplevel (); top.Add (menuBar); @@ -39,12 +40,12 @@ public void AddMenuBarItem_RemoveMenuItem_Dynamically () menuItem.RemoveMenuItem (); Assert.Single (menuBar.Menus); Assert.Null (menuBar.Menus [0].Children); - Assert.Contains (Key.N.WithAlt, menuBar.HotKeyBindings.Bindings); - Assert.DoesNotContain (Key.I, menuBar.HotKeyBindings.Bindings); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.N.WithAlt, out _)); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.I, out _)); menuBarItem.RemoveMenuItem (); Assert.Empty (menuBar.Menus); - Assert.DoesNotContain (Key.N.WithAlt, menuBar.HotKeyBindings.Bindings); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.N.WithAlt, out _)); top.Dispose (); } @@ -2998,12 +2999,12 @@ public void Update_ShortcutKey_KeyBindings_Old_ShortcutKey_Is_Removed () ] }; - Assert.Contains (Key.A.WithCtrl, menuBar.HotKeyBindings.Bindings); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.A.WithCtrl, out _)); menuBar.Menus [0].Children! [0].ShortcutKey = Key.B.WithCtrl; - Assert.DoesNotContain (Key.A.WithCtrl, menuBar.HotKeyBindings.Bindings); - Assert.Contains (Key.B.WithCtrl, menuBar.HotKeyBindings.Bindings); + Assert.False (menuBar.HotKeyBindings.TryGet (Key.A.WithCtrl, out _)); + Assert.True (menuBar.HotKeyBindings.TryGet (Key.B.WithCtrl, out _)); } [Fact] diff --git a/UnitTests/Views/ShortcutTests.cs b/UnitTests/Views/ShortcutTests.cs index 7041913ddc..eae2e7ad40 100644 --- a/UnitTests/Views/ShortcutTests.cs +++ b/UnitTests/Views/ShortcutTests.cs @@ -281,11 +281,11 @@ public void Key_Changing_Removes_Previous_Binding () var shortcut = new Shortcut (); shortcut.Key = Key.A; - Assert.Contains (Key.A, shortcut.HotKeyBindings.Bindings.Keys); + Assert.True(shortcut.HotKeyBindings.TryGet(Key.A, out _)); shortcut.Key = Key.B; - Assert.DoesNotContain (Key.A, shortcut.HotKeyBindings.Bindings.Keys); - Assert.Contains (Key.B, shortcut.HotKeyBindings.Bindings.Keys); + Assert.False (shortcut.HotKeyBindings.TryGet (Key.A, out _)); + Assert.True (shortcut.HotKeyBindings.TryGet (Key.B, out _)); } // Test Key gets bound correctly @@ -313,14 +313,14 @@ public void BindKeyToApplication_Changing_Adjusts_KeyBindings () var shortcut = new Shortcut (); shortcut.Key = Key.A; - Assert.Contains (Key.A, shortcut.HotKeyBindings.Bindings.Keys); + Assert.True (shortcut.HotKeyBindings.TryGet(Key.A, out _)); shortcut.BindKeyToApplication = true; - Assert.DoesNotContain (Key.A, shortcut.HotKeyBindings.Bindings.Keys); + Assert.False (shortcut.HotKeyBindings.TryGet (Key.A, out _)); Assert.NotEmpty (Application.KeyBindings.GetBindings(Key.A)); shortcut.BindKeyToApplication = false; - Assert.Contains (Key.A, shortcut.HotKeyBindings.Bindings.Keys); + Assert.True (shortcut.HotKeyBindings.TryGet (Key.A, out _)); Assert.Empty (Application.KeyBindings.GetBindings (Key.A)); }