Skip to content

Commit

Permalink
Fixes/inline host change propagation (AvaloniaUI#14914)
Browse files Browse the repository at this point in the history
* Add failing test

* Propagate InlineHost to children
  • Loading branch information
Gillibald authored Mar 11, 2024
1 parent fc709df commit 9849da2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Avalonia.Controls/Documents/InlineCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ private void OnInlineHostChanged(IInlineHost? oldValue, IInlineHost? newValue)
{
foreach (var child in this)
{
child.InlineHost = newValue;

if (child is not InlineUIContainer container)
{
continue;
Expand Down
8 changes: 5 additions & 3 deletions src/Avalonia.Controls/Documents/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ internal override void OnInlineHostChanged(IInlineHost? oldValue, IInlineHost? n

private void OnInlinesChanged(InlineCollection? oldValue, InlineCollection? newValue)
{
void OnInlinesInvalidated(object? sender, EventArgs e)
=> InlineHost?.Invalidate();

if (oldValue is not null)
{
oldValue.LogicalChildren = null;
Expand All @@ -93,6 +90,11 @@ void OnInlinesInvalidated(object? sender, EventArgs e)
newValue.InlineHost = InlineHost;
newValue.Invalidated += OnInlinesInvalidated;
}

return;

void OnInlinesInvalidated(object? sender, EventArgs e)
=> InlineHost?.Invalidate();
}
}
}
17 changes: 17 additions & 0 deletions tests/Avalonia.Controls.UnitTests/TextBlockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,23 @@ public void Changing_Inlines_Should_Reset_Inlines_Parent()
}
}

[Fact]
public void Changing_InlineHost_Should_Propagate_To_Nested_Inlines()
{
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
{
var target = new TextBlock();

var span = new Span { Inlines = new InlineCollection { new Run { Text = "World" } } };

var inlines = new InlineCollection{ new Run{Text = "Hello "}, span };

target.Inlines = inlines;

Assert.Equal(target, span.InlineHost);
}
}

[Fact]
public void Changing_Inlines_Should_Reset_VisualChildren()
{
Expand Down

0 comments on commit 9849da2

Please sign in to comment.