ConstantValueEntry.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using Avalonia.Data;
  4. #nullable enable
  5. namespace Avalonia.PropertyStore
  6. {
  7. /// <summary>
  8. /// Represents an untyped interface to <see cref="ConstantValueEntry{T}"/>.
  9. /// </summary>
  10. internal interface IConstantValueEntry : IPriorityValueEntry, IDisposable
  11. {
  12. }
  13. /// <summary>
  14. /// Stores a value with a priority in a <see cref="ValueStore"/> or
  15. /// <see cref="PriorityValue{T}"/>.
  16. /// </summary>
  17. /// <typeparam name="T">The property type.</typeparam>
  18. internal class ConstantValueEntry<T> : IPriorityValueEntry<T>, IConstantValueEntry
  19. {
  20. private IValueSink _sink;
  21. private Optional<T> _value;
  22. public ConstantValueEntry(
  23. StyledPropertyBase<T> property,
  24. [AllowNull] T value,
  25. BindingPriority priority,
  26. IValueSink sink)
  27. {
  28. Property = property;
  29. _value = value;
  30. Priority = priority;
  31. _sink = sink;
  32. }
  33. public StyledPropertyBase<T> Property { get; }
  34. public BindingPriority Priority { get; private set; }
  35. Optional<object> IValue.GetValue() => _value.ToObject();
  36. public Optional<T> GetValue(BindingPriority maxPriority = BindingPriority.Animation)
  37. {
  38. return Priority >= maxPriority ? _value : Optional<T>.Empty;
  39. }
  40. public void Dispose()
  41. {
  42. var oldValue = _value;
  43. _value = default;
  44. Priority = BindingPriority.Unset;
  45. _sink.Completed(Property, this, oldValue);
  46. }
  47. public void Reparent(IValueSink sink) => _sink = sink;
  48. public void Start() { }
  49. public void RaiseValueChanged(
  50. IValueSink sink,
  51. IAvaloniaObject owner,
  52. AvaloniaProperty property,
  53. Optional<object> oldValue,
  54. Optional<object> newValue)
  55. {
  56. sink.ValueChanged(new AvaloniaPropertyChangedEventArgs<T>(
  57. owner,
  58. (AvaloniaProperty<T>)property,
  59. oldValue.GetValueOrDefault<T>(),
  60. newValue.GetValueOrDefault<T>(),
  61. Priority));
  62. }
  63. }
  64. }