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

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

@ -11,6 +11,21 @@ namespace Avalonia.Styling;
[TypeConverter(typeof(ThemeVariantTypeConverter))]
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>
/// Creates a new instance of the <see cref="ThemeVariant"/>
/// </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.
/// - <see cref="ContextRequestedEvent"/> and other context menu related members.
/// </remarks>
public class Control : InputElement, IDataTemplateHost, INamed, IVisualBrushInitialize, ISetterValue, IThemeVariantHost
public class Control : InputElement, IDataTemplateHost, INamed, IVisualBrushInitialize, ISetterValue
{
/// <summary>
/// Defines the <see cref="FocusAdorner"/> property.
@ -162,11 +162,6 @@ namespace Avalonia.Controls
set => SetValue(TagProperty, value);
}
/// <inheritdoc />
public ThemeVariant ActualThemeVariant => GetValue(ThemeVariantScope.ActualThemeVariantProperty);
public event EventHandler? ActualThemeVariantChanged;
/// <summary>
/// Occurs when the user has completed a context input gesture, such as a right-click.
/// </summary>
@ -531,17 +526,6 @@ namespace Avalonia.Controls
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>
public class ThemeVariantScope : Decorator
{
/// <summary>
/// Defines the <see cref="Control.ActualThemeVariant"/> property.
/// </summary>
/// <inheritdoc cref="ThemeVariant.ActualThemeVariantProperty" />
public static readonly StyledProperty<ThemeVariant> ActualThemeVariantProperty =
AvaloniaProperty.Register<ThemeVariantScope, ThemeVariant>(
nameof(ActualThemeVariant),
inherits: true);
ThemeVariant.ActualThemeVariantProperty.AddOwner<ThemeVariantScope>();
/// <summary>
/// Defines the <see cref="RequestedThemeVariant"/> property.
/// </summary>
/// <inheritdoc cref="ThemeVariant.RequestedThemeVariantProperty" />
public static readonly StyledProperty<ThemeVariant?> RequestedThemeVariantProperty =
AvaloniaProperty.Register<ThemeVariantScope, ThemeVariant?>(
nameof(RequestedThemeVariant),
defaultValue: ThemeVariant.Default);
ThemeVariant.RequestedThemeVariantProperty.AddOwner<ThemeVariantScope>();
/// <summary>
/// Gets or sets the UI theme variant that is used by the control (and its child elements) for resource determination.

Loading…
Cancel
Save