Browse Source

Merge pull request #3378 from tig/v2_3377-ASCIICustomButtonTest-NetDriver

Fixes #3377. `NetDriver` not rendering menu correctly
Tig 1 year ago
parent
commit
875282818a

+ 1 - 1
Terminal.Gui/ConsoleDrivers/EscSeqUtils/EscSeqUtils.cs

@@ -775,7 +775,7 @@ public static class EscSeqUtils
             lastMouseButtonPressed = buttonState;
             isButtonPressed = true;
 
-            point ??= pos;
+            point = pos;
 
             if ((mouseFlags [0] & MouseFlags.ReportMousePosition) == 0)
             {

+ 49 - 24
Terminal.Gui/ConsoleDrivers/NetDriver.cs

@@ -874,7 +874,7 @@ internal class NetDriver : ConsoleDriver
         int rows = Rows;
         int cols = Cols;
         var output = new StringBuilder ();
-        var redrawAttr = new Attribute ();
+        Attribute? redrawAttr = null;
         int lastCol = -1;
 
         CursorVisibility? savedVisibitity = _cachedCursorVisibility;
@@ -1583,19 +1583,56 @@ internal class NetDriver : ConsoleDriver
                 return MapToKeyCodeModifiers (keyInfo.Modifiers & ~ConsoleModifiers.Shift, (KeyCode)keyInfo.KeyChar);
         }
 
-        ConsoleKey key = keyInfo.Key;
+        // Handle control keys whose VK codes match the related ASCII value (those below ASCII 33) like ESC
+        if (keyInfo.Key != ConsoleKey.None && Enum.IsDefined (typeof (KeyCode), (uint)keyInfo.Key))
+        {
+            if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control) && keyInfo.Key == ConsoleKey.I)
+            {
+                return KeyCode.Tab;
+            }
+
+            if (keyInfo.Key == ConsoleKey.Tab)
+            {
+                return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.Key));
+            }
+        }
 
-        // A..Z are special cased:
-        // - Alone, they represent lowercase a...z
-        // - With ShiftMask they are A..Z
-        // - If CapsLock is on the above is reversed.
-        // - If Alt and/or Ctrl are present, treat as upper case
-        if (keyInfo.Key is >= ConsoleKey.A and <= ConsoleKey.Z)
+        // Handle control keys (e.g. CursorUp)
+        if (keyInfo.Key != ConsoleKey.None
+            && Enum.IsDefined (typeof (KeyCode), (uint)keyInfo.Key + (uint)KeyCode.MaxCodePoint))
+        {
+            return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.Key + (uint)KeyCode.MaxCodePoint));
+        }
+
+        if (((ConsoleKey)keyInfo.KeyChar) is >= ConsoleKey.A and <= ConsoleKey.Z)
+        {
+            // Shifted
+            keyInfo = new ConsoleKeyInfo (
+                                          keyInfo.KeyChar,
+                                          (ConsoleKey)keyInfo.KeyChar,
+                                          true,
+                                          keyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt),
+                                          keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control));
+        }
+
+        if ((ConsoleKey)keyInfo.KeyChar - 32 is >= ConsoleKey.A and <= ConsoleKey.Z)
+        {
+            // Unshifted
+            keyInfo = new ConsoleKeyInfo (
+                                          keyInfo.KeyChar,
+                                          (ConsoleKey)(keyInfo.KeyChar - 32),
+                                          false,
+                                          keyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt),
+                                          keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control));
+        }
+
+        if (keyInfo.Key is >= ConsoleKey.A and <= ConsoleKey.Z )
         {
             if (keyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt)
                 || keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control))
             {
-                return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)(uint)keyInfo.Key);
+                // NetDriver doesn't support Shift-Ctrl/Shift-Alt combos
+                return MapToKeyCodeModifiers (keyInfo.Modifiers & ~ConsoleModifiers.Shift, (KeyCode)keyInfo.Key);
             }
 
             if (keyInfo.Modifiers == ConsoleModifiers.Shift)
@@ -1603,27 +1640,15 @@ internal class NetDriver : ConsoleDriver
                 // If ShiftMask is on  add the ShiftMask
                 if (char.IsUpper (keyInfo.KeyChar))
                 {
-                    return (KeyCode)(uint)keyInfo.Key | KeyCode.ShiftMask;
+                    return (KeyCode)keyInfo.Key | KeyCode.ShiftMask;
                 }
             }
 
-            return (KeyCode)keyInfo.KeyChar;
-        }
-
-        // Handle control keys whose VK codes match the related ASCII value (those below ASCII 33) like ESC
-        if (keyInfo.Key != ConsoleKey.None && Enum.IsDefined (typeof (KeyCode), (uint)keyInfo.Key))
-        {
-            return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)keyInfo.Key);
+            return (KeyCode)keyInfo.Key;
         }
 
-        // Handle control keys (e.g. CursorUp)
-        if (keyInfo.Key != ConsoleKey.None
-            && Enum.IsDefined (typeof (KeyCode), (uint)keyInfo.Key + (uint)KeyCode.MaxCodePoint))
-        {
-            return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.Key + (uint)KeyCode.MaxCodePoint));
-        }
 
-        return (KeyCode)keyInfo.KeyChar;
+        return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.Key));
     }
 
     #endregion Keyboard Handling

+ 0 - 1
Terminal.Gui/Views/Menu/MenuBar.cs

@@ -479,7 +479,6 @@ public class MenuBar : View
             Driver.AddRune ((Rune)' ');
         }
 
-        Move (1, 0);
         var pos = 0;
 
         for (var i = 0; i < Menus.Length; i++)

+ 28 - 17
UICatalog/Scenarios/ASCIICustomButton.cs

@@ -14,17 +14,19 @@ public class ASCIICustomButtonTest : Scenario
     private MenuItem _miSmallerWindow;
     private ScrollViewTestWindow _scrollViewTestWindow;
 
-    public override void Init ()
+    public override void Main ()
     {
+        _smallerWindow = false;
+
         Application.Init ();
-        _scrollViewTestWindow = new ScrollViewTestWindow ();
+        Toplevel top = new ();
 
         var menu = new MenuBar
         {
             Menus =
             [
                 new MenuBarItem (
-                                 "Window Size",
+                                 "_Window Size",
                                  new []
                                  {
                                      _miSmallerWindow =
@@ -50,20 +52,24 @@ public class ASCIICustomButtonTest : Scenario
                                 )
             ]
         };
-        Top = new ();
-        Top.Add (menu, _scrollViewTestWindow);
-        Application.Run (Top);
-    }
-
-    public override void Run () { }
 
-    private void ChangeWindowSize ()
-    {
-        _smallerWindow = (bool)(_miSmallerWindow.Checked = !_miSmallerWindow.Checked);
-        _scrollViewTestWindow.Dispose ();
-        Top.Remove (_scrollViewTestWindow);
         _scrollViewTestWindow = new ScrollViewTestWindow ();
-        Top.Add (_scrollViewTestWindow);
+
+        top.Add (menu, _scrollViewTestWindow);
+        Application.Run (top);
+        top.Dispose ();
+
+        return;
+
+        void ChangeWindowSize ()
+        {
+            _smallerWindow = (bool)(_miSmallerWindow.Checked = !_miSmallerWindow.Checked);
+            top.Remove (_scrollViewTestWindow);
+            _scrollViewTestWindow.Dispose ();
+
+            _scrollViewTestWindow = new ScrollViewTestWindow ();
+            top.Add (_scrollViewTestWindow);
+        }
     }
 
     public class ASCIICustomButton : Button
@@ -199,7 +205,7 @@ public class ASCIICustomButtonTest : Scenario
                     Width = BUTTON_WIDTH,
                     Height = BUTTON_HEIGHT
                 };
-                button.CustomInitialize ();
+                button.Initialized += Button_Initialized;
                 button.Accept += Button_Clicked;
                 button.PointerEnter += Button_PointerEnter;
                 button.MouseClick += Button_MouseClick;
@@ -218,7 +224,7 @@ public class ASCIICustomButtonTest : Scenario
                 Width = BUTTON_WIDTH,
                 Height = BUTTON_HEIGHT
             };
-            closeButton.CustomInitialize ();
+            closeButton.Initialized += Button_Initialized;
             closeButton.Accept += Button_Clicked;
             closeButton.PointerEnter += Button_PointerEnter;
             closeButton.MouseClick += Button_MouseClick;
@@ -244,6 +250,11 @@ public class ASCIICustomButtonTest : Scenario
                 Add (titleLabel, _scrollView);
             }
         }
+        private void Button_Initialized (object sender, EventArgs e)
+        {
+            var button = sender as ASCIICustomButton;
+            button?.CustomInitialize ();
+        }
 
         private void Button_Clicked (object sender, EventArgs e)
         {

+ 2 - 2
UICatalog/UICatalog.cs

@@ -897,7 +897,7 @@ internal class UICatalogApp
 
             miIsMenuBorderDisabled.Shortcut =
                 (KeyCode)new Key (miIsMenuBorderDisabled!.Title!.Substring (14, 1) [0]).WithAlt
-                                                                                       .WithCtrl;
+                                                                                       .WithCtrl.NoShift;
             miIsMenuBorderDisabled.CheckType |= MenuItemCheckStyle.Checked;
 
             miIsMenuBorderDisabled.Action += () =>
@@ -919,7 +919,7 @@ internal class UICatalogApp
             miIsMouseDisabled = new () { Title = "_Disable Mouse" };
 
             miIsMouseDisabled.Shortcut =
-                (KeyCode)new Key (miIsMouseDisabled!.Title!.Substring (1, 1) [0]).WithAlt.WithCtrl;
+                (KeyCode)new Key (miIsMouseDisabled!.Title!.Substring (1, 1) [0]).WithAlt.WithCtrl.NoShift;
             miIsMouseDisabled.CheckType |= MenuItemCheckStyle.Checked;
 
             miIsMouseDisabled.Action += () =>