Browse Source
Merge pull request #11344 from garstenauer/fix-11324-dynamicresource-not-updated-on-theme-change
Fix: FloatingResourceObservable should handle ActualThemeVariantChanged
pull/11363/head
Max Katz
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
49 additions and
2 deletions
-
src/Avalonia.Base/Controls/ResourceNodeExtensions.cs
-
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ThemeDictionariesTests.cs
|
|
|
@ -221,11 +221,25 @@ namespace Avalonia.Controls |
|
|
|
{ |
|
|
|
_owner.ResourcesChanged += ResourcesChanged; |
|
|
|
} |
|
|
|
if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost) |
|
|
|
{ |
|
|
|
themeVariantHost.ActualThemeVariantChanged += ActualThemeVariantChanged; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected override void Deinitialize() |
|
|
|
{ |
|
|
|
_target.OwnerChanged -= OwnerChanged; |
|
|
|
|
|
|
|
if (_owner is not null) |
|
|
|
{ |
|
|
|
_owner.ResourcesChanged -= ResourcesChanged; |
|
|
|
} |
|
|
|
if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost) |
|
|
|
{ |
|
|
|
themeVariantHost.ActualThemeVariantChanged -= ActualThemeVariantChanged; |
|
|
|
} |
|
|
|
|
|
|
|
_owner = null; |
|
|
|
} |
|
|
|
|
|
|
|
@ -253,7 +267,7 @@ namespace Avalonia.Controls |
|
|
|
} |
|
|
|
if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost) |
|
|
|
{ |
|
|
|
themeVariantHost.ActualThemeVariantChanged += ActualThemeVariantChanged; |
|
|
|
themeVariantHost.ActualThemeVariantChanged -= ActualThemeVariantChanged; |
|
|
|
} |
|
|
|
|
|
|
|
_owner = _target.Owner; |
|
|
|
@ -264,7 +278,7 @@ namespace Avalonia.Controls |
|
|
|
} |
|
|
|
if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost2) |
|
|
|
{ |
|
|
|
themeVariantHost2.ActualThemeVariantChanged -= ActualThemeVariantChanged; |
|
|
|
themeVariantHost2.ActualThemeVariantChanged += ActualThemeVariantChanged; |
|
|
|
} |
|
|
|
|
|
|
|
PublishNext(); |
|
|
|
|
|
|
|
@ -110,6 +110,39 @@ public class ThemeDictionariesTests : XamlTestBase |
|
|
|
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] |
|
|
|
public void Intermediate_StaticResource_Can_Be_Reached_From_ThemeDictionaries() |
|
|
|
{ |
|
|
|
|