HitTesting.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Avalonia.Controls.Shapes;
  2. using Avalonia.Layout;
  3. using Avalonia.Media;
  4. using Avalonia.Rendering;
  5. using Avalonia.UnitTests;
  6. using Xunit;
  7. namespace Avalonia.Skia.UnitTests
  8. {
  9. public class HitTesting
  10. {
  11. [Fact]
  12. public void Hit_Test_Should_Respect_Fill()
  13. {
  14. using (AvaloniaLocator.EnterScope())
  15. {
  16. SkiaPlatform.Initialize();
  17. var root = new TestRoot
  18. {
  19. Width = 100,
  20. Height = 100,
  21. Child = new Ellipse
  22. {
  23. Width = 100,
  24. Height = 100,
  25. Fill = Brushes.Red,
  26. HorizontalAlignment = HorizontalAlignment.Center,
  27. VerticalAlignment = VerticalAlignment.Center
  28. }
  29. };
  30. root.Renderer = new DeferredRenderer(root, null);
  31. root.Measure(Size.Infinity);
  32. root.Arrange(new Rect(root.DesiredSize));
  33. var outsideResult = root.Renderer.HitTest(new Point(10, 10), root, null);
  34. var insideResult = root.Renderer.HitTest(new Point(50, 50), root, null);
  35. Assert.Empty(outsideResult);
  36. Assert.Equal(new[] {root.Child}, insideResult);
  37. }
  38. }
  39. [Fact]
  40. public void Hit_Test_Should_Respect_Stroke()
  41. {
  42. using (AvaloniaLocator.EnterScope())
  43. {
  44. SkiaPlatform.Initialize();
  45. var root = new TestRoot
  46. {
  47. Width = 100,
  48. Height = 100,
  49. Child = new Ellipse
  50. {
  51. Width = 100,
  52. Height = 100,
  53. Stroke = Brushes.Red,
  54. StrokeThickness = 5,
  55. HorizontalAlignment = HorizontalAlignment.Center,
  56. VerticalAlignment = VerticalAlignment.Center
  57. }
  58. };
  59. root.Renderer = new DeferredRenderer(root, null);
  60. root.Measure(Size.Infinity);
  61. root.Arrange(new Rect(root.DesiredSize));
  62. var outsideResult = root.Renderer.HitTest(new Point(50, 50), root, null);
  63. var insideResult = root.Renderer.HitTest(new Point(1, 50), root, null);
  64. Assert.Empty(outsideResult);
  65. Assert.Equal(new[] { root.Child }, insideResult);
  66. }
  67. }
  68. }
  69. }