Skip to content

Commit

Permalink
Merge pull request unoplatform#13866 from unoplatform/dev/jela/resour…
Browse files Browse the repository at this point in the history
…ces-alias
  • Loading branch information
jeromelaban authored Oct 2, 2023
2 parents 78cd421 + 9c6f2d8 commit e85bf7a
Show file tree
Hide file tree
Showing 19 changed files with 478 additions and 64 deletions.
1 change: 1 addition & 0 deletions build/cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"language": "en",
"words": [
"Avalonia",
"ambiently",
"binlog",
"Blazor",
"blockquotes",
Expand Down
8 changes: 8 additions & 0 deletions doc/articles/migrating-from-previous-releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ This change ensures that the XAML parser will only look for types in an explicit

In order to resolve types properly in a conditional XAML namespace, make use to use the [new syntax introduced in Uno 4.8](https://platform.uno/docs/articles/platform-specific-xaml.html?q=condition#specifying-namespaces).

#### ResourceDictionary now require an explicit Uri reference

Resources dictionaries are now required to be explicitly referenced by URI to be considered during resource resolution. Applications that are already running properly on WinAppSDK should not be impacted by this change.

The reason for this change is the alignment of the inclusion behavior with WinUI, which does not automatically place dictionaries as ambiently available.

This behavior can be disabled by using `FeatureConfiguration.ResourceDictionary.IncludeUnreferencedDictionaries`, by setting the value `true`.

#### `IsEnabled` property is moved from `FrameworkElement` to `Control`
This property was incorrectly located on `FrameworkElement` but its behavior has not changed.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<StaticResource x:Key="MyAliasBrush" ResourceKey="MyBrush" />
</ResourceDictionary>
<ResourceDictionary x:Key="Default">
<StaticResource x:Key="MyAliasBrush" ResourceKey="MyBrush" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

</ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="MyBrush" Color="{ThemeResource MyColor}" />
</ResourceDictionary>
<ResourceDictionary x:Key="Default">
<SolidColorBrush x:Key="MyBrush" Color="{ThemeResource MyColor}" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style x:Key="MyButtonStyle" TargetType="Button">
<Setter Property="Background" Value="{ThemeResource MyBrush}" />
</Style>

<Style x:Key="MyButtonWithAliasStyle" TargetType="Button">
<Setter Property="Background" Value="{ThemeResource MyAliasBrush}" />
</Style>
</ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<Color x:Key="MyColor">Green</Color>
</ResourceDictionary>
<ResourceDictionary x:Key="Default">
<Color x:Key="MyColor">DarkGreen</Color>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

<x:String x:Key="DummyResource">Dummy</x:String>
</ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<Color x:Key="MyColor">Red</Color>

</ResourceDictionary>
<ResourceDictionary x:Key="Default">
<Color x:Key="MyColor">DarkRed</Color>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

<x:String x:Key="DummyResource">Dummy</x:String>
</ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<Page x:Class="Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml.Controls.ThemeResource_Theme_Changing_Override"
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"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<local:ThemeResource_Theme_Changing_Override_Custom Mode="Red" />
</Page.Resources>
<StackPanel VerticalAlignment="Center"
HorizontalAlignment="Center"
Spacing="20">

<StackPanel Orientation="Horizontal"
Spacing="20"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Center"
Spacing="20">
<TextBlock Text="Page Resources"
HorizontalAlignment="Center" />
<Button x:Name="button01"
x:FieldModifier="public"
Background="{ThemeResource MyBrush}"
Width="200"
Height="100">
<TextBlock Text="MyBrush Should Be Red"
TextWrapping="WrapWholeWords"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Button>
<Button x:Name="button02"
x:FieldModifier="public"
Background="{ThemeResource MyAliasBrush}"
Width="200"
Height="100">
<TextBlock Text="MyAliasBrush Should Be Red"
TextWrapping="WrapWholeWords"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Button>
<Button x:Name="button03"
x:FieldModifier="public"
Style="{StaticResource MyButtonStyle}"
Width="200"
Height="100">
<TextBlock Text="MyButtonStyle Should Be Red"
TextWrapping="WrapWholeWords"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Button>
<Button x:Name="button04"
x:FieldModifier="public"
Style="{StaticResource MyButtonWithAliasStyle}"
Width="200"
Height="100">
<TextBlock Text="MyButtonWithAliasStyle Should Be Red"
TextWrapping="WrapWholeWords"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Button>
</StackPanel>
<StackPanel Spacing="20">
<StackPanel.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:ThemeResource_Theme_Changing_Override_Custom Mode="Green" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</StackPanel.Resources>
<TextBlock Text="Local Override Resources"
HorizontalAlignment="Center" />
<Button x:Name="button01_override"
x:FieldModifier="public"
Background="{ThemeResource MyBrush}"
Width="200"
Height="100">
<TextBlock Text="MyBrush Should Be Green"
TextWrapping="WrapWholeWords"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Button>
<Button x:Name="button02_override"
x:FieldModifier="public"
Background="{ThemeResource MyAliasBrush}"
Width="200"
Height="100">
<TextBlock Text="MyAliasBrush Should Be Green"
TextWrapping="WrapWholeWords"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Button>
<Button x:Name="button03_override"
x:FieldModifier="public"
Style="{StaticResource MyButtonStyle}"
Width="200"
Height="100">
<TextBlock Text="MyButtonStyle Should Be Green"
TextWrapping="WrapWholeWords"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Button>
<Button x:Name="button04_override"
x:FieldModifier="public"
Style="{StaticResource MyButtonWithAliasStyle}"
Width="200"
Height="100">
<TextBlock Text="MyButtonWithAliasStyle Should Be Green"
TextWrapping="WrapWholeWords"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Button>
</StackPanel>
</StackPanel>
</StackPanel>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml.Controls;

/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class ThemeResource_Theme_Changing_Override : Page
{
public ThemeResource_Theme_Changing_Override()
{
this.InitializeComponent();
}
}

public class ThemeResource_Theme_Changing_Override_Custom : ResourceDictionary
{
private const string GreenUri = "ms-appx:///Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Controls/ThemeResource_TCO_MyColorGreen.xaml";
private const string RedUri = "ms-appx:///Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Controls/ThemeResource_TCO_MyColorRed.xaml";
private const string BrushUri = "ms-appx:///Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Controls/ThemeResource_TCO_MyBrush.xaml";
private const string AliasUri = "ms-appx:///Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Controls/ThemeResource_TCO_MyAlias.xaml";
private const string ButtonUri = "ms-appx:///Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Controls/ThemeResource_TCO_MyButton.xaml";

private string _mode;

public string Mode
{
get => _mode;
set
{
_mode = value;
var colorUri = _mode == "Green" ? GreenUri : RedUri;

var myBrush = new ResourceDictionary { Source = new Uri(BrushUri) };
var myAlias = new ResourceDictionary { Source = new Uri(AliasUri) };
var myButton = new ResourceDictionary { Source = new Uri(ButtonUri) };

myBrush.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri(colorUri) });
myAlias.MergedDictionaries.Add(myBrush);
myButton.MergedDictionaries.Add(myAlias);

MergedDictionaries.Add(myButton);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,42 @@ public async Task When_ThemeResource_Style_Switch()
}
}

[TestMethod]
public async Task When_Theme_Changed()
{
using var _ = StyleHelper.UseFluentStyles();

var control = new ThemeResource_Theme_Changing_Override();
WindowHelper.WindowContent = control;

await WindowHelper.WaitForIdle();

Assert.AreEqual(Colors.Red, (control.button01.Background as SolidColorBrush)?.Color);
Assert.AreEqual(Colors.Red, (control.button02.Background as SolidColorBrush)?.Color);
Assert.AreEqual(Colors.Red, (control.button03.Background as SolidColorBrush)?.Color);
Assert.AreEqual(Colors.Red, (control.button04.Background as SolidColorBrush)?.Color);

Assert.AreEqual(Colors.Green, (control.button01_override.Background as SolidColorBrush)?.Color);
Assert.AreEqual(Colors.Green, (control.button02_override.Background as SolidColorBrush)?.Color);
Assert.AreEqual(Colors.Green, (control.button03_override.Background as SolidColorBrush)?.Color);
Assert.AreEqual(Colors.Green, (control.button04_override.Background as SolidColorBrush)?.Color);

using (ThemeHelper.UseDarkTheme())
{
await WindowHelper.WaitForIdle();

Assert.AreEqual(Colors.DarkRed, (control.button01.Background as SolidColorBrush)?.Color);
Assert.AreEqual(Colors.DarkRed, (control.button02.Background as SolidColorBrush)?.Color);
Assert.AreEqual(Colors.DarkRed, (control.button03.Background as SolidColorBrush)?.Color);
Assert.AreEqual(Colors.DarkRed, (control.button04.Background as SolidColorBrush)?.Color);

Assert.AreEqual(Colors.DarkGreen, (control.button01_override.Background as SolidColorBrush)?.Color);
Assert.AreEqual(Colors.DarkGreen, (control.button02_override.Background as SolidColorBrush)?.Color);
Assert.AreEqual(Colors.DarkGreen, (control.button03_override.Background as SolidColorBrush)?.Color);
Assert.AreEqual(Colors.DarkGreen, (control.button04_override.Background as SolidColorBrush)?.Color);
}
}

private async Task When_DefaultForeground(Color lightThemeColor, Color darkThemeColor)
{
var run = new Run()
Expand Down
1 change: 0 additions & 1 deletion src/Uno.UI.Tests/Windows_UI_Xaml/Given_ThemeResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ async Task GoTo(string stateName)
}

[TestMethod]
[Ignore("DoubleAnimation not supported by Uno.IS_UNIT_TESTS")]
public async Task When_Visual_States_DoubleAnimation_Theme_Changed_Reapplied()
{
var page = new ThemeResource_In_Visual_States_Page();
Expand Down
9 changes: 9 additions & 0 deletions src/Uno.UI/FeatureConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ public static class DependencyObject
= true;
}

public static class ResourceDictionary
{
/// <summary>
/// Determines whether unreferenced ResourceDictionary present in the assembly
/// are accessible from app resources.
/// </summary>
public static bool IncludeUnreferencedDictionaries { get; set; }
}

public static class Font
{
private static string _symbolsFont =
Expand Down
5 changes: 5 additions & 0 deletions src/Uno.UI/UI/Xaml/Data/ResourceUpdateReason.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ internal enum ResourceUpdateReason
/// </summary>
HotReload = 4,

/// <summary>
/// Update marked as XamlLoader
/// </summary>
XamlParser = 8,

/// <summary>
/// Updates that should be propagated recursively through the visual tree
/// </summary>
Expand Down
Loading

0 comments on commit e85bf7a

Please sign in to comment.