Screen.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.ComponentModel;
  3. using Avalonia.Diagnostics;
  4. using Avalonia.Metadata;
  5. using Avalonia.Utilities;
  6. namespace Avalonia.Platform
  7. {
  8. /// <summary>
  9. /// Describes the orientation of a screen.
  10. /// </summary>
  11. public enum ScreenOrientation
  12. {
  13. /// <summary>
  14. /// No screen orientation is specified.
  15. /// </summary>
  16. None,
  17. /// <summary>
  18. /// Specifies that the monitor is oriented in landscape mode where the width of the screen viewing area is greater than the height.
  19. /// </summary>
  20. Landscape = 1,
  21. /// <summary>
  22. /// Specifies that the monitor rotated 90 degrees in the clockwise direction to orient the screen in portrait mode
  23. /// where the height of the screen viewing area is greater than the width.
  24. /// </summary>
  25. Portrait = 2,
  26. /// <summary>
  27. /// Specifies that the monitor rotated another 90 degrees in the clockwise direction (to equal 180 degrees) to orient the screen in landscape mode
  28. /// where the width of the screen viewing area is greater than the height.
  29. /// This landscape mode is flipped 180 degrees from the Landscape mode.
  30. /// </summary>
  31. LandscapeFlipped = 4,
  32. /// <summary>
  33. /// Specifies that the monitor rotated another 90 degrees in the clockwise direction (to equal 270 degrees) to orient the screen in portrait mode
  34. /// where the height of the screen viewing area is greater than the width. This portrait mode is flipped 180 degrees from the Portrait mode.
  35. /// </summary>
  36. PortraitFlipped = 8
  37. }
  38. /// <summary>
  39. /// Represents a single display screen.
  40. /// </summary>
  41. public class Screen : IEquatable<Screen>
  42. {
  43. /// <summary>
  44. /// Gets the device name associated with a display.
  45. /// </summary>
  46. public string? DisplayName { get; protected set; }
  47. /// <summary>
  48. /// Gets the current orientation of a screen.
  49. /// </summary>
  50. public ScreenOrientation CurrentOrientation { get; protected set; }
  51. /// <summary>
  52. /// Gets the scaling factor applied to the screen by the operating system.
  53. /// </summary>
  54. /// <remarks>
  55. /// Multiply this value by 100 to get a percentage.
  56. /// Both X and Y scaling factors are assumed uniform.
  57. /// </remarks>
  58. public double Scaling { get; protected set; } = 1;
  59. /// <inheritdoc cref="Scaling"/>
  60. [Obsolete("Use the Scaling property instead.", true), EditorBrowsable(EditorBrowsableState.Never)]
  61. public double PixelDensity => Scaling;
  62. /// <summary>
  63. /// Gets the overall pixel-size and position of the screen.
  64. /// </summary>
  65. /// <remarks>
  66. /// This generally is the raw pixel counts in both the X and Y direction.
  67. /// </remarks>
  68. public PixelRect Bounds { get; protected set; }
  69. /// <summary>
  70. /// Gets the actual working-area pixel-size of the screen.
  71. /// </summary>
  72. /// <remarks>
  73. /// This area may be smaller than <see href="Bounds"/> to account for notches and
  74. /// other block-out areas such as taskbars etc.
  75. /// </remarks>
  76. public PixelRect WorkingArea { get; protected set; }
  77. /// <summary>
  78. /// Gets a value indicating whether the screen is the primary one.
  79. /// </summary>
  80. public bool IsPrimary { get; protected set; }
  81. /// <inheritdoc cref="IsPrimary"/>
  82. [Obsolete("Use the IsPrimary property instead.", true), EditorBrowsable(EditorBrowsableState.Never)]
  83. public bool Primary => IsPrimary;
  84. /// <summary>
  85. /// Initializes a new instance of the <see cref="Screen"/> class.
  86. /// </summary>
  87. /// <param name="scaling">The scaling factor applied to the screen by the operating system.</param>
  88. /// <param name="bounds">The overall pixel-size of the screen.</param>
  89. /// <param name="workingArea">The actual working-area pixel-size of the screen.</param>
  90. /// <param name="isPrimary">Whether the screen is the primary one.</param>
  91. [Unstable(ObsoletionMessages.MayBeRemovedInAvalonia12)]
  92. public Screen(double scaling, PixelRect bounds, PixelRect workingArea, bool isPrimary)
  93. {
  94. Scaling = scaling;
  95. Bounds = bounds;
  96. WorkingArea = workingArea;
  97. IsPrimary = isPrimary;
  98. }
  99. private protected Screen() { }
  100. /// <summary>
  101. /// Tries to get the platform handle for the Screen.
  102. /// </summary>
  103. /// <returns>
  104. /// An <see cref="IPlatformHandle"/> describing the screen handle, or null if the handle
  105. /// could not be retrieved.
  106. /// </returns>
  107. public virtual IPlatformHandle? TryGetPlatformHandle() => null;
  108. /// <inheritdoc/>
  109. public bool Equals(Screen? other)
  110. {
  111. if (other is null) return false;
  112. if (ReferenceEquals(this, other)) return true;
  113. return base.Equals(other);
  114. }
  115. public static bool operator ==(Screen? left, Screen? right)
  116. {
  117. return Equals(left, right);
  118. }
  119. public static bool operator !=(Screen? left, Screen? right)
  120. {
  121. return !Equals(left, right);
  122. }
  123. /// <inheritdoc/>
  124. public override string ToString()
  125. {
  126. var sb = StringBuilderCache.Acquire();
  127. sb.Append("Screen");
  128. sb.Append(" { ");
  129. // Only printing properties that are supposed to be immutable:
  130. sb.AppendFormat("{0} = {1}", nameof(DisplayName), DisplayName);
  131. if (TryGetPlatformHandle() is { } platformHandle)
  132. {
  133. sb.AppendFormat(", {0}: {1}", platformHandle.HandleDescriptor, platformHandle.Handle);
  134. }
  135. sb.Append(" } ");
  136. return StringBuilderCache.GetStringAndRelease(sb);
  137. }
  138. /// <summary>
  139. /// When screen is removed, we should at least empty all the properties.
  140. /// </summary>
  141. internal void OnRemoved()
  142. {
  143. DisplayName = null;
  144. Bounds = WorkingArea = default;
  145. Scaling = default;
  146. CurrentOrientation = default;
  147. }
  148. }
  149. }