ScreenPage.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Globalization;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Media;
  6. using Avalonia.Platform;
  7. using Avalonia.Rendering;
  8. using Avalonia.Threading;
  9. namespace ControlCatalog.Pages
  10. {
  11. public class ScreenPage : UserControl
  12. {
  13. private double _leftMost;
  14. protected override bool BypassFlowDirectionPolicies => true;
  15. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  16. {
  17. base.OnAttachedToVisualTree(e);
  18. if(VisualRoot is Window w)
  19. {
  20. w.PositionChanged += (_, _) => InvalidateVisual();
  21. }
  22. }
  23. public override void Render(DrawingContext context)
  24. {
  25. base.Render(context);
  26. if (!(VisualRoot is Window w))
  27. {
  28. return;
  29. }
  30. var screens = w.Screens.All;
  31. var scaling = ((IRenderRoot)w).RenderScaling;
  32. var drawBrush = Brushes.Black;
  33. Pen p = new Pen(drawBrush);
  34. if (screens != null)
  35. foreach (Screen screen in screens)
  36. {
  37. if (screen.Bounds.X / 10f < _leftMost)
  38. {
  39. _leftMost = screen.Bounds.X / 10f;
  40. Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background);
  41. return;
  42. }
  43. Rect boundsRect = new Rect(screen.Bounds.X / 10f + Math.Abs(_leftMost), screen.Bounds.Y / 10f, screen.Bounds.Width / 10f,
  44. screen.Bounds.Height / 10f);
  45. Rect workingAreaRect = new Rect(screen.WorkingArea.X / 10f + Math.Abs(_leftMost), screen.WorkingArea.Y / 10f, screen.WorkingArea.Width / 10f,
  46. screen.WorkingArea.Height / 10f);
  47. context.DrawRectangle(p, boundsRect);
  48. context.DrawRectangle(p, workingAreaRect);
  49. var formattedText = CreateFormattedText($"Bounds: {screen.Bounds.Width}:{screen.Bounds.Height}");
  50. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height));
  51. formattedText =
  52. CreateFormattedText($"WorkArea: {screen.WorkingArea.Width}:{screen.WorkingArea.Height}");
  53. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height + 20));
  54. formattedText = CreateFormattedText($"Scaling: {screen.Scaling * 100}%");
  55. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height + 40));
  56. formattedText = CreateFormattedText($"IsPrimary: {screen.IsPrimary}");
  57. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height + 60));
  58. formattedText =
  59. CreateFormattedText(
  60. $"Current: {screen.Equals(w.Screens.ScreenFromBounds(new PixelRect(w.Position, PixelSize.FromSize(w.Bounds.Size, scaling))))}");
  61. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height + 80));
  62. }
  63. context.DrawRectangle(p, new Rect(w.Position.X / 10f + Math.Abs(_leftMost), w.Position.Y / 10f, w.Bounds.Width / 10, w.Bounds.Height / 10));
  64. }
  65. private static FormattedText CreateFormattedText(string textToFormat)
  66. {
  67. return new FormattedText(textToFormat, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
  68. Typeface.Default, 12, Brushes.Green);
  69. }
  70. }
  71. }