Browse Source

Workaround for the inherited default value bug

pull/10149/head
Max Katz 4 years ago
parent
commit
af70f74c88
  1. 3
      src/Avalonia.Controls/ThemeVariantScope.cs
  2. 73
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ThemeDictionariesTests.cs

3
src/Avalonia.Controls/ThemeVariantScope.cs

@ -13,8 +13,7 @@ namespace Avalonia.Controls
public static readonly StyledProperty<ThemeVariant> ActualThemeVariantProperty =
AvaloniaProperty.Register<ThemeVariantScope, ThemeVariant>(
nameof(ActualThemeVariant),
inherits: true,
defaultValue: ThemeVariant.Light);
inherits: true);
/// <summary>
/// Defines the <see cref="RequestedThemeVariant"/> property.

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

@ -1,8 +1,12 @@
using Avalonia.Controls;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Markup.Data;
using Avalonia.Markup.Xaml.MarkupExtensions;
using Avalonia.Media;
using Avalonia.Styling;
using Avalonia.UnitTests;
using Moq;
using Xunit;
@ -444,4 +448,71 @@ public class ThemeDictionariesTests : XamlTestBase
Assert.Equal(Colors.Black, ((ISolidColorBrush)border.Background)!.Color);
}
[Fact]
public void Theme_Switch_Works_In_Nested_Scope()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var window = (Window)AvaloniaRuntimeXamlLoader.Load(@"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
RequestedThemeVariant='Dark'>
<ThemeVariantScope Name='Scope'>
<TextBlock Name='Text' />
</ThemeVariantScope>
</Window>");
window.ApplyTemplate();
var scope = window.FindControl<ThemeVariantScope>("Scope")!;
var text = window.FindControl<TextBlock>("Text")!;
Assert.Equal(ThemeVariant.Dark, text.ActualThemeVariant);
Assert.Equal(Color.Parse("#dedede"), ((ISolidColorBrush)text.Foreground!).Color);
scope.RequestedThemeVariant = ThemeVariant.Light;
Assert.Equal(ThemeVariant.Light, text.ActualThemeVariant);
Assert.Equal(Colors.Black, ((ISolidColorBrush)text.Foreground!).Color);
}
}
[Fact]
public void Theme_Switch_Works_In_With_Popup()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var window = (Window)AvaloniaRuntimeXamlLoader.Load(@"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<ThemeVariantScope Name='Scope' RequestedThemeVariant='Dark'>
<Popup Name='Popup'>
<Border Width='100' Height='100'
Background='{DynamicResource ThemeBackgroundBrush}'>
</Border>
</Popup>
</ThemeVariantScope>
</Window>");
window.Show();
var scope = window.FindControl<ThemeVariantScope>("Scope")!;
var popup = window.FindControl<Popup>("Popup")!;
popup.IsOpen = true;
var border = (Border)popup.Child!;
Assert.Equal(ThemeVariant.Dark, popup.ActualThemeVariant);
Assert.Equal(ThemeVariant.Dark, border.ActualThemeVariant);
Assert.Equal(Color.Parse("#282828"), ((ISolidColorBrush)border.Background!).Color);
scope.RequestedThemeVariant = ThemeVariant.Light;
Assert.Equal(ThemeVariant.Light, popup.ActualThemeVariant);
Assert.Equal(ThemeVariant.Light, border.ActualThemeVariant);
Assert.Equal(Colors.White, ((ISolidColorBrush)border.Background!).Color);
}
}
}
}

Loading…
Cancel
Save