FakeInputProcessor.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 (IApplication? app, MouseEventArgs mouseEvent)
  25. {
  26. // FakeDriver uses ConsoleKeyInfo as its input record type, which cannot represent mouse events.
  27. // TODO: Verify this is correct. This didn't check the threadId before.
  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 (app is {} && app.MainThreadId != Thread.CurrentThread.ManagedThreadId)
  32. {
  33. // Application is running - use Invoke to defer to next iteration
  34. app?.Invoke ((_) => RaiseMouseEvent (mouseEvent));
  35. }
  36. else
  37. {
  38. // Not in Application context (unit tests) - raise immediately
  39. RaiseMouseEvent (mouseEvent);
  40. }
  41. }
  42. }