/
Slider.cs
174 lines (146 loc) · 5.57 KB
/
Slider.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
using System;
using System.ComponentModel;
using System.Windows.Input;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform;
namespace Xamarin.Forms
{
[RenderWith(typeof(_SliderRenderer))]
public class Slider : View, ISliderController, IElementConfiguration<Slider>
{
public static readonly BindableProperty MinimumProperty = BindableProperty.Create("Minimum", typeof(double), typeof(Slider), 0d, validateValue: (bindable, value) =>
{
var slider = (Slider)bindable;
return (double)value < slider.Maximum;
}, coerceValue: (bindable, value) =>
{
var slider = (Slider)bindable;
slider.Value = slider.Value.Clamp((double)value, slider.Maximum);
return value;
});
public static readonly BindableProperty MaximumProperty = BindableProperty.Create("Maximum", typeof(double), typeof(Slider), 1d, validateValue: (bindable, value) =>
{
var slider = (Slider)bindable;
return (double)value > slider.Minimum;
}, coerceValue: (bindable, value) =>
{
var slider = (Slider)bindable;
slider.Value = slider.Value.Clamp(slider.Minimum, (double)value);
return value;
});
public static readonly BindableProperty ValueProperty = BindableProperty.Create("Value", typeof(double), typeof(Slider), 0d, BindingMode.TwoWay, coerceValue: (bindable, value) =>
{
var slider = (Slider)bindable;
return ((double)value).Clamp(slider.Minimum, slider.Maximum);
}, propertyChanged: (bindable, oldValue, newValue) =>
{
var slider = (Slider)bindable;
slider.ValueChanged?.Invoke(slider, new ValueChangedEventArgs((double)oldValue, (double)newValue));
});
public static readonly BindableProperty MinimumTrackColorProperty = BindableProperty.Create(nameof(MinimumTrackColor), typeof(Color), typeof(Slider), Color.Default);
public static readonly BindableProperty MaximumTrackColorProperty = BindableProperty.Create(nameof(MaximumTrackColor), typeof(Color), typeof(Slider), Color.Default);
public static readonly BindableProperty ThumbColorProperty = BindableProperty.Create(nameof(ThumbColor), typeof(Color), typeof(Slider), Color.Default);
public static readonly BindableProperty ThumbImageSourceProperty = BindableProperty.Create(nameof(ThumbImageSource), typeof(ImageSource), typeof(Slider), default(ImageSource));
[Obsolete("ThumbImageProperty is obsolete as of 4.0.0. Please use ThumbImageSourceProperty instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty ThumbImageProperty = ThumbImageSourceProperty;
public static readonly BindableProperty DragStartedCommandProperty = BindableProperty.Create(nameof(DragStartedCommand), typeof(ICommand), typeof(Slider), default(ICommand));
public static readonly BindableProperty DragCompletedCommandProperty = BindableProperty.Create(nameof(DragCompletedCommand), typeof(ICommand), typeof(Slider), default(ICommand));
readonly Lazy<PlatformConfigurationRegistry<Slider>> _platformConfigurationRegistry;
public Slider()
{
_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<Slider>>(() => new PlatformConfigurationRegistry<Slider>(this));
}
public Slider(double min, double max, double val) : this()
{
if (min >= max)
throw new ArgumentOutOfRangeException("min");
if (max > Minimum)
{
Maximum = max;
Minimum = min;
}
else
{
Minimum = min;
Maximum = max;
}
Value = val.Clamp(min, max);
}
public Color MinimumTrackColor
{
get { return (Color)GetValue(MinimumTrackColorProperty); }
set { SetValue(MinimumTrackColorProperty, value); }
}
public Color MaximumTrackColor
{
get { return (Color)GetValue(MaximumTrackColorProperty); }
set { SetValue(MaximumTrackColorProperty, value); }
}
public Color ThumbColor
{
get { return (Color)GetValue(ThumbColorProperty); }
set { SetValue(ThumbColorProperty, value); }
}
public ImageSource ThumbImageSource
{
get { return (ImageSource)GetValue(ThumbImageSourceProperty); }
set { SetValue(ThumbImageSourceProperty, value); }
}
[Obsolete("ThumbImage is obsolete as of 4.0.0. Please use ThumbImageSource instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public FileImageSource ThumbImage
{
get { return GetValue(ThumbImageProperty) as FileImageSource; }
set { SetValue(ThumbImageProperty, value); }
}
public ICommand DragStartedCommand
{
get { return (ICommand)GetValue(DragStartedCommandProperty); }
set { SetValue(DragStartedCommandProperty, value); }
}
public ICommand DragCompletedCommand
{
get { return (ICommand)GetValue(DragCompletedCommandProperty); }
set { SetValue(DragCompletedCommandProperty, value); }
}
public double Maximum
{
get { return (double)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
public double Minimum
{
get { return (double)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
public double Value
{
get { return (double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public event EventHandler<ValueChangedEventArgs> ValueChanged;
public event EventHandler DragStarted;
public event EventHandler DragCompleted;
void ISliderController.SendDragStarted()
{
if (IsEnabled)
{
DragStartedCommand?.Execute(null);
DragStarted?.Invoke(this, null);
}
}
void ISliderController.SendDragCompleted()
{
if (IsEnabled)
{
DragCompletedCommand?.Execute(null);
DragCompleted?.Invoke(this, null);
}
}
public IPlatformElementConfiguration<T, Slider> On<T>() where T : IConfigPlatform
{
return _platformConfigurationRegistry.Value.On<T>();
}
}
}