FakeInputProcessor.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System.Collections.Concurrent;
  2. namespace Terminal.Gui.Drivers;
  3. /// <summary>
  4. /// Input processor for <see cref="FakeInput"/>, deals in <see cref="ConsoleKeyInfo"/> stream
  5. /// </summary>
  6. public class FakeInputProcessor : InputProcessorImpl<ConsoleKeyInfo>
  7. {
  8. /// <inheritdoc/>
  9. public FakeInputProcessor (ConcurrentQueue<ConsoleKeyInfo> inputBuffer) : base (inputBuffer, new NetKeyConverter ())
  10. {
  11. DriverName = "fake";
  12. }
  13. /// <inheritdoc/>
  14. protected override void Process (ConsoleKeyInfo input)
  15. {
  16. Logging.Trace ($"input: {input.KeyChar}");
  17. foreach (Tuple<char, ConsoleKeyInfo> released in Parser.ProcessInput (Tuple.Create (input.KeyChar, input)))
  18. {
  19. Logging.Trace($"released: {released.Item1}");
  20. ProcessAfterParsing (released.Item2);
  21. }
  22. }
  23. /// <inheritdoc />
  24. public override void EnqueueMouseEvent (MouseEventArgs mouseEvent)
  25. {
  26. // FakeDriver uses ConsoleKeyInfo as its input record type, which cannot represent mouse events.
  27. // If Application.Invoke is available (running in Application context), defer to next iteration
  28. // to ensure proper timing - the event is raised after views are laid out.
  29. // Otherwise (unit tests), raise immediately so tests can verify synchronously.
  30. if (Application.MainThreadId is { })
  31. {
  32. // Application is running - use Invoke to defer to next iteration
  33. Application.Invoke (() => RaiseMouseEvent (mouseEvent));
  34. }
  35. else
  36. {
  37. // Not in Application context (unit tests) - raise immediately
  38. RaiseMouseEvent (mouseEvent);
  39. }
  40. }
  41. }