Browse Source

Merge pull request #10059 from AvaloniaUI/refactor/remove-styledpropertybase

Remove StyledPropertyBase class.
pull/10058/head
Max Katz 4 years ago
committed by GitHub
parent
commit
10db38de13
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      src/Avalonia.Base/AvaloniaObject.cs
  2. 8
      src/Avalonia.Base/AvaloniaObjectExtensions.cs
  3. 20
      src/Avalonia.Base/PropertyStore/EffectiveValue`1.cs
  4. 4
      src/Avalonia.Base/PropertyStore/ImmediateValueEntry.cs
  5. 8
      src/Avalonia.Base/PropertyStore/ImmediateValueFrame.cs
  6. 6
      src/Avalonia.Base/PropertyStore/LocalValueBindingObserver.cs
  7. 4
      src/Avalonia.Base/PropertyStore/LocalValueUntypedBindingObserver.cs
  8. 4
      src/Avalonia.Base/PropertyStore/SourceUntypedBindingEntry.cs
  9. 6
      src/Avalonia.Base/PropertyStore/TypedBindingEntry.cs
  10. 2
      src/Avalonia.Base/PropertyStore/UntypedValueUtils.cs
  11. 18
      src/Avalonia.Base/PropertyStore/ValueStore.cs
  12. 208
      src/Avalonia.Base/StyledProperty.cs
  13. 237
      src/Avalonia.Base/StyledPropertyBase.cs
  14. 4
      src/Avalonia.Base/Styling/PropertySetterInstance.cs
  15. 2
      src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlWellKnownTypes.cs

16
src/Avalonia.Base/AvaloniaObject.cs

@ -132,7 +132,7 @@ namespace Avalonia
switch (property)
{
case StyledPropertyBase<T> styled:
case StyledProperty<T> styled:
ClearValue(styled);
break;
case DirectPropertyBase<T> direct:
@ -147,7 +147,7 @@ namespace Avalonia
/// Clears a <see cref="AvaloniaProperty"/>'s local value.
/// </summary>
/// <param name="property">The property.</param>
public void ClearValue<T>(StyledPropertyBase<T> property)
public void ClearValue<T>(StyledProperty<T> property)
{
property = property ?? throw new ArgumentNullException(nameof(property));
VerifyAccess();
@ -220,7 +220,7 @@ namespace Avalonia
/// <typeparam name="T">The type of the property.</typeparam>
/// <param name="property">The property.</param>
/// <returns>The value.</returns>
public T GetValue<T>(StyledPropertyBase<T> property)
public T GetValue<T>(StyledProperty<T> property)
{
_ = property ?? throw new ArgumentNullException(nameof(property));
VerifyAccess();
@ -243,7 +243,7 @@ namespace Avalonia
}
/// <inheritdoc/>
public Optional<T> GetBaseValue<T>(StyledPropertyBase<T> property)
public Optional<T> GetBaseValue<T>(StyledProperty<T> property)
{
_ = property ?? throw new ArgumentNullException(nameof(property));
VerifyAccess();
@ -309,7 +309,7 @@ namespace Avalonia
/// An <see cref="IDisposable"/> if setting the property can be undone, otherwise null.
/// </returns>
public IDisposable? SetValue<T>(
StyledPropertyBase<T> property,
StyledProperty<T> property,
T value,
BindingPriority priority = BindingPriority.LocalValue)
{
@ -373,7 +373,7 @@ namespace Avalonia
/// A disposable which can be used to terminate the binding.
/// </returns>
public IDisposable Bind<T>(
StyledPropertyBase<T> property,
StyledProperty<T> property,
IObservable<object?> source,
BindingPriority priority = BindingPriority.LocalValue)
{
@ -396,7 +396,7 @@ namespace Avalonia
/// A disposable which can be used to terminate the binding.
/// </returns>
public IDisposable Bind<T>(
StyledPropertyBase<T> property,
StyledProperty<T> property,
IObservable<T> source,
BindingPriority priority = BindingPriority.LocalValue)
{
@ -419,7 +419,7 @@ namespace Avalonia
/// A disposable which can be used to terminate the binding.
/// </returns>
public IDisposable Bind<T>(
StyledPropertyBase<T> property,
StyledProperty<T> property,
IObservable<BindingValue<T>> source,
BindingPriority priority = BindingPriority.LocalValue)
{

8
src/Avalonia.Base/AvaloniaObjectExtensions.cs

@ -146,7 +146,7 @@ namespace Avalonia
return property switch
{
StyledPropertyBase<T> styled => target.Bind(styled, source, priority),
StyledProperty<T> styled => target.Bind(styled, source, priority),
DirectPropertyBase<T> direct => target.Bind(direct, source),
_ => throw new NotSupportedException("Unsupported AvaloniaProperty type."),
};
@ -170,7 +170,7 @@ namespace Avalonia
{
return property switch
{
StyledPropertyBase<T> styled => target.Bind(styled, source, priority),
StyledProperty<T> styled => target.Bind(styled, source, priority),
DirectPropertyBase<T> direct => target.Bind(direct, source),
_ => throw new NotSupportedException("Unsupported AvaloniaProperty type."),
};
@ -231,7 +231,7 @@ namespace Avalonia
return property switch
{
StyledPropertyBase<T> styled => target.GetValue(styled),
StyledProperty<T> styled => target.GetValue(styled),
DirectPropertyBase<T> direct => target.GetValue(direct),
_ => throw new NotSupportedException("Unsupported AvaloniaProperty type.")
};
@ -280,7 +280,7 @@ namespace Avalonia
return property switch
{
StyledPropertyBase<T> styled => target.GetBaseValue(styled),
StyledProperty<T> styled => target.GetBaseValue(styled),
DirectPropertyBase<T> direct => target.GetValue(direct),
_ => throw new NotSupportedException("Unsupported AvaloniaProperty type.")
};

20
src/Avalonia.Base/PropertyStore/EffectiveValue`1.cs

@ -19,7 +19,7 @@ namespace Avalonia.PropertyStore
private T? _baseValue;
private UncommonFields? _uncommon;
public EffectiveValue(AvaloniaObject owner, StyledPropertyBase<T> property)
public EffectiveValue(AvaloniaObject owner, StyledProperty<T> property)
{
Priority = BindingPriority.Unset;
BasePriority = BindingPriority.Unset;
@ -57,12 +57,12 @@ namespace Avalonia.PropertyStore
Debug.Assert(priority != BindingPriority.LocalValue);
UpdateValueEntry(value, priority);
SetAndRaiseCore(owner, (StyledPropertyBase<T>)value.Property, GetValue(value), priority);
SetAndRaiseCore(owner, (StyledProperty<T>)value.Property, GetValue(value), priority);
}
public void SetLocalValueAndRaise(
ValueStore owner,
StyledPropertyBase<T> property,
StyledProperty<T> property,
T value)
{
SetAndRaiseCore(owner, property, value, BindingPriority.LocalValue);
@ -82,7 +82,7 @@ namespace Avalonia.PropertyStore
{
Debug.Assert(oldValue is not null || newValue is not null);
var p = (StyledPropertyBase<T>)property;
var p = (StyledProperty<T>)property;
var o = oldValue is not null ? ((EffectiveValue<T>)oldValue).Value : _metadata.DefaultValue;
var n = newValue is not null ? ((EffectiveValue<T>)newValue).Value : _metadata.DefaultValue;
var priority = newValue is not null ? BindingPriority.Inherited : BindingPriority.Unset;
@ -98,7 +98,7 @@ namespace Avalonia.PropertyStore
Debug.Assert(Priority != BindingPriority.Animation);
Debug.Assert(BasePriority != BindingPriority.Unset);
UpdateValueEntry(null, BindingPriority.Animation);
SetAndRaiseCore(owner, (StyledPropertyBase<T>)property, _baseValue!, BasePriority);
SetAndRaiseCore(owner, (StyledProperty<T>)property, _baseValue!, BasePriority);
}
public override void CoerceValue(ValueStore owner, AvaloniaProperty property)
@ -107,7 +107,7 @@ namespace Avalonia.PropertyStore
return;
SetAndRaiseCore(
owner,
(StyledPropertyBase<T>)property,
(StyledProperty<T>)property,
_uncommon._uncoercedValue!,
Priority,
_uncommon._uncoercedBaseValue!,
@ -117,10 +117,10 @@ namespace Avalonia.PropertyStore
public override void DisposeAndRaiseUnset(ValueStore owner, AvaloniaProperty property)
{
UnsubscribeValueEntries();
DisposeAndRaiseUnset(owner, (StyledPropertyBase<T>)property);
DisposeAndRaiseUnset(owner, (StyledProperty<T>)property);
}
public void DisposeAndRaiseUnset(ValueStore owner, StyledPropertyBase<T> property)
public void DisposeAndRaiseUnset(ValueStore owner, StyledProperty<T> property)
{
BindingPriority priority;
T oldValue;
@ -156,7 +156,7 @@ namespace Avalonia.PropertyStore
private void SetAndRaiseCore(
ValueStore owner,
StyledPropertyBase<T> property,
StyledProperty<T> property,
T value,
BindingPriority priority)
{
@ -203,7 +203,7 @@ namespace Avalonia.PropertyStore
private void SetAndRaiseCore(
ValueStore owner,
StyledPropertyBase<T> property,
StyledProperty<T> property,
T value,
BindingPriority priority,
T baseValue,

4
src/Avalonia.Base/PropertyStore/ImmediateValueEntry.cs

@ -9,7 +9,7 @@ namespace Avalonia.PropertyStore
public ImmediateValueEntry(
ImmediateValueFrame owner,
StyledPropertyBase<T> property,
StyledProperty<T> property,
T value)
{
_owner = owner;
@ -17,7 +17,7 @@ namespace Avalonia.PropertyStore
Property = property;
}
public StyledPropertyBase<T> Property { get; }
public StyledProperty<T> Property { get; }
public bool HasValue => true;
AvaloniaProperty IValueEntry.Property => Property;

8
src/Avalonia.Base/PropertyStore/ImmediateValueFrame.cs

@ -15,7 +15,7 @@ namespace Avalonia.PropertyStore
}
public TypedBindingEntry<T> AddBinding<T>(
StyledPropertyBase<T> property,
StyledProperty<T> property,
IObservable<BindingValue<T>> source)
{
var e = new TypedBindingEntry<T>(this, property, source);
@ -24,7 +24,7 @@ namespace Avalonia.PropertyStore
}
public TypedBindingEntry<T> AddBinding<T>(
StyledPropertyBase<T> property,
StyledProperty<T> property,
IObservable<T> source)
{
var e = new TypedBindingEntry<T>(this, property, source);
@ -33,7 +33,7 @@ namespace Avalonia.PropertyStore
}
public SourceUntypedBindingEntry<T> AddBinding<T>(
StyledPropertyBase<T> property,
StyledProperty<T> property,
IObservable<object?> source)
{
var e = new SourceUntypedBindingEntry<T>(this, property, source);
@ -41,7 +41,7 @@ namespace Avalonia.PropertyStore
return e;
}
public ImmediateValueEntry<T> AddValue<T>(StyledPropertyBase<T> property, T value)
public ImmediateValueEntry<T> AddValue<T>(StyledProperty<T> property, T value)
{
var e = new ImmediateValueEntry<T>(this, property, value);
Add(e);

6
src/Avalonia.Base/PropertyStore/LocalValueBindingObserver.cs

@ -11,13 +11,13 @@ namespace Avalonia.PropertyStore
private readonly ValueStore _owner;
private IDisposable? _subscription;
public LocalValueBindingObserver(ValueStore owner, StyledPropertyBase<T> property)
public LocalValueBindingObserver(ValueStore owner, StyledProperty<T> property)
{
_owner = owner;
Property = property;
}
public StyledPropertyBase<T> Property { get;}
public StyledProperty<T> Property { get;}
public void Start(IObservable<T> source)
{
@ -41,7 +41,7 @@ namespace Avalonia.PropertyStore
public void OnNext(T value)
{
static void Execute(ValueStore owner, StyledPropertyBase<T> property, T value)
static void Execute(ValueStore owner, StyledProperty<T> property, T value)
{
if (property.ValidateValue?.Invoke(value) != false)
owner.SetValue(property, value, BindingPriority.LocalValue);

4
src/Avalonia.Base/PropertyStore/LocalValueUntypedBindingObserver.cs

@ -11,13 +11,13 @@ namespace Avalonia.PropertyStore
private readonly ValueStore _owner;
private IDisposable? _subscription;
public LocalValueUntypedBindingObserver(ValueStore owner, StyledPropertyBase<T> property)
public LocalValueUntypedBindingObserver(ValueStore owner, StyledProperty<T> property)
{
_owner = owner;
Property = property;
}
public StyledPropertyBase<T> Property { get; }
public StyledProperty<T> Property { get; }
public void Start(IObservable<object?> source)
{

4
src/Avalonia.Base/PropertyStore/SourceUntypedBindingEntry.cs

@ -13,14 +13,14 @@ namespace Avalonia.PropertyStore
public SourceUntypedBindingEntry(
ValueFrame frame,
StyledPropertyBase<TTarget> property,
StyledProperty<TTarget> property,
IObservable<object?> source)
: base(frame, property, source)
{
_validate = property.ValidateValue;
}
public new StyledPropertyBase<TTarget> Property => (StyledPropertyBase<TTarget>)base.Property;
public new StyledProperty<TTarget> Property => (StyledProperty<TTarget>)base.Property;
protected override BindingValue<TTarget> ConvertAndValidate(object? value)
{

6
src/Avalonia.Base/PropertyStore/TypedBindingEntry.cs

@ -11,7 +11,7 @@ namespace Avalonia.PropertyStore
{
public TypedBindingEntry(
ValueFrame frame,
StyledPropertyBase<T> property,
StyledProperty<T> property,
IObservable<T> source)
: base(frame, property, source)
{
@ -19,13 +19,13 @@ namespace Avalonia.PropertyStore
public TypedBindingEntry(
ValueFrame frame,
StyledPropertyBase<T> property,
StyledProperty<T> property,
IObservable<BindingValue<T>> source)
: base(frame, property, source)
{
}
public new StyledPropertyBase<T> Property => (StyledPropertyBase<T>)base.Property;
public new StyledProperty<T> Property => (StyledProperty<T>)base.Property;
protected override BindingValue<T> ConvertAndValidate(T value)
{

2
src/Avalonia.Base/PropertyStore/UntypedValueUtils.cs

@ -26,7 +26,7 @@ namespace Avalonia.PropertyStore
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = TrimmingMessages.ImplicitTypeConvertionSupressWarningMessage)]
public static bool TryConvertAndValidate<T>(
StyledPropertyBase<T> property,
StyledProperty<T> property,
object? value,
[MaybeNullWhen(false)] out T result)
{

18
src/Avalonia.Base/PropertyStore/ValueStore.cs

@ -43,7 +43,7 @@ namespace Avalonia.PropertyStore
}
public IDisposable AddBinding<T>(
StyledPropertyBase<T> property,
StyledProperty<T> property,
IObservable<BindingValue<T>> source,
BindingPriority priority)
{
@ -71,7 +71,7 @@ namespace Avalonia.PropertyStore
}
public IDisposable AddBinding<T>(
StyledPropertyBase<T> property,
StyledProperty<T> property,
IObservable<T> source,
BindingPriority priority)
{
@ -99,7 +99,7 @@ namespace Avalonia.PropertyStore
}
public IDisposable AddBinding<T>(
StyledPropertyBase<T> property,
StyledProperty<T> property,
IObservable<object?> source,
BindingPriority priority)
{
@ -165,7 +165,7 @@ namespace Avalonia.PropertyStore
}
}
public IDisposable? SetValue<T>(StyledPropertyBase<T> property, T value, BindingPriority priority)
public IDisposable? SetValue<T>(StyledProperty<T> property, T value, BindingPriority priority)
{
if (property.ValidateValue?.Invoke(value) == false)
{
@ -219,7 +219,7 @@ namespace Avalonia.PropertyStore
return GetDefaultValue(property);
}
public T GetValue<T>(StyledPropertyBase<T> property)
public T GetValue<T>(StyledProperty<T> property)
{
if (_effectiveValues.TryGetValue(property, out var v))
return ((EffectiveValue<T>)v).Value;
@ -248,7 +248,7 @@ namespace Avalonia.PropertyStore
v.CoerceValue(this, property);
}
public Optional<T> GetBaseValue<T>(StyledPropertyBase<T> property)
public Optional<T> GetBaseValue<T>(StyledProperty<T> property)
{
if (TryGetEffectiveValue(property, out var v) &&
((EffectiveValue<T>)v).TryGetBaseValue(out var baseValue))
@ -450,7 +450,7 @@ namespace Avalonia.PropertyStore
/// <param name="oldValue">The old value of the property.</param>
/// <param name="value">The effective value instance.</param>
public void OnInheritedEffectiveValueChanged<T>(
StyledPropertyBase<T> property,
StyledProperty<T> property,
T oldValue,
EffectiveValue<T> value)
{
@ -475,7 +475,7 @@ namespace Avalonia.PropertyStore
/// </summary>
/// <param name="property">The property whose value changed.</param>
/// <param name="oldValue">The old value of the property.</param>
public void OnInheritedEffectiveValueDisposed<T>(StyledPropertyBase<T> property, T oldValue)
public void OnInheritedEffectiveValueDisposed<T>(StyledProperty<T> property, T oldValue)
{
Debug.Assert(property.Inherits);
@ -520,7 +520,7 @@ namespace Avalonia.PropertyStore
/// <param name="oldValue">The old value of the property.</param>
/// <param name="newValue">The new value of the property.</param>
public void OnAncestorInheritedValueChanged<T>(
StyledPropertyBase<T> property,
StyledProperty<T> property,
T oldValue,
T newValue)
{

208
src/Avalonia.Base/StyledProperty.cs

@ -1,14 +1,18 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Avalonia.Data;
using Avalonia.PropertyStore;
using Avalonia.Utilities;
namespace Avalonia
{
/// <summary>
/// A styled avalonia property.
/// </summary>
public class StyledProperty<TValue> : StyledPropertyBase<TValue>
public class StyledProperty<TValue> : AvaloniaProperty<TValue>, IStyledPropertyAccessor
{
/// <summary>
/// Initializes a new instance of the <see cref="StyledPropertyBase{T}"/> class.
/// Initializes a new instance of the <see cref="StyledProperty{T}"/> class.
/// </summary>
/// <param name="name">The name of the property.</param>
/// <param name="ownerType">The type of the class that registers the property.</param>
@ -23,20 +27,30 @@ namespace Avalonia
bool inherits = false,
Func<TValue, bool>? validate = null,
Action<AvaloniaObject, bool>? notifying = null)
: base(name, ownerType, metadata, inherits, validate, notifying)
: base(name, ownerType, metadata, notifying)
{
Inherits = inherits;
ValidateValue = validate;
HasCoercion |= metadata.CoerceValue != null;
if (validate?.Invoke(metadata.DefaultValue) == false)
{
throw new ArgumentException(
$"'{metadata.DefaultValue}' is not a valid default value for '{name}'.");
}
}
/// <summary>
/// Initializes a new instance of the <see cref="StyledPropertyBase{T}"/> class.
/// Gets the value validation callback for the property.
/// </summary>
/// <param name="source">The property to add the owner to.</param>
/// <param name="ownerType">The type of the class that registers the property.</param>
internal StyledProperty(StyledPropertyBase<TValue> source, Type ownerType)
: base(source, ownerType)
{
}
public Func<TValue, bool>? ValidateValue { get; }
/// <summary>
/// Gets a value indicating whether this property has any value coercion callbacks defined
/// in its metadata.
/// </summary>
internal bool HasCoercion { get; private set; }
/// <summary>
/// Registers the property on another type.
/// </summary>
@ -47,5 +61,177 @@ namespace Avalonia
AvaloniaPropertyRegistry.Instance.Register(typeof(TOwner), this);
return this;
}
public TValue CoerceValue(AvaloniaObject instance, TValue baseValue)
{
var metadata = GetMetadata(instance.GetType());
if (metadata.CoerceValue != null)
{
return metadata.CoerceValue.Invoke(instance, baseValue);
}
return baseValue;
}
/// <summary>
/// Gets the default value for the property on the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>The default value.</returns>
public TValue GetDefaultValue(Type type)
{
return GetMetadata(type).DefaultValue;
}
/// <summary>
/// Gets the property metadata for the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>
/// The property metadata.
/// </returns>
public new StyledPropertyMetadata<TValue> GetMetadata(Type type)
{
_ = type ?? throw new ArgumentNullException(nameof(type));
return (StyledPropertyMetadata<TValue>)base.GetMetadata(type);
}
/// <summary>
/// Overrides the default value for the property on the specified type.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="defaultValue">The default value.</param>
public void OverrideDefaultValue<T>(TValue defaultValue) where T : AvaloniaObject
{
OverrideDefaultValue(typeof(T), defaultValue);
}
/// <summary>
/// Overrides the default value for the property on the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="defaultValue">The default value.</param>
public void OverrideDefaultValue(Type type, TValue defaultValue)
{
OverrideMetadata(type, new StyledPropertyMetadata<TValue>(defaultValue));
}
/// <summary>
/// Overrides the metadata for the property on the specified type.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="metadata">The metadata.</param>
public void OverrideMetadata<T>(StyledPropertyMetadata<TValue> metadata) where T : AvaloniaObject
{
base.OverrideMetadata(typeof(T), metadata);
}
/// <summary>
/// Overrides the metadata for the property on the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="metadata">The metadata.</param>
public void OverrideMetadata(Type type, StyledPropertyMetadata<TValue> metadata)
{
if (ValidateValue != null)
{
if (!ValidateValue(metadata.DefaultValue))
{
throw new ArgumentException(
$"'{metadata.DefaultValue}' is not a valid default value for '{Name}'.");
}
}
HasCoercion |= metadata.CoerceValue != null;
base.OverrideMetadata(type, metadata);
}
/// <summary>
/// Gets the string representation of the property.
/// </summary>
/// <returns>The property's string representation.</returns>
public override string ToString()
{
return Name;
}
/// <inheritdoc/>
object? IStyledPropertyAccessor.GetDefaultValue(Type type) => GetDefaultBoxedValue(type);
bool IStyledPropertyAccessor.ValidateValue(object? value)
{
if (value is null && !typeof(TValue).IsValueType)
return ValidateValue?.Invoke(default!) ?? true;
if (value is TValue typed)
return ValidateValue?.Invoke(typed) ?? true;
return false;
}
internal override EffectiveValue CreateEffectiveValue(AvaloniaObject o)
{
return new EffectiveValue<TValue>(o, this);
}
/// <inheritdoc/>
internal override void RouteClearValue(AvaloniaObject o)
{
o.ClearValue<TValue>(this);
}
/// <inheritdoc/>
internal override object? RouteGetValue(AvaloniaObject o)
{
return o.GetValue<TValue>(this);
}
/// <inheritdoc/>
internal override object? RouteGetBaseValue(AvaloniaObject o)
{
var value = o.GetBaseValue<TValue>(this);
return value.HasValue ? value.Value : AvaloniaProperty.UnsetValue;
}
/// <inheritdoc/>
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = TrimmingMessages.ImplicitTypeConvertionSupressWarningMessage)]
internal override IDisposable? RouteSetValue(
AvaloniaObject target,
object? value,
BindingPriority priority)
{
if (value == BindingOperations.DoNothing)
{
return null;
}
else if (value == UnsetValue)
{
target.ClearValue(this);
return null;
}
else if (TypeUtilities.TryConvertImplicit(PropertyType, value, out var converted))
{
return target.SetValue<TValue>(this, (TValue)converted!, priority);
}
else
{
var type = value?.GetType().FullName ?? "(null)";
throw new ArgumentException($"Invalid value for Property '{Name}': '{value}' ({type})");
}
}
internal override IDisposable RouteBind(
AvaloniaObject target,
IObservable<object?> source,
BindingPriority priority)
{
return target.Bind<TValue>(this, source, priority);
}
private object? GetDefaultBoxedValue(Type type)
{
_ = type ?? throw new ArgumentNullException(nameof(type));
return GetMetadata(type).DefaultValue;
}
}
}

237
src/Avalonia.Base/StyledPropertyBase.cs

@ -1,237 +0,0 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Avalonia.Data;
using Avalonia.PropertyStore;
using Avalonia.Utilities;
namespace Avalonia
{
/// <summary>
/// Base class for styled properties.
/// </summary>
public abstract class StyledPropertyBase<TValue> : AvaloniaProperty<TValue>, IStyledPropertyAccessor
{
/// <summary>
/// Initializes a new instance of the <see cref="StyledPropertyBase{T}"/> class.
/// </summary>
/// <param name="name">The name of the property.</param>
/// <param name="ownerType">The type of the class that registers the property.</param>
/// <param name="metadata">The property metadata.</param>
/// <param name="inherits">Whether the property inherits its value.</param>
/// <param name="validate">A value validation callback.</param>
/// <param name="notifying">A <see cref="AvaloniaProperty.Notifying"/> callback.</param>
protected StyledPropertyBase(
string name,
Type ownerType,
StyledPropertyMetadata<TValue> metadata,
bool inherits = false,
Func<TValue, bool>? validate = null,
Action<AvaloniaObject, bool>? notifying = null)
: base(name, ownerType, metadata, notifying)
{
Inherits = inherits;
ValidateValue = validate;
HasCoercion |= metadata.CoerceValue != null;
if (validate?.Invoke(metadata.DefaultValue) == false)
{
throw new ArgumentException(
$"'{metadata.DefaultValue}' is not a valid default value for '{name}'.");
}
}
/// <summary>
/// Initializes a new instance of the <see cref="StyledPropertyBase{T}"/> class.
/// </summary>
/// <param name="source">The property to add the owner to.</param>
/// <param name="ownerType">The type of the class that registers the property.</param>
protected StyledPropertyBase(StyledPropertyBase<TValue> source, Type ownerType)
: base(source, ownerType, null)
{
Inherits = source.Inherits;
}
/// <summary>
/// Gets the value validation callback for the property.
/// </summary>
public Func<TValue, bool>? ValidateValue { get; }
/// <summary>
/// Gets a value indicating whether this property has any value coercion callbacks defined
/// in its metadata.
/// </summary>
internal bool HasCoercion { get; private set; }
public TValue CoerceValue(AvaloniaObject instance, TValue baseValue)
{
var metadata = GetMetadata(instance.GetType());
if (metadata.CoerceValue != null)
{
return metadata.CoerceValue.Invoke(instance, baseValue);
}
return baseValue;
}
/// <summary>
/// Gets the default value for the property on the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>The default value.</returns>
public TValue GetDefaultValue(Type type)
{
return GetMetadata(type).DefaultValue;
}
/// <summary>
/// Gets the property metadata for the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>
/// The property metadata.
/// </returns>
public new StyledPropertyMetadata<TValue> GetMetadata(Type type)
{
_ = type ?? throw new ArgumentNullException(nameof(type));
return (StyledPropertyMetadata<TValue>)base.GetMetadata(type);
}
/// <summary>
/// Overrides the default value for the property on the specified type.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="defaultValue">The default value.</param>
public void OverrideDefaultValue<T>(TValue defaultValue) where T : AvaloniaObject
{
OverrideDefaultValue(typeof(T), defaultValue);
}
/// <summary>
/// Overrides the default value for the property on the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="defaultValue">The default value.</param>
public void OverrideDefaultValue(Type type, TValue defaultValue)
{
OverrideMetadata(type, new StyledPropertyMetadata<TValue>(defaultValue));
}
/// <summary>
/// Overrides the metadata for the property on the specified type.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="metadata">The metadata.</param>
public void OverrideMetadata<T>(StyledPropertyMetadata<TValue> metadata) where T : AvaloniaObject
{
base.OverrideMetadata(typeof(T), metadata);
}
/// <summary>
/// Overrides the metadata for the property on the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="metadata">The metadata.</param>
public void OverrideMetadata(Type type, StyledPropertyMetadata<TValue> metadata)
{
if (ValidateValue != null)
{
if (!ValidateValue(metadata.DefaultValue))
{
throw new ArgumentException(
$"'{metadata.DefaultValue}' is not a valid default value for '{Name}'.");
}
}
HasCoercion |= metadata.CoerceValue != null;
base.OverrideMetadata(type, metadata);
}
/// <summary>
/// Gets the string representation of the property.
/// </summary>
/// <returns>The property's string representation.</returns>
public override string ToString()
{
return Name;
}
/// <inheritdoc/>
object? IStyledPropertyAccessor.GetDefaultValue(Type type) => GetDefaultBoxedValue(type);
bool IStyledPropertyAccessor.ValidateValue(object? value)
{
if (value is null && !typeof(TValue).IsValueType)
return ValidateValue?.Invoke(default!) ?? true;
if (value is TValue typed)
return ValidateValue?.Invoke(typed) ?? true;
return false;
}
internal override EffectiveValue CreateEffectiveValue(AvaloniaObject o)
{
return new EffectiveValue<TValue>(o, this);
}
/// <inheritdoc/>
internal override void RouteClearValue(AvaloniaObject o)
{
o.ClearValue<TValue>(this);
}
/// <inheritdoc/>
internal override object? RouteGetValue(AvaloniaObject o)
{
return o.GetValue<TValue>(this);
}
/// <inheritdoc/>
internal override object? RouteGetBaseValue(AvaloniaObject o)
{
var value = o.GetBaseValue<TValue>(this);
return value.HasValue ? value.Value : AvaloniaProperty.UnsetValue;
}
/// <inheritdoc/>
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = TrimmingMessages.ImplicitTypeConvertionSupressWarningMessage)]
internal override IDisposable? RouteSetValue(
AvaloniaObject target,
object? value,
BindingPriority priority)
{
if (value == BindingOperations.DoNothing)
{
return null;
}
else if (value == UnsetValue)
{
target.ClearValue(this);
return null;
}
else if (TypeUtilities.TryConvertImplicit(PropertyType, value, out var converted))
{
return target.SetValue<TValue>(this, (TValue)converted!, priority);
}
else
{
var type = value?.GetType().FullName ?? "(null)";
throw new ArgumentException($"Invalid value for Property '{Name}': '{value}' ({type})");
}
}
internal override IDisposable RouteBind(
AvaloniaObject target,
IObservable<object?> source,
BindingPriority priority)
{
return target.Bind<TValue>(this, source, priority);
}
private object? GetDefaultBoxedValue(Type type)
{
_ = type ?? throw new ArgumentNullException(nameof(type));
return GetMetadata(type).DefaultValue;
}
}
}

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

@ -14,7 +14,7 @@ namespace Avalonia.Styling
ISetterInstance
{
private readonly StyledElement _target;
private readonly StyledPropertyBase<T>? _styledProperty;
private readonly StyledProperty<T>? _styledProperty;
private readonly DirectPropertyBase<T>? _directProperty;
private readonly T _value;
private IDisposable? _subscription;
@ -22,7 +22,7 @@ namespace Avalonia.Styling
public PropertySetterInstance(
StyledElement target,
StyledPropertyBase<T> property,
StyledProperty<T> property,
T value)
{
_target = target;

2
src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlWellKnownTypes.cs

@ -126,7 +126,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers
AvaloniaObjectSetStyledPropertyValue = AvaloniaObject
.FindMethod(m => m.IsPublic && !m.IsStatic && m.Name == "SetValue"
&& m.Parameters.Count == 3
&& m.Parameters[0].Name == "StyledPropertyBase`1"
&& m.Parameters[0].Name == "StyledProperty`1"
&& m.Parameters[2].Equals(BindingPriority));
IBinding = cfg.TypeSystem.GetType("Avalonia.Data.IBinding");
IDisposable = cfg.TypeSystem.GetType("System.IDisposable");

Loading…
Cancel
Save