AttachedLayout.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // This source file is adapted from the WinUI project.
  2. // (https://github.com/microsoft/microsoft-ui-xaml)
  3. //
  4. // Licensed to The Avalonia Project under MIT License, courtesy of The .NET Foundation.
  5. using System;
  6. namespace Avalonia.Layout
  7. {
  8. /// <summary>
  9. /// Represents the base class for an object that sizes and arranges child elements for a host.
  10. /// </summary>
  11. public abstract class AttachedLayout : AvaloniaObject
  12. {
  13. internal string LayoutId { get; set; }
  14. /// <summary>
  15. /// Occurs when the measurement state (layout) has been invalidated.
  16. /// </summary>
  17. public event EventHandler MeasureInvalidated;
  18. /// <summary>
  19. /// Occurs when the arrange state (layout) has been invalidated.
  20. /// </summary>
  21. public event EventHandler ArrangeInvalidated;
  22. /// <summary>
  23. /// Initializes any per-container state the layout requires when it is attached to an
  24. /// <see cref="ILayoutable"/> container.
  25. /// </summary>
  26. /// <param name="context">
  27. /// The context object that facilitates communication between the layout and its host
  28. /// container.
  29. /// </param>
  30. /// <remarks>
  31. /// Container elements that support attached layouts should call this method when a layout
  32. /// instance is first assigned. The container is expected to give the attached layout
  33. /// instance a way to store and retrieve any per-container state by way of the provided
  34. /// context. It is also the responsibility of the container to not reuse the context, or
  35. /// otherwise expose the state from one layout to another.
  36. ///
  37. /// When an attached layout is removed the container should release any reference to the
  38. /// layout state it stored.
  39. ///
  40. /// Override <see cref="NonVirtualizingLayout.InitializeForContextCore"/> or
  41. /// <see cref="VirtualizingLayout.InitializeForContextCore"/> to provide the behavior for
  42. /// this method in a derived class.
  43. /// </remarks>
  44. public abstract void InitializeForContext(LayoutContext context);
  45. /// <summary>
  46. /// Removes any state the layout previously stored on the ILayoutable container.
  47. /// </summary>
  48. /// <param name="context">
  49. /// The context object that facilitates communication between the layout and its host
  50. /// container.
  51. /// </param>
  52. public abstract void UninitializeForContext(LayoutContext context);
  53. /// <summary>
  54. /// Suggests a DesiredSize for a container element. A container element that supports
  55. /// attached layouts should call this method from their own MeasureOverride implementations
  56. /// to form a recursive layout update. The attached layout is expected to call the Measure
  57. /// for each of the container’s ILayoutable children.
  58. /// </summary>
  59. /// <param name="context">
  60. /// The context object that facilitates communication between the layout and its host
  61. /// container.
  62. /// </param>
  63. /// <param name="availableSize">
  64. /// The available space that a container can allocate to a child object. A child object can
  65. /// request a larger space than what is available; the provided size might be accommodated
  66. /// if scrolling or other resize behavior is possible in that particular container.
  67. /// </param>
  68. /// <returns></returns>
  69. public abstract Size Measure(LayoutContext context, Size availableSize);
  70. /// <summary>
  71. /// Positions child elements and determines a size for a container UIElement. Container
  72. /// elements that support attached layouts should call this method from their layout
  73. /// override implementations to form a recursive layout update.
  74. /// </summary>
  75. /// <param name="context">
  76. /// The context object that facilitates communication between the layout and its host
  77. /// container.
  78. /// </param>
  79. /// <param name="finalSize">
  80. /// The final size that the container computes for the child in layout.
  81. /// </param>
  82. /// <returns>The actual size that is used after the element is arranged in layout.</returns>
  83. public abstract Size Arrange(LayoutContext context, Size finalSize);
  84. /// <summary>
  85. /// Invalidates the measurement state (layout) for all ILayoutable containers that reference
  86. /// this layout.
  87. /// </summary>
  88. protected void InvalidateMeasure() => MeasureInvalidated?.Invoke(this, EventArgs.Empty);
  89. /// <summary>
  90. /// Invalidates the arrange state (layout) for all UIElement containers that reference this
  91. /// layout. After the invalidation, the UIElement will have its layout updated, which
  92. /// occurs asynchronously.
  93. /// </summary>
  94. protected void InvalidateArrange() => ArrangeInvalidated?.Invoke(this, EventArgs.Empty);
  95. }
  96. }