LayoutManager.cs 6.8 KB

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