ScreenPage.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Markup.Xaml;
  5. using Avalonia.Media;
  6. using Avalonia.Platform;
  7. using Avalonia.Rendering;
  8. namespace ControlCatalog.Pages
  9. {
  10. public class ScreenPage : UserControl
  11. {
  12. private double _leftMost;
  13. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  14. {
  15. base.OnAttachedToVisualTree(e);
  16. Window w = (Window)VisualRoot;
  17. w.PositionChanged += (sender, args) => InvalidateVisual();
  18. }
  19. public override void Render(DrawingContext context)
  20. {
  21. base.Render(context);
  22. if (!(VisualRoot is Window w))
  23. {
  24. return;
  25. }
  26. var screens = w.Screens.All;
  27. var scaling = ((IRenderRoot)w).RenderScaling;
  28. Pen p = new Pen(Brushes.Black);
  29. if (screens != null)
  30. foreach (Screen screen in screens)
  31. {
  32. if (screen.Bounds.X / 10f < _leftMost)
  33. {
  34. _leftMost = screen.Bounds.X / 10f;
  35. InvalidateVisual();
  36. return;
  37. }
  38. Rect boundsRect = new Rect(screen.Bounds.X / 10f + Math.Abs(_leftMost), screen.Bounds.Y / 10f, screen.Bounds.Width / 10f,
  39. screen.Bounds.Height / 10f);
  40. Rect workingAreaRect = new Rect(screen.WorkingArea.X / 10f + Math.Abs(_leftMost), screen.WorkingArea.Y / 10f, screen.WorkingArea.Width / 10f,
  41. screen.WorkingArea.Height / 10f);
  42. context.DrawRectangle(p, boundsRect);
  43. context.DrawRectangle(p, workingAreaRect);
  44. FormattedText text = new FormattedText()
  45. {
  46. Typeface = Typeface.Default
  47. };
  48. text.Text = $"Bounds: {screen.Bounds.Width}:{screen.Bounds.Height}";
  49. context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height), text);
  50. text.Text = $"WorkArea: {screen.WorkingArea.Width}:{screen.WorkingArea.Height}";
  51. context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 20), text);
  52. text.Text = $"Scaling: {screen.PixelDensity * 100}%";
  53. context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 40), text);
  54. text.Text = $"Primary: {screen.Primary}";
  55. context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 60), text);
  56. text.Text = $"Current: {screen.Equals(w.Screens.ScreenFromBounds(new PixelRect(w.Position, PixelSize.FromSize(w.Bounds.Size, scaling))))}";
  57. context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 80), text);
  58. }
  59. context.DrawRectangle(p, new Rect(w.Position.X / 10f + Math.Abs(_leftMost), w.Position.Y / 10, w.Bounds.Width / 10, w.Bounds.Height / 10));
  60. }
  61. }
  62. }