Skip to content

Commit

Permalink
Make AnsiEscapeSequenceRequest agnostic of each driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
BDisp committed Oct 13, 2024
1 parent 409dac2 commit 9366998
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,58 +56,21 @@ public static bool TryExecuteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest,
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)
{
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
Console.Write (ansiRequest.Request);
Expand Down Expand Up @@ -156,18 +119,8 @@ public static bool TryExecuteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest,

if (savedIsReportingMouseMoves)
{
switch (Application.Driver)
{
case NetDriver:
NetEvents._suspendRead = false;
netDriver!.StartReportingMouseMoves ();

break;
case CursesDriver cursesDriver:
cursesDriver.StartReportingMouseMoves ();

break;
}
driver!.IsSuspendRead = false;
driver.StartReportingMouseMoves ();
}
}

Expand Down
20 changes: 20 additions & 0 deletions Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,16 @@ public virtual Attribute MakeColor (in Color foreground, in Color background)

#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;

Expand Down Expand Up @@ -608,6 +618,16 @@ public void OnMouseEvent (MouseEvent a)
/// <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
}

Expand Down
14 changes: 11 additions & 3 deletions Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class CursesDriver : ConsoleDriver
private MouseFlags _lastMouseFlags;
private UnixMainLoop _mainLoopDriver;
private object _processInputToken;
private bool _isSuspendRead;

public override int Cols
{
Expand Down Expand Up @@ -177,9 +178,16 @@ public override bool SetCursorVisibility (CursorVisibility visibility)
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)
{
Expand All @@ -189,7 +197,7 @@ public void StartReportingMouseMoves ()
}
}

public void StopReportingMouseMoves ()
public override void StopReportingMouseMoves ()
{
if (!RunningUnitTests)
{
Expand Down
22 changes: 22 additions & 0 deletions Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ public Behaviors (
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;
Expand Down Expand Up @@ -337,6 +351,8 @@ private KeyCode MapKey (ConsoleKeyInfo keyInfo)
}

private CursorVisibility _savedCursorVisibility;
private bool _isReportingMouseMoves;
private bool _isSuspendRead;

private void MockKeyPressedHandler (ConsoleKeyInfo consoleKeyInfo)
{
Expand Down Expand Up @@ -392,6 +408,12 @@ public override void SendKeys (char keyChar, ConsoleKey key, bool shift, bool al
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);
Expand Down
27 changes: 24 additions & 3 deletions Terminal.Gui/ConsoleDrivers/NetDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,7 @@ private bool SetCursorPosition (int col, int row)
}

private CursorVisibility? _cachedCursorVisibility;
private bool _isSuspendRead;

public override void UpdateCursor ()
{
Expand Down Expand Up @@ -1376,9 +1377,16 @@ public override bool EnsureCursorVisibility ()

#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)
{
Expand All @@ -1388,14 +1396,27 @@ public void StartReportingMouseMoves ()
}
}

public void StopReportingMouseMoves ()
public override void StopReportingMouseMoves ()
{
if (!RunningUnitTests)
{
Console.Out.Write (EscSeqUtils.CSI_DisableMouseEvents);

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 MouseEvent ToDriverMouse (NetEvents.MouseEvent me)
Expand Down
21 changes: 21 additions & 0 deletions Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,20 @@ public WindowsDriver ()

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)
Expand Down Expand Up @@ -1162,6 +1176,11 @@ public override void SendKeys (char keyChar, ConsoleKey key, bool shift, bool al
}
}

/// <inheritdoc />
public override void StartReportingMouseMoves () { throw new NotImplementedException (); }

/// <inheritdoc />
public override void StopReportingMouseMoves () { throw new NotImplementedException (); }

#region Not Implemented

Expand All @@ -1188,6 +1207,8 @@ public WindowsConsole.ConsoleKeyInfoEx ToConsoleKeyInfoEx (WindowsConsole.KeyEve
#region Cursor Handling

private CursorVisibility? _cachedCursorVisibility;
private bool _isReportingMouseMoves;
private bool _isSuspendRead;

public override void UpdateCursor ()
{
Expand Down

0 comments on commit 9366998

Please sign in to comment.