Browse Source

Make StyledElement use theme variants again

pull/10149/head
Max Katz 3 years ago
parent
commit
74ef92a4a8
  1. 21
      src/Avalonia.Base/StyledElement.cs
  2. 15
      src/Avalonia.Base/Styling/ThemeVariant.cs
  3. 18
      src/Avalonia.Controls/Control.cs
  4. 16
      src/Avalonia.Controls/ThemeVariantScope.cs

21
src/Avalonia.Base/StyledElement.cs

@ -28,7 +28,7 @@ namespace Avalonia
public class StyledElement : Animatable, public class StyledElement : Animatable,
IDataContextProvider, IDataContextProvider,
ILogical, ILogical,
IResourceHost, IThemeVariantHost,
IStyleHost, IStyleHost,
IStyleable, IStyleable,
ISetLogicalParent, ISetLogicalParent,
@ -143,6 +143,9 @@ namespace Avalonia
/// </summary> /// </summary>
public event EventHandler<ResourcesChangedEventArgs>? ResourcesChanged; public event EventHandler<ResourcesChangedEventArgs>? ResourcesChanged;
/// <inheritdoc />
public event EventHandler? ActualThemeVariantChanged;
/// <summary> /// <summary>
/// Gets or sets the name of the styled element. /// Gets or sets the name of the styled element.
/// </summary> /// </summary>
@ -299,6 +302,9 @@ namespace Avalonia
/// </summary> /// </summary>
public StyledElement? Parent { get; private set; } public StyledElement? Parent { get; private set; }
/// <inheritdoc />
public ThemeVariant ActualThemeVariant => GetValue(ThemeVariant.ActualThemeVariantProperty);
/// <summary> /// <summary>
/// Gets the styled element's logical parent. /// Gets the styled element's logical parent.
/// </summary> /// </summary>
@ -618,7 +624,20 @@ namespace Avalonia
base.OnPropertyChanged(change); base.OnPropertyChanged(change);
if (change.Property == ThemeProperty) if (change.Property == ThemeProperty)
{
OnControlThemeChanged(); OnControlThemeChanged();
}
else if (change.Property == ThemeVariant.RequestedThemeVariantProperty)
{
if (change.GetNewValue<ThemeVariant>() 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() private protected virtual void OnControlThemeChanged()

15
src/Avalonia.Base/Styling/ThemeVariant.cs

@ -11,6 +11,21 @@ namespace Avalonia.Styling;
[TypeConverter(typeof(ThemeVariantTypeConverter))] [TypeConverter(typeof(ThemeVariantTypeConverter))]
public sealed record ThemeVariant public sealed record ThemeVariant
{ {
/// <summary>
/// Defines the ActualThemeVariant property.
/// </summary>
internal static readonly StyledProperty<ThemeVariant> ActualThemeVariantProperty =
AvaloniaProperty.Register<StyledElement, ThemeVariant>(
"ActualThemeVariant",
inherits: true);
/// <summary>
/// Defines the RequestedThemeVariant property.
/// </summary>
internal static readonly StyledProperty<ThemeVariant?> RequestedThemeVariantProperty =
AvaloniaProperty.Register<StyledElement, ThemeVariant?>(
"RequestedThemeVariant", defaultValue: Default);
/// <summary> /// <summary>
/// Creates a new instance of the <see cref="ThemeVariant"/> /// Creates a new instance of the <see cref="ThemeVariant"/>
/// </summary> /// </summary>

18
src/Avalonia.Controls/Control.cs

@ -24,7 +24,7 @@ namespace Avalonia.Controls
/// - A <see cref="Tag"/> property to allow user-defined data to be attached to the control. /// - A <see cref="Tag"/> property to allow user-defined data to be attached to the control.
/// - <see cref="ContextRequestedEvent"/> and other context menu related members. /// - <see cref="ContextRequestedEvent"/> and other context menu related members.
/// </remarks> /// </remarks>
public class Control : InputElement, IDataTemplateHost, INamed, IVisualBrushInitialize, ISetterValue, IThemeVariantHost public class Control : InputElement, IDataTemplateHost, INamed, IVisualBrushInitialize, ISetterValue
{ {
/// <summary> /// <summary>
/// Defines the <see cref="FocusAdorner"/> property. /// Defines the <see cref="FocusAdorner"/> property.
@ -162,11 +162,6 @@ namespace Avalonia.Controls
set => SetValue(TagProperty, value); set => SetValue(TagProperty, value);
} }
/// <inheritdoc />
public ThemeVariant ActualThemeVariant => GetValue(ThemeVariantScope.ActualThemeVariantProperty);
public event EventHandler? ActualThemeVariantChanged;
/// <summary> /// <summary>
/// Occurs when the user has completed a context input gesture, such as a right-click. /// Occurs when the user has completed a context input gesture, such as a right-click.
/// </summary> /// </summary>
@ -531,17 +526,6 @@ namespace Avalonia.Controls
RaiseEvent(sizeChangedEventArgs); RaiseEvent(sizeChangedEventArgs);
} }
} }
else if (change.Property == ThemeVariantScope.RequestedThemeVariantProperty)
{
if (change.GetNewValue<ThemeVariant>() is {} themeVariant && themeVariant != ThemeVariant.Default)
SetValue(ThemeVariantScope.ActualThemeVariantProperty, themeVariant);
else
ClearValue(ThemeVariantScope.ActualThemeVariantProperty);
}
else if (change.Property == ThemeVariantScope.ActualThemeVariantProperty)
{
ActualThemeVariantChanged?.Invoke(this, EventArgs.Empty);
}
} }
} }
} }

16
src/Avalonia.Controls/ThemeVariantScope.cs

@ -7,21 +7,13 @@ namespace Avalonia.Controls
/// </summary> /// </summary>
public class ThemeVariantScope : Decorator public class ThemeVariantScope : Decorator
{ {
/// <summary> /// <inheritdoc cref="ThemeVariant.ActualThemeVariantProperty" />
/// Defines the <see cref="Control.ActualThemeVariant"/> property.
/// </summary>
public static readonly StyledProperty<ThemeVariant> ActualThemeVariantProperty = public static readonly StyledProperty<ThemeVariant> ActualThemeVariantProperty =
AvaloniaProperty.Register<ThemeVariantScope, ThemeVariant>( ThemeVariant.ActualThemeVariantProperty.AddOwner<ThemeVariantScope>();
nameof(ActualThemeVariant),
inherits: true);
/// <summary> /// <inheritdoc cref="ThemeVariant.RequestedThemeVariantProperty" />
/// Defines the <see cref="RequestedThemeVariant"/> property.
/// </summary>
public static readonly StyledProperty<ThemeVariant?> RequestedThemeVariantProperty = public static readonly StyledProperty<ThemeVariant?> RequestedThemeVariantProperty =
AvaloniaProperty.Register<ThemeVariantScope, ThemeVariant?>( ThemeVariant.RequestedThemeVariantProperty.AddOwner<ThemeVariantScope>();
nameof(RequestedThemeVariant),
defaultValue: ThemeVariant.Default);
/// <summary> /// <summary>
/// Gets or sets the UI theme variant that is used by the control (and its child elements) for resource determination. /// Gets or sets the UI theme variant that is used by the control (and its child elements) for resource determination.

Loading…
Cancel
Save