Browse Source

Fix FloatingResourceObservable listening for ActualThemeVariantChanged

pull/11344/head
Martin Garstenauer 3 years ago
parent
commit
ec8cbeb37d
  1. 18
      src/Avalonia.Base/Controls/ResourceNodeExtensions.cs
  2. 33
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ThemeDictionariesTests.cs

18
src/Avalonia.Base/Controls/ResourceNodeExtensions.cs

@ -221,11 +221,25 @@ namespace Avalonia.Controls
{ {
_owner.ResourcesChanged += ResourcesChanged; _owner.ResourcesChanged += ResourcesChanged;
} }
if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost)
{
themeVariantHost.ActualThemeVariantChanged += ActualThemeVariantChanged;
}
} }
protected override void Deinitialize() protected override void Deinitialize()
{ {
_target.OwnerChanged -= OwnerChanged; _target.OwnerChanged -= OwnerChanged;
if (_owner is not null)
{
_owner.ResourcesChanged -= ResourcesChanged;
}
if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost)
{
themeVariantHost.ActualThemeVariantChanged -= ActualThemeVariantChanged;
}
_owner = null; _owner = null;
} }
@ -253,7 +267,7 @@ namespace Avalonia.Controls
} }
if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost) if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost)
{ {
themeVariantHost.ActualThemeVariantChanged += ActualThemeVariantChanged; themeVariantHost.ActualThemeVariantChanged -= ActualThemeVariantChanged;
} }
_owner = _target.Owner; _owner = _target.Owner;
@ -264,7 +278,7 @@ namespace Avalonia.Controls
} }
if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost2) if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost2)
{ {
themeVariantHost2.ActualThemeVariantChanged -= ActualThemeVariantChanged; themeVariantHost2.ActualThemeVariantChanged += ActualThemeVariantChanged;
} }
PublishNext(); PublishNext();

33
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ThemeDictionariesTests.cs

@ -110,6 +110,39 @@ public class ThemeDictionariesTests : XamlTestBase
Assert.Equal(Colors.Black, ((ISolidColorBrush)border.Background)!.Color); Assert.Equal(Colors.Black, ((ISolidColorBrush)border.Background)!.Color);
} }
[Fact]
public void DynamicResource_In_ResourceProvider_Updated_When_Control_Theme_Changed()
{
var themeVariantScope = new ThemeVariantScope
{
RequestedThemeVariant = ThemeVariant.Light,
Resources = new ResourceDictionary
{
ThemeDictionaries =
{
[ThemeVariant.Dark] = new ResourceDictionary { ["DemoBackground"] = Brushes.Black },
[ThemeVariant.Light] = new ResourceDictionary { ["DemoBackground"] = Brushes.White }
}
},
Child = new Border()
};
var resources = (ResourceDictionary)AvaloniaRuntimeXamlLoader.Load(@"
<ResourceDictionary xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<GeometryDrawing x:Key='Geo' Brush='{DynamicResource DemoBackground}' />
</ResourceDictionary>");
themeVariantScope.Resources.MergedDictionaries.Add(resources);
var geo = (GeometryDrawing)themeVariantScope.FindResource("Geo");
Assert.NotNull(geo);
Assert.Equal(Colors.White, ((ISolidColorBrush)geo.Brush)!.Color);
themeVariantScope.RequestedThemeVariant = ThemeVariant.Dark;
Assert.Equal(Colors.Black, ((ISolidColorBrush)geo.Brush)!.Color);
}
[Fact] [Fact]
public void Intermediate_StaticResource_Can_Be_Reached_From_ThemeDictionaries() public void Intermediate_StaticResource_Can_Be_Reached_From_ThemeDictionaries()
{ {

Loading…
Cancel
Save