AvaloniaModuleGridPanel.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Input;
  5. using Avalonia.Utilities;
  6. namespace InABox.Avalonia.Components.ModuleGrid;
  7. public class AvaloniaModuleGridPanel : Panel, INavigableContainer
  8. {
  9. public static readonly StyledProperty<int> ItemsPerLineProperty =
  10. AvaloniaProperty.Register<AvaloniaModuleGridPanel, int>(nameof(ItemsPerLine), 3);
  11. static AvaloniaModuleGridPanel()
  12. {
  13. AffectsMeasure<AvaloniaModuleGridPanel>(ItemsPerLineProperty);
  14. }
  15. public int ItemsPerLine
  16. {
  17. get => GetValue(ItemsPerLineProperty);
  18. set => SetValue(ItemsPerLineProperty, value);
  19. }
  20. IInputElement INavigableContainer.GetControl(NavigationDirection direction, IInputElement? from, bool wrap)
  21. {
  22. var index = from is Control cFrom ? Children.IndexOf(cFrom) : -1;
  23. switch (direction)
  24. {
  25. case NavigationDirection.First:
  26. index = 0;
  27. break;
  28. case NavigationDirection.Last:
  29. index = Children.Count - 1;
  30. break;
  31. case NavigationDirection.Next:
  32. ++index;
  33. break;
  34. case NavigationDirection.Previous:
  35. --index;
  36. break;
  37. case NavigationDirection.Left:
  38. index -= 1;
  39. break;
  40. case NavigationDirection.Right:
  41. index += 1;
  42. break;
  43. case NavigationDirection.Up:
  44. index = -1;
  45. break;
  46. case NavigationDirection.Down:
  47. index = -1;
  48. break;
  49. }
  50. if (index >= 0 && index < Children.Count) return Children[index];
  51. return this;
  52. }
  53. protected override Size MeasureOverride(Size constraint)
  54. {
  55. var itemWidth = constraint.Width / ItemsPerLine;
  56. MutableSize currentLineSize = new();
  57. MutableSize panelSize = new();
  58. Size lineConstraint = new(constraint.Width, constraint.Height);
  59. Size childConstraint = new(itemWidth, constraint.Height);
  60. for (int i = 0, count = Children.Count; i < count; i++)
  61. {
  62. var child = Children[i];
  63. if (child is null) continue;
  64. child.Measure(childConstraint);
  65. Size childSize = new(itemWidth, child.DesiredSize.Height);
  66. if (MathUtilities.GreaterThan(currentLineSize.Width + childSize.Width, lineConstraint.Width))
  67. {
  68. // Need to switch to another line
  69. panelSize.Width = Math.Max(currentLineSize.Width, panelSize.Width);
  70. panelSize.Height += currentLineSize.Height;
  71. currentLineSize = new MutableSize(childSize);
  72. }
  73. else
  74. {
  75. // Continue to accumulate a line
  76. currentLineSize.Width += childSize.Width;
  77. currentLineSize.Height = Math.Max(childSize.Height, currentLineSize.Height);
  78. }
  79. }
  80. // The last line size, if any should be added
  81. panelSize.Width = Math.Max(currentLineSize.Width, panelSize.Width);
  82. panelSize.Height += currentLineSize.Height;
  83. return panelSize.ToSize();
  84. }
  85. protected override Size ArrangeOverride(Size finalSize)
  86. {
  87. var itemWidth = finalSize.Width / ItemsPerLine;
  88. var firstInLine = 0;
  89. double accumulatedHeight = 0;
  90. var currentLineSize = new MutableSize();
  91. for (var i = 0; i < Children.Count; i++)
  92. {
  93. var child = Children[i];
  94. if (child == null) continue;
  95. MutableSize itemSize = new(itemWidth, child.DesiredSize.Height);
  96. if (MathUtilities.GreaterThan(currentLineSize.Width + itemSize.Width, finalSize.Width))
  97. {
  98. // Need to switch to another line
  99. ArrangeLine(accumulatedHeight, currentLineSize.Height, firstInLine, i, itemWidth);
  100. accumulatedHeight += currentLineSize.Height;
  101. currentLineSize = itemSize;
  102. firstInLine = i;
  103. }
  104. else
  105. {
  106. // Continue to accumulate a line
  107. currentLineSize.Width += itemSize.Width;
  108. currentLineSize.Height = Math.Max(itemSize.Height, currentLineSize.Height);
  109. }
  110. }
  111. if (firstInLine < Children.Count)
  112. // Arrange the last line, if any
  113. ArrangeLine(accumulatedHeight, currentLineSize.Height, firstInLine, Children.Count, itemWidth);
  114. return finalSize;
  115. }
  116. private void ArrangeLine(double y, double height, int start, int end, double width)
  117. {
  118. double x = 0;
  119. for (var i = start; i < end; i++)
  120. {
  121. var child = Children[i];
  122. if (child == null) continue;
  123. child.Arrange(new Rect(x, y, width, height));
  124. x += width;
  125. }
  126. }
  127. private struct MutableSize
  128. {
  129. internal MutableSize(double width, double height)
  130. {
  131. Width = width;
  132. Height = height;
  133. }
  134. internal MutableSize(Size size)
  135. {
  136. Width = size.Width;
  137. Height = size.Height;
  138. }
  139. internal double Width;
  140. internal double Height;
  141. internal Size ToSize()
  142. {
  143. return new Size(Width, Height);
  144. }
  145. }
  146. }