-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
MaterialProgressBarRenderer.cs
174 lines (145 loc) · 4.08 KB
/
MaterialProgressBarRenderer.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 CoreGraphics;
using MaterialComponents;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using MProgressView = MaterialComponents.ProgressView;
namespace Xamarin.Forms.Material.iOS
{
public class MaterialProgressBarRenderer : ViewRenderer<ProgressBar, MProgressView>
{
SemanticColorScheme _defaultColorScheme;
SemanticColorScheme _colorScheme;
ContainerScheme _containerScheme;
protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
{
_colorScheme?.Dispose();
_colorScheme = CreateSemanticColorScheme();
_containerScheme?.Dispose();
_containerScheme = new ContainerScheme();
base.OnElementChanged(e);
if (e.NewElement != null)
{
if (Control == null)
{
_defaultColorScheme = CreateSemanticColorScheme();
SetNativeControl(CreateNativeControl());
}
UpdateProgressColor();
UpdateProgress();
ApplyTheme();
}
}
[Obsolete]
[EditorBrowsable(EditorBrowsableState.Never)]
protected virtual BasicColorScheme CreateColorScheme()
{
return null;
}
protected virtual SemanticColorScheme CreateSemanticColorScheme()
{
return MaterialColors.Light.CreateColorScheme();
}
protected virtual void ApplyTheme()
{
_containerScheme.ColorScheme = _colorScheme;
Control.ApplyTheme(_containerScheme);
Control.TrackTintColor = _containerScheme.ColorScheme.BackgroundColor;
}
protected override MProgressView CreateNativeControl() => new MProgressView();
public override CGSize SizeThatFits(CGSize size)
{
var result = base.SizeThatFits(size);
var height = result.Height;
if (height == 0)
{
if (nfloat.IsInfinity(size.Height))
height = 4;
else
height = size.Height;
}
return new CGSize(10, height);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var updatedTheme = false;
if (e.PropertyName == ProgressBar.ProgressColorProperty.PropertyName)
{
UpdateProgressColor();
updatedTheme = true;
}
else if (e.PropertyName == ProgressBar.ProgressProperty.PropertyName)
{
UpdateProgress();
}
if (updatedTheme)
ApplyTheme();
}
protected override void SetBackgroundColor(Color color)
{
// Do not call base to avoid the actual background view changing color.
//base.SetBackgroundColor(color);
if (Control == null)
return;
UpdateAllColors();
ApplyTheme();
}
void UpdateProgressColor() => UpdateAllColors();
void UpdateAllColors()
{
Color progressColor = Element.ProgressColor;
Color backgroundColor = Element.BackgroundColor;
if (progressColor.IsDefault)
{
if(backgroundColor.IsDefault)
{
// reset everything to defaults
_colorScheme = CreateSemanticColorScheme();
var progress = _colorScheme.PrimaryColor;
progress.GetRGBA(out _, out _, out _, out var alpha);
_colorScheme.BackgroundColor = progress.ColorWithAlpha(alpha * MaterialColors.SliderTrackAlpha);
}
else
{
// handle the case where only the background is set
var background = backgroundColor.ToUIColor();
_colorScheme = new SemanticColorScheme() { BackgroundColor = background };
}
}
else if (!progressColor.IsDefault)
{
if (backgroundColor.IsDefault)
{
// handle the case where only the progress is set
var progress = progressColor.ToUIColor();
progress.GetRGBA(out _, out _, out _, out var alpha);
_colorScheme = new SemanticColorScheme()
{
PrimaryColor = progress,
BackgroundColor = progress.ColorWithAlpha(alpha * MaterialColors.SliderTrackAlpha)
};
}
else
{
// handle the case where both are set
var background = backgroundColor.ToUIColor();
var progress = progressColor.ToUIColor();
_colorScheme = new SemanticColorScheme()
{
PrimaryColor = progress,
BackgroundColor = background
};
}
}
}
void UpdateProgress()
{
if (Control == null)
return;
Control.Progress = (float)Element.Progress;
}
}
}