Skip to content

Commit

Permalink
fix: hyperlink foreground logic to make TestRespectHyperlinkForegroun…
Browse files Browse the repository at this point in the history
…d work
  • Loading branch information
ramezgerges authored and MartinZikmund committed Sep 5, 2024
1 parent 8240b8e commit 354ee29
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
15 changes: 12 additions & 3 deletions src/Uno.UI/UI/Xaml/Documents/Hyperlink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public sealed partial class Hyperlink : Span

private const string HyperlinkForegroundPressedKey = "HyperlinkForegroundPressed";
private const string HyperlinkForegroundPointerOverKey = "HyperlinkForegroundPointerOver";
private protected override Brush DefaultTextForegroundBrush => DefaultBrushes.HyperlinkForegroundBrush;
private const string HyperlinkForeground = nameof(HyperlinkForeground);

public
#if __WASM__
Expand Down Expand Up @@ -232,7 +232,7 @@ internal void OnClick()
}
#endregion

private void SetCurrentForeground()
internal void SetCurrentForeground()
{
if (_pressedPointer is { }
&& Application.Current.Resources.TryGetValue(HyperlinkForegroundPressedKey, out var pressedBrush))
Expand All @@ -244,9 +244,18 @@ private void SetCurrentForeground()
{
this.SetValue(ForegroundProperty, hoveredBrush, DependencyPropertyValuePrecedences.Animations);
}
else
else // normal
{
// this is close, although not identical, to what the WinUI source does
this.ClearValue(ForegroundProperty, DependencyPropertyValuePrecedences.Animations);
if (this.GetCurrentHighestValuePrecedence(ForegroundProperty) == DependencyPropertyValuePrecedences.Local)
{
this.SetValue(ForegroundProperty, this.GetValue(ForegroundProperty), DependencyPropertyValuePrecedences.Animations);
}
else if (Application.Current.Resources.TryGetValue(HyperlinkForeground, out var defaultBrush))
{
this.SetValue(ForegroundProperty, defaultBrush, DependencyPropertyValuePrecedences.Animations);
}
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/Uno.UI/UI/Xaml/Documents/TextElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,20 +379,22 @@ internal FrameworkElement GetContainingFrameworkElement()

public void OnThemeChanged() => SetDefaultForeground(ForegroundProperty);

private protected virtual Brush DefaultTextForegroundBrush => DefaultBrushes.TextForegroundBrush;

#if __WASM__
private protected
#if __WASM__ // On Wasm, we inherit UIElement, and so we need to shadow UIElement.SetDefaultForeground.
private protected new
#else
private
#endif
void SetDefaultForeground(DependencyProperty foregroundProperty)
{
if (this is Hyperlink)
if (this is Hyperlink hl)
{
// Hyperlink doesn't appear to inherit foreground from the parent.
// So, we set this with ImplicitStyle precedence which is a higher precedence than Inheritance.
this.SetValue(foregroundProperty, DefaultTextForegroundBrush, DependencyPropertyValuePrecedences.ImplicitStyle);
hl.SetCurrentForeground();
}
else
{
this.SetValue(foregroundProperty, DefaultBrushes.TextForegroundBrush, DependencyPropertyValuePrecedences.DefaultValue);
}

((IDependencyObjectStoreProvider)this).Store.SetLastUsedTheme(Application.Current?.RequestedThemeForResources);
Expand Down
11 changes: 1 addition & 10 deletions src/Uno.UI/UI/Xaml/Media/DefaultBrushes.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#nullable enable

using System.Diagnostics;
using Uno.Helpers.Theming;
using Windows.ApplicationModel.Core;
using Windows.UI;
Expand All @@ -12,22 +11,14 @@ namespace Uno.UI.Xaml.Media;
internal static class DefaultBrushes
{
private const string DefaultTextForegroundThemeBrushKey = "DefaultTextForegroundThemeBrush";
private const string HyperlinkForegroundKey = "HyperlinkForeground";

private static Brush? _textForegroundBrush;
private static Brush? _hyperlinkForegroundBrush;

internal static Brush TextForegroundBrush => GetDefaultTextBrush(DefaultTextForegroundThemeBrushKey, ref _textForegroundBrush);

internal static Brush HyperlinkForegroundBrush => GetDefaultTextBrush(HyperlinkForegroundKey, ref _hyperlinkForegroundBrush);

internal static SolidColorBrush SelectionHighlightColor { get; } = new SolidColorBrush(Color.FromArgb(255, 0, 120, 212));

internal static void ResetDefaultThemeBrushes()
{
_textForegroundBrush = null;
_hyperlinkForegroundBrush = null;
}
internal static void ResetDefaultThemeBrushes() => _textForegroundBrush = null;

private static Brush GetDefaultTextBrush(string key, ref Brush? brush)
{
Expand Down

0 comments on commit 354ee29

Please sign in to comment.