From 4058c396dba3029087845ff95318942cb030e14e Mon Sep 17 00:00:00 2001 From: Marc-Antoine Soucy Date: Wed, 11 Oct 2023 15:55:14 -0400 Subject: [PATCH] feat: add resource override path extensions --- doc/material-controls-extensions.md | 65 +++++++++++++++++++ .../Extensions/ControlExtensions.cs | 29 +++++++++ .../ControlExtensionsSamplePage.xaml | 59 ++++++++++++++++- .../ControlResourcesOverride/CheckBox.xaml | 45 +++++++++++++ .../Uno.Themes.Samples.Shared.projitems | 4 ++ 5 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 src/samples/UWP/Uno.Themes.Samples.Shared/ControlResourcesOverride/CheckBox.xaml diff --git a/doc/material-controls-extensions.md b/doc/material-controls-extensions.md index cb8131294..67ec02965 100644 --- a/doc/material-controls-extensions.md +++ b/doc/material-controls-extensions.md @@ -131,3 +131,68 @@ The following control styles have support for surface tint: | Control | Supporting Styles | |---------|---------------------| | Button | ElevatedButtonStyle | + + +## ResourcePathOverride + +This extension allows you to set the path of a dictionary in your project for a specific control. It makes [lightweight styling](lightweight-styling.md) easier by enabling controls, like buttons, to link directly to resource dictionaries instead of requiring each resource to be written on the page. This means you can have multiple buttons with the same style but different colors simply by linking them to different resource dictionaries. It also allows for the reuse of these dictionaries across various controls, making the styling process more efficient and manageable. + +Here is an example of how LightWeightStyling could be applied could be used on a checkbox. +* CheckBox: + + ```xml + + ``` + +* ControlResourcesOverride/CheckBox.xaml content : + + ```xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ``` + diff --git a/src/library/Uno.Material/Extensions/ControlExtensions.cs b/src/library/Uno.Material/Extensions/ControlExtensions.cs index 33a89d008..1ec5536e0 100644 --- a/src/library/Uno.Material/Extensions/ControlExtensions.cs +++ b/src/library/Uno.Material/Extensions/ControlExtensions.cs @@ -130,6 +130,35 @@ public static class ControlExtensions #endregion + #region DependencyProperty: ResourceOverridePath + public static readonly DependencyProperty ResourceOverridePathProperty = + DependencyProperty.RegisterAttached( + "ResourceOverridePath", + typeof(string), + typeof(ControlExtensions), + new PropertyMetadata(null, OnResourceOverridePathChanged)); + + public static void SetResourceOverridePath(FrameworkElement element, string value) + { + element.SetValue(ResourceOverridePathProperty, value); + } + + public static string GetResourceOverridePath(FrameworkElement element) + { + return (string)element.GetValue(ResourceOverridePathProperty); + } + + private static void OnResourceOverridePathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is FrameworkElement fe) + { + fe.Resources = e.NewValue is string path + ? new ResourceDictionary() { Source = new Uri(path, UriKind.RelativeOrAbsolute) } + : default; + } + } + #endregion + private static void OnElevationChanged(DependencyObject element, DependencyPropertyChangedEventArgs e) => SurfaceTintExtensions.OnElevationChanged(element, (int)e.NewValue); diff --git a/src/samples/UWP/Uno.Themes.Samples.Shared/Content/Extensions/ControlExtensionsSamplePage.xaml b/src/samples/UWP/Uno.Themes.Samples.Shared/Content/Extensions/ControlExtensionsSamplePage.xaml index 810edc992..ec4a18a0b 100644 --- a/src/samples/UWP/Uno.Themes.Samples.Shared/Content/Extensions/ControlExtensionsSamplePage.xaml +++ b/src/samples/UWP/Uno.Themes.Samples.Shared/Content/Extensions/ControlExtensionsSamplePage.xaml @@ -2,12 +2,13 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:example="http://schemas.microsoft.com/expression/blend/2008" xmlns:android="http://uno.ui/android" xmlns:ios="http://uno.ui/ios" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:um="using:Uno.Material" xmlns:smtx="using:ShowMeTheXAML" - mc:Ignorable="d android ios"> + mc:Ignorable="d android ios example"> M 2 0 C 0.8999999761581421 0 0 0.8999999761581421 0 2 C 0 3.100000023841858 0.8999999761581421 4 2 4 C 3.100000023841858 4 4 3.100000023841858 4 2 C 4 0.8999999761581421 3.100000023841858 0 2 0 Z M 14 0 C 12.899999976158142 0 12 0.8999999761581421 12 2 C 12 3.100000023841858 12.899999976158142 4 14 4 C 15.100000023841858 4 16 3.100000023841858 16 2 C 16 0.8999999761581421 15.100000023841858 0 14 0 Z M 8 0 C 6.899999976158142 0 6 0.8999999761581421 6 2 C 6 3.100000023841858 6.899999976158142 4 8 4 C 9.100000023841858 4 10 3.100000023841858 10 2 C 10 0.8999999761581421 9.100000023841858 0 8 0 Z M 2 4 C 3.100000023841858 4 4 3.100000023841858 4 2 C 4 0.8999999761581421 3.100000023841858 0 2 0 C 0.8999999761581421 0 0 0.8999999761581421 0 2 C 0 3.100000023841858 0.8999999761581421 4 2 4 Z M 2 6 C 0.8999999761581421 6 0 6.899999976158142 0 8 C 0 9.100000023841858 0.8999999761581421 10 2 10 C 3.100000023841858 10 4 9.100000023841858 4 8 C 4 6.899999976158142 3.100000023841858 6 2 6 Z M 2 12 C 0.8999999761581421 12 0 12.899999976158142 0 14 C 0 15.100000023841858 0.8999999761581421 16 2 16 C 3.100000023841858 16 4 15.100000023841858 4 14 C 4 12.899999976158142 3.100000023841858 12 2 12 Z @@ -151,6 +152,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/samples/UWP/Uno.Themes.Samples.Shared/ControlResourcesOverride/CheckBox.xaml b/src/samples/UWP/Uno.Themes.Samples.Shared/ControlResourcesOverride/CheckBox.xaml new file mode 100644 index 000000000..90a455103 --- /dev/null +++ b/src/samples/UWP/Uno.Themes.Samples.Shared/ControlResourcesOverride/CheckBox.xaml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/samples/UWP/Uno.Themes.Samples.Shared/Uno.Themes.Samples.Shared.projitems b/src/samples/UWP/Uno.Themes.Samples.Shared/Uno.Themes.Samples.Shared.projitems index 4a2637c1d..92f2d69e7 100644 --- a/src/samples/UWP/Uno.Themes.Samples.Shared/Uno.Themes.Samples.Shared.projitems +++ b/src/samples/UWP/Uno.Themes.Samples.Shared/Uno.Themes.Samples.Shared.projitems @@ -335,6 +335,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + Designer MSBuild:Compile