forked from xamarin/Xamarin.Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSliderRenderer.cs
184 lines (157 loc) · 4.93 KB
/
SliderRenderer.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
using System;
using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
namespace Xamarin.Forms.Platform.UWP
{
public class SliderRenderer : ViewRenderer<Slider, FormsSlider>
{
Brush defaultforegroundcolor;
Brush defaultbackgroundcolor;
Brush _defaultThumbColor;
protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
if (Control == null)
{
var slider = new FormsSlider();
SetNativeControl(slider);
slider.Ready += (sender, args) =>
{
UpdateThumbColor();
UpdateThumbImage();
};
Control.Minimum = e.NewElement.Minimum;
Control.Maximum = e.NewElement.Maximum;
Control.Value = e.NewElement.Value;
slider.ValueChanged += OnNativeValueChanged;
defaultforegroundcolor = slider.Foreground;
defaultbackgroundcolor = slider.Background;
// Even when using Center/CenterAndExpand, a Slider has an oddity where it looks
// off-center in its layout by a smidge. The default templates are slightly different
// between 8.1/UWP; the 8.1 rows are 17/Auto/32 and UWP are 18/Auto/18. The value of
// the hardcoded 8.1 rows adds up to 49 (when halved is 24.5) and UWP are 36 (18). Using
// a difference of about 6 pixels to correct this oddity seems to make them both center
// more correctly.
//
// The VerticalAlignment needs to be set as well since a control would not actually be
// centered if a larger HeightRequest is set.
if (Element.VerticalOptions.Alignment == LayoutAlignment.Center && Control.Orientation == Windows.UI.Xaml.Controls.Orientation.Horizontal)
{
Control.VerticalAlignment = VerticalAlignment.Center;
slider.Margin = new Windows.UI.Xaml.Thickness(0, 7, 0, 0);
}
}
double stepping = Math.Min((e.NewElement.Maximum - e.NewElement.Minimum) / 10, 1);
Control.StepFrequency = stepping;
Control.SmallChange = stepping;
UpdateFlowDirection();
UpdateSliderColors();
}
}
void UpdateSliderColors()
{
UpdateMinimumTrackColor();
UpdateMaximumTrackColor();
}
void UpdateMinimumTrackColor()
{
if (Control != null)
{
if (Element.MinimumTrackColor == Color.Default)
Control.Foreground = defaultforegroundcolor;
else
Control.Foreground = Element.MinimumTrackColor.ToBrush();
}
}
void UpdateMaximumTrackColor()
{
if (Control != null)
{
if (Element.MaximumTrackColor == Color.Default)
Control.Background = defaultbackgroundcolor;
else
Control.Background = Element.MaximumTrackColor.ToBrush();
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == Slider.MinimumProperty.PropertyName)
Control.Minimum = Element.Minimum;
else if (e.PropertyName == Slider.MaximumProperty.PropertyName)
Control.Maximum = Element.Maximum;
else if (e.PropertyName == Slider.ValueProperty.PropertyName)
{
if (Control.Value != Element.Value)
Control.Value = Element.Value;
}
else if (e.PropertyName == VisualElement.FlowDirectionProperty.PropertyName)
UpdateFlowDirection();
else if (e.PropertyName == Slider.MinimumTrackColorProperty.PropertyName)
UpdateMinimumTrackColor();
else if (e.PropertyName == Slider.MaximumTrackColorProperty.PropertyName)
UpdateMaximumTrackColor();
else if (e.PropertyName == Slider.ThumbColorProperty.PropertyName)
UpdateThumbColor();
else if (e.PropertyName == Slider.ThumbImageProperty.PropertyName)
UpdateThumbImage();
}
void UpdateThumbColor()
{
if (Element == null)
{
return;
}
var thumb = Control?.Thumb;
if (thumb == null)
{
return;
}
BrushHelpers.UpdateColor(Element.ThumbColor, ref _defaultThumbColor,
() => thumb.Background, brush => thumb.Background = brush);
}
void UpdateThumbImage()
{
if (Element == null || Control == null)
{
return;
}
var thumbImage = Element.ThumbImage;
if (thumbImage == null)
{
Control.ThumbImage = null;
return;
}
Control.ThumbImage = new BitmapImage(new Uri($"ms-appx:///{thumbImage.File}"));
}
protected override void UpdateBackgroundColor()
{
if (Control != null)
{
Color backgroundColor = Element.BackgroundColor;
if (!backgroundColor.IsDefault)
{
Control.Background = backgroundColor.ToBrush();
}
else
{
Control.ClearValue(Windows.UI.Xaml.Controls.Control.BackgroundProperty);
}
}
}
void UpdateFlowDirection()
{
Control.UpdateFlowDirection(Element);
}
protected override bool PreventGestureBubbling { get; set; } = true;
void OnNativeValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
((IElementController)Element).SetValueFromRenderer(Slider.ValueProperty, e.NewValue);
}
}
}