ScreenPage.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Globalization;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Markup.Xaml;
  6. using Avalonia.Media;
  7. using Avalonia.Platform;
  8. using Avalonia.Rendering;
  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. Window w = (Window)VisualRoot;
  19. w.PositionChanged += (sender, args) => InvalidateVisual();
  20. }
  21. public override void Render(DrawingContext context)
  22. {
  23. base.Render(context);
  24. if (!(VisualRoot is Window w))
  25. {
  26. return;
  27. }
  28. var screens = w.Screens.All;
  29. var scaling = ((IRenderRoot)w).RenderScaling;
  30. var drawBrush = Brushes.Black;
  31. Pen p = new Pen(drawBrush);
  32. if (screens != null)
  33. foreach (Screen screen in screens)
  34. {
  35. if (screen.Bounds.X / 10f < _leftMost)
  36. {
  37. _leftMost = screen.Bounds.X / 10f;
  38. InvalidateVisual();
  39. return;
  40. }
  41. Rect boundsRect = new Rect(screen.Bounds.X / 10f + Math.Abs(_leftMost), screen.Bounds.Y / 10f, screen.Bounds.Width / 10f,
  42. screen.Bounds.Height / 10f);
  43. Rect workingAreaRect = new Rect(screen.WorkingArea.X / 10f + Math.Abs(_leftMost), screen.WorkingArea.Y / 10f, screen.WorkingArea.Width / 10f,
  44. screen.WorkingArea.Height / 10f);
  45. context.DrawRectangle(p, boundsRect);
  46. context.DrawRectangle(p, workingAreaRect);
  47. var formattedText = CreateFormattedText($"Bounds: {screen.Bounds.Width}:{screen.Bounds.Height}");
  48. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height));
  49. formattedText =
  50. CreateFormattedText($"WorkArea: {screen.WorkingArea.Width}:{screen.WorkingArea.Height}");
  51. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height + 20));
  52. formattedText = CreateFormattedText($"Scaling: {screen.PixelDensity * 100}%");
  53. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height + 40));
  54. formattedText = CreateFormattedText($"Primary: {screen.Primary}");
  55. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height + 60));
  56. formattedText =
  57. CreateFormattedText(
  58. $"Current: {screen.Equals(w.Screens.ScreenFromBounds(new PixelRect(w.Position, PixelSize.FromSize(w.Bounds.Size, scaling))))}");
  59. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height + 80));
  60. }
  61. context.DrawRectangle(p, new Rect(w.Position.X / 10f + Math.Abs(_leftMost), w.Position.Y / 10f, w.Bounds.Width / 10, w.Bounds.Height / 10));
  62. }
  63. private FormattedText CreateFormattedText(string textToFormat)
  64. {
  65. return new FormattedText(textToFormat, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
  66. Typeface.Default, 12, Brushes.Green);
  67. }
  68. }
  69. }