-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathRangeBase.cs
200 lines (175 loc) · 6.55 KB
/
RangeBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using Avalonia.Data;
using Avalonia.Interactivity;
using Avalonia.Utilities;
namespace Avalonia.Controls.Primitives
{
/// <summary>
/// Base class for controls that display a value within a range.
/// </summary>
public abstract class RangeBase : TemplatedControl
{
private bool _isDataContextChanging;
/// <summary>
/// Defines the <see cref="Minimum"/> property.
/// </summary>
public static readonly StyledProperty<double> MinimumProperty =
AvaloniaProperty.Register<RangeBase, double>(nameof(Minimum), coerce: CoerceMinimum);
/// <summary>
/// Defines the <see cref="Maximum"/> property.
/// </summary>
public static readonly StyledProperty<double> MaximumProperty =
AvaloniaProperty.Register<RangeBase, double>(nameof(Maximum), 100, coerce: CoerceMaximum);
/// <summary>
/// Defines the <see cref="Value"/> property.
/// </summary>
public static readonly StyledProperty<double> ValueProperty =
AvaloniaProperty.Register<RangeBase, double>(nameof(Value),
defaultBindingMode: BindingMode.TwoWay,
coerce: CoerceValue);
/// <summary>
/// Defines the <see cref="SmallChange"/> property.
/// </summary>
public static readonly StyledProperty<double> SmallChangeProperty =
AvaloniaProperty.Register<RangeBase, double>(nameof(SmallChange), 1);
/// <summary>
/// Defines the <see cref="LargeChange"/> property.
/// </summary>
public static readonly StyledProperty<double> LargeChangeProperty =
AvaloniaProperty.Register<RangeBase, double>(nameof(LargeChange), 10);
/// <summary>
/// Defines the <see cref="ValueChanged"/> event.
/// </summary>
public static readonly RoutedEvent<RangeBaseValueChangedEventArgs> ValueChangedEvent =
RoutedEvent.Register<RangeBase, RangeBaseValueChangedEventArgs>(
nameof(ValueChanged), RoutingStrategies.Bubble);
/// <summary>
/// Occurs when the <see cref="Value"/> property changes.
/// </summary>
public event EventHandler<RangeBaseValueChangedEventArgs>? ValueChanged
{
add => AddHandler(ValueChangedEvent, value);
remove => RemoveHandler(ValueChangedEvent, value);
}
/// <summary>
/// Gets or sets the minimum possible value.
/// </summary>
public double Minimum
{
get => GetValue(MinimumProperty);
set => SetValue(MinimumProperty, value);
}
private static double CoerceMinimum(AvaloniaObject sender, double value)
{
return ValidateDouble(value) ? value : sender.GetValue(MinimumProperty);
}
private void OnMinimumChanged()
{
if (IsInitialized && !_isDataContextChanging)
{
CoerceValue(MaximumProperty);
CoerceValue(ValueProperty);
}
}
/// <summary>
/// Gets or sets the maximum possible value.
/// </summary>
public double Maximum
{
get => GetValue(MaximumProperty);
set => SetValue(MaximumProperty, value);
}
private static double CoerceMaximum(AvaloniaObject sender, double value)
{
return ValidateDouble(value)
? Math.Max(value, sender.GetValue(MinimumProperty))
: sender.GetValue(MaximumProperty);
}
private void OnMaximumChanged()
{
if (IsInitialized && !_isDataContextChanging)
{
CoerceValue(MinimumProperty);
CoerceValue(ValueProperty);
}
}
/// <summary>
/// Gets or sets the current value.
/// </summary>
public double Value
{
get => GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}
private static double CoerceValue(AvaloniaObject sender, double value)
{
return ValidateDouble(value)
? MathUtilities.Clamp(value, sender.GetValue(MinimumProperty), sender.GetValue(MaximumProperty))
: sender.GetValue(ValueProperty);
}
/// <summary>
/// Gets or sets the small increment value added or subtracted from the <see cref="Value"/>.
/// </summary>
public double SmallChange
{
get => GetValue(SmallChangeProperty);
set => SetValue(SmallChangeProperty, value);
}
/// <summary>
/// Gets or sets the large increment value added or subtracted from the <see cref="Value"/>.
/// </summary>
public double LargeChange
{
get => GetValue(LargeChangeProperty);
set => SetValue(LargeChangeProperty, value);
}
/// <inheritdoc/>
protected override void OnInitialized()
{
base.OnInitialized();
CoerceValue(MaximumProperty);
CoerceValue(ValueProperty);
}
/// <inheritdoc/>
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == MinimumProperty)
{
OnMinimumChanged();
}
else if (change.Property == MaximumProperty)
{
OnMaximumChanged();
}
else if (change.Property == ValueProperty)
{
var valueChangedEventArgs = new RangeBaseValueChangedEventArgs(
change.GetOldValue<double>(),
change.GetNewValue<double>(),
ValueChangedEvent);
RaiseEvent(valueChangedEventArgs);
}
}
/// <inheritdoc />
protected override void OnDataContextBeginUpdate()
{
_isDataContextChanging = true;
base.OnDataContextBeginUpdate();
}
/// <inheritdoc />
protected override void OnDataContextEndUpdate()
{
base.OnDataContextEndUpdate();
_isDataContextChanging = false;
}
/// <summary>
/// Checks if the double value is not infinity nor NaN.
/// </summary>
/// <param name="value">The value.</param>
private static bool ValidateDouble(double value)
{
return !double.IsInfinity(value) && !double.IsNaN(value);
}
}
}