LayoutManager.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Avalonia.Logging;
  7. using Avalonia.Threading;
  8. namespace Avalonia.Layout
  9. {
  10. /// <summary>
  11. /// Manages measuring and arranging of controls.
  12. /// </summary>
  13. public class LayoutManager : ILayoutManager
  14. {
  15. private readonly Queue<ILayoutable> _toMeasure = new Queue<ILayoutable>();
  16. private readonly Queue<ILayoutable> _toArrange = new Queue<ILayoutable>();
  17. private bool _queued;
  18. private bool _running;
  19. /// <summary>
  20. /// Gets the layout manager.
  21. /// </summary>
  22. public static ILayoutManager Instance => AvaloniaLocator.Current.GetService<ILayoutManager>();
  23. /// <inheritdoc/>
  24. public void InvalidateMeasure(ILayoutable control)
  25. {
  26. Contract.Requires<ArgumentNullException>(control != null);
  27. Dispatcher.UIThread.VerifyAccess();
  28. if (!control.IsAttachedToVisualTree)
  29. {
  30. #if DEBUG
  31. throw new AvaloniaInternalException(
  32. "LayoutManager.InvalidateMeasure called on a control that is detached from the visual tree.");
  33. #else
  34. return;
  35. #endif
  36. }
  37. _toMeasure.Enqueue(control);
  38. _toArrange.Enqueue(control);
  39. QueueLayoutPass();
  40. }
  41. /// <inheritdoc/>
  42. public void InvalidateArrange(ILayoutable control)
  43. {
  44. Contract.Requires<ArgumentNullException>(control != null);
  45. Dispatcher.UIThread.VerifyAccess();
  46. if (!control.IsAttachedToVisualTree)
  47. {
  48. #if DEBUG
  49. throw new AvaloniaInternalException(
  50. "LayoutManager.InvalidateArrange called on a control that is detached from the visual tree.");
  51. #else
  52. return;
  53. #endif
  54. }
  55. _toArrange.Enqueue(control);
  56. QueueLayoutPass();
  57. }
  58. /// <inheritdoc/>
  59. public void ExecuteLayoutPass()
  60. {
  61. const int MaxPasses = 3;
  62. Dispatcher.UIThread.VerifyAccess();
  63. if (!_running)
  64. {
  65. _running = true;
  66. Logger.Information(
  67. LogArea.Layout,
  68. this,
  69. "Started layout pass. To measure: {Measure} To arrange: {Arrange}",
  70. _toMeasure.Count,
  71. _toArrange.Count);
  72. var stopwatch = new System.Diagnostics.Stopwatch();
  73. stopwatch.Start();
  74. try
  75. {
  76. for (var pass = 0; pass < MaxPasses; ++pass)
  77. {
  78. ExecuteMeasurePass();
  79. ExecuteArrangePass();
  80. if (_toMeasure.Count == 0)
  81. {
  82. break;
  83. }
  84. }
  85. }
  86. finally
  87. {
  88. _running = false;
  89. }
  90. stopwatch.Stop();
  91. Logger.Information(LogArea.Layout, this, "Layout pass finised in {Time}", stopwatch.Elapsed);
  92. }
  93. _queued = false;
  94. }
  95. /// <inheritdoc/>
  96. public void ExecuteInitialLayoutPass(ILayoutRoot root)
  97. {
  98. Measure(root);
  99. Arrange(root);
  100. // Running the initial layout pass may have caused some control to be invalidated
  101. // so run a full layout pass now (this usually due to scrollbars; its not known
  102. // whether they will need to be shown until the layout pass has run and if the
  103. // first guess was incorrect the layout will need to be updated).
  104. ExecuteLayoutPass();
  105. }
  106. private void ExecuteMeasurePass()
  107. {
  108. while (_toMeasure.Count > 0)
  109. {
  110. var control = _toMeasure.Dequeue();
  111. if (!control.IsMeasureValid && control.IsAttachedToVisualTree)
  112. {
  113. Measure(control);
  114. }
  115. }
  116. }
  117. private void ExecuteArrangePass()
  118. {
  119. while (_toArrange.Count > 0 && _toMeasure.Count == 0)
  120. {
  121. var control = _toArrange.Dequeue();
  122. if (!control.IsArrangeValid && control.IsAttachedToVisualTree)
  123. {
  124. Arrange(control);
  125. }
  126. }
  127. }
  128. private void Measure(ILayoutable control)
  129. {
  130. // Controls closest to the visual root need to be arranged first. We don't try to store
  131. // ordered invalidation lists, instead we traverse the tree upwards, measuring the
  132. // controls closest to the root first. This has been shown by benchmarks to be the
  133. // fastest and most memory-efficent algorithm.
  134. if (control.VisualParent is ILayoutable parent)
  135. {
  136. Measure(parent);
  137. }
  138. // If the control being measured has IsMeasureValid == true here then its measure was
  139. // handed by an ancestor and can be ignored. The measure may have also caused the
  140. // control to be removed.
  141. if (!control.IsMeasureValid && control.IsAttachedToVisualTree)
  142. {
  143. if (control is ILayoutRoot root)
  144. {
  145. root.Measure(Size.Infinity);
  146. }
  147. else
  148. {
  149. control.Measure(control.PreviousMeasure.Value);
  150. }
  151. }
  152. }
  153. private void Arrange(ILayoutable control)
  154. {
  155. if (control.VisualParent is ILayoutable parent)
  156. {
  157. Arrange(parent);
  158. }
  159. if (!control.IsArrangeValid && control.IsAttachedToVisualTree)
  160. {
  161. if (control is IEmbeddedLayoutRoot embeddedRoot)
  162. control.Arrange(new Rect(embeddedRoot.AllocatedSize));
  163. else if (control is ILayoutRoot root)
  164. control.Arrange(new Rect(root.DesiredSize));
  165. else if (control.PreviousArrange != null)
  166. {
  167. // Has been observed that PreviousArrange sometimes is null, probably a bug somewhere else.
  168. // Condition observed: control.VisualParent is Scrollbar, control is Border.
  169. control.Arrange(control.PreviousArrange.Value);
  170. }
  171. }
  172. }
  173. private void QueueLayoutPass()
  174. {
  175. if (!_queued && !_running)
  176. {
  177. Dispatcher.UIThread.InvokeAsync(ExecuteLayoutPass, DispatcherPriority.Render);
  178. _queued = true;
  179. }
  180. }
  181. }
  182. }