Browse Source

Remove IStyleable

pull/20613/head
Julien Lebosquain 2 days ago
parent
commit
415d0a6d08
No known key found for this signature in database GPG Key ID: 1833CAD10ACC46FD
  1. 27
      src/Avalonia.Base/StyledElement.cs
  2. 2
      src/Avalonia.Base/Styling/ControlTheme.cs
  3. 28
      src/Avalonia.Base/Styling/IStyleable.cs
  4. 4
      src/Avalonia.Base/Styling/NestingSelector.cs
  5. 2
      src/Avalonia.Base/Styling/TypeNameAndClassSelector.cs
  6. 2
      src/Avalonia.Controls/ItemsControl.cs

27
src/Avalonia.Base/StyledElement.cs

@ -21,7 +21,6 @@ namespace Avalonia
/// Extends an <see cref="Animatable"/> with the following features:
///
/// - An inherited <see cref="DataContext"/>.
/// - Implements <see cref="IStyleable"/> to allow styling to work on the styled element.
/// - Implements <see cref="ILogical"/> to form part of a logical tree.
/// - A collection of class strings for custom styling.
/// </summary>
@ -35,10 +34,7 @@ namespace Avalonia
ISetInheritanceParent,
ISupportInitialize,
INamed,
IAvaloniaListItemValidator<ILogical>,
#pragma warning disable CS0618 // Type or member is obsolete
IStyleable
#pragma warning restore CS0618 // Type or member is obsolete
IAvaloniaListItemValidator<ILogical>
{
/// <summary>
/// Defines the <see cref="DataContext"/> property.
@ -330,9 +326,6 @@ namespace Avalonia
bool IResourceNode.HasResources => (_resources?.HasResources ?? false) ||
(((IResourceNode?)_styles)?.HasResources ?? false);
/// <inheritdoc/>
IAvaloniaReadOnlyList<string> IStyleable.Classes => Classes;
/// <inheritdoc/>
bool IStyleHost.IsStylesInitialized => _styles != null;
@ -668,7 +661,7 @@ namespace Avalonia
// If the Theme property is not set, try to find a ControlTheme resource with our StyleKey.
if (_implicitTheme is null)
{
var key = GetStyleKey(this);
var key = StyleKey;
if (this.TryFindResource(key, out var value) && value is ControlTheme t)
_implicitTheme = t;
@ -699,22 +692,6 @@ namespace Avalonia
}
}
/// <summary>
/// Internal getter for <see cref="IStyleable.StyleKey"/> so that we only need to suppress the obsolete
/// warning in one place.
/// </summary>
/// <param name="e">The element</param>
/// <remarks>
/// <see cref="IStyleable"/> is obsolete and will be removed in a future version, but for backwards
/// compatibility we need to support code which overrides <see cref="IStyleable.StyleKey"/>.
/// </remarks>
internal static Type GetStyleKey(StyledElement e)
{
#pragma warning disable CS0618 // Type or member is obsolete
return ((IStyleable)e).StyleKey;
#pragma warning restore CS0618 // Type or member is obsolete
}
private static void DataContextNotifying(AvaloniaObject o, bool updateStarted)
{
if (o is StyledElement element)

2
src/Avalonia.Base/Styling/ControlTheme.cs

@ -50,7 +50,7 @@ namespace Avalonia.Styling
using var activity = Diagnostic.AttachingStyle()?
.AddTag(Diagnostic.Tags.Style, this);
if (HasSettersOrAnimations && TargetType.IsAssignableFrom(StyledElement.GetStyleKey(target)))
if (HasSettersOrAnimations && TargetType.IsAssignableFrom(target.StyleKey))
{
Attach(target, null, type, true);
activity?.AddTag(Diagnostic.Tags.SelectorResult, SelectorMatchResult.AlwaysThisType);

28
src/Avalonia.Base/Styling/IStyleable.cs

@ -1,28 +0,0 @@
using System;
using Avalonia.Collections;
namespace Avalonia.Styling
{
/// <summary>
/// Interface for styleable elements.
/// </summary>
[Obsolete("This interface may be removed in 12.0. Use StyledElement, or override StyledElement.StyleKeyOverride to override the StyleKey for a class.")]
public interface IStyleable : INamed
{
/// <summary>
/// Gets the list of classes for the control.
/// </summary>
IAvaloniaReadOnlyList<string> Classes { get; }
/// <summary>
/// Gets the type by which the control is styled.
/// </summary>
[Obsolete("Override StyledElement.StyleKeyOverride instead.")]
Type StyleKey { get; }
/// <summary>
/// Gets the template parent of this element if the control comes from a template.
/// </summary>
AvaloniaObject? TemplatedParent { get; }
}
}

4
src/Avalonia.Base/Styling/NestingSelector.cs

@ -23,7 +23,7 @@ namespace Avalonia.Styling
{
if (theme.TargetType is null)
throw new InvalidOperationException("ControlTheme has no TargetType.");
return theme.TargetType.IsAssignableFrom(StyledElement.GetStyleKey(control)) ?
return theme.TargetType.IsAssignableFrom(control.StyleKey) ?
SelectorMatch.AlwaysThisType :
SelectorMatch.NeverThisType;
}
@ -31,7 +31,7 @@ namespace Avalonia.Styling
{
if (queryTheme.TargetType is null)
throw new InvalidOperationException("ControlTheme has no TargetType.");
return queryTheme.TargetType.IsAssignableFrom(StyledElement.GetStyleKey(control)) ?
return queryTheme.TargetType.IsAssignableFrom(control.StyleKey) ?
SelectorMatch.AlwaysThisType :
SelectorMatch.NeverThisType;
}

2
src/Avalonia.Base/Styling/TypeNameAndClassSelector.cs

@ -93,7 +93,7 @@ namespace Avalonia.Styling
{
if (TargetType != null)
{
var controlType = StyledElement.GetStyleKey(control) ?? control.GetType();
var controlType = control.StyleKey ?? control.GetType();
if (IsConcreteType)
{

2
src/Avalonia.Controls/ItemsControl.cs

@ -720,7 +720,7 @@ namespace Avalonia.Controls
{
var itemContainerTheme = ItemContainerTheme;
if (itemContainerTheme?.TargetType?.IsAssignableFrom(GetStyleKey(container)) == true)
if (itemContainerTheme?.TargetType?.IsAssignableFrom(container.StyleKey) == true)
{
// We have an ItemContainerTheme and it matches the container. Set the Theme
// property, and mark the container as having had ItemContainerTheme applied.

Loading…
Cancel
Save