PosAlign.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. internal 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. int dimensionsSum = 0;
  57. foreach (var view in views)
  58. {
  59. if (!HasGroupId (view, dimension, groupId)) {
  60. continue;
  61. }
  62. PosAlign? posAlign = dimension == Dimension.Width
  63. ? view.X as PosAlign
  64. : view.Y as PosAlign;
  65. if (posAlign is { })
  66. {
  67. dimensionsSum += dimension == Dimension.Width
  68. ? view.Frame.Width
  69. : view.Frame.Height;
  70. }
  71. }
  72. // Align
  73. return dimensionsSum;
  74. }
  75. internal static bool HasGroupId (View v, Dimension dimension, int groupId)
  76. {
  77. return dimension switch
  78. {
  79. Dimension.Width when v.X.Has<PosAlign> (out PosAlign pos) => pos.GroupId == groupId,
  80. Dimension.Height when v.Y.Has<PosAlign> (out PosAlign pos) => pos.GroupId == groupId,
  81. _ => false
  82. };
  83. }
  84. /// <summary>
  85. /// Gets the identifier of a set of views that should be aligned together. When only a single
  86. /// set of views in a SuperView is aligned, setting <see cref="GroupId"/> is not needed because it defaults to 0.
  87. /// </summary>
  88. public int GroupId { get; init; }
  89. /// <inheritdoc/>
  90. public override string ToString () { return $"Align(alignment={Aligner.Alignment},modes={Aligner.AlignmentModes},groupId={GroupId})"; }
  91. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  92. {
  93. if (_cachedLocation.HasValue && Aligner.ContainerSize == superviewDimension && !us.NeedsLayout)
  94. {
  95. return _cachedLocation.Value;
  96. }
  97. IList<View>? groupViews;
  98. if (us.SuperView is null)
  99. {
  100. groupViews = new List<View> ();
  101. groupViews.Add (us);
  102. }
  103. else
  104. {
  105. groupViews = us.SuperView!.Subviews.Where (v => HasGroupId (v, dimension, GroupId)).ToList ();
  106. }
  107. AlignAndUpdateGroup (GroupId, groupViews, dimension, superviewDimension);
  108. if (_cachedLocation.HasValue)
  109. {
  110. return _cachedLocation.Value;
  111. }
  112. return 0;
  113. }
  114. internal override int GetAnchor (int width) { return _cachedLocation ?? 0 - width; }
  115. /// <summary>
  116. /// Aligns the views in <paramref name="views"/> that have the same group ID as <paramref name="groupId"/>.
  117. /// Updates each view's cached _location.
  118. /// </summary>
  119. /// <param name="groupId"></param>
  120. /// <param name="views"></param>
  121. /// <param name="dimension"></param>
  122. /// <param name="size"></param>
  123. private static void AlignAndUpdateGroup (int groupId, IList<View> views, Dimension dimension, int size)
  124. {
  125. List<int> dimensionsList = new ();
  126. // PERF: If this proves a perf issue, consider caching a ref to this list in each item
  127. List<PosAlign?> posAligns = views.Where (v => PosAlign.HasGroupId (v, dimension, groupId))
  128. .Select (v => dimension == Dimension.Width ? v.X as PosAlign : v.Y as PosAlign)
  129. .ToList ();
  130. // PERF: We iterate over viewsInGroup multiple times here.
  131. Aligner? firstInGroup = null;
  132. // Update the dimensionList with the sizes of the views
  133. for (var index = 0; index < posAligns.Count; index++)
  134. {
  135. if (posAligns [index] is { })
  136. {
  137. if (firstInGroup is null)
  138. {
  139. firstInGroup = posAligns [index]!.Aligner;
  140. }
  141. dimensionsList.Add (dimension == Dimension.Width
  142. ? views [index].Width!.Calculate(0, size, views [index], dimension)
  143. : views [index].Height!.Calculate (0, size, views [index], dimension));
  144. }
  145. }
  146. if (firstInGroup is null)
  147. {
  148. return;
  149. }
  150. // Update the first item in the group with the new container size.
  151. firstInGroup.ContainerSize = size;
  152. // Align
  153. int [] locations = firstInGroup.Align (dimensionsList.ToArray ());
  154. // Update the cached location for each item
  155. for (int posIndex = 0, locIndex = 0; posIndex < posAligns.Count; posIndex++)
  156. {
  157. if (posAligns [posIndex] is { })
  158. {
  159. posAligns [posIndex]!._cachedLocation = locations [locIndex++];
  160. }
  161. }
  162. }
  163. private void Aligner_PropertyChanged (object? sender, PropertyChangedEventArgs e) { _cachedLocation = null; }
  164. }