Skip to content

Commit

Permalink
Merge pull request #10273 from jp2masa/issue-10226
Browse files Browse the repository at this point in the history
Ensure templated parent control theme is applied
  • Loading branch information
grokys authored Feb 21, 2023
2 parents 7267e1c + f0a6f44 commit b8b6324
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/Avalonia.Base/Layout/Layoutable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,12 @@ private protected override void OnControlThemeChanged()
InvalidateMeasure();
}

internal override void OnTemplatedParentControlThemeChanged()
{
base.OnTemplatedParentControlThemeChanged();
InvalidateMeasure();
}

/// <summary>
/// Called when the layout manager raises a LayoutUpdated event.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Base/StyledElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public virtual void EndInit()
/// </returns>
public bool ApplyStyling()
{
if (_initCount == 0 && (!_stylesApplied || !_themeApplied))
if (_initCount == 0 && (!_stylesApplied || !_themeApplied || !_templatedParentThemeApplied))
{
GetValueStore().BeginStyling();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void Unrelated_Styles_Are_Not_Detached_When_Theme_Property_Cleared()
target.Theme = null;
Assert.Equal("style", target.Tag);
}

[Fact]
public void TemplatedParent_Theme_Is_Detached_From_Template_Controls_When_Theme_Property_Cleared()
{
Expand Down Expand Up @@ -539,20 +539,50 @@ public void Theme_Can_Be_Set_To_LocalValue_While_Updating_Due_To_Style_Class()
Assert.Same(target.Theme, theme3);
}

[Fact]
public void TemplatedParent_Theme_Change_Applies_To_Children()
{
var theme = CreateDerivedTheme();
var target = CreateTarget();

Assert.Null(target.Theme);
Assert.Null(target.Template);

var root = CreateRoot(target, theme.BasedOn);

Assert.NotNull(target.Theme);
Assert.NotNull(target.Template);

root.Styles.Add(new Style(x => x.OfType<ThemedControl>().Class("foo"))
{
Setters = { new Setter(StyledElement.ThemeProperty, theme) }
});

root.LayoutManager.ExecuteLayoutPass();

var border = Assert.IsType<Border>(target.VisualChild);
Assert.Equal(Brushes.Red, border.Background);

target.Classes.Add("foo");
root.LayoutManager.ExecuteLayoutPass();

Assert.Equal(Brushes.Green, border.Background);
}

private static ThemedControl CreateTarget()
{
return new ThemedControl();
}

private static TestRoot CreateRoot(Control child)
private static TestRoot CreateRoot(Control child, ControlTheme? theme = null)
{
var result = new TestRoot()
{
Styles =
{
new Style(x => x.OfType<ThemedControl>())
{
Setters = { new Setter(StyledElement.ThemeProperty, CreateTheme()) }
Setters = { new Setter(StyledElement.ThemeProperty, theme ?? CreateTheme()) }
}
}
};
Expand Down Expand Up @@ -580,8 +610,8 @@ private static ControlTheme CreateTheme(string tag = "theme")
{
new Style(x => x.Nesting().Template().OfType<Border>())
{
Setters =
{
Setters =
{
new Setter(Border.BackgroundProperty, Brushes.Red),
new Setter(Control.TagProperty, tag),
}
Expand Down

0 comments on commit b8b6324

Please sign in to comment.