Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ThemeResource bindings are not scope-updated on Style switch #13016

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<UserControl x:Class="Uno.UI.RuntimeTests.When_ThemeResource_Style_Switch_Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<UserControl.Resources>
<Style x:Key="FirstButtonStyle" TargetType="Button">
<Setter Property="Background" Value="{ThemeResource ButtonBackground}" />
</Style>
<Style x:Key="SecondButtonStyle" TargetType="Button">
<Setter Property="Background" Value="{ThemeResource ButtonBackgroundPressed}" />
</Style>
</UserControl.Resources>
<StackPanel>
<Border>
<Border.Resources>
<SolidColorBrush x:Key="ButtonBackground" Color="Blue" />
<SolidColorBrush x:Key="ButtonBackgroundPressed" Color="Red" />
</Border.Resources>
<Button x:Name="TestBtn" Style="{StaticResource FirstButtonStyle}" Width="100" Height="100" />
</Border>
</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace Uno.UI.RuntimeTests;

public sealed partial class When_ThemeResource_Style_Switch_Page : UserControl
{
public When_ThemeResource_Style_Switch_Page()
{
this.InitializeComponent();
}

public Button TestButton => TestBtn;
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,29 @@ public async Task When_AppLevel_Resource_SplitButton_Override()
}
}

[TestMethod]
public async Task When_ThemeResource_Style_Switch()
{
using (StyleHelper.UseFluentStyles())
{
var SUT = new When_ThemeResource_Style_Switch_Page();
WindowHelper.WindowContent = SUT;
await WindowHelper.WaitForLoaded(SUT);

var color = ((SolidColorBrush)SUT.TestButton.Background).Color;

Assert.AreEqual(Colors.Blue, color);

SUT.TestButton.Style = (Style)SUT.Resources["SecondButtonStyle"];

await WindowHelper.WaitForIdle();

color = ((SolidColorBrush)SUT.TestButton.Background).Color;

Assert.AreEqual(Colors.Red, color);
}
}

private async Task When_DefaultForeground(Color lightThemeColor, Color darkThemeColor)
{
var run = new Run()
Expand Down
12 changes: 7 additions & 5 deletions src/Uno.UI/UI/Xaml/Style/Style.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

using Uno.Extensions;
using Uno.Foundation.Logging;
using Uno.UI;
using Windows.UI.Xaml.Data;
Expand Down Expand Up @@ -73,7 +71,7 @@ internal void ApplyTo(DependencyObject o, DependencyPropertyValuePrecedences pre
return;
}

var localPrecedenceDisposable = DependencyObjectExtensions.OverrideLocalPrecedence(o, precedence);
IDisposable? localPrecedenceDisposable = null;

EnsureSetterMap();

Expand All @@ -82,6 +80,7 @@ internal void ApplyTo(DependencyObject o, DependencyPropertyValuePrecedences pre
#endif
{
ResourceResolver.PushNewScope(_xamlScope);
localPrecedenceDisposable = DependencyObjectExtensions.OverrideLocalPrecedence(o, precedence);

if (_flattenedSetters != null)
{
Expand All @@ -91,15 +90,18 @@ internal void ApplyTo(DependencyObject o, DependencyPropertyValuePrecedences pre
}
}

localPrecedenceDisposable?.Dispose();
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved
localPrecedenceDisposable = null;

// Check tree for resource binding values, since some Setters may have set ThemeResource-backed values
(o as IDependencyObjectStoreProvider)!.Store.UpdateResourceBindings(ResourceUpdateReason.StaticResourceLoading);
(o as IDependencyObjectStoreProvider)!.Store.UpdateResourceBindings(ResourceUpdateReason.ResolvedOnLoading);
}
#if !HAS_EXPENSIVE_TRYFINALLY
finally
#endif
{
ResourceResolver.PopScope();
localPrecedenceDisposable?.Dispose();
ResourceResolver.PopScope();
}
}

Expand Down