ValueStore.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Data;
  4. using Avalonia.Utilities;
  5. namespace Avalonia
  6. {
  7. internal class ValueStore : IPriorityValueOwner
  8. {
  9. private readonly AvaloniaPropertyValueStore<object> _propertyValues;
  10. private readonly AvaloniaPropertyValueStore<object> _deferredSetters;
  11. private readonly AvaloniaObject _owner;
  12. public ValueStore(AvaloniaObject owner)
  13. {
  14. _owner = owner;
  15. _propertyValues = new AvaloniaPropertyValueStore<object>();
  16. _deferredSetters = new AvaloniaPropertyValueStore<object>();
  17. }
  18. public IDisposable AddBinding(
  19. AvaloniaProperty property,
  20. IObservable<object> source,
  21. BindingPriority priority)
  22. {
  23. PriorityValue priorityValue;
  24. if (_propertyValues.TryGetValue(property, out var v))
  25. {
  26. priorityValue = v as PriorityValue;
  27. if (priorityValue == null)
  28. {
  29. priorityValue = CreatePriorityValue(property);
  30. priorityValue.SetValue(v, (int)BindingPriority.LocalValue);
  31. _propertyValues.SetValue(property, priorityValue);
  32. }
  33. }
  34. else
  35. {
  36. priorityValue = CreatePriorityValue(property);
  37. _propertyValues.AddValue(property, priorityValue);
  38. }
  39. return priorityValue.Add(source, (int)priority);
  40. }
  41. public void AddValue(AvaloniaProperty property, object value, int priority)
  42. {
  43. PriorityValue priorityValue;
  44. if (_propertyValues.TryGetValue(property, out var v))
  45. {
  46. priorityValue = v as PriorityValue;
  47. if (priorityValue == null)
  48. {
  49. if (priority == (int)BindingPriority.LocalValue)
  50. {
  51. Validate(property, ref value);
  52. _propertyValues.SetValue(property, value);
  53. Changed(property, priority, v, value);
  54. return;
  55. }
  56. else
  57. {
  58. priorityValue = CreatePriorityValue(property);
  59. priorityValue.SetValue(v, (int)BindingPriority.LocalValue);
  60. _propertyValues.SetValue(property, priorityValue);
  61. }
  62. }
  63. }
  64. else
  65. {
  66. if (value == AvaloniaProperty.UnsetValue)
  67. {
  68. return;
  69. }
  70. if (priority == (int)BindingPriority.LocalValue)
  71. {
  72. Validate(property, ref value);
  73. _propertyValues.AddValue(property, value);
  74. Changed(property, priority, AvaloniaProperty.UnsetValue, value);
  75. return;
  76. }
  77. else
  78. {
  79. priorityValue = CreatePriorityValue(property);
  80. _propertyValues.AddValue(property, priorityValue);
  81. }
  82. }
  83. priorityValue.SetValue(value, priority);
  84. }
  85. public void BindingNotificationReceived(AvaloniaProperty property, BindingNotification notification)
  86. {
  87. _owner.BindingNotificationReceived(property, notification);
  88. }
  89. public void Changed(AvaloniaProperty property, int priority, object oldValue, object newValue)
  90. {
  91. _owner.PriorityValueChanged(property, priority, oldValue, newValue);
  92. }
  93. public IDictionary<AvaloniaProperty, object> GetSetValues()
  94. {
  95. return _propertyValues.ToDictionary();
  96. }
  97. public void LogError(AvaloniaProperty property, Exception e)
  98. {
  99. _owner.LogBindingError(property, e);
  100. }
  101. public object GetValue(AvaloniaProperty property)
  102. {
  103. var result = AvaloniaProperty.UnsetValue;
  104. if (_propertyValues.TryGetValue(property, out var value))
  105. {
  106. result = (value is PriorityValue priorityValue) ? priorityValue.Value : value;
  107. }
  108. return result;
  109. }
  110. public bool IsAnimating(AvaloniaProperty property)
  111. {
  112. return _propertyValues.TryGetValue(property, out var value) && value is PriorityValue priority && priority.IsAnimating;
  113. }
  114. public bool IsSet(AvaloniaProperty property)
  115. {
  116. if (_propertyValues.TryGetValue(property, out var value))
  117. {
  118. return ((value as PriorityValue)?.Value ?? value) != AvaloniaProperty.UnsetValue;
  119. }
  120. return false;
  121. }
  122. public void Revalidate(AvaloniaProperty property)
  123. {
  124. if (_propertyValues.TryGetValue(property, out var value))
  125. {
  126. (value as PriorityValue)?.Revalidate();
  127. }
  128. }
  129. public void VerifyAccess() => _owner.VerifyAccess();
  130. private PriorityValue CreatePriorityValue(AvaloniaProperty property)
  131. {
  132. var validate = ((IStyledPropertyAccessor)property).GetValidationFunc(_owner.GetType());
  133. Func<object, object> validate2 = null;
  134. if (validate != null)
  135. {
  136. validate2 = v => validate(_owner, v);
  137. }
  138. return new PriorityValue(
  139. this,
  140. property,
  141. property.PropertyType,
  142. validate2);
  143. }
  144. private void Validate(AvaloniaProperty property, ref object value)
  145. {
  146. var validate = ((IStyledPropertyAccessor)property).GetValidationFunc(_owner.GetType());
  147. if (validate != null && value != AvaloniaProperty.UnsetValue)
  148. {
  149. value = validate(_owner, value);
  150. }
  151. }
  152. private DeferredSetter<T> GetDeferredSetter<T>(AvaloniaProperty property)
  153. {
  154. if (_deferredSetters.TryGetValue(property, out var deferredSetter))
  155. {
  156. return (DeferredSetter<T>)deferredSetter;
  157. }
  158. var newDeferredSetter = new DeferredSetter<T>();
  159. _deferredSetters.AddValue(property, newDeferredSetter);
  160. return newDeferredSetter;
  161. }
  162. public DeferredSetter<object> GetNonDirectDeferredSetter(AvaloniaProperty property)
  163. {
  164. return GetDeferredSetter<object>(property);
  165. }
  166. public DeferredSetter<T> GetDirectDeferredSetter<T>(AvaloniaProperty<T> property)
  167. {
  168. return GetDeferredSetter<T>(property);
  169. }
  170. }
  171. }