InlineCollection.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using Avalonia.Collections;
  3. using Avalonia.LogicalTree;
  4. using Avalonia.Metadata;
  5. using Avalonia.Utilities;
  6. namespace Avalonia.Controls.Documents
  7. {
  8. /// <summary>
  9. /// A collection of <see cref="Inline"/>s.
  10. /// </summary>
  11. [WhitespaceSignificantCollection]
  12. public class InlineCollection : AvaloniaList<Inline>
  13. {
  14. private IAvaloniaList<ILogical>? _logicalChildren;
  15. private IInlineHost? _inlineHost;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="InlineCollection"/> class.
  18. /// </summary>
  19. public InlineCollection()
  20. {
  21. ResetBehavior = ResetBehavior.Remove;
  22. this.ForEachItem(
  23. x =>
  24. {
  25. x.InlineHost = InlineHost;
  26. LogicalChildren?.Add(x);
  27. Invalidate();
  28. },
  29. x =>
  30. {
  31. LogicalChildren?.Remove(x);
  32. x.InlineHost = InlineHost;
  33. Invalidate();
  34. },
  35. () => throw new NotSupportedException());
  36. }
  37. internal IAvaloniaList<ILogical>? LogicalChildren
  38. {
  39. get => _logicalChildren;
  40. set
  41. {
  42. var oldValue = _logicalChildren;
  43. _logicalChildren = value;
  44. OnParentChanged(oldValue, value);
  45. }
  46. }
  47. internal IInlineHost? InlineHost
  48. {
  49. get => _inlineHost;
  50. set
  51. {
  52. _inlineHost = value;
  53. OnInlineHostChanged(value);
  54. }
  55. }
  56. /// <summary>
  57. /// Gets or adds the text held by the inlines collection.
  58. /// <remarks>
  59. /// Can be null for complex content.
  60. /// </remarks>
  61. /// </summary>
  62. public string? Text
  63. {
  64. get
  65. {
  66. if (Count == 0)
  67. {
  68. return null;
  69. }
  70. var builder = StringBuilderCache.Acquire();
  71. foreach (var inline in this)
  72. {
  73. inline.AppendText(builder);
  74. }
  75. return StringBuilderCache.GetStringAndRelease(builder);
  76. }
  77. }
  78. public override void Add(Inline inline)
  79. {
  80. if (InlineHost is TextBlock textBlock && !string.IsNullOrEmpty(textBlock.Text))
  81. {
  82. base.Add(new Run(textBlock.Text));
  83. textBlock.ClearTextInternal();
  84. }
  85. base.Add(inline);
  86. }
  87. /// <summary>
  88. /// Adds a text segment to the collection.
  89. /// <remarks>
  90. /// For non complex content this appends the text to the end of currently held text.
  91. /// For complex content this adds a <see cref="Run"/> to the collection.
  92. /// </remarks>
  93. /// </summary>
  94. /// <param name="text">The to be added text.</param>
  95. public void Add(string text)
  96. {
  97. if (InlineHost is TextBlock textBlock && !textBlock.HasComplexContent)
  98. {
  99. textBlock.Text += text;
  100. }
  101. else
  102. {
  103. Add(new Run(text));
  104. }
  105. }
  106. /// <summary>
  107. /// Adds a control wrapped inside a <see cref="InlineUIContainer"/> to the collection.
  108. /// </summary>
  109. /// <param name="control">The to be added control.</param>
  110. public void Add(Control control)
  111. {
  112. Add(new InlineUIContainer(control));
  113. }
  114. /// <summary>
  115. /// Raised when an inline in the collection changes.
  116. /// </summary>
  117. public event EventHandler? Invalidated;
  118. /// <summary>
  119. /// Raises the <see cref="Invalidated"/> event.
  120. /// </summary>
  121. protected void Invalidate()
  122. {
  123. if (InlineHost != null)
  124. {
  125. InlineHost.Invalidate();
  126. }
  127. Invalidated?.Invoke(this, EventArgs.Empty);
  128. }
  129. private void OnParentChanged(IAvaloniaList<ILogical>? oldParent, IAvaloniaList<ILogical>? newParent)
  130. {
  131. foreach (var child in this)
  132. {
  133. if (oldParent != newParent)
  134. {
  135. if (oldParent != null)
  136. {
  137. oldParent.Remove(child);
  138. }
  139. if (newParent != null)
  140. {
  141. newParent.Add(child);
  142. }
  143. }
  144. }
  145. }
  146. private void OnInlineHostChanged(IInlineHost? inlineHost)
  147. {
  148. foreach (var child in this)
  149. {
  150. child.InlineHost = inlineHost;
  151. }
  152. }
  153. }
  154. }