Browse Source

Make AnsiEscapeSequenceRequest agnostic of each driver.

BDisp 9 months ago
parent
commit
e5c30eb442

+ 8 - 55
Terminal.Gui/ConsoleDrivers/AnsiEscapeSequence/AnsiEscapeSequenceRequest.cs

@@ -56,58 +56,21 @@ public class AnsiEscapeSequenceRequest
         var response = new StringBuilder ();
         var response = new StringBuilder ();
         var error = new StringBuilder ();
         var error = new StringBuilder ();
         var savedIsReportingMouseMoves = false;
         var savedIsReportingMouseMoves = false;
-        NetDriver? netDriver = null;
+        ConsoleDriver? driver = null;
         var values = new string? [] { null };
         var values = new string? [] { null };
 
 
         try
         try
         {
         {
-            switch (Application.Driver)
-            {
-                case NetDriver:
-                    netDriver = Application.Driver as NetDriver;
-                    savedIsReportingMouseMoves = netDriver!.IsReportingMouseMoves;
-
-                    if (savedIsReportingMouseMoves)
-                    {
-                        netDriver.StopReportingMouseMoves ();
-                    }
-
-                    while (Console.KeyAvailable)
-                    {
-                        netDriver._mainLoopDriver._netEvents._waitForStart.Set ();
-                        netDriver._mainLoopDriver._netEvents._waitForStart.Reset ();
-
-                        netDriver._mainLoopDriver._netEvents._forceRead = true;
-                    }
-
-                    netDriver._mainLoopDriver._netEvents._forceRead = false;
+            driver = Application.Driver;
 
 
-                    break;
-                case CursesDriver cursesDriver:
-                    savedIsReportingMouseMoves = cursesDriver.IsReportingMouseMoves;
-
-                    if (savedIsReportingMouseMoves)
-                    {
-                        cursesDriver.StopReportingMouseMoves ();
-                    }
-
-                    break;
-            }
+            savedIsReportingMouseMoves = driver!.IsReportingMouseMoves;
 
 
-            if (netDriver is { })
+            if (savedIsReportingMouseMoves)
             {
             {
-                NetEvents._suspendRead = true;
+                driver.StopReportingMouseMoves ();
             }
             }
-            else
-            {
-                Thread.Sleep (100); // Allow time for mouse stopping and to flush the input buffer
 
 
-                // Flush the input buffer to avoid reading stale input
-                while (Console.KeyAvailable)
-                {
-                    Console.ReadKey (true);
-                }
-            }
+            driver!.IsSuspendRead = true;
 
 
             // Send the ANSI escape sequence
             // Send the ANSI escape sequence
             Console.Write (ansiRequest.Request);
             Console.Write (ansiRequest.Request);
@@ -156,18 +119,8 @@ public class AnsiEscapeSequenceRequest
 
 
             if (savedIsReportingMouseMoves)
             if (savedIsReportingMouseMoves)
             {
             {
-                switch (Application.Driver)
-                {
-                    case NetDriver:
-                        NetEvents._suspendRead = false;
-                        netDriver!.StartReportingMouseMoves ();
-
-                        break;
-                    case CursesDriver cursesDriver:
-                        cursesDriver.StartReportingMouseMoves ();
-
-                        break;
-                }
+                driver!.IsSuspendRead = false;
+                driver.StartReportingMouseMoves ();
             }
             }
         }
         }
 
 

+ 20 - 0
Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs

@@ -562,6 +562,16 @@ public abstract class ConsoleDriver
 
 
     #region Mouse and Keyboard
     #region Mouse and Keyboard
 
 
+    /// <summary>
+    /// Gets whether the mouse is reporting move events.
+    /// </summary>
+    public abstract bool IsReportingMouseMoves { get; internal set; }
+
+    /// <summary>
+    /// Gets whether the terminal is reading input.
+    /// </summary>
+    public abstract bool IsSuspendRead { get; internal set; }
+
     /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="KeyUp"/>.</summary>
     /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="KeyUp"/>.</summary>
     public event EventHandler<Key>? KeyDown;
     public event EventHandler<Key>? KeyDown;
 
 
@@ -608,6 +618,16 @@ public abstract class ConsoleDriver
     /// <param name="ctrl">If <see langword="true"/> simulates the Ctrl key being pressed.</param>
     /// <param name="ctrl">If <see langword="true"/> simulates the Ctrl key being pressed.</param>
     public abstract void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool ctrl);
     public abstract void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool ctrl);
 
 
+    /// <summary>
+    /// Provide handling for the terminal start reporting mouse events.
+    /// </summary>
+    public abstract void StartReportingMouseMoves ();
+
+    /// <summary>
+    /// Provide handling for the terminal stop reporting mouse events.
+    /// </summary>
+    public abstract void StopReportingMouseMoves ();
+
     #endregion
     #endregion
 }
 }
 
 

+ 11 - 3
Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

@@ -18,6 +18,7 @@ internal class CursesDriver : ConsoleDriver
     private MouseFlags _lastMouseFlags;
     private MouseFlags _lastMouseFlags;
     private UnixMainLoop _mainLoopDriver;
     private UnixMainLoop _mainLoopDriver;
     private object _processInputToken;
     private object _processInputToken;
+    private bool _isSuspendRead;
 
 
     public override int Cols
     public override int Cols
     {
     {
@@ -177,9 +178,16 @@ internal class CursesDriver : ConsoleDriver
         return true;
         return true;
     }
     }
 
 
-    public bool IsReportingMouseMoves { get; private set; }
+    public override bool IsReportingMouseMoves { get; internal set; }
 
 
-    public void StartReportingMouseMoves ()
+    /// <inheritdoc />
+    public override bool IsSuspendRead
+    {
+        get => _isSuspendRead;
+        internal set => _isSuspendRead = value;
+    }
+
+    public override void StartReportingMouseMoves ()
     {
     {
         if (!RunningUnitTests)
         if (!RunningUnitTests)
         {
         {
@@ -189,7 +197,7 @@ internal class CursesDriver : ConsoleDriver
         }
         }
     }
     }
 
 
-    public void StopReportingMouseMoves ()
+    public override void StopReportingMouseMoves ()
     {
     {
         if (!RunningUnitTests)
         if (!RunningUnitTests)
         {
         {

+ 22 - 0
Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs

@@ -40,6 +40,20 @@ public class FakeDriver : ConsoleDriver
     public static Behaviors FakeBehaviors = new ();
     public static Behaviors FakeBehaviors = new ();
     public override bool SupportsTrueColor => false;
     public override bool SupportsTrueColor => false;
 
 
+    /// <inheritdoc />
+    public override bool IsReportingMouseMoves
+    {
+        get => _isReportingMouseMoves;
+        internal set => _isReportingMouseMoves = value;
+    }
+
+    /// <inheritdoc />
+    public override bool IsSuspendRead
+    {
+        get => _isSuspendRead;
+        internal set => _isSuspendRead = value;
+    }
+
     public FakeDriver ()
     public FakeDriver ()
     {
     {
         Cols = FakeConsole.WindowWidth = FakeConsole.BufferWidth = FakeConsole.WIDTH;
         Cols = FakeConsole.WindowWidth = FakeConsole.BufferWidth = FakeConsole.WIDTH;
@@ -337,6 +351,8 @@ public class FakeDriver : ConsoleDriver
     }
     }
 
 
     private CursorVisibility _savedCursorVisibility;
     private CursorVisibility _savedCursorVisibility;
+    private bool _isReportingMouseMoves;
+    private bool _isSuspendRead;
 
 
     private void MockKeyPressedHandler (ConsoleKeyInfo consoleKeyInfo)
     private void MockKeyPressedHandler (ConsoleKeyInfo consoleKeyInfo)
     {
     {
@@ -392,6 +408,12 @@ public class FakeDriver : ConsoleDriver
         MockKeyPressedHandler (new ConsoleKeyInfo (keyChar, key, shift, alt, control));
         MockKeyPressedHandler (new ConsoleKeyInfo (keyChar, key, shift, alt, control));
     }
     }
 
 
+    /// <inheritdoc />
+    public override void StartReportingMouseMoves () { throw new NotImplementedException (); }
+
+    /// <inheritdoc />
+    public override void StopReportingMouseMoves () { throw new NotImplementedException (); }
+
     public void SetBufferSize (int width, int height)
     public void SetBufferSize (int width, int height)
     {
     {
         FakeConsole.SetBufferSize (width, height);
         FakeConsole.SetBufferSize (width, height);

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

@@ -1328,6 +1328,7 @@ internal class NetDriver : ConsoleDriver
     }
     }
 
 
     private CursorVisibility? _cachedCursorVisibility;
     private CursorVisibility? _cachedCursorVisibility;
+    private bool _isSuspendRead;
 
 
     public override void UpdateCursor ()
     public override void UpdateCursor ()
     {
     {
@@ -1376,9 +1377,16 @@ internal class NetDriver : ConsoleDriver
 
 
     #region Mouse Handling
     #region Mouse Handling
 
 
-    public bool IsReportingMouseMoves { get; private set; }
+    public override bool IsReportingMouseMoves { get; internal set; }
 
 
-    public void StartReportingMouseMoves ()
+    /// <inheritdoc />
+    public override bool IsSuspendRead
+    {
+        get => _isSuspendRead;
+        internal set => _isSuspendRead = _suspendRead = value;
+    }
+
+    public override void StartReportingMouseMoves ()
     {
     {
         if (!RunningUnitTests)
         if (!RunningUnitTests)
         {
         {
@@ -1388,7 +1396,7 @@ internal class NetDriver : ConsoleDriver
         }
         }
     }
     }
 
 
-    public void StopReportingMouseMoves ()
+    public override void StopReportingMouseMoves ()
     {
     {
         if (!RunningUnitTests)
         if (!RunningUnitTests)
         {
         {
@@ -1396,6 +1404,19 @@ internal class NetDriver : ConsoleDriver
 
 
             IsReportingMouseMoves = false;
             IsReportingMouseMoves = false;
         }
         }
+
+        while (_mainLoopDriver is { _netEvents: { }} && Console.KeyAvailable)
+        {
+            _mainLoopDriver._netEvents._waitForStart.Set ();
+            _mainLoopDriver._netEvents._waitForStart.Reset ();
+
+            _mainLoopDriver._netEvents._forceRead = true;
+        }
+
+        if (_mainLoopDriver is { _netEvents: { } })
+        {
+            _mainLoopDriver._netEvents._forceRead = false;
+        }
     }
     }
 
 
     private MouseEventArgs ToDriverMouse (NetEvents.MouseEvent me)
     private MouseEventArgs ToDriverMouse (NetEvents.MouseEvent me)

+ 21 - 0
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -1035,6 +1035,20 @@ internal class WindowsDriver : ConsoleDriver
 
 
     public override bool SupportsTrueColor => RunningUnitTests || (Environment.OSVersion.Version.Build >= 14931 && _isWindowsTerminal);
     public override bool SupportsTrueColor => RunningUnitTests || (Environment.OSVersion.Version.Build >= 14931 && _isWindowsTerminal);
 
 
+    /// <inheritdoc />
+    public override bool IsReportingMouseMoves
+    {
+        get => _isReportingMouseMoves;
+        internal set => _isReportingMouseMoves = value;
+    }
+
+    /// <inheritdoc />
+    public override bool IsSuspendRead
+    {
+        get => _isSuspendRead;
+        internal set => _isSuspendRead = value;
+    }
+
     public WindowsConsole WinConsole { get; private set; }
     public WindowsConsole WinConsole { get; private set; }
 
 
     public WindowsConsole.KeyEventRecord FromVKPacketToKeyEventRecord (WindowsConsole.KeyEventRecord keyEvent)
     public WindowsConsole.KeyEventRecord FromVKPacketToKeyEventRecord (WindowsConsole.KeyEventRecord keyEvent)
@@ -1162,6 +1176,11 @@ internal class WindowsDriver : ConsoleDriver
         }
         }
     }
     }
 
 
+    /// <inheritdoc />
+    public override void StartReportingMouseMoves () { throw new NotImplementedException (); }
+
+    /// <inheritdoc />
+    public override void StopReportingMouseMoves () { throw new NotImplementedException (); }
 
 
     #region Not Implemented
     #region Not Implemented
 
 
@@ -1188,6 +1207,8 @@ internal class WindowsDriver : ConsoleDriver
     #region Cursor Handling
     #region Cursor Handling
 
 
     private CursorVisibility? _cachedCursorVisibility;
     private CursorVisibility? _cachedCursorVisibility;
+    private bool _isReportingMouseMoves;
+    private bool _isSuspendRead;
 
 
     public override void UpdateCursor ()
     public override void UpdateCursor ()
     {
     {