Browse Source

Merge pull request #9349 from Gillibald/fixes/txtBlockTextUpdate

Fixes TextBlock TextProperty update handling
pull/9361/head
Max Katz 3 years ago
committed by GitHub
parent
commit
aa7cccb495
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 55
      src/Avalonia.Controls/Documents/InlineCollection.cs
  2. 19
      src/Avalonia.Controls/TextBlock.cs
  3. 22
      tests/Avalonia.Controls.UnitTests/TextBlockTests.cs

55
src/Avalonia.Controls/Documents/InlineCollection.cs

@ -89,56 +89,45 @@ namespace Avalonia.Controls.Documents
} }
public override void Add(Inline inline)
{
if (InlineHost is TextBlock textBlock && !string.IsNullOrEmpty(textBlock._text))
{
base.Add(new Run(textBlock._text));
textBlock._text = null;
}
base.Add(inline);
}
/// <summary> /// <summary>
/// Add a text segment to the collection. /// Adds a text segment to the collection.
/// <remarks> /// <remarks>
/// For non complex content this appends the text to the end of currently held text. /// For non complex content this appends the text to the end of currently held text.
/// For complex content this adds a <see cref="Run"/> to the collection. /// For complex content this adds a <see cref="Run"/> to the collection.
/// </remarks> /// </remarks>
/// </summary> /// </summary>
/// <param name="text"></param> /// <param name="text">The to be added text.</param>
public void Add(string text) public void Add(string text)
{ {
AddText(text); if (InlineHost is TextBlock textBlock && !textBlock.HasComplexContent)
}
public override void Add(Inline inline)
{
OnAdd();
base.Add(inline);
}
public void Add(IControl child)
{
OnAdd();
base.Add(new InlineUIContainer(child));
}
private void AddText(string text)
{
if (LogicalChildren is TextBlock textBlock && !textBlock.HasComplexContent)
{ {
textBlock._text += text; textBlock._text += text;
} }
else else
{ {
base.Add(new Run(text)); Add(new Run(text));
} }
} }
private void OnAdd() /// <summary>
/// Adds a control wrapped inside a <see cref="InlineUIContainer"/> to the collection.
/// </summary>
/// <param name="control">The to be added control.</param>
public void Add(IControl control)
{ {
if (LogicalChildren is TextBlock textBlock) Add(new InlineUIContainer(control));
{
if (!textBlock.HasComplexContent && !string.IsNullOrEmpty(textBlock._text))
{
base.Add(new Run(textBlock._text));
textBlock._text = null;
}
}
} }
/// <summary> /// <summary>

19
src/Avalonia.Controls/TextBlock.cs

@ -597,23 +597,12 @@ namespace Avalonia.Controls
protected virtual void SetText(string? text) protected virtual void SetText(string? text)
{ {
if (Inlines != null && Inlines.Count > 0) if (HasComplexContent)
{
var oldValue = Inlines.Text;
if (!string.IsNullOrEmpty(text))
{
Inlines.Add(text);
}
text = Inlines.Text;
RaisePropertyChanged(TextProperty, oldValue, text);
}
else
{ {
SetAndRaise(TextProperty, ref _text, text); Inlines?.Clear();
} }
SetAndRaise(TextProperty, ref _text, text);
} }
/// <summary> /// <summary>

22
tests/Avalonia.Controls.UnitTests/TextBlockTests.cs

@ -3,6 +3,7 @@ using Avalonia.Controls.Documents;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
using Avalonia.Data; using Avalonia.Data;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Metadata;
using Avalonia.Rendering; using Avalonia.Rendering;
using Avalonia.UnitTests; using Avalonia.UnitTests;
using Moq; using Moq;
@ -181,5 +182,26 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(60, button.Bounds.Left); Assert.Equal(60, button.Bounds.Left);
} }
} }
[Fact]
public void Setting_Text_Should_Reset_Inlines()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var target = new TextBlock();
target.Inlines.Add(new Run("Hello World"));
Assert.Equal("Hello World", target.Text);
Assert.Equal(1, target.Inlines.Count);
target.Text = "1234";
Assert.Equal("1234", target.Text);
Assert.Equal(0, target.Inlines.Count);
}
}
} }
} }

Loading…
Cancel
Save