/
MaterialSliderRenderer.cs
151 lines (123 loc) · 4.17 KB
/
MaterialSliderRenderer.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
using System;
using System.ComponentModel;
using CoreGraphics;
using MaterialComponents;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using MSlider = MaterialComponents.Slider;
namespace Xamarin.Forms.Material.iOS
{
public class MaterialSliderRenderer : ViewRenderer<Slider, MSlider>
{
SemanticColorScheme _defaultColorScheme;
SemanticColorScheme _colorScheme;
protected override void Dispose(bool disposing)
{
if (Control != null)
{
Control.Delegate = null;
Control.ValueChanged -= OnControlValueChanged;
}
base.Dispose(disposing);
}
protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
{
_colorScheme?.Dispose();
_colorScheme = CreateColorScheme();
base.OnElementChanged(e);
if (e.NewElement != null)
{
if (Control == null)
{
_defaultColorScheme = CreateColorScheme();
SetNativeControl(CreateNativeControl());
Control.Continuous = true;
Control.ValueChanged += OnControlValueChanged;
}
UpdateMaximum();
UpdateMinimum();
UpdateValue();
ApplyTheme();
}
}
protected virtual void ApplyTheme()
{
#pragma warning disable CS0618 // Type or member is obsolete
SliderColorThemer.ApplySemanticColorScheme(CreateColorScheme(), Control);
#pragma warning restore CS0618 // Type or member is obsolete
// TODO: This is not very safe as Google may change the way it
// colors the control.
// Right now, this must always come after the theme as this
// is an unsupported operation.
OverrideThemeColors();
}
public override CGSize SizeThatFits(CGSize size)
{
var result = base.SizeThatFits(size);
var height = result.Height;
if (height == 0)
height = nfloat.IsInfinity(size.Height) ? 12 : size.Height;
return new CGSize(12, height);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var updatedTheme = false;
if (e.PropertyName == Slider.MaximumProperty.PropertyName)
{
UpdateMaximum();
}
else if (e.PropertyName == Slider.MinimumProperty.PropertyName)
{
UpdateMinimum();
}
else if (e.PropertyName == Slider.ValueProperty.PropertyName)
{
UpdateValue();
}
else if (e.PropertyName == Slider.MinimumTrackColorProperty.PropertyName || e.PropertyName == Slider.MaximumTrackColorProperty.PropertyName || e.PropertyName == Slider.ThumbColorProperty.PropertyName)
{
updatedTheme = true;
}
if (updatedTheme)
ApplyTheme();
}
protected override MSlider CreateNativeControl() => new MSlider { StatefulApiEnabled = true };
protected virtual SemanticColorScheme CreateColorScheme() => MaterialColors.Light.CreateColorScheme();
void UpdateMaximum() => Control.MaximumValue = (nfloat)Element.Maximum;
void UpdateMinimum() => Control.MinimumValue = (nfloat)Element.Minimum;
void OnControlValueChanged(object sender, EventArgs eventArgs) => Element.SetValueFromRenderer(Slider.ValueProperty, Control.Value);
void UpdateValue()
{
nfloat value = (nfloat)Element.Value;
if (value != Control.Value)
Control.Value = value;
}
void OverrideThemeColors()
{
Color minColor = Element.MinimumTrackColor;
Color maxColor = Element.MaximumTrackColor;
Color thumbColor = Element.ThumbColor;
// jump out as we want the defaults
if (minColor.IsDefault && maxColor.IsDefault && thumbColor.IsDefault)
return;
if (!minColor.IsDefault)
{
Control.SetTrackFillColor(minColor.ToUIColor(), UIControlState.Normal);
// if no max color was specified, then use a shade of the min
if (maxColor.IsDefault)
{
Control.GetTrackBackgroundColor(UIControlState.Normal).GetRGBA(out _, out _, out _, out var baseAlpha);
Control.SetTrackBackgroundColor(minColor.MultiplyAlpha(baseAlpha).ToUIColor(), UIControlState.Normal);
}
if (thumbColor.IsDefault)
Control.SetThumbColor(minColor.ToUIColor(), UIControlState.Normal);
}
if (!maxColor.IsDefault)
Control.SetTrackBackgroundColor(maxColor.ToUIColor(), UIControlState.Normal);
if (!thumbColor.IsDefault)
Control.SetThumbColor(thumbColor.ToUIColor(), UIControlState.Normal);
}
}
}