FlowGrid.cs 5.2 KB

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