Skip to content

Commit

Permalink
feat: Support for DefaultTextForegroundThemeBrush
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed May 3, 2023
1 parent c05740a commit 95324b5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/Uno.UI.RuntimeTests/Helpers/StyleHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Text;
using Uno.Disposables;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
Expand Down Expand Up @@ -81,7 +81,14 @@ public static IDisposable UseFluentStyles()
var xcr = new Microsoft.UI.Xaml.Controls.XamlControlsResources();
resources.MergedDictionaries.Insert(0, xcr);

return new DisposableAction(() => resources.MergedDictionaries.Remove(xcr));
// Force default brushes to be reloaded
UIElement.ResetDefaultThemeBrushes();

return new DisposableAction(() =>
{
resources.MergedDictionaries.Remove(xcr);
UIElement.ResetDefaultThemeBrushes();
});
#endif
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Uno.UI/UI/Xaml/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ private void SetRequestedTheme(ApplicationTheme requestedTheme)

private void OnResourcesChanged(ResourceUpdateReason updateReason)
{
UIElement.ResetDefaultThemeBrushes();
foreach (var contentRoot in WinUICoreServices.Instance.ContentRootCoordinator.ContentRoots)
{
if (GetTreeRoot(contentRoot) is { } root)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public partial class ContentPresenter : FrameworkElement, ICustomClippingElement

private void InitializeContentPresenter()
{
SetDefaultForeground(ForegroundProperty);
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public partial class TextBlock : DependencyObject
public TextBlock()
{
IFrameworkElementHelper.Initialize(this);
SetDefaultForeground(ForegroundProperty);

InitializeProperties();

InitializePartial();
Expand Down
1 change: 1 addition & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ partial class TextBlock : FrameworkElement, IBlock

public TextBlock()
{
SetDefaultForeground(ForegroundProperty);
_textVisual = new TextVisual(Visual.Compositor, this);

Visual.Children.InsertAtBottom(_textVisual);
Expand Down
5 changes: 1 addition & 4 deletions src/Uno.UI/UI/Xaml/FrameworkElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -964,10 +964,7 @@ internal virtual void UpdateThemeBindings(ResourceUpdateReason updateReason)
/// <param name="foregroundProperty">The appropriate property for the calling instance.</param>
private protected void SetDefaultForeground(DependencyProperty foregroundProperty)
{
(this).SetValue(foregroundProperty,
Application.Current == null || Application.Current.RequestedTheme == ApplicationTheme.Light
? SolidColorBrushHelper.Black
: SolidColorBrushHelper.White, DependencyPropertyValuePrecedences.DefaultValue);
(this).SetValue(foregroundProperty, UIElement.GetDefaultTextBrush(), DependencyPropertyValuePrecedences.DefaultValue);
}

#region AutomationPeer
Expand Down
30 changes: 30 additions & 0 deletions src/Uno.UI/UI/Xaml/UIElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
using Windows.UI.Composition;
using Windows.Graphics.Display;
using Uno.UI.Extensions;
using Windows.UI.Xaml.Documents;
using Windows.ApplicationModel.Core;
using Uno.UI.Xaml.Media;

#if __IOS__
using UIKit;
Expand All @@ -44,6 +47,8 @@ public partial class UIElement : DependencyObject, IXUidProvider, IUIElement

private static readonly Type[] _bringIntoViewRequestedArgs = new[] { typeof(BringIntoViewRequestedEventArgs) };

private static Brush _defaultTextBrush;

private readonly SerialDisposable _clipSubscription = new SerialDisposable();
private string _uid;

Expand Down Expand Up @@ -183,6 +188,31 @@ internal virtual bool GetDefaultValue2(DependencyProperty property, out object d
return false;
}

internal static void ResetDefaultThemeBrushes()
{
_defaultTextBrush = null;
}

internal static Brush GetDefaultTextBrush()
{
if (_defaultTextBrush is null)
{
if (Application.Current.Resources.TryGetValue("DefaultTextForegroundThemeBrush", out var defaultBrushObject) &&
defaultBrushObject is Brush defaultBrush)
{
_defaultTextBrush = defaultBrush;
}
else
{
// Fallback to black/white
_defaultTextBrush = CoreApplication.RequestedTheme == Uno.Helpers.Theming.SystemTheme.Dark ?
SolidColorBrushHelper.White : SolidColorBrushHelper.Black;
}
}

return _defaultTextBrush;
}

public Vector2 ActualSize => new Vector2((float)GetActualWidth(), (float)GetActualHeight());

/// <summary>
Expand Down

0 comments on commit 95324b5

Please sign in to comment.