ShowWindowTest.axaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Input;
  6. using Avalonia.Interactivity;
  7. using Avalonia.Threading;
  8. namespace IntegrationTestApp
  9. {
  10. public class MeasureBorder : Border
  11. {
  12. protected override Size MeasureOverride(Size availableSize)
  13. {
  14. MeasuredWith = availableSize;
  15. return base.MeasureOverride(availableSize);
  16. }
  17. public static readonly StyledProperty<Size> MeasuredWithProperty = AvaloniaProperty.Register<MeasureBorder, Size>(
  18. nameof(MeasuredWith));
  19. public Size MeasuredWith
  20. {
  21. get => GetValue(MeasuredWithProperty);
  22. set => SetValue(MeasuredWithProperty, value);
  23. }
  24. }
  25. public partial class ShowWindowTest : Window
  26. {
  27. private readonly DispatcherTimer? _timer;
  28. private readonly TextBox? _orderTextBox;
  29. private int _mouseMoveCount;
  30. private int _mouseReleaseCount;
  31. private int _doubleClickCount;
  32. private int _mouseDownCount;
  33. public ShowWindowTest()
  34. {
  35. InitializeComponent();
  36. DataContext = this;
  37. PositionChanged += (s, e) => CurrentPosition.Text = $"{Position}";
  38. PointerMoved += OnPointerMoved;
  39. PointerPressed += OnPointerPressed;
  40. PointerReleased += OnPointerReleased;
  41. PointerExited += (_, e) => ResetCounters();
  42. DoubleTapped += OnDoubleTapped;
  43. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  44. {
  45. _orderTextBox = CurrentOrder;
  46. _timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(250) };
  47. _timer.Tick += TimerOnTick;
  48. _timer.Start();
  49. }
  50. }
  51. protected override void OnOpened(EventArgs e)
  52. {
  53. base.OnOpened(e);
  54. var scaling = PlatformImpl!.DesktopScaling;
  55. CurrentPosition.Text = $"{Position}";
  56. CurrentScreenRect.Text = $"{Screens.ScreenFromVisual(this)?.WorkingArea}";
  57. CurrentScaling.Text = $"{scaling}";
  58. if (Owner is not null)
  59. {
  60. var owner = (Window)Owner;
  61. CurrentOwnerRect.Text = $"{owner.Position}, {PixelSize.FromSize(owner.FrameSize!.Value, scaling)}";
  62. }
  63. }
  64. protected override void OnClosed(EventArgs e)
  65. {
  66. base.OnClosed(e);
  67. _timer?.Stop();
  68. }
  69. private void TimerOnTick(object? sender, EventArgs e)
  70. {
  71. _orderTextBox!.Text = MacOSIntegration.GetOrderedIndex(this).ToString();
  72. }
  73. private void AddToWidth_Click(object? sender, RoutedEventArgs e) => Width = Bounds.Width + 10;
  74. private void AddToHeight_Click(object? sender, RoutedEventArgs e) => Height = Bounds.Height + 10;
  75. private void OnPointerMoved(object? sender, Avalonia.Input.PointerEventArgs e)
  76. {
  77. _mouseMoveCount++;
  78. UpdateCounterDisplays();
  79. }
  80. private void OnPointerPressed(object? sender, Avalonia.Input.PointerPressedEventArgs e)
  81. {
  82. _mouseDownCount++;
  83. UpdateCounterDisplays();
  84. }
  85. private void OnPointerReleased(object? sender, Avalonia.Input.PointerReleasedEventArgs e)
  86. {
  87. _mouseReleaseCount++;
  88. UpdateCounterDisplays();
  89. }
  90. public void ResetCounters()
  91. {
  92. _mouseMoveCount = 0;
  93. _mouseReleaseCount = 0;
  94. _doubleClickCount = 0;
  95. _mouseDownCount = 0;
  96. UpdateCounterDisplays();
  97. }
  98. private void OnDoubleTapped(object? sender, Avalonia.Input.TappedEventArgs e)
  99. {
  100. _doubleClickCount++;
  101. UpdateCounterDisplays();
  102. }
  103. private void UpdateCounterDisplays()
  104. {
  105. var mouseMoveCountTextBox = this.FindControl<TextBox>("MouseMoveCount");
  106. var mouseDownCountTextBox = this.FindControl<TextBox>("MouseDownCount");
  107. var mouseReleaseCountTextBox = this.FindControl<TextBox>("MouseReleaseCount");
  108. var doubleClickCountTextBox = this.FindControl<TextBox>("DoubleClickCount");
  109. if (mouseMoveCountTextBox != null)
  110. mouseMoveCountTextBox.Text = _mouseMoveCount.ToString();
  111. if (mouseDownCountTextBox != null)
  112. mouseDownCountTextBox.Text = _mouseDownCount.ToString();
  113. if (mouseReleaseCountTextBox != null)
  114. mouseReleaseCountTextBox.Text = _mouseReleaseCount.ToString();
  115. if (doubleClickCountTextBox != null)
  116. doubleClickCountTextBox.Text = _doubleClickCount.ToString();
  117. }
  118. public void ShowTitleAreaControl()
  119. {
  120. var titleAreaControl = this.FindControl<Grid>("TitleAreaControl");
  121. if (titleAreaControl == null) return;
  122. titleAreaControl.IsVisible = true;
  123. var titleBarHeight = ExtendClientAreaTitleBarHeightHint > 0 ? ExtendClientAreaTitleBarHeightHint : 30;
  124. titleAreaControl.Margin = new Thickness(110, -titleBarHeight, 8, 0);
  125. titleAreaControl.Height = titleBarHeight;
  126. }
  127. }
  128. }