Browse Source

Fixes #4147. HotKeySpecifier 0xFFFF does not clear HotKey (#4508)

* Initial plan

* Fix: Set HotKey to Key.Empty when HotKeySpecifier is 0xFFFF

Co-authored-by: tig <[email protected]>

* Fix code style: Use explicit type instead of var for View

Co-authored-by: tig <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: tig <[email protected]>
Copilot 9 hours ago
parent
commit
8f4ad8a7d4

+ 1 - 0
Terminal.Gui/ViewBase/View.Keyboard.cs

@@ -225,6 +225,7 @@ public partial class View // Keyboard APIs
     {
         if (HotKeySpecifier == new Rune ('\xFFFF'))
         {
+            HotKey = Key.Empty;
             return; // throw new InvalidOperationException ("Can't set HotKey unless a TextFormatter has been created");
         }
 

+ 39 - 0
Tests/UnitTestsParallelizable/ViewBase/Keyboard/HotKeyTests.cs

@@ -373,4 +373,43 @@ public class HotKeyTests
         Assert.Equal ("", view.Title);
         Assert.Equal (KeyCode.Null, view.HotKey);
     }
+
+    [Fact]
+    public void HotKeySpecifier_0xFFFF_Clears_HotKey ()
+    {
+        // Arrange: Create a view with a hotkey
+        View view = new () { HotKeySpecifier = (Rune)'_', Title = "_Test" };
+        Assert.Equal (KeyCode.T, view.HotKey);
+
+        // Act: Set HotKeySpecifier to 0xFFFF
+        view.HotKeySpecifier = (Rune)0xFFFF;
+
+        // Assert: HotKey should be cleared
+        Assert.Equal (KeyCode.Null, view.HotKey);
+    }
+
+    [Fact]
+    public void HotKeySpecifier_0xFFFF_Before_Title_Set_Prevents_HotKey ()
+    {
+        // Arrange & Act: Set HotKeySpecifier to 0xFFFF before setting Title
+        View view = new () { HotKeySpecifier = (Rune)0xFFFF };
+        view.Title = "_Test";
+
+        // Assert: HotKey should remain empty
+        Assert.Equal (KeyCode.Null, view.HotKey);
+    }
+
+    [Fact]
+    public void HotKeySpecifier_0xFFFF_With_Underscore_In_Title ()
+    {
+        // Arrange & Act: This is the scenario from the bug report
+        View view = new ()
+        {
+            HotKeySpecifier = (Rune)0xFFFF,
+            Title = "my label with an _underscore"
+        };
+
+        // Assert: HotKey should be empty (no hotkey should be set)
+        Assert.Equal (KeyCode.Null, view.HotKey);
+    }
 }