diff --git a/src/Controls/src/Core/Platform/Windows/Extensions/FrameworkElementExtensions.cs b/src/Controls/src/Core/Platform/Windows/Extensions/FrameworkElementExtensions.cs deleted file mode 100644 index 43a232fcc22f..000000000000 --- a/src/Controls/src/Core/Platform/Windows/Extensions/FrameworkElementExtensions.cs +++ /dev/null @@ -1,98 +0,0 @@ -#nullable disable -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Microsoft.Maui.Controls.Internals; -using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Controls; -using Microsoft.UI.Xaml.Media; -using WBinding = Microsoft.UI.Xaml.Data.Binding; -using WBindingExpression = Microsoft.UI.Xaml.Data.BindingExpression; -using WBrush = Microsoft.UI.Xaml.Media.Brush; - -namespace Microsoft.Maui.Controls.Platform -{ - internal static class FrameworkElementExtensions - { - static readonly Lazy> ForegroundProperties = - new Lazy>(() => new ConcurrentDictionary()); - - public static WBrush GetForeground(this FrameworkElement element) - { - if (element == null) - throw new ArgumentNullException("element"); - - return (WBrush)element.GetValue(GetForegroundProperty(element)); - } - - public static WBinding GetForegroundBinding(this FrameworkElement element) - { - WBindingExpression expr = element.GetBindingExpression(GetForegroundProperty(element)); - if (expr == null) - return null; - - return expr.ParentBinding; - } - - public static object GetForegroundCache(this FrameworkElement element) - { - WBinding binding = GetForegroundBinding(element); - if (binding != null) - return binding; - - return GetForeground(element); - } - - public static void RestoreForegroundCache(this FrameworkElement element, object cache) - { - var binding = cache as WBinding; - if (binding != null) - SetForeground(element, binding); - else - SetForeground(element, (WBrush)cache); - } - - public static void SetForeground(this FrameworkElement element, WBrush foregroundBrush) - { - if (element == null) - throw new ArgumentNullException("element"); - - element.SetValue(GetForegroundProperty(element), foregroundBrush); - } - - public static void SetForeground(this FrameworkElement element, WBinding binding) - { - if (element == null) - throw new ArgumentNullException("element"); - - element.SetBinding(GetForegroundProperty(element), binding); - } - - static DependencyProperty GetForegroundProperty(FrameworkElement element) - { - if (element is Control) - return Control.ForegroundProperty; - if (element is TextBlock) - return TextBlock.ForegroundProperty; - - Type type = element.GetType(); - - DependencyProperty foregroundProperty; - if (!ForegroundProperties.Value.TryGetValue(type, out foregroundProperty)) - { - FieldInfo field = ReflectionExtensions.GetFields(type).FirstOrDefault(f => f.Name == "ForegroundProperty"); - if (field == null) - throw new ArgumentException("type is not a Foregroundable type"); - - var property = (DependencyProperty)field.GetValue(null); - ForegroundProperties.Value.TryAdd(type, property); - - return property; - } - - return foregroundProperty; - } - } -} \ No newline at end of file diff --git a/src/Core/src/Platform/Windows/ActivityIndicatorExtensions.cs b/src/Core/src/Platform/Windows/ActivityIndicatorExtensions.cs index a0125a7e54f5..9b7a5911a1b3 100644 --- a/src/Core/src/Platform/Windows/ActivityIndicatorExtensions.cs +++ b/src/Core/src/Platform/Windows/ActivityIndicatorExtensions.cs @@ -2,6 +2,7 @@ using Microsoft.Maui.Graphics; using Microsoft.Maui.Primitives; using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Media; namespace Microsoft.Maui.Platform { @@ -11,6 +12,7 @@ public static void UpdateIsRunning(this ProgressRing platformActivityIndicator, { platformActivityIndicator.IsActive = virtualView.IsRunning; } + public static void UpdateColor(this ProgressRing platformActivityIndicator, IActivityIndicator activityIndicator) { platformActivityIndicator.UpdateColor(activityIndicator, null); @@ -18,19 +20,38 @@ public static void UpdateColor(this ProgressRing platformActivityIndicator, IAct public static void UpdateColor(this ProgressRing platformActivityIndicator, IActivityIndicator activityIndicator, object? foregroundDefault) { - Color color = activityIndicator.Color; + var color = activityIndicator.Color; + Brush? brush = null; if (color.IsDefault()) { - if (foregroundDefault != null) - platformActivityIndicator.RestoreForegroundCache(foregroundDefault); + if(foregroundDefault is Brush defaultBrush) + brush = defaultBrush; + } + else + { + brush = color.ToPlatform(); + } + + if (brush is null) + { + platformActivityIndicator.Resources.RemoveKeys(ProgressRingForegroundResourceKeys); + platformActivityIndicator.ClearValue(ProgressRing.ForegroundProperty); } else { - platformActivityIndicator.Foreground = color.ToPlatform(); + platformActivityIndicator.Resources.SetValueForAllKey(ProgressRingForegroundResourceKeys, brush); + platformActivityIndicator.Foreground = brush; } + + platformActivityIndicator.RefreshThemeResources(); } + static readonly string[] ProgressRingForegroundResourceKeys = + { + "ProgressRingForegroundThemeBrush" + }; + public static void UpdateWidth(this ProgressRing platformActivityIndicator, IActivityIndicator activityIndicator) { if (Dimension.IsExplicitSet(activityIndicator.Width)) diff --git a/src/Core/src/Platform/Windows/FrameworkElementExtensions.cs b/src/Core/src/Platform/Windows/FrameworkElementExtensions.cs index a5a48381ec37..dbf40f829bc1 100644 --- a/src/Core/src/Platform/Windows/FrameworkElementExtensions.cs +++ b/src/Core/src/Platform/Windows/FrameworkElementExtensions.cs @@ -1,28 +1,18 @@ #nullable enable using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Reflection; -using System.Threading.Tasks; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; using Windows.UI.ViewManagement; -using WBinding = Microsoft.UI.Xaml.Data.Binding; -using WBindingExpression = Microsoft.UI.Xaml.Data.BindingExpression; -using WBrush = Microsoft.UI.Xaml.Media.Brush; using WPoint = Windows.Foundation.Point; namespace Microsoft.Maui.Platform { internal static class FrameworkElementExtensions { - static readonly Lazy> ForegroundProperties = - new Lazy>(() => new ConcurrentDictionary()); - public static T? GetResource(this FrameworkElement element, string key, T? def = default) { if (element.Resources.TryGetValue(key, out var resource)) @@ -31,59 +21,6 @@ internal static class FrameworkElementExtensions return def; } - public static WBrush GetForeground(this FrameworkElement element) - { - if (element == null) - throw new ArgumentNullException(nameof(element)); - - return (WBrush)element.GetValue(GetForegroundProperty(element)); - } - - public static WBinding? GetForegroundBinding(this FrameworkElement element) - { - WBindingExpression expr = element.GetBindingExpression(GetForegroundProperty(element)); - - if (expr == null) - return null; - - return expr.ParentBinding; - } - - public static object GetForegroundCache(this FrameworkElement element) - { - WBinding? binding = GetForegroundBinding(element); - - if (binding != null) - return binding; - - return GetForeground(element); - } - - public static void RestoreForegroundCache(this FrameworkElement element, object cache) - { - var binding = cache as WBinding; - if (binding != null) - SetForeground(element, binding); - else - SetForeground(element, (WBrush)cache); - } - - public static void SetForeground(this FrameworkElement element, WBrush foregroundBrush) - { - if (element == null) - throw new ArgumentNullException(nameof(element)); - - element.SetValue(GetForegroundProperty(element), foregroundBrush); - } - - public static void SetForeground(this FrameworkElement element, WBinding binding) - { - if (element == null) - throw new ArgumentNullException(nameof(element)); - - element.SetBinding(GetForegroundProperty(element), binding); - } - public static void UpdateVerticalTextAlignment(this Control platformControl, ITextAlignment textAlignment) { platformControl.VerticalAlignment = textAlignment.VerticalTextAlignment.ToPlatformVerticalAlignment(); @@ -168,29 +105,6 @@ internal static void TryUpdateResource(this FrameworkElement element, object new element?.RefreshThemeResources(); } - static DependencyProperty? GetForegroundProperty(FrameworkElement element) - { - if (element is Control) - return Control.ForegroundProperty; - if (element is TextBlock) - return TextBlock.ForegroundProperty; - - Type type = element.GetType(); - - if (!ForegroundProperties.Value.TryGetValue(type, out var foregroundProperty)) - { - if (ReflectionExtensions.GetFields(type).FirstOrDefault(f => f.Name == "ForegroundProperty") is not FieldInfo field) - throw new ArgumentException("type is not a Foregroundable type"); - - if (field.GetValue(null) is DependencyProperty property) - ForegroundProperties.Value.TryAdd(type, property); - - return null; - } - - return foregroundProperty; - } - internal static IEnumerable GetChildren(this DependencyObject parent) where T : DependencyObject { int myChildrenCount = VisualTreeHelper.GetChildrenCount(parent); diff --git a/src/Core/tests/DeviceTests/Handlers/ActivityIndicator/ActivityIndicatorHandlerTests.cs b/src/Core/tests/DeviceTests/Handlers/ActivityIndicator/ActivityIndicatorHandlerTests.cs index 348219fee721..13f73d557398 100644 --- a/src/Core/tests/DeviceTests/Handlers/ActivityIndicator/ActivityIndicatorHandlerTests.cs +++ b/src/Core/tests/DeviceTests/Handlers/ActivityIndicator/ActivityIndicatorHandlerTests.cs @@ -36,5 +36,24 @@ public async Task BackgroundUpdatesCorrectly(uint color) await ValidateHasColor(activityIndicator, expected, () => activityIndicator.Background = new SolidPaintStub(expected), nameof(activityIndicator.Background)); } + +#if WINDOWS + [Theory(DisplayName = "Foreground Updates Correctly")] + [InlineData(0xFFFF0000)] + [InlineData(0xFF00FF00)] + [InlineData(0xFF0000FF)] + public async Task ForegroundUpdatesCorrectly(uint color) + { + var expected = Color.FromUint(color); + + var activityIndicator = new ActivityIndicatorStub() + { + Color = Color.FromUint(0xFF888888), + IsRunning = true + }; + + await ValidateHasColor(activityIndicator, expected, () => activityIndicator.Color = expected, nameof(activityIndicator.Color)); + } +#endif } -} \ No newline at end of file +}