ShowWindowTest.axaml.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Markup.Xaml;
  6. using Avalonia.Threading;
  7. namespace IntegrationTestApp
  8. {
  9. public class MeasureBorder : Border
  10. {
  11. protected override Size MeasureOverride(Size availableSize)
  12. {
  13. MeasuredWith = availableSize;
  14. return base.MeasureOverride(availableSize);
  15. }
  16. public static readonly StyledProperty<Size> MeasuredWithProperty = AvaloniaProperty.Register<MeasureBorder, Size>(
  17. nameof(MeasuredWith));
  18. public Size MeasuredWith
  19. {
  20. get => GetValue(MeasuredWithProperty);
  21. set => SetValue(MeasuredWithProperty, value);
  22. }
  23. }
  24. public class ShowWindowTest : Window
  25. {
  26. private readonly DispatcherTimer? _timer;
  27. private readonly TextBox? _orderTextBox;
  28. public ShowWindowTest()
  29. {
  30. InitializeComponent();
  31. DataContext = this;
  32. PositionChanged += (s, e) => this.GetControl<TextBox>("Position").Text = $"{Position}";
  33. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  34. {
  35. _orderTextBox = this.GetControl<TextBox>("Order");
  36. _timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(250) };
  37. _timer.Tick += TimerOnTick;
  38. _timer.Start();
  39. }
  40. }
  41. private void InitializeComponent()
  42. {
  43. AvaloniaXamlLoader.Load(this);
  44. }
  45. protected override void OnOpened(EventArgs e)
  46. {
  47. base.OnOpened(e);
  48. var scaling = PlatformImpl!.DesktopScaling;
  49. this.GetControl<TextBox>("Position").Text = $"{Position}";
  50. this.GetControl<TextBox>("ScreenRect").Text = $"{Screens.ScreenFromVisual(this)?.WorkingArea}";
  51. this.GetControl<TextBox>("Scaling").Text = $"{scaling}";
  52. if (Owner is not null)
  53. {
  54. var ownerRect = this.GetControl<TextBox>("OwnerRect");
  55. var owner = (Window)Owner;
  56. ownerRect.Text = $"{owner.Position}, {PixelSize.FromSize(owner.FrameSize!.Value, scaling)}";
  57. }
  58. }
  59. protected override void OnClosed(EventArgs e)
  60. {
  61. base.OnClosed(e);
  62. _timer?.Stop();
  63. }
  64. private void TimerOnTick(object? sender, EventArgs e)
  65. {
  66. _orderTextBox!.Text = MacOSIntegration.GetOrderedIndex(this).ToString();
  67. }
  68. }
  69. }