From 6595fb6570d8decc95913257bb25f38563f0a19b Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Thu, 22 Aug 2024 21:18:26 +0300 Subject: [PATCH] chore: Few adjustments - not yet complete --- .../Helpers/StyleHelper.cs | 14 -------- .../UI/Xaml/Controls/Control/Control.cs | 9 ----- .../Controls/FocusVisual/SystemFocusVisual.cs | 1 - src/Uno.UI/UI/Xaml/DependencyProperty.cs | 35 +++++++++++++++++++ src/Uno.UI/UI/Xaml/Documents/TextElement.cs | 21 +++++------ src/Uno.UI/UI/Xaml/FrameworkElement.cs | 30 ++-------------- src/Uno.UI/UI/Xaml/ResourceResolver.cs | 2 +- 7 files changed, 49 insertions(+), 63 deletions(-) diff --git a/src/Uno.UI.RuntimeTests/Helpers/StyleHelper.cs b/src/Uno.UI.RuntimeTests/Helpers/StyleHelper.cs index 862ace2df302..114686b5451e 100644 --- a/src/Uno.UI.RuntimeTests/Helpers/StyleHelper.cs +++ b/src/Uno.UI.RuntimeTests/Helpers/StyleHelper.cs @@ -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(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 } } diff --git a/src/Uno.UI/UI/Xaml/Controls/Control/Control.cs b/src/Uno.UI/UI/Xaml/Controls/Control/Control.cs index 863f1bf215e6..2f6700f6043f 100644 --- a/src/Uno.UI/UI/Xaml/Controls/Control/Control.cs +++ b/src/Uno.UI/UI/Xaml/Controls/Control/Control.cs @@ -43,7 +43,6 @@ public partial class Control : FrameworkElement private void InitializeControl() { - SetDefaultForeground(ForegroundProperty); SubscribeToOverridenRoutedEvents(); OnIsFocusableChanged(); @@ -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) diff --git a/src/Uno.UI/UI/Xaml/Controls/FocusVisual/SystemFocusVisual.cs b/src/Uno.UI/UI/Xaml/Controls/FocusVisual/SystemFocusVisual.cs index caba7ab77897..c1cf7855db06 100644 --- a/src/Uno.UI/UI/Xaml/Controls/FocusVisual/SystemFocusVisual.cs +++ b/src/Uno.UI/UI/Xaml/Controls/FocusVisual/SystemFocusVisual.cs @@ -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; diff --git a/src/Uno.UI/UI/Xaml/DependencyProperty.cs b/src/Uno.UI/UI/Xaml/DependencyProperty.cs index 65eb6c42d969..c698079bf9ad 100644 --- a/src/Uno.UI/UI/Xaml/DependencyProperty.cs +++ b/src/Uno.UI/UI/Xaml/DependencyProperty.cs @@ -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; @@ -477,6 +479,23 @@ 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) @@ -484,6 +503,22 @@ internal object GetDefaultValue(UIElement referenceObject, Type forType) 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)) diff --git a/src/Uno.UI/UI/Xaml/Documents/TextElement.cs b/src/Uno.UI/UI/Xaml/Documents/TextElement.cs index cf9ee42fdb2c..d99068928f75 100644 --- a/src/Uno.UI/UI/Xaml/Documents/TextElement.cs +++ b/src/Uno.UI/UI/Xaml/Documents/TextElement.cs @@ -167,7 +167,7 @@ protected virtual void OnFontSizeChanged() public Brush Foreground { - get => GetForegroundValue(); + get => (Brush)GetValue(ForegroundProperty); set { if (value != null && !(value is SolidColorBrush)) @@ -175,14 +175,19 @@ public Brush Foreground 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() { @@ -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); } diff --git a/src/Uno.UI/UI/Xaml/FrameworkElement.cs b/src/Uno.UI/UI/Xaml/FrameworkElement.cs index 8f7a1c728989..1e1ec15d6ed1 100644 --- a/src/Uno.UI/UI/Xaml/FrameworkElement.cs +++ b/src/Uno.UI/UI/Xaml/FrameworkElement.cs @@ -612,11 +612,7 @@ public Thickness FocusVisualSecondaryThickness public Brush FocusVisualSecondaryBrush { - get - { - EnsureFocusVisualBrushDefaults(); - return GetFocusVisualSecondaryBrushValue(); - } + get => GetFocusVisualSecondaryBrushValue(); set => SetFocusVisualSecondaryBrushValue(value); } @@ -633,11 +629,7 @@ public Thickness FocusVisualPrimaryThickness public Brush FocusVisualPrimaryBrush { - get - { - EnsureFocusVisualBrushDefaults(); - return GetFocusVisualPrimaryBrushValue(); - } + get => GetFocusVisualPrimaryBrushValue(); set => SetFocusVisualPrimaryBrushValue(value); } @@ -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; - } - /// /// Gets or sets whether a disabled control can receive focus. /// @@ -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 diff --git a/src/Uno.UI/UI/Xaml/ResourceResolver.cs b/src/Uno.UI/UI/Xaml/ResourceResolver.cs index 45a51c7c379f..93ee027235ba 100644 --- a/src/Uno.UI/UI/Xaml/ResourceResolver.cs +++ b/src/Uno.UI/UI/Xaml/ResourceResolver.cs @@ -379,7 +379,7 @@ private static bool TryVisualTreeRetrieval(in SpecializedResourceDictionary.Reso /// /// Try to retrieve a resource statically (at parse time). This will check resources in 'xaml scope' first, then top-level resources. /// - 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