RangeBase.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using Avalonia.Data;
  5. using Avalonia.Utilities;
  6. namespace Avalonia.Controls.Primitives
  7. {
  8. /// <summary>
  9. /// Base class for controls that display a value within a range.
  10. /// </summary>
  11. public abstract class RangeBase : TemplatedControl
  12. {
  13. /// <summary>
  14. /// Defines the <see cref="Minimum"/> property.
  15. /// </summary>
  16. public static readonly DirectProperty<RangeBase, double> MinimumProperty =
  17. AvaloniaProperty.RegisterDirect<RangeBase, double>(
  18. nameof(Minimum),
  19. o => o.Minimum,
  20. (o, v) => o.Minimum = v);
  21. /// <summary>
  22. /// Defines the <see cref="Maximum"/> property.
  23. /// </summary>
  24. public static readonly DirectProperty<RangeBase, double> MaximumProperty =
  25. AvaloniaProperty.RegisterDirect<RangeBase, double>(
  26. nameof(Maximum),
  27. o => o.Maximum,
  28. (o, v) => o.Maximum = v);
  29. /// <summary>
  30. /// Defines the <see cref="Value"/> property.
  31. /// </summary>
  32. public static readonly DirectProperty<RangeBase, double> ValueProperty =
  33. AvaloniaProperty.RegisterDirect<RangeBase, double>(
  34. nameof(Value),
  35. o => o.Value,
  36. (o, v) => o.Value = v,
  37. defaultBindingMode: BindingMode.TwoWay);
  38. /// <summary>
  39. /// Defines the <see cref="SmallChange"/> property.
  40. /// </summary>
  41. public static readonly StyledProperty<double> SmallChangeProperty =
  42. AvaloniaProperty.Register<RangeBase, double>(nameof(SmallChange), 1);
  43. /// <summary>
  44. /// Defines the <see cref="LargeChange"/> property.
  45. /// </summary>
  46. public static readonly StyledProperty<double> LargeChangeProperty =
  47. AvaloniaProperty.Register<RangeBase, double>(nameof(LargeChange), 10);
  48. private double _minimum;
  49. private double _maximum = 100.0;
  50. private double _value;
  51. /// <summary>
  52. /// Initializes a new instance of the <see cref="RangeBase"/> class.
  53. /// </summary>
  54. public RangeBase()
  55. {
  56. }
  57. /// <summary>
  58. /// Gets or sets the minimum value.
  59. /// </summary>
  60. public double Minimum
  61. {
  62. get
  63. {
  64. return _minimum;
  65. }
  66. set
  67. {
  68. ValidateDouble(value, "Minimum");
  69. if (IsInitialized)
  70. {
  71. SetAndRaise(MinimumProperty, ref _minimum, value);
  72. Maximum = ValidateMaximum(Maximum);
  73. Value = ValidateValue(Value);
  74. }
  75. else
  76. {
  77. SetAndRaise(MinimumProperty, ref _minimum, value);
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// Gets or sets the maximum value.
  83. /// </summary>
  84. public double Maximum
  85. {
  86. get
  87. {
  88. return _maximum;
  89. }
  90. set
  91. {
  92. ValidateDouble(value, "Maximum");
  93. if (IsInitialized)
  94. {
  95. value = ValidateMaximum(value);
  96. SetAndRaise(MaximumProperty, ref _maximum, value);
  97. Value = ValidateValue(Value);
  98. }
  99. else
  100. {
  101. SetAndRaise(MaximumProperty, ref _maximum, value);
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// Gets or sets the current value.
  107. /// </summary>
  108. public double Value
  109. {
  110. get
  111. {
  112. return _value;
  113. }
  114. set
  115. {
  116. ValidateDouble(value, "Value");
  117. if (IsInitialized)
  118. {
  119. value = ValidateValue(value);
  120. SetAndRaise(ValueProperty, ref _value, value);
  121. }
  122. else
  123. {
  124. SetAndRaise(ValueProperty, ref _value, value);
  125. }
  126. }
  127. }
  128. public double SmallChange
  129. {
  130. get => GetValue(SmallChangeProperty);
  131. set => SetValue(SmallChangeProperty, value);
  132. }
  133. public double LargeChange
  134. {
  135. get => GetValue(LargeChangeProperty);
  136. set => SetValue(LargeChangeProperty, value);
  137. }
  138. protected override void OnInitialized()
  139. {
  140. base.OnInitialized();
  141. Maximum = ValidateMaximum(Maximum);
  142. Value = ValidateValue(Value);
  143. }
  144. /// <summary>
  145. /// Throws an exception if the double value is NaN or Inf.
  146. /// </summary>
  147. /// <param name="value">The value.</param>
  148. /// <param name="property">The name of the property being set.</param>
  149. private static void ValidateDouble(double value, string property)
  150. {
  151. if (double.IsInfinity(value) || double.IsNaN(value))
  152. {
  153. throw new ArgumentException($"{value} is not a valid value for {property}.");
  154. }
  155. }
  156. /// <summary>
  157. /// Validates/coerces the <see cref="Maximum"/> property.
  158. /// </summary>
  159. /// <param name="value">The value.</param>
  160. /// <returns>The coerced value.</returns>
  161. private double ValidateMaximum(double value)
  162. {
  163. return Math.Max(value, Minimum);
  164. }
  165. /// <summary>
  166. /// Validates/coerces the <see cref="Value"/> property.
  167. /// </summary>
  168. /// <param name="value">The value.</param>
  169. /// <returns>The coerced value.</returns>
  170. private double ValidateValue(double value)
  171. {
  172. return MathUtilities.Clamp(value, Minimum, Maximum);
  173. }
  174. }
  175. }