DispatcherPriority.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. namespace Avalonia.Threading
  3. {
  4. /// <summary>
  5. /// Defines the priorities with which jobs can be invoked on a <see cref="Dispatcher"/>.
  6. /// </summary>
  7. public readonly struct DispatcherPriority : IEquatable<DispatcherPriority>, IComparable<DispatcherPriority>
  8. {
  9. /// <summary>
  10. /// The integer value of the priority
  11. /// </summary>
  12. public int Value { get; }
  13. private DispatcherPriority(int value)
  14. {
  15. Value = value;
  16. }
  17. /// <summary>
  18. /// Minimum possible priority
  19. /// </summary>
  20. public static readonly DispatcherPriority MinValue = new(0);
  21. /// <summary>
  22. /// The job will be processed when the system is idle.
  23. /// </summary>
  24. [Obsolete("WPF compatibility")] public static readonly DispatcherPriority SystemIdle = MinValue;
  25. /// <summary>
  26. /// The job will be processed when the application is idle.
  27. /// </summary>
  28. [Obsolete("WPF compatibility")] public static readonly DispatcherPriority ApplicationIdle = MinValue;
  29. /// <summary>
  30. /// The job will be processed after background operations have completed.
  31. /// </summary>
  32. [Obsolete("WPF compatibility")] public static readonly DispatcherPriority ContextIdle = MinValue;
  33. /// <summary>
  34. /// The job will be processed with normal priority.
  35. /// </summary>
  36. public static readonly DispatcherPriority Normal = MinValue;
  37. /// <summary>
  38. /// The job will be processed after other non-idle operations have completed.
  39. /// </summary>
  40. public static readonly DispatcherPriority Background = new(MinValue + 1);
  41. /// <summary>
  42. /// The job will be processed with the same priority as input.
  43. /// </summary>
  44. public static readonly DispatcherPriority Input = new(Background + 1);
  45. /// <summary>
  46. /// The job will be processed after layout and render but before input.
  47. /// </summary>
  48. public static readonly DispatcherPriority Loaded = new(Input + 1);
  49. /// <summary>
  50. /// The job will be processed with the same priority as render.
  51. /// </summary>
  52. public static readonly DispatcherPriority Render = new(Loaded + 1);
  53. /// <summary>
  54. /// The job will be processed with the same priority as composition updates.
  55. /// </summary>
  56. public static readonly DispatcherPriority Composition = new(Render + 1);
  57. /// <summary>
  58. /// The job will be processed with before composition updates.
  59. /// </summary>
  60. public static readonly DispatcherPriority PreComposition = new(Composition + 1);
  61. /// <summary>
  62. /// The job will be processed with the same priority as layout.
  63. /// </summary>
  64. public static readonly DispatcherPriority Layout = new(PreComposition + 1);
  65. /// <summary>
  66. /// The job will be processed with the same priority as data binding.
  67. /// </summary>
  68. [Obsolete("WPF compatibility")] public static readonly DispatcherPriority DataBind = MinValue;
  69. /// <summary>
  70. /// The job will be processed before other asynchronous operations.
  71. /// </summary>
  72. public static readonly DispatcherPriority Send = new(Layout + 1);
  73. /// <summary>
  74. /// Maximum possible priority
  75. /// </summary>
  76. public static readonly DispatcherPriority MaxValue = Send;
  77. // Note: unlike ctor this one is validating
  78. public static DispatcherPriority FromValue(int value)
  79. {
  80. if (value < MinValue.Value || value > MaxValue.Value)
  81. throw new ArgumentOutOfRangeException(nameof(value));
  82. return new DispatcherPriority(value);
  83. }
  84. public static implicit operator int(DispatcherPriority priority) => priority.Value;
  85. public static implicit operator DispatcherPriority(int value) => FromValue(value);
  86. /// <inheritdoc />
  87. public bool Equals(DispatcherPriority other) => Value == other.Value;
  88. /// <inheritdoc />
  89. public override bool Equals(object? obj) => obj is DispatcherPriority other && Equals(other);
  90. /// <inheritdoc />
  91. public override int GetHashCode() => Value.GetHashCode();
  92. public static bool operator ==(DispatcherPriority left, DispatcherPriority right) => left.Value == right.Value;
  93. public static bool operator !=(DispatcherPriority left, DispatcherPriority right) => left.Value != right.Value;
  94. public static bool operator <(DispatcherPriority left, DispatcherPriority right) => left.Value < right.Value;
  95. public static bool operator >(DispatcherPriority left, DispatcherPriority right) => left.Value > right.Value;
  96. public static bool operator <=(DispatcherPriority left, DispatcherPriority right) => left.Value <= right.Value;
  97. public static bool operator >=(DispatcherPriority left, DispatcherPriority right) => left.Value >= right.Value;
  98. /// <inheritdoc />
  99. public int CompareTo(DispatcherPriority other) => Value.CompareTo(other.Value);
  100. }
  101. }