ShowWindowTest.axaml.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 ShowWindowTest : Window
  10. {
  11. private readonly DispatcherTimer? _timer;
  12. private readonly TextBox? _orderTextBox;
  13. public ShowWindowTest()
  14. {
  15. InitializeComponent();
  16. DataContext = this;
  17. PositionChanged += (s, e) => this.GetControl<TextBox>("CurrentPosition").Text = $"{Position}";
  18. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  19. {
  20. _orderTextBox = this.GetControl<TextBox>("CurrentOrder");
  21. _timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(250) };
  22. _timer.Tick += TimerOnTick;
  23. _timer.Start();
  24. }
  25. }
  26. private void InitializeComponent()
  27. {
  28. AvaloniaXamlLoader.Load(this);
  29. }
  30. protected override void OnOpened(EventArgs e)
  31. {
  32. base.OnOpened(e);
  33. var scaling = PlatformImpl!.DesktopScaling;
  34. this.GetControl<TextBox>("CurrentPosition").Text = $"{Position}";
  35. this.GetControl<TextBox>("CurrentScreenRect").Text = $"{Screens.ScreenFromVisual(this)?.WorkingArea}";
  36. this.GetControl<TextBox>("CurrentScaling").Text = $"{scaling}";
  37. if (Owner is not null)
  38. {
  39. var ownerRect = this.GetControl<TextBox>("CurrentOwnerRect");
  40. var owner = (Window)Owner;
  41. ownerRect.Text = $"{owner.Position}, {PixelSize.FromSize(owner.FrameSize!.Value, scaling)}";
  42. }
  43. }
  44. protected override void OnClosed(EventArgs e)
  45. {
  46. base.OnClosed(e);
  47. _timer?.Stop();
  48. }
  49. private void TimerOnTick(object? sender, EventArgs e)
  50. {
  51. _orderTextBox!.Text = MacOSIntegration.GetOrderedIndex(this).ToString();
  52. }
  53. }
  54. }