diff --git a/src/Avalonia.Base/StyledElement.cs b/src/Avalonia.Base/StyledElement.cs index 405b874fed..94af8385a8 100644 --- a/src/Avalonia.Base/StyledElement.cs +++ b/src/Avalonia.Base/StyledElement.cs @@ -28,7 +28,7 @@ namespace Avalonia public class StyledElement : Animatable, IDataContextProvider, ILogical, - IResourceHost, + IThemeVariantHost, IStyleHost, IStyleable, ISetLogicalParent, @@ -143,6 +143,9 @@ namespace Avalonia /// public event EventHandler? ResourcesChanged; + /// + public event EventHandler? ActualThemeVariantChanged; + /// /// Gets or sets the name of the styled element. /// @@ -299,6 +302,9 @@ namespace Avalonia /// public StyledElement? Parent { get; private set; } + /// + public ThemeVariant ActualThemeVariant => GetValue(ThemeVariant.ActualThemeVariantProperty); + /// /// Gets the styled element's logical parent. /// @@ -618,7 +624,20 @@ namespace Avalonia base.OnPropertyChanged(change); if (change.Property == ThemeProperty) + { OnControlThemeChanged(); + } + else if (change.Property == ThemeVariant.RequestedThemeVariantProperty) + { + if (change.GetNewValue() is {} themeVariant && themeVariant != ThemeVariant.Default) + SetValue(ThemeVariant.ActualThemeVariantProperty, themeVariant); + else + ClearValue(ThemeVariant.ActualThemeVariantProperty); + } + else if (change.Property == ThemeVariant.ActualThemeVariantProperty) + { + ActualThemeVariantChanged?.Invoke(this, EventArgs.Empty); + } } private protected virtual void OnControlThemeChanged() diff --git a/src/Avalonia.Base/Styling/ThemeVariant.cs b/src/Avalonia.Base/Styling/ThemeVariant.cs index 5a6a8785a5..389136b0f5 100644 --- a/src/Avalonia.Base/Styling/ThemeVariant.cs +++ b/src/Avalonia.Base/Styling/ThemeVariant.cs @@ -11,6 +11,21 @@ namespace Avalonia.Styling; [TypeConverter(typeof(ThemeVariantTypeConverter))] public sealed record ThemeVariant { + /// + /// Defines the ActualThemeVariant property. + /// + internal static readonly StyledProperty ActualThemeVariantProperty = + AvaloniaProperty.Register( + "ActualThemeVariant", + inherits: true); + + /// + /// Defines the RequestedThemeVariant property. + /// + internal static readonly StyledProperty RequestedThemeVariantProperty = + AvaloniaProperty.Register( + "RequestedThemeVariant", defaultValue: Default); + /// /// Creates a new instance of the /// diff --git a/src/Avalonia.Controls/Control.cs b/src/Avalonia.Controls/Control.cs index c9e8f0d045..dbece12575 100644 --- a/src/Avalonia.Controls/Control.cs +++ b/src/Avalonia.Controls/Control.cs @@ -24,7 +24,7 @@ namespace Avalonia.Controls /// - A property to allow user-defined data to be attached to the control. /// - and other context menu related members. /// - public class Control : InputElement, IDataTemplateHost, INamed, IVisualBrushInitialize, ISetterValue, IThemeVariantHost + public class Control : InputElement, IDataTemplateHost, INamed, IVisualBrushInitialize, ISetterValue { /// /// Defines the property. @@ -162,11 +162,6 @@ namespace Avalonia.Controls set => SetValue(TagProperty, value); } - /// - public ThemeVariant ActualThemeVariant => GetValue(ThemeVariantScope.ActualThemeVariantProperty); - - public event EventHandler? ActualThemeVariantChanged; - /// /// Occurs when the user has completed a context input gesture, such as a right-click. /// @@ -531,17 +526,6 @@ namespace Avalonia.Controls RaiseEvent(sizeChangedEventArgs); } } - else if (change.Property == ThemeVariantScope.RequestedThemeVariantProperty) - { - if (change.GetNewValue() is {} themeVariant && themeVariant != ThemeVariant.Default) - SetValue(ThemeVariantScope.ActualThemeVariantProperty, themeVariant); - else - ClearValue(ThemeVariantScope.ActualThemeVariantProperty); - } - else if (change.Property == ThemeVariantScope.ActualThemeVariantProperty) - { - ActualThemeVariantChanged?.Invoke(this, EventArgs.Empty); - } } } } diff --git a/src/Avalonia.Controls/ThemeVariantScope.cs b/src/Avalonia.Controls/ThemeVariantScope.cs index b29f1ee56e..f5ad4b2f94 100644 --- a/src/Avalonia.Controls/ThemeVariantScope.cs +++ b/src/Avalonia.Controls/ThemeVariantScope.cs @@ -7,21 +7,13 @@ namespace Avalonia.Controls /// public class ThemeVariantScope : Decorator { - /// - /// Defines the property. - /// + /// public static readonly StyledProperty ActualThemeVariantProperty = - AvaloniaProperty.Register( - nameof(ActualThemeVariant), - inherits: true); + ThemeVariant.ActualThemeVariantProperty.AddOwner(); - /// - /// Defines the property. - /// + /// public static readonly StyledProperty RequestedThemeVariantProperty = - AvaloniaProperty.Register( - nameof(RequestedThemeVariant), - defaultValue: ThemeVariant.Default); + ThemeVariant.RequestedThemeVariantProperty.AddOwner(); /// /// Gets or sets the UI theme variant that is used by the control (and its child elements) for resource determination.