WindowExtensions.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Threading.Tasks;
  2. using Avalonia.Controls;
  3. using Avalonia.Interactivity;
  4. using Avalonia.Platform;
  5. using Xunit;
  6. namespace Avalonia.IntegrationTests.Win32;
  7. internal static class WindowExtensions
  8. {
  9. public static PixelRect ToPixelRect(this UnmanagedMethods.RECT rect)
  10. => new(new PixelPoint(rect.left, rect.top), new PixelPoint(rect.right, rect.bottom));
  11. public static Task WhenLoadedAsync(this Window window)
  12. {
  13. if (window.IsLoaded)
  14. return Task.CompletedTask;
  15. var tcs = new TaskCompletionSource();
  16. window.Loaded += OnLoaded;
  17. return tcs.Task;
  18. void OnLoaded(object? sender, RoutedEventArgs e)
  19. {
  20. window.Loaded -= OnLoaded;
  21. tcs.TrySetResult();
  22. }
  23. }
  24. public static Screen GetScreen(this Window window)
  25. {
  26. var screen = window.Screens.ScreenFromWindow(window);
  27. Assert.NotNull(screen);
  28. return screen;
  29. }
  30. public static PixelSize GetWin32ClientSize(this Window window)
  31. {
  32. var platformHandle = window.TryGetPlatformHandle();
  33. Assert.NotNull(platformHandle);
  34. Assert.True(UnmanagedMethods.GetClientRect(platformHandle.Handle, out var rect));
  35. return rect.ToPixelRect().Size;
  36. }
  37. public static PixelRect GetWin32WindowBounds(this Window window)
  38. {
  39. var platformHandle = window.TryGetPlatformHandle();
  40. Assert.NotNull(platformHandle);
  41. Assert.True(UnmanagedMethods.GetWindowRect(platformHandle.Handle, out var rect));
  42. return rect.ToPixelRect();
  43. }
  44. }