Skip to content

Commit

Permalink
chore: Few adjustments - not yet complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Aug 22, 2024
1 parent acb7501 commit 6595fb6
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 63 deletions.
14 changes: 0 additions & 14 deletions src/Uno.UI.RuntimeTests/Helpers/StyleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,13 @@ public static IDisposable UseFluentStyles()

// Force default brushes to be reloaded
DefaultBrushes.ResetDefaultThemeBrushes();
ResetIslandRootForeground();

return new DisposableAction(() =>
{
resources.MergedDictionaries.Remove(xcr);
DefaultBrushes.ResetDefaultThemeBrushes();
ResetIslandRootForeground();
});
#endif
}

#if !WINAPPSDK
private static void ResetIslandRootForeground()
{
if (Uno.UI.Xaml.Core.CoreServices.Instance.InitializationType == Xaml.Core.InitializationType.IslandsOnly &&
VisualTreeUtils.FindVisualChildByType<Control>(TestServices.WindowHelper.XamlRoot.VisualTree.RootElement) is { } control)
{
// Ensure the root element's Foreground is set correctly
control.SetValue(Control.ForegroundProperty, DefaultBrushes.TextForegroundBrush, DependencyPropertyValuePrecedences.DefaultValue);
}
}
#endif
}
}
9 changes: 0 additions & 9 deletions src/Uno.UI/UI/Xaml/Controls/Control/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public partial class Control : FrameworkElement

private void InitializeControl()
{
SetDefaultForeground(ForegroundProperty);
SubscribeToOverridenRoutedEvents();
OnIsFocusableChanged();

Expand All @@ -59,14 +58,6 @@ private void InitializeControl()

internal override bool IsEnabledOverride() => IsEnabled && base.IsEnabledOverride();

internal override void UpdateThemeBindings(Data.ResourceUpdateReason updateReason)
{
base.UpdateThemeBindings(updateReason);

//override the default value from dependency property based on application theme
SetDefaultForeground(ForegroundProperty);
}

private protected override Type GetDefaultStyleKey() => DefaultStyleKey as Type;

protected override void OnBackgroundChanged(DependencyPropertyChangedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ private static void OnFocusedElementChanged(DependencyObject dependencyObject, D

if (args.NewValue is FrameworkElement element)
{
element.EnsureFocusVisualBrushDefaults();
element.SizeChanged += focusVisual.FocusedElementSizeChanged;
#if !UNO_HAS_ENHANCED_LIFECYCLE
element.LayoutUpdated += focusVisual.FocusedElementLayoutUpdated;
Expand Down
35 changes: 35 additions & 0 deletions src/Uno.UI/UI/Xaml/DependencyProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Documents;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
using Uno;
using Uno.Extensions;
using Uno.UI;
using Uno.UI.Dispatching;
using Uno.UI.Xaml.Media;

#if __ANDROID__
using _View = Android.Views.View;
Expand Down Expand Up @@ -477,13 +479,46 @@ private static DependencyProperty[] InternalGetFrameworkPropertiesForType(Type t
return output.ToArray();
}

private bool TryGetDefaultInheritedPropertyValue(out object defaultValue)
{
if (this == TextElement.ForegroundProperty ||
this == TextBlock.ForegroundProperty ||
this == Control.ForegroundProperty ||
this == RichTextBlock.ForegroundProperty ||
this == ContentPresenter.ForegroundProperty ||
this == IconElement.ForegroundProperty)
{
defaultValue = DefaultBrushes.TextForegroundBrush;
return true;
}

defaultValue = null;
return false;
}

internal object GetDefaultValue(UIElement referenceObject, Type forType)
{
if (referenceObject?.GetDefaultValue2(this, out var defaultValue) == true)
{
return defaultValue;
}

if (IsInherited && TryGetDefaultInheritedPropertyValue(out var value))
{
return value;
}

if (this == FrameworkElement.FocusVisualPrimaryBrushProperty &&
ResourceResolver.TryStaticRetrieval("SystemControlFocusVisualPrimaryBrush", null, out var primaryBrush))
{
return primaryBrush;
}
else if (this == FrameworkElement.FocusVisualSecondaryBrushProperty &&
ResourceResolver.TryStaticRetrieval("SystemControlFocusVisualSecondaryBrush", null, out var secondaryBrush))
{
return secondaryBrush;
}

if (this == Shape.StretchProperty)
{
if (forType == typeof(Rectangle) || forType == typeof(Ellipse))
Expand Down
21 changes: 11 additions & 10 deletions src/Uno.UI/UI/Xaml/Documents/TextElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,27 @@ protected virtual void OnFontSizeChanged()

public Brush Foreground
{
get => GetForegroundValue();
get => (Brush)GetValue(ForegroundProperty);
set
{
if (value != null && !(value is SolidColorBrush))
{
throw new InvalidOperationException("Specified brush is not a SolidColorBrush");
}

SetForegroundValue(value);
SetValue(ForegroundProperty, value);
}
}

[GeneratedDependencyProperty(Options = FrameworkPropertyMetadataOptions.Inherits, ChangedCallback = true, ChangedCallbackName = nameof(OnForegroundChanged))]
public static DependencyProperty ForegroundProperty { get; } = CreateForegroundProperty();

private static Brush GetForegroundDefaultValue() => SolidColorBrushHelper.Black;
public static DependencyProperty ForegroundProperty { get; } = DependencyProperty.Register(
nameof(Foreground),
typeof(Brush),
typeof(TextElement),
new FrameworkPropertyMetadata(
defaultValue: SolidColorBrushHelper.Black,
options: FrameworkPropertyMetadataOptions.Inherits,
propertyChangedCallback: (instance, args) => ((TextElement)instance).OnForegroundChanged()
));

protected virtual void OnForegroundChanged()
{
Expand Down Expand Up @@ -389,10 +394,6 @@ void SetDefaultForeground(DependencyProperty foregroundProperty)
// So, we set this with ImplicitStyle precedence which is a higher precedence than Inheritance.
this.SetValue(foregroundProperty, DefaultTextForegroundBrush, DependencyPropertyValuePrecedences.ImplicitStyle);
}
else
{
this.SetValue(foregroundProperty, DefaultTextForegroundBrush, DependencyPropertyValuePrecedences.DefaultValue);
}

((IDependencyObjectStoreProvider)this).Store.SetLastUsedTheme(Application.Current?.RequestedThemeForResources);
}
Expand Down
30 changes: 2 additions & 28 deletions src/Uno.UI/UI/Xaml/FrameworkElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,11 +612,7 @@ public Thickness FocusVisualSecondaryThickness

public Brush FocusVisualSecondaryBrush
{
get
{
EnsureFocusVisualBrushDefaults();
return GetFocusVisualSecondaryBrushValue();
}
get => GetFocusVisualSecondaryBrushValue();
set => SetFocusVisualSecondaryBrushValue(value);
}

Expand All @@ -633,11 +629,7 @@ public Thickness FocusVisualPrimaryThickness

public Brush FocusVisualPrimaryBrush
{
get
{
EnsureFocusVisualBrushDefaults();
return GetFocusVisualPrimaryBrushValue();
}
get => GetFocusVisualPrimaryBrushValue();
set => SetFocusVisualPrimaryBrushValue(value);
}

Expand All @@ -655,21 +647,6 @@ public Thickness FocusVisualMargin
[GeneratedDependencyProperty]
public static DependencyProperty FocusVisualMarginProperty { get; } = CreateFocusVisualMarginProperty();

private bool _focusVisualBrushesInitialized;

internal void EnsureFocusVisualBrushDefaults()
{
if (_focusVisualBrushesInitialized)
{
return;
}

ResourceResolver.ApplyResource(this, FocusVisualPrimaryBrushProperty, new SpecializedResourceDictionary.ResourceKey("SystemControlFocusVisualPrimaryBrush"), ResourceUpdateReason.ThemeResource, null, DependencyPropertyValuePrecedences.DefaultValue);
ResourceResolver.ApplyResource(this, FocusVisualSecondaryBrushProperty, new SpecializedResourceDictionary.ResourceKey("SystemControlFocusVisualSecondaryBrush"), ResourceUpdateReason.ThemeResource, null, DependencyPropertyValuePrecedences.DefaultValue);

_focusVisualBrushesInitialized = true;
}

/// <summary>
/// Gets or sets whether a disabled control can receive focus.
/// </summary>
Expand Down Expand Up @@ -986,9 +963,6 @@ internal virtual void UpdateThemeBindings(ResourceUpdateReason updateReason)
Resources?.UpdateThemeBindings(updateReason);
(this as IDependencyObjectStoreProvider).Store.UpdateResourceBindings(updateReason);

// After theme change, the focus visual brushes may not reflect the correct settings
_focusVisualBrushesInitialized = false;

if (updateReason == ResourceUpdateReason.ThemeResource)
{
// Trigger ActualThemeChanged if relevant
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/ResourceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private static bool TryVisualTreeRetrieval(in SpecializedResourceDictionary.Reso
/// <summary>
/// Try to retrieve a resource statically (at parse time). This will check resources in 'xaml scope' first, then top-level resources.
/// </summary>
private static bool TryStaticRetrieval(in SpecializedResourceDictionary.ResourceKey resourceKey, object context, out object value)
internal static bool TryStaticRetrieval(in SpecializedResourceDictionary.ResourceKey resourceKey, object context, out object value)
{
// This block is a manual enumeration to avoid the foreach pattern
// See https://github.com/dotnet/runtime/issues/56309 for details
Expand Down

0 comments on commit 6595fb6

Please sign in to comment.