OrientationBasedMeasures.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // This source file is adapted from the WinUI project.
  2. // (https://github.com/microsoft/microsoft-ui-xaml)
  3. //
  4. // Licensed to The Avalonia Project under MIT License, courtesy of The .NET Foundation.
  5. namespace Avalonia.Layout
  6. {
  7. internal enum ScrollOrientation
  8. {
  9. Vertical,
  10. Horizontal,
  11. }
  12. internal class OrientationBasedMeasures
  13. {
  14. public ScrollOrientation ScrollOrientation { get; set; } = ScrollOrientation.Vertical;
  15. public double Major(in Size size) => ScrollOrientation == ScrollOrientation.Vertical ? size.Height : size.Width;
  16. public double Minor(in Size size) => ScrollOrientation == ScrollOrientation.Vertical ? size.Width : size.Height;
  17. public double MajorSize(in Rect rect) => ScrollOrientation == ScrollOrientation.Vertical ? rect.Height : rect.Width;
  18. public double MinorSize(in Rect rect) => ScrollOrientation == ScrollOrientation.Vertical ? rect.Width : rect.Height;
  19. public double MajorStart(in Rect rect) => ScrollOrientation == ScrollOrientation.Vertical ? rect.Y : rect.X;
  20. public double MinorStart(in Rect rect) => ScrollOrientation == ScrollOrientation.Vertical ? rect.X : rect.Y;
  21. public double MajorEnd(in Rect rect) => ScrollOrientation == ScrollOrientation.Vertical ? rect.Bottom : rect.Right;
  22. public double MinorEnd(in Rect rect) => ScrollOrientation == ScrollOrientation.Vertical ? rect.Right : rect.Bottom;
  23. public void SetMajorSize(ref Rect rect, double value)
  24. {
  25. if (ScrollOrientation == ScrollOrientation.Vertical)
  26. {
  27. rect = rect.WithHeight(value);
  28. }
  29. else
  30. {
  31. rect = rect.WithWidth(value);
  32. }
  33. }
  34. public void SetMinorSize(ref Rect rect, double value)
  35. {
  36. if (ScrollOrientation == ScrollOrientation.Vertical)
  37. {
  38. rect = rect.WithWidth(value);
  39. }
  40. else
  41. {
  42. rect = rect.WithHeight(value);
  43. }
  44. }
  45. public void SetMajorStart(ref Rect rect, double value)
  46. {
  47. if (ScrollOrientation == ScrollOrientation.Vertical)
  48. {
  49. rect = rect.WithY(value);
  50. }
  51. else
  52. {
  53. rect = rect.WithX(value);
  54. }
  55. }
  56. public void SetMinorStart(ref Rect rect, double value)
  57. {
  58. if (ScrollOrientation == ScrollOrientation.Vertical)
  59. {
  60. rect = rect.WithX(value);
  61. }
  62. else
  63. {
  64. rect = rect.WithY(value);
  65. }
  66. }
  67. public Rect MinorMajorRect(double minor, double major, double minorSize, double majorSize)
  68. {
  69. return ScrollOrientation == ScrollOrientation.Vertical ?
  70. new Rect(minor, major, minorSize, majorSize) :
  71. new Rect(major, minor, majorSize, minorSize);
  72. }
  73. public Point MinorMajorPoint(double minor, double major)
  74. {
  75. return ScrollOrientation == ScrollOrientation.Vertical ?
  76. new Point(minor, major) :
  77. new Point(major, minor);
  78. }
  79. public Size MinorMajorSize(double minor, double major)
  80. {
  81. return ScrollOrientation == ScrollOrientation.Vertical ?
  82. new Size(minor, major) :
  83. new Size(major, minor);
  84. }
  85. }
  86. }