Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

29 changes: 25 additions & 4 deletions src/Core/src/Platform/Windows/ActivityIndicatorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -11,26 +12,46 @@ public static void UpdateIsRunning(this ProgressRing platformActivityIndicator,
{
platformActivityIndicator.IsActive = virtualView.IsRunning;
}

public static void UpdateColor(this ProgressRing platformActivityIndicator, IActivityIndicator activityIndicator)
{
platformActivityIndicator.UpdateColor(activityIndicator, null);
}

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))
Expand Down
86 changes: 0 additions & 86 deletions src/Core/src/Platform/Windows/FrameworkElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<ConcurrentDictionary<Type, DependencyProperty>> ForegroundProperties =
new Lazy<ConcurrentDictionary<Type, DependencyProperty>>(() => new ConcurrentDictionary<Type, DependencyProperty>());

public static T? GetResource<T>(this FrameworkElement element, string key, T? def = default)
{
if (element.Resources.TryGetValue(key, out var resource))
Expand All @@ -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();
Expand Down Expand Up @@ -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<T?> GetChildren<T>(this DependencyObject parent) where T : DependencyObject
{
int myChildrenCount = VisualTreeHelper.GetChildrenCount(parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}