mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2026-01-01 16:59:35 +01:00
Make AnsiEscapeSequenceRequest agnostic of each driver.
This commit is contained in:
@@ -56,58 +56,21 @@ public class AnsiEscapeSequenceRequest
|
||||
var response = new StringBuilder ();
|
||||
var error = new StringBuilder ();
|
||||
var savedIsReportingMouseMoves = false;
|
||||
NetDriver? netDriver = null;
|
||||
ConsoleDriver? driver = null;
|
||||
var values = new string? [] { null };
|
||||
|
||||
try
|
||||
{
|
||||
switch (Application.Driver)
|
||||
driver = Application.Driver;
|
||||
|
||||
savedIsReportingMouseMoves = driver!.IsReportingMouseMoves;
|
||||
|
||||
if (savedIsReportingMouseMoves)
|
||||
{
|
||||
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;
|
||||
|
||||
break;
|
||||
case CursesDriver cursesDriver:
|
||||
savedIsReportingMouseMoves = cursesDriver.IsReportingMouseMoves;
|
||||
|
||||
if (savedIsReportingMouseMoves)
|
||||
{
|
||||
cursesDriver.StopReportingMouseMoves ();
|
||||
}
|
||||
|
||||
break;
|
||||
driver.StopReportingMouseMoves ();
|
||||
}
|
||||
|
||||
if (netDriver is { })
|
||||
{
|
||||
NetEvents._suspendRead = true;
|
||||
}
|
||||
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
|
||||
Console.Write (ansiRequest.Request);
|
||||
@@ -156,18 +119,8 @@ public class AnsiEscapeSequenceRequest
|
||||
|
||||
if (savedIsReportingMouseMoves)
|
||||
{
|
||||
switch (Application.Driver)
|
||||
{
|
||||
case NetDriver:
|
||||
NetEvents._suspendRead = false;
|
||||
netDriver!.StartReportingMouseMoves ();
|
||||
|
||||
break;
|
||||
case CursesDriver cursesDriver:
|
||||
cursesDriver.StartReportingMouseMoves ();
|
||||
|
||||
break;
|
||||
}
|
||||
driver!.IsSuspendRead = false;
|
||||
driver.StartReportingMouseMoves ();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -562,6 +562,16 @@ public abstract class ConsoleDriver
|
||||
|
||||
#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>
|
||||
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>
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ internal class CursesDriver : ConsoleDriver
|
||||
private MouseFlags _lastMouseFlags;
|
||||
private UnixMainLoop _mainLoopDriver;
|
||||
private object _processInputToken;
|
||||
private bool _isSuspendRead;
|
||||
|
||||
public override int Cols
|
||||
{
|
||||
@@ -177,9 +178,16 @@ internal class CursesDriver : ConsoleDriver
|
||||
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)
|
||||
{
|
||||
@@ -189,7 +197,7 @@ internal class CursesDriver : ConsoleDriver
|
||||
}
|
||||
}
|
||||
|
||||
public void StopReportingMouseMoves ()
|
||||
public override void StopReportingMouseMoves ()
|
||||
{
|
||||
if (!RunningUnitTests)
|
||||
{
|
||||
|
||||
@@ -40,6 +40,20 @@ public class FakeDriver : ConsoleDriver
|
||||
public static Behaviors FakeBehaviors = new ();
|
||||
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 ()
|
||||
{
|
||||
Cols = FakeConsole.WindowWidth = FakeConsole.BufferWidth = FakeConsole.WIDTH;
|
||||
@@ -337,6 +351,8 @@ public class FakeDriver : ConsoleDriver
|
||||
}
|
||||
|
||||
private CursorVisibility _savedCursorVisibility;
|
||||
private bool _isReportingMouseMoves;
|
||||
private bool _isSuspendRead;
|
||||
|
||||
private void MockKeyPressedHandler (ConsoleKeyInfo consoleKeyInfo)
|
||||
{
|
||||
@@ -392,6 +408,12 @@ public class FakeDriver : ConsoleDriver
|
||||
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)
|
||||
{
|
||||
FakeConsole.SetBufferSize (width, height);
|
||||
|
||||
@@ -1328,6 +1328,7 @@ internal class NetDriver : ConsoleDriver
|
||||
}
|
||||
|
||||
private CursorVisibility? _cachedCursorVisibility;
|
||||
private bool _isSuspendRead;
|
||||
|
||||
public override void UpdateCursor ()
|
||||
{
|
||||
@@ -1376,9 +1377,16 @@ internal class NetDriver : ConsoleDriver
|
||||
|
||||
#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)
|
||||
{
|
||||
@@ -1388,7 +1396,7 @@ internal class NetDriver : ConsoleDriver
|
||||
}
|
||||
}
|
||||
|
||||
public void StopReportingMouseMoves ()
|
||||
public override void StopReportingMouseMoves ()
|
||||
{
|
||||
if (!RunningUnitTests)
|
||||
{
|
||||
@@ -1396,6 +1404,19 @@ internal class NetDriver : ConsoleDriver
|
||||
|
||||
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)
|
||||
|
||||
@@ -1035,6 +1035,20 @@ internal class WindowsDriver : ConsoleDriver
|
||||
|
||||
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.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
|
||||
|
||||
@@ -1188,6 +1207,8 @@ internal class WindowsDriver : ConsoleDriver
|
||||
#region Cursor Handling
|
||||
|
||||
private CursorVisibility? _cachedCursorVisibility;
|
||||
private bool _isReportingMouseMoves;
|
||||
private bool _isSuspendRead;
|
||||
|
||||
public override void UpdateCursor ()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user