PosAlign.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #nullable enable
  2. using System.ComponentModel;
  3. using System.Text.RegularExpressions;
  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.Layout()"/>
  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 record PosAlign : Pos
  25. {
  26. /// <summary>
  27. /// The cached location. Used to store the calculated location to minimize recalculating it.
  28. /// </summary>
  29. internal int? _cachedLocation;
  30. private readonly Aligner? _aligner;
  31. /// <summary>
  32. /// Gets the alignment settings.
  33. /// </summary>
  34. public required Aligner Aligner
  35. {
  36. get => _aligner!;
  37. init
  38. {
  39. if (_aligner is { })
  40. {
  41. _aligner.PropertyChanged -= Aligner_PropertyChanged;
  42. }
  43. _aligner = value;
  44. _aligner.PropertyChanged += Aligner_PropertyChanged;
  45. }
  46. }
  47. // TODO: PosAlign.CalculateMinDimension is a hack. Need to figure out a better way of doing this.
  48. /// <summary>
  49. /// Returns the minimum size a group of views with the same <paramref name="groupId"/> can be.
  50. /// </summary>
  51. /// <param name="groupId"></param>
  52. /// <param name="views"></param>
  53. /// <param name="dimension"></param>
  54. /// <returns></returns>
  55. public static int CalculateMinDimension (int groupId, IList<View> views, Dimension dimension)
  56. {
  57. List<int> dimensionsList = new ();
  58. // PERF: If this proves a perf issue, consider caching a ref to this list in each item
  59. List<View> viewsInGroup = views.Where (v => HasGroupId (v, dimension, groupId)).ToList ();
  60. if (viewsInGroup.Count == 0)
  61. {
  62. return 0;
  63. }
  64. // PERF: We iterate over viewsInGroup multiple times here.
  65. // Update the dimensionList with the sizes of the views
  66. for (var index = 0; index < viewsInGroup.Count; index++)
  67. {
  68. View view = viewsInGroup [index];
  69. PosAlign? posAlign = dimension == Dimension.Width ? view.X as PosAlign : view.Y as PosAlign;
  70. if (posAlign is { })
  71. {
  72. dimensionsList.Add (dimension == Dimension.Width ? view.Frame.Width : view.Frame.Height);
  73. }
  74. }
  75. // Align
  76. return dimensionsList.Sum ();
  77. }
  78. internal static bool HasGroupId (View v, Dimension dimension, int groupId)
  79. {
  80. return dimension switch
  81. {
  82. Dimension.Width when v.X.Has<PosAlign> (out PosAlign pos) => pos.GroupId == groupId,
  83. Dimension.Height when v.Y.Has<PosAlign> (out PosAlign pos) => pos.GroupId == groupId,
  84. _ => false
  85. };
  86. }
  87. /// <summary>
  88. /// Gets the identifier of a set of views that should be aligned together. When only a single
  89. /// set of views in a SuperView is aligned, setting <see cref="GroupId"/> is not needed because it defaults to 0.
  90. /// </summary>
  91. public int GroupId { get; init; }
  92. /// <inheritdoc/>
  93. public override string ToString () { return $"Align(alignment={Aligner.Alignment},modes={Aligner.AlignmentModes},groupId={GroupId})"; }
  94. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  95. {
  96. if (_cachedLocation.HasValue && Aligner.ContainerSize == superviewDimension && !us.NeedsLayout)
  97. {
  98. return _cachedLocation.Value;
  99. }
  100. IList<View>? groupViews;
  101. if (us.SuperView is null)
  102. {
  103. groupViews = new List<View> ();
  104. groupViews.Add (us);
  105. }
  106. else
  107. {
  108. groupViews = us.SuperView!.Subviews.Where (v => HasGroupId (v, dimension, GroupId)).ToList ();
  109. }
  110. AlignAndUpdateGroup (GroupId, groupViews, dimension, superviewDimension);
  111. if (_cachedLocation.HasValue)
  112. {
  113. return _cachedLocation.Value;
  114. }
  115. return 0;
  116. }
  117. internal override int GetAnchor (int width) { return _cachedLocation ?? 0 - width; }
  118. /// <summary>
  119. /// Aligns the views in <paramref name="views"/> that have the same group ID as <paramref name="groupId"/>.
  120. /// Updates each view's cached _location.
  121. /// </summary>
  122. /// <param name="groupId"></param>
  123. /// <param name="views"></param>
  124. /// <param name="dimension"></param>
  125. /// <param name="size"></param>
  126. private static void AlignAndUpdateGroup (int groupId, IList<View> views, Dimension dimension, int size)
  127. {
  128. List<int> dimensionsList = new ();
  129. // PERF: If this proves a perf issue, consider caching a ref to this list in each item
  130. List<PosAlign?> posAligns = views.Where (v => PosAlign.HasGroupId (v, dimension, groupId))
  131. .Select (v => dimension == Dimension.Width ? v.X as PosAlign : v.Y as PosAlign)
  132. .ToList ();
  133. // PERF: We iterate over viewsInGroup multiple times here.
  134. Aligner? firstInGroup = null;
  135. // Update the dimensionList with the sizes of the views
  136. for (var index = 0; index < posAligns.Count; index++)
  137. {
  138. if (posAligns [index] is { })
  139. {
  140. if (firstInGroup is null)
  141. {
  142. firstInGroup = posAligns [index]!.Aligner;
  143. }
  144. dimensionsList.Add (dimension == Dimension.Width
  145. ? views [index].Width!.Calculate(0, size, views [index], dimension)
  146. : views [index].Height!.Calculate (0, size, views [index], dimension));
  147. }
  148. }
  149. if (firstInGroup is null)
  150. {
  151. return;
  152. }
  153. // Update the first item in the group with the new container size.
  154. firstInGroup.ContainerSize = size;
  155. // Align
  156. int [] locations = firstInGroup.Align (dimensionsList.ToArray ());
  157. // Update the cached location for each item
  158. for (int posIndex = 0, locIndex = 0; posIndex < posAligns.Count; posIndex++)
  159. {
  160. if (posAligns [posIndex] is { })
  161. {
  162. posAligns [posIndex]!._cachedLocation = locations [locIndex++];
  163. }
  164. }
  165. }
  166. private void Aligner_PropertyChanged (object? sender, PropertyChangedEventArgs e) { _cachedLocation = null; }
  167. }