PosAlign.cs 6.9 KB

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