PosAlign.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #nullable enable
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. namespace Terminal.Gui;
  5. /// <summary>
  6. /// Enables alignment of a set of views.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// Updating the properties of <see cref="Aligner"/> is supported, but will not automatically cause re-layout to
  11. /// happen. <see cref="View.LayoutSubviews"/>
  12. /// must be called on the SuperView.
  13. /// </para>
  14. /// <para>
  15. /// Views that should be aligned together must have a distinct <see cref="GroupId"/>. When only a single
  16. /// set of views is aligned within a SuperView, setting <see cref="GroupId"/> is optional because it defaults to 0.
  17. /// </para>
  18. /// <para>
  19. /// The first view added to the Superview with a given <see cref="GroupId"/> is used to determine the alignment of
  20. /// the group.
  21. /// The alignment is applied to all views with the same <see cref="GroupId"/>.
  22. /// </para>
  23. /// </remarks>
  24. public class PosAlign : Pos
  25. {
  26. /// <summary>
  27. /// The cached location. Used to store the calculated location to minimize recalculating it.
  28. /// </summary>
  29. private int? _cachedLocation;
  30. /// <summary>
  31. /// Gets the identifier of a set of views that should be aligned together. When only a single
  32. /// set of views in a SuperView is aligned, setting <see cref="GroupId"/> is not needed because it defaults to 0.
  33. /// </summary>
  34. public int GroupId { get; init; }
  35. private readonly Aligner? _aligner;
  36. /// <summary>
  37. /// Gets the alignment settings.
  38. /// </summary>
  39. public required Aligner Aligner
  40. {
  41. get => _aligner!;
  42. init
  43. {
  44. if (_aligner is { })
  45. {
  46. _aligner.PropertyChanged -= Aligner_PropertyChanged;
  47. }
  48. _aligner = value;
  49. _aligner.PropertyChanged += Aligner_PropertyChanged;
  50. }
  51. }
  52. /// <summary>
  53. /// Aligns the views in <paramref name="views"/> that have the same group ID as <paramref name="groupId"/>.
  54. /// Updates each view's cached _location.
  55. /// </summary>
  56. /// <param name="groupId"></param>
  57. /// <param name="views"></param>
  58. /// <param name="dimension"></param>
  59. /// <param name="size"></param>
  60. private static void AlignAndUpdateGroup (int groupId, IList<View> views, Dimension dimension, int size)
  61. {
  62. List<int> dimensionsList = new ();
  63. // PERF: If this proves a perf issue, consider caching a ref to this list in each item
  64. List<View> viewsInGroup = views.Where (
  65. v =>
  66. {
  67. return dimension switch
  68. {
  69. Dimension.Width when v.X is PosAlign alignX => alignX.GroupId == groupId,
  70. Dimension.Height when v.Y is PosAlign alignY => alignY.GroupId == groupId,
  71. _ => false
  72. };
  73. })
  74. .ToList ();
  75. if (viewsInGroup.Count == 0)
  76. {
  77. return;
  78. }
  79. // PERF: We iterate over viewsInGroup multiple times here.
  80. Aligner? firstInGroup = null;
  81. // Update the dimensionList with the sizes of the views
  82. for (var index = 0; index < viewsInGroup.Count; index++)
  83. {
  84. View view = viewsInGroup [index];
  85. PosAlign? posAlign = dimension == Dimension.Width ? view.X as PosAlign : view.Y as PosAlign;
  86. if (posAlign is { })
  87. {
  88. if (index == 0)
  89. {
  90. firstInGroup = posAlign.Aligner;
  91. }
  92. dimensionsList.Add (dimension == Dimension.Width ? view.Frame.Width : view.Frame.Height);
  93. }
  94. }
  95. // Update the first item in the group with the new container size.
  96. firstInGroup!.ContainerSize = size;
  97. // Align
  98. int [] locations = firstInGroup.Align (dimensionsList.ToArray ());
  99. // Update the cached location for each item
  100. for (var index = 0; index < viewsInGroup.Count; index++)
  101. {
  102. View view = viewsInGroup [index];
  103. PosAlign? align = dimension == Dimension.Width ? view.X as PosAlign : view.Y as PosAlign;
  104. if (align is { })
  105. {
  106. align._cachedLocation = locations [index];
  107. }
  108. }
  109. }
  110. private void Aligner_PropertyChanged (object? sender, PropertyChangedEventArgs e) { _cachedLocation = null; }
  111. /// <inheritdoc/>
  112. public override bool Equals (object? other)
  113. {
  114. return other is PosAlign align
  115. && GroupId == align.GroupId
  116. && align.Aligner.Alignment == Aligner.Alignment
  117. && align.Aligner.AlignmentModes == Aligner.AlignmentModes;
  118. }
  119. /// <inheritdoc/>
  120. public override int GetHashCode () { return HashCode.Combine (Aligner, GroupId); }
  121. /// <inheritdoc/>
  122. public override string ToString () { return $"Align(alignment={Aligner.Alignment},modes={Aligner.AlignmentModes},groupId={GroupId})"; }
  123. internal override int GetAnchor (int width) { return _cachedLocation ?? 0 - width; }
  124. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  125. {
  126. if (_cachedLocation.HasValue && Aligner.ContainerSize == superviewDimension)
  127. {
  128. return _cachedLocation.Value;
  129. }
  130. if (us?.SuperView is null)
  131. {
  132. return 0;
  133. }
  134. AlignAndUpdateGroup (GroupId, us.SuperView.Subviews, dimension, superviewDimension);
  135. if (_cachedLocation.HasValue)
  136. {
  137. return _cachedLocation.Value;
  138. }
  139. return 0;
  140. }
  141. internal int CalculateMinDimension (int groupId, IList<View> views, Dimension dimension)
  142. {
  143. List<int> dimensionsList = new ();
  144. // PERF: If this proves a perf issue, consider caching a ref to this list in each item
  145. List<View> viewsInGroup = views.Where (
  146. v =>
  147. {
  148. return dimension switch
  149. {
  150. Dimension.Width when v.X is PosAlign alignX => alignX.GroupId == groupId,
  151. Dimension.Height when v.Y is PosAlign alignY => alignY.GroupId == groupId,
  152. _ => false
  153. };
  154. })
  155. .ToList ();
  156. if (viewsInGroup.Count == 0)
  157. {
  158. return 0;
  159. }
  160. // PERF: We iterate over viewsInGroup multiple times here.
  161. Aligner? firstInGroup = null;
  162. // Update the dimensionList with the sizes of the views
  163. for (var index = 0; index < viewsInGroup.Count; index++)
  164. {
  165. View view = viewsInGroup [index];
  166. PosAlign? posAlign = dimension == Dimension.Width ? view.X as PosAlign : view.Y as PosAlign;
  167. if (posAlign is { })
  168. {
  169. if (index == 0)
  170. {
  171. firstInGroup = posAlign.Aligner;
  172. }
  173. dimensionsList.Add (dimension == Dimension.Width ? view.Frame.Width : view.Frame.Height);
  174. }
  175. }
  176. // Align
  177. var aligner = firstInGroup;
  178. aligner.ContainerSize = dimensionsList.Sum();
  179. int [] locations = aligner.Align (dimensionsList.ToArray ());
  180. return locations.Sum ();
  181. }
  182. }