Skip to content

Commit eb58108

Browse files
authored
Rework foreground color and remove outdated methods (#21238)
* Rework foreground color and remove outdated methods * - remove excess namespaces * - add tests * Update ActivityIndicatorHandlerTests.cs * - deleted unused extension method
1 parent 1980a26 commit eb58108

File tree

4 files changed

+45
-189
lines changed

4 files changed

+45
-189
lines changed

src/Controls/src/Core/Platform/Windows/Extensions/FrameworkElementExtensions.cs

Lines changed: 0 additions & 98 deletions
This file was deleted.

src/Core/src/Platform/Windows/ActivityIndicatorExtensions.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Maui.Graphics;
33
using Microsoft.Maui.Primitives;
44
using Microsoft.UI.Xaml.Controls;
5+
using Microsoft.UI.Xaml.Media;
56

67
namespace Microsoft.Maui.Platform
78
{
@@ -11,26 +12,46 @@ public static void UpdateIsRunning(this ProgressRing platformActivityIndicator,
1112
{
1213
platformActivityIndicator.IsActive = virtualView.IsRunning;
1314
}
15+
1416
public static void UpdateColor(this ProgressRing platformActivityIndicator, IActivityIndicator activityIndicator)
1517
{
1618
platformActivityIndicator.UpdateColor(activityIndicator, null);
1719
}
1820

1921
public static void UpdateColor(this ProgressRing platformActivityIndicator, IActivityIndicator activityIndicator, object? foregroundDefault)
2022
{
21-
Color color = activityIndicator.Color;
23+
var color = activityIndicator.Color;
24+
Brush? brush = null;
2225

2326
if (color.IsDefault())
2427
{
25-
if (foregroundDefault != null)
26-
platformActivityIndicator.RestoreForegroundCache(foregroundDefault);
28+
if(foregroundDefault is Brush defaultBrush)
29+
brush = defaultBrush;
30+
}
31+
else
32+
{
33+
brush = color.ToPlatform();
34+
}
35+
36+
if (brush is null)
37+
{
38+
platformActivityIndicator.Resources.RemoveKeys(ProgressRingForegroundResourceKeys);
39+
platformActivityIndicator.ClearValue(ProgressRing.ForegroundProperty);
2740
}
2841
else
2942
{
30-
platformActivityIndicator.Foreground = color.ToPlatform();
43+
platformActivityIndicator.Resources.SetValueForAllKey(ProgressRingForegroundResourceKeys, brush);
44+
platformActivityIndicator.Foreground = brush;
3145
}
46+
47+
platformActivityIndicator.RefreshThemeResources();
3248
}
3349

50+
static readonly string[] ProgressRingForegroundResourceKeys =
51+
{
52+
"ProgressRingForegroundThemeBrush"
53+
};
54+
3455
public static void UpdateWidth(this ProgressRing platformActivityIndicator, IActivityIndicator activityIndicator)
3556
{
3657
if (Dimension.IsExplicitSet(activityIndicator.Width))

src/Core/src/Platform/Windows/FrameworkElementExtensions.cs

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
#nullable enable
22
using System;
3-
using System.Collections.Concurrent;
43
using System.Collections.Generic;
54
using System.Diagnostics;
65
using System.Diagnostics.CodeAnalysis;
7-
using System.Linq;
8-
using System.Reflection;
9-
using System.Threading.Tasks;
106
using Microsoft.UI.Xaml;
117
using Microsoft.UI.Xaml.Controls;
128
using Microsoft.UI.Xaml.Media;
139
using Windows.UI.ViewManagement;
14-
using WBinding = Microsoft.UI.Xaml.Data.Binding;
15-
using WBindingExpression = Microsoft.UI.Xaml.Data.BindingExpression;
16-
using WBrush = Microsoft.UI.Xaml.Media.Brush;
1710
using WPoint = Windows.Foundation.Point;
1811

1912
namespace Microsoft.Maui.Platform
2013
{
2114
internal static class FrameworkElementExtensions
2215
{
23-
static readonly Lazy<ConcurrentDictionary<Type, DependencyProperty>> ForegroundProperties =
24-
new Lazy<ConcurrentDictionary<Type, DependencyProperty>>(() => new ConcurrentDictionary<Type, DependencyProperty>());
25-
2616
public static T? GetResource<T>(this FrameworkElement element, string key, T? def = default)
2717
{
2818
if (element.Resources.TryGetValue(key, out var resource))
@@ -31,59 +21,6 @@ internal static class FrameworkElementExtensions
3121
return def;
3222
}
3323

34-
public static WBrush GetForeground(this FrameworkElement element)
35-
{
36-
if (element == null)
37-
throw new ArgumentNullException(nameof(element));
38-
39-
return (WBrush)element.GetValue(GetForegroundProperty(element));
40-
}
41-
42-
public static WBinding? GetForegroundBinding(this FrameworkElement element)
43-
{
44-
WBindingExpression expr = element.GetBindingExpression(GetForegroundProperty(element));
45-
46-
if (expr == null)
47-
return null;
48-
49-
return expr.ParentBinding;
50-
}
51-
52-
public static object GetForegroundCache(this FrameworkElement element)
53-
{
54-
WBinding? binding = GetForegroundBinding(element);
55-
56-
if (binding != null)
57-
return binding;
58-
59-
return GetForeground(element);
60-
}
61-
62-
public static void RestoreForegroundCache(this FrameworkElement element, object cache)
63-
{
64-
var binding = cache as WBinding;
65-
if (binding != null)
66-
SetForeground(element, binding);
67-
else
68-
SetForeground(element, (WBrush)cache);
69-
}
70-
71-
public static void SetForeground(this FrameworkElement element, WBrush foregroundBrush)
72-
{
73-
if (element == null)
74-
throw new ArgumentNullException(nameof(element));
75-
76-
element.SetValue(GetForegroundProperty(element), foregroundBrush);
77-
}
78-
79-
public static void SetForeground(this FrameworkElement element, WBinding binding)
80-
{
81-
if (element == null)
82-
throw new ArgumentNullException(nameof(element));
83-
84-
element.SetBinding(GetForegroundProperty(element), binding);
85-
}
86-
8724
public static void UpdateVerticalTextAlignment(this Control platformControl, ITextAlignment textAlignment)
8825
{
8926
platformControl.VerticalAlignment = textAlignment.VerticalTextAlignment.ToPlatformVerticalAlignment();
@@ -168,29 +105,6 @@ internal static void TryUpdateResource(this FrameworkElement element, object new
168105
element?.RefreshThemeResources();
169106
}
170107

171-
static DependencyProperty? GetForegroundProperty(FrameworkElement element)
172-
{
173-
if (element is Control)
174-
return Control.ForegroundProperty;
175-
if (element is TextBlock)
176-
return TextBlock.ForegroundProperty;
177-
178-
Type type = element.GetType();
179-
180-
if (!ForegroundProperties.Value.TryGetValue(type, out var foregroundProperty))
181-
{
182-
if (ReflectionExtensions.GetFields(type).FirstOrDefault(f => f.Name == "ForegroundProperty") is not FieldInfo field)
183-
throw new ArgumentException("type is not a Foregroundable type");
184-
185-
if (field.GetValue(null) is DependencyProperty property)
186-
ForegroundProperties.Value.TryAdd(type, property);
187-
188-
return null;
189-
}
190-
191-
return foregroundProperty;
192-
}
193-
194108
internal static IEnumerable<T?> GetChildren<T>(this DependencyObject parent) where T : DependencyObject
195109
{
196110
int myChildrenCount = VisualTreeHelper.GetChildrenCount(parent);

src/Core/tests/DeviceTests/Handlers/ActivityIndicator/ActivityIndicatorHandlerTests.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,24 @@ public async Task BackgroundUpdatesCorrectly(uint color)
3636

3737
await ValidateHasColor(activityIndicator, expected, () => activityIndicator.Background = new SolidPaintStub(expected), nameof(activityIndicator.Background));
3838
}
39+
40+
#if WINDOWS
41+
[Theory(DisplayName = "Foreground Updates Correctly")]
42+
[InlineData(0xFFFF0000)]
43+
[InlineData(0xFF00FF00)]
44+
[InlineData(0xFF0000FF)]
45+
public async Task ForegroundUpdatesCorrectly(uint color)
46+
{
47+
var expected = Color.FromUint(color);
48+
49+
var activityIndicator = new ActivityIndicatorStub()
50+
{
51+
Color = Color.FromUint(0xFF888888),
52+
IsRunning = true
53+
};
54+
55+
await ValidateHasColor(activityIndicator, expected, () => activityIndicator.Color = expected, nameof(activityIndicator.Color));
56+
}
57+
#endif
3958
}
40-
}
59+
}

0 commit comments

Comments
 (0)