InlineCollection.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Text;
  3. using Avalonia.Collections;
  4. using Avalonia.LogicalTree;
  5. using Avalonia.Metadata;
  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 readonly IInlineHost? _host;
  15. private string? _text = string.Empty;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="InlineCollection"/> class.
  18. /// </summary>
  19. public InlineCollection(ILogical parent) : this(parent, null) { }
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="InlineCollection"/> class.
  22. /// </summary>
  23. internal InlineCollection(ILogical parent, IInlineHost? host = null) : base(0)
  24. {
  25. _host = host;
  26. ResetBehavior = ResetBehavior.Remove;
  27. this.ForEachItem(
  28. x =>
  29. {
  30. ((ISetLogicalParent)x).SetParent(parent);
  31. x.InlineHost = host;
  32. host?.Invalidate();
  33. },
  34. x =>
  35. {
  36. ((ISetLogicalParent)x).SetParent(null);
  37. x.InlineHost = host;
  38. host?.Invalidate();
  39. },
  40. () => throw new NotSupportedException());
  41. }
  42. public bool HasComplexContent => Count > 0;
  43. /// <summary>
  44. /// Gets or adds the text held by the inlines collection.
  45. /// <remarks>
  46. /// Can be null for complex content.
  47. /// </remarks>
  48. /// </summary>
  49. public string? Text
  50. {
  51. get
  52. {
  53. if (!HasComplexContent)
  54. {
  55. return _text;
  56. }
  57. var builder = new StringBuilder();
  58. foreach(var inline in this)
  59. {
  60. inline.AppendText(builder);
  61. }
  62. return builder.ToString();
  63. }
  64. set
  65. {
  66. if (HasComplexContent)
  67. {
  68. Add(new Run(value));
  69. }
  70. else
  71. {
  72. _text = value;
  73. }
  74. }
  75. }
  76. /// <summary>
  77. /// Add a text segment to the collection.
  78. /// <remarks>
  79. /// For non complex content this appends the text to the end of currently held text.
  80. /// For complex content this adds a <see cref="Run"/> to the collection.
  81. /// </remarks>
  82. /// </summary>
  83. /// <param name="text"></param>
  84. public void Add(string text)
  85. {
  86. if (HasComplexContent)
  87. {
  88. Add(new Run(text));
  89. }
  90. else
  91. {
  92. _text += text;
  93. }
  94. }
  95. public void Add(IControl child)
  96. {
  97. var implicitRun = new InlineUIContainer(child);
  98. Add(implicitRun);
  99. }
  100. public override void Add(Inline item)
  101. {
  102. if (!HasComplexContent)
  103. {
  104. if (!string.IsNullOrEmpty(_text))
  105. {
  106. base.Add(new Run(_text));
  107. }
  108. _text = string.Empty;
  109. }
  110. base.Add(item);
  111. }
  112. /// <summary>
  113. /// Raised when an inline in the collection changes.
  114. /// </summary>
  115. public event EventHandler? Invalidated;
  116. /// <summary>
  117. /// Raises the <see cref="Invalidated"/> event.
  118. /// </summary>
  119. protected void Invalidate()
  120. {
  121. if(_host != null)
  122. {
  123. _host.Invalidate();
  124. }
  125. Invalidated?.Invoke(this, EventArgs.Empty);
  126. }
  127. private void Invalidate(object? sender, EventArgs e) => Invalidate();
  128. }
  129. }