Skip to content

Commit

Permalink
Merge pull request #13016 from unoplatform/dev/mazi/style-setters-the…
Browse files Browse the repository at this point in the history
…ming

fix: `ThemeResource` bindings are not scope-updated on `Style` switch
  • Loading branch information
MartinZikmund authored Jul 27, 2023
2 parents d6222e8 + b97d8c5 commit 7dd4964
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
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();
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

0 comments on commit 7dd4964

Please sign in to comment.