ScreenPage.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Net.Http.Headers;
  5. using Avalonia;
  6. using Avalonia.Controls;
  7. using Avalonia.Media;
  8. using Avalonia.Platform;
  9. using Avalonia.Rendering;
  10. using Avalonia.Threading;
  11. namespace ControlCatalog.Pages
  12. {
  13. public class ScreenPage : UserControl
  14. {
  15. private double _leftMost;
  16. private double _topMost;
  17. private IBrush _primaryBrush = SolidColorBrush.Parse("#FF0078D7");
  18. private IBrush _defaultBrush = Brushes.LightGray;
  19. private IPen _activePen = new Pen(Brushes.Black);
  20. private IPen _defaultPen = new Pen(Brushes.DarkGray);
  21. protected override bool BypassFlowDirectionPolicies => true;
  22. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  23. {
  24. base.OnAttachedToVisualTree(e);
  25. if(VisualRoot is Window w)
  26. {
  27. w.PositionChanged += (_, _) => InvalidateVisual();
  28. }
  29. }
  30. public override void Render(DrawingContext context)
  31. {
  32. base.Render(context);
  33. if (!(VisualRoot is Window w))
  34. {
  35. return;
  36. }
  37. var screens = w.Screens.All;
  38. var scaling = ((IRenderRoot)w).RenderScaling;
  39. var activeScreen = w.Screens.ScreenFromBounds(new PixelRect(w.Position, PixelSize.FromSize(w.Bounds.Size, scaling)));
  40. double maxBottom = 0;
  41. for (int i = 0; i<screens.Count; i++ )
  42. {
  43. var screen = screens[i];
  44. if (screen.Bounds.X / 10f < _leftMost)
  45. {
  46. _leftMost = screen.Bounds.X / 10f;
  47. Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background);
  48. return;
  49. }
  50. if (screen.Bounds.Y / 10f < _topMost)
  51. {
  52. _topMost = screen.Bounds.Y / 10f;
  53. Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background);
  54. return;
  55. }
  56. bool primary = screen.IsPrimary;
  57. bool active = screen.Equals(activeScreen);
  58. Rect boundsRect = new Rect(screen.Bounds.X / 10f + Math.Abs(_leftMost), screen.Bounds.Y / 10f+Math.Abs(_topMost), screen.Bounds.Width / 10f,
  59. screen.Bounds.Height / 10f);
  60. Rect workingAreaRect = new Rect(screen.WorkingArea.X / 10f + Math.Abs(_leftMost), screen.WorkingArea.Y / 10f+Math.Abs(_topMost), screen.WorkingArea.Width / 10f,
  61. screen.WorkingArea.Height / 10f);
  62. context.DrawRectangle(primary ? _primaryBrush : _defaultBrush, active ? _activePen : _defaultPen, boundsRect);
  63. context.DrawRectangle(primary ? _primaryBrush : _defaultBrush, active ? _activePen : _defaultPen, workingAreaRect);
  64. var identifier = CreateScreenIdentifier((i+1).ToString(), primary);
  65. var center = boundsRect.Center - new Point(identifier.Width / 2.0f, identifier.Height / 2.0f);
  66. context.DrawText(identifier, center);
  67. maxBottom = Math.Max(maxBottom, boundsRect.Bottom);
  68. }
  69. double currentHeight = maxBottom;
  70. for(int i = 0; i< screens.Count; i++)
  71. {
  72. var screen = screens[i];
  73. var formattedText = CreateFormattedText($"Screen {i+1}", 18);
  74. context.DrawText(formattedText, new Point(0, currentHeight));
  75. currentHeight += 25;
  76. formattedText = CreateFormattedText($"Bounds: {screen.Bounds.Width}:{screen.Bounds.Height}");
  77. context.DrawText(formattedText, new Point(15, currentHeight));
  78. currentHeight += 20;
  79. formattedText = CreateFormattedText($"WorkArea: {screen.WorkingArea.Width}:{screen.WorkingArea.Height}");
  80. context.DrawText(formattedText, new Point(15, currentHeight));
  81. currentHeight += 20;
  82. formattedText = CreateFormattedText($"Scaling: {screen.Scaling * 100}%");
  83. context.DrawText(formattedText, new Point(15, currentHeight));
  84. currentHeight += 20;
  85. formattedText = CreateFormattedText($"IsPrimary: {screen.IsPrimary}");
  86. context.DrawText(formattedText, new Point(15, currentHeight));
  87. currentHeight += 20;
  88. formattedText = CreateFormattedText( $"Current: {screen.Equals(activeScreen)}");
  89. context.DrawText(formattedText, new Point(15, currentHeight));
  90. currentHeight += 30;
  91. }
  92. context.DrawRectangle(_activePen, new Rect(w.Position.X / 10f + Math.Abs(_leftMost), w.Position.Y / 10f+Math.Abs(_topMost), w.Bounds.Width / 10, w.Bounds.Height / 10));
  93. }
  94. private static FormattedText CreateFormattedText(string textToFormat, double size = 12)
  95. {
  96. return new FormattedText(textToFormat, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
  97. Typeface.Default, size, Brushes.Green);
  98. }
  99. private static FormattedText CreateScreenIdentifier(string textToFormat, bool primary)
  100. {
  101. return new FormattedText(textToFormat, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, Typeface.Default, 20, primary ? Brushes.White : Brushes.Black);
  102. }
  103. }
  104. }