MouseUpdateControllerSession.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System.Diagnostics;
  2. using System.Threading;
  3. using Avalonia;
  4. using Avalonia.Input;
  5. using Avalonia.Threading;
  6. namespace PixiEditor.AvaloniaUI.Models.Controllers.InputDevice;
  7. #nullable enable
  8. internal class MouseUpdateControllerSession : IDisposable
  9. {
  10. private const double IntervalMs = 1000 / 142.0; //142 Hz
  11. private readonly Action onStartListening;
  12. private readonly Action onStopListening;
  13. private readonly Action<PointerEventArgs> onMouseMove;
  14. private readonly AutoResetEvent resetEvent = new(false);
  15. private readonly object lockObj = new();
  16. /// <summary>
  17. /// <see cref="MouseUpdateControllerSession"/> doesn't rely on attaching and detaching mouse move handler,
  18. /// it just ignores mouse move events when not listening. <br/>
  19. /// Yet it still calls <see cref="onStartListening"/> and <see cref="onStopListening"/> which can be used to attach and detach event handler elsewhere.
  20. /// </summary>
  21. private bool isListening = true;
  22. private bool isDisposed = false;
  23. public MouseUpdateControllerSession(Action onStartListening, Action onStopListening, Action<PointerEventArgs> onMouseMove)
  24. {
  25. this.onStartListening = onStartListening;
  26. this.onStopListening = onStopListening;
  27. this.onMouseMove = onMouseMove;
  28. Thread timerThread = new(TimerLoop)
  29. {
  30. IsBackground = true, Name = "MouseUpdateController thread"
  31. };
  32. timerThread.Start();
  33. onStartListening();
  34. }
  35. public void MouseMoveInput(PointerEventArgs e)
  36. {
  37. if (!isListening || isDisposed)
  38. return;
  39. bool lockWasTaken = false;
  40. try
  41. {
  42. Monitor.TryEnter(lockObj, ref lockWasTaken);
  43. if (lockWasTaken)
  44. {
  45. isListening = false;
  46. onStopListening();
  47. onMouseMove(e);
  48. resetEvent.Set();
  49. }
  50. }
  51. finally
  52. {
  53. if (lockWasTaken)
  54. Monitor.Exit(lockObj);
  55. }
  56. }
  57. public void Dispose()
  58. {
  59. isDisposed = true;
  60. resetEvent.Dispose();
  61. }
  62. private void TimerLoop()
  63. {
  64. try
  65. {
  66. long lastThreadIter = Stopwatch.GetTimestamp();
  67. while (!isDisposed)
  68. {
  69. // call waitOne periodically instead of waiting infinitely to make sure we crash or exit when resetEvent is disposed
  70. if (!resetEvent.WaitOne(300))
  71. {
  72. lastThreadIter = Stopwatch.GetTimestamp();
  73. continue;
  74. }
  75. lock (lockObj)
  76. {
  77. double sleepDur = Math.Clamp(IntervalMs - Stopwatch.GetElapsedTime(lastThreadIter).TotalMilliseconds, 0, IntervalMs);
  78. lastThreadIter += (long)(IntervalMs * Stopwatch.Frequency / 1000);
  79. if (sleepDur > 0)
  80. Thread.Sleep((int)Math.Round(sleepDur));
  81. if (isDisposed)
  82. return;
  83. isListening = true;
  84. Dispatcher.UIThread.Invoke(() =>
  85. {
  86. if (!isDisposed)
  87. onStartListening();
  88. });
  89. }
  90. }
  91. }
  92. catch (ObjectDisposedException)
  93. {
  94. return;
  95. }
  96. catch (Exception e)
  97. {
  98. Dispatcher.UIThread.Post(() => throw new AggregateException("Input handling thread died", e), DispatcherPriority.SystemIdle);
  99. throw;
  100. }
  101. }
  102. }