FakeInputProcessor.cs 1.7 KB

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