Browse Source

Got new button fn working on WindowsDriver; Now debugging netdriver and curses

Tig 1 year ago
parent
commit
1cca002393

+ 11 - 13
Terminal.Gui/Application.cs

@@ -1456,15 +1456,6 @@ public static partial class Application
         // TODO: In PR #3273, FindDeepestView will return adornments. Update logic below to fix adornment mouse handling
         // TODO: In PR #3273, FindDeepestView will return adornments. Update logic below to fix adornment mouse handling
         var view = View.FindDeepestView (Current, a.MouseEvent.X, a.MouseEvent.Y);
         var view = View.FindDeepestView (Current, a.MouseEvent.X, a.MouseEvent.Y);
 
 
-        if (view is { WantContinuousButtonPressed: true })
-        {
-            WantContinuousButtonPressedView = view;
-        }
-        else
-        {
-            WantContinuousButtonPressedView = null;
-        }
-
         if (view is { })
         if (view is { })
         {
         {
             a.MouseEvent.View = view;
             a.MouseEvent.View = view;
@@ -1494,10 +1485,7 @@ public static partial class Application
 
 
             if (MouseGrabView.Bounds.Contains (viewRelativeMouseEvent.X, viewRelativeMouseEvent.Y) is false)
             if (MouseGrabView.Bounds.Contains (viewRelativeMouseEvent.X, viewRelativeMouseEvent.Y) is false)
             {
             {
-                // The mouse has moved outside the bounds of the view that
-                // grabbed the mouse, so we tell the view that last got 
-                // OnMouseEnter the mouse is leaving
-                // BUGBUG: That sentence makes no sense. Either I'm missing something or this logic is flawed.
+                // The mouse has moved outside the bounds of the view that grabbed the mouse
                 _mouseEnteredView?.OnMouseLeave (a.MouseEvent);
                 _mouseEnteredView?.OnMouseLeave (a.MouseEvent);
             }
             }
 
 
@@ -1508,6 +1496,16 @@ public static partial class Application
             }
             }
         }
         }
 
 
+        if (view is { WantContinuousButtonPressed: true })
+        {
+            WantContinuousButtonPressedView = view;
+        }
+        else
+        {
+            WantContinuousButtonPressedView = null;
+        }
+
+
         if (view is not Adornment)
         if (view is not Adornment)
         {
         {
             if ((view is null || view == OverlappedTop)
             if ((view is null || view == OverlappedTop)

+ 6 - 0
Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

@@ -2,6 +2,7 @@
 // Driver.cs: Curses-based Driver
 // Driver.cs: Curses-based Driver
 //
 //
 
 
+using System.Diagnostics;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
 using Terminal.Gui.ConsoleDrivers;
 using Terminal.Gui.ConsoleDrivers;
 using Unix.Terminal;
 using Unix.Terminal;
@@ -798,6 +799,9 @@ internal class CursesDriver : ConsoleDriver
                    || flag.HasFlag (MouseFlags.Button4DoubleClicked);
                    || flag.HasFlag (MouseFlags.Button4DoubleClicked);
         }
         }
 
 
+        Debug.WriteLine ($"CursesDriver: ({pos.X},{pos.Y}) - {mouseFlag}");
+
+
         if ((WasButtonReleased (mouseFlag) && IsButtonNotPressed (_lastMouseFlags)) || (IsButtonClickedOrDoubleClicked (mouseFlag) && _lastMouseFlags == 0))
         if ((WasButtonReleased (mouseFlag) && IsButtonNotPressed (_lastMouseFlags)) || (IsButtonClickedOrDoubleClicked (mouseFlag) && _lastMouseFlags == 0))
         {
         {
             return;
             return;
@@ -806,6 +810,8 @@ internal class CursesDriver : ConsoleDriver
         _lastMouseFlags = mouseFlag;
         _lastMouseFlags = mouseFlag;
 
 
         var me = new MouseEvent { Flags = mouseFlag, X = pos.X, Y = pos.Y };
         var me = new MouseEvent { Flags = mouseFlag, X = pos.X, Y = pos.Y };
+        Debug.WriteLine ($"CursesDriver: ({me.X},{me.Y}) - {me.Flags}");
+
         OnMouseEvent (new MouseEventEventArgs (me));
         OnMouseEvent (new MouseEventEventArgs (me));
     }
     }
 
 

+ 2 - 2
Terminal.Gui/ConsoleDrivers/NetDriver.cs

@@ -1137,7 +1137,7 @@ internal class NetDriver : ConsoleDriver
                 break;
                 break;
             case EventType.Mouse:
             case EventType.Mouse:
                 MouseEvent me = ToDriverMouse (inputEvent.MouseEvent);
                 MouseEvent me = ToDriverMouse (inputEvent.MouseEvent);
-                //Debug.WriteLine ($"NetDriver: ({me.X},{me.Y}) - {me.Flags}");
+                Debug.WriteLine ($"NetDriver: ({me.X},{me.Y}) - {me.Flags}");
                 OnMouseEvent (new MouseEventEventArgs (me));
                 OnMouseEvent (new MouseEventEventArgs (me));
 
 
                 break;
                 break;
@@ -1379,7 +1379,7 @@ internal class NetDriver : ConsoleDriver
 
 
     private MouseEvent ToDriverMouse (NetEvents.MouseEvent me)
     private MouseEvent ToDriverMouse (NetEvents.MouseEvent me)
     {
     {
-       // System.Diagnostics.Debug.WriteLine ($"X: {me.Position.X}; Y: {me.Position.Y}; ButtonState: {me.ButtonState}");
+       //System.Diagnostics.Debug.WriteLine ($"X: {me.Position.X}; Y: {me.Position.Y}; ButtonState: {me.ButtonState}");
 
 
         MouseFlags mouseFlag = 0;
         MouseFlags mouseFlag = 0;
 
 

+ 23 - 6
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -1695,7 +1695,13 @@ internal class WindowsDriver : ConsoleDriver
 
 
     private async Task ProcessButtonDoubleClickedAsync ()
     private async Task ProcessButtonDoubleClickedAsync ()
     {
     {
-        await Task.Delay (300);
+        // Only delay if there's not a view wanting continuous button presses
+        if (Application.WantContinuousButtonPressedView is null)
+        {
+            // QUESTION: Why 300ms?
+            await Task.Delay (300);
+        }
+
         _isButtonDoubleClicked = false;
         _isButtonDoubleClicked = false;
         _isOneFingerDoubleClicked = false;
         _isOneFingerDoubleClicked = false;
 
 
@@ -1704,10 +1710,16 @@ internal class WindowsDriver : ConsoleDriver
 
 
     private async Task ProcessContinuousButtonPressedAsync (MouseFlags mouseFlag)
     private async Task ProcessContinuousButtonPressedAsync (MouseFlags mouseFlag)
     {
     {
+        // When a user presses-and-holds, start generating pressed events every `startDelay`
+        // After `iterationsUntilFast` iterations, speed them up to `fastDelay` ms
+        const int startDelay = 500;
+        const int iterationsUntilFast = 4;
+        const int fastDelay = 50;
+
+        int iterations = 0;
+        int delay = startDelay;
         while (_isButtonPressed)
         while (_isButtonPressed)
         {
         {
-            await Task.Delay (100);
-
             var me = new MouseEvent
             var me = new MouseEvent
             {
             {
                 X = _pointMove.X,
                 X = _pointMove.X,
@@ -1715,13 +1727,18 @@ internal class WindowsDriver : ConsoleDriver
                 Flags = mouseFlag
                 Flags = mouseFlag
             };
             };
 
 
-            View view = Application.WantContinuousButtonPressedView;
-
-            if (view is null)
+            if (Application.WantContinuousButtonPressedView is null)
             {
             {
                 break;
                 break;
             }
             }
 
 
+            if (iterations++ >= iterationsUntilFast)
+            {
+                delay = fastDelay;
+            }
+            await Task.Delay (delay);
+
+            //Debug.WriteLine($"ProcessContinuousButtonPressedAsync: {view}");
             if (_isButtonPressed && (mouseFlag & MouseFlags.ReportMousePosition) == 0)
             if (_isButtonPressed && (mouseFlag & MouseFlags.ReportMousePosition) == 0)
             {
             {
                 Application.Invoke (() => OnMouseEvent (new MouseEventEventArgs (me)));
                 Application.Invoke (() => OnMouseEvent (new MouseEventEventArgs (me)));

+ 35 - 25
Terminal.Gui/Views/Button.cs

@@ -67,52 +67,62 @@ public class Button : View
 
 
     private void Button_MouseEvent (object sender, MouseEventEventArgs e)
     private void Button_MouseEvent (object sender, MouseEventEventArgs e)
     {
     {
-        if (e.MouseEvent.Flags.HasFlag(MouseFlags.Button1Clicked))
+        // Default behavior is to invoke Accept (via HotKey) on clicked.
+        if (!WantContinuousButtonPressed &&
+            Application.MouseGrabView != this &&
+            e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked))
         {
         {
-            if (Application.MouseGrabView != this)
-            {
-                e.Handled = InvokeCommand (Command.HotKey) == true;
-
-                return;
-            }
+            e.Handled = InvokeCommand (Command.HotKey) == true;
+            return;
         }
         }
 
 
-        if (e.MouseEvent.Flags == MouseFlags.Button1Pressed)
+        if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
         {
         {
-            if (Application.MouseGrabView == this)
+            // If WantContinuousButtonPressed is true, and this is not the first pressed event,
+            // invoke Accept (via HotKey)
+            if (WantContinuousButtonPressed && Application.MouseGrabView == this)
             {
             {
                 e.Handled = InvokeCommand (Command.HotKey) == true;
                 e.Handled = InvokeCommand (Command.HotKey) == true;
                 return;
                 return;
             }
             }
 
 
-            SetFocus();
-            Application.GrabMouse(this);
+            // The first time we get pressed event, grab the mouse and invert the colors
+            if (Application.MouseGrabView != this)
+            {
+                Application.GrabMouse (this);
+                _savedColorScheme = ColorScheme;
+                var cs = new ColorScheme (new Attribute (ColorScheme.Normal.Background, ColorScheme.Normal.Foreground));
+                ColorScheme = cs;
 
 
-            _savedColorScheme = ColorScheme;
-            var cs = new ColorScheme (new Attribute (ColorScheme.Normal.Background, ColorScheme.Normal.Foreground));
-            ColorScheme = cs;
+                // Set the focus, but don't invoke Accept
+                SetFocus ();
+            }
         }
         }
 
 
-        if (e.MouseEvent.Flags.HasFlag(MouseFlags.Button1Released))
+        if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Released))
         {
         {
-            Application.UngrabMouse ();
-
-            e.Handled = InvokeCommand (Command.HotKey) == true;
-
-            if (_savedColorScheme is { })
+            // When the mouse is released, if WantContinuousButtonPressed is set, invoke Accept one last time.
+            if (WantContinuousButtonPressed)
             {
             {
-                ColorScheme = _savedColorScheme;
+                e.Handled = InvokeCommand (Command.HotKey) == true;
             }
             }
 
 
-            _savedColorScheme = null;
+            if (Application.MouseGrabView == this)
+            {
+                Application.UngrabMouse ();
+                if (_savedColorScheme is { })
+                {
+                    ColorScheme = _savedColorScheme;
+                    _savedColorScheme = null;
+                }
+            }
         }
         }
     }
     }
 
 
     /// <inheritdoc />
     /// <inheritdoc />
-    public override bool OnLeave (View view)
+    protected internal override bool OnMouseLeave (MouseEvent e)
     {
     {
-        //Application.UngrabMouse();
-        return base.OnLeave (view);
+        return base.OnMouseLeave (e);
     }
     }
 
 
     private void Button_MouseClick (object sender, MouseEventEventArgs e)
     private void Button_MouseClick (object sender, MouseEventEventArgs e)

+ 13 - 2
UICatalog/Scenarios/Buttons.cs

@@ -396,7 +396,7 @@ public class Buttons : Scenario
         {
         {
             X = Pos.Right (label) + 1,
             X = Pos.Right (label) + 1,
             Y = Pos.Top (label),
             Y = Pos.Top (label),
-            Title = $"Accept Count (press-and-hold): {acceptCount}",
+            Title = $"Accept Count: {acceptCount}",
             WantContinuousButtonPressed = true,
             WantContinuousButtonPressed = true,
         };
         };
         repeatButton.Accept += (s, e) =>
         repeatButton.Accept += (s, e) =>
@@ -404,7 +404,18 @@ public class Buttons : Scenario
                                    repeatButton.Title = $"Accept Count: {++acceptCount}";
                                    repeatButton.Title = $"Accept Count: {++acceptCount}";
                                };
                                };
 
 
-        main.Add(label, repeatButton);
+        var enableCB = new CheckBox ()
+        {
+            X = Pos.Right (repeatButton) + 1,
+            Y = Pos.Top (repeatButton),
+            Title = "Enabled",
+            Checked = true,
+        };
+        enableCB.Toggled += (s, e) =>
+                            {
+                                repeatButton.Enabled = !repeatButton.Enabled;
+                            };
+        main.Add(label, repeatButton, enableCB);
 
 
         main.Ready += (s, e) => radioGroup.Refresh ();
         main.Ready += (s, e) => radioGroup.Refresh ();
         Application.Run (main);
         Application.Run (main);