This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathSwitchRenderer.cs
167 lines (142 loc) · 3.95 KB
/
SwitchRenderer.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
using System;
using System.ComponentModel;
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Widget;
using ASwitch = Android.Widget.Switch;
namespace Xamarin.Forms.Platform.Android
{
public class SwitchRenderer : ViewRenderer<Switch, ASwitch>, CompoundButton.IOnCheckedChangeListener
{
Drawable _defaultTrackDrawable;
bool _changedThumbColor;
public SwitchRenderer(Context context) : base(context)
{
AutoPackage = false;
}
[Obsolete("This constructor is obsolete as of version 2.5. Please use SwitchRenderer(Context) instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public SwitchRenderer()
{
AutoPackage = false;
}
void CompoundButton.IOnCheckedChangeListener.OnCheckedChanged(CompoundButton buttonView, bool isChecked)
{
((IViewController)Element).SetValueFromRenderer(Switch.IsToggledProperty, isChecked);
UpdateOnColor();
}
public override SizeRequest GetDesiredSize(int widthConstraint, int heightConstraint)
{
SizeRequest sizeConstraint = base.GetDesiredSize(widthConstraint, heightConstraint);
if (sizeConstraint.Request.Width == 0)
{
int width = widthConstraint;
if (widthConstraint <= 0)
width = (int)Context.GetThemeAttributeDp(global::Android.Resource.Attribute.SwitchMinWidth);
sizeConstraint = new SizeRequest(new Size(width, sizeConstraint.Request.Height), new Size(width, sizeConstraint.Minimum.Height));
}
return sizeConstraint;
}
protected override void Dispose(bool disposing)
{
if (disposing && Control != null)
{
if (Element != null)
Element.Toggled -= HandleToggled;
Control.SetOnCheckedChangeListener(null);
_defaultTrackDrawable?.Dispose();
_defaultTrackDrawable = null;
}
base.Dispose(disposing);
}
protected override ASwitch CreateNativeControl()
{
return new ASwitch(Context);
}
protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
e.OldElement.Toggled -= HandleToggled;
if (e.NewElement != null)
{
if (Control == null)
{
var aswitch = CreateNativeControl();
aswitch.SetOnCheckedChangeListener(this);
SetNativeControl(aswitch);
_defaultTrackDrawable = Control.TrackDrawable;
}
else
{
UpdateEnabled(); // Normally set by SetNativeControl, but not when the Control is reused.
}
e.NewElement.Toggled += HandleToggled;
Control.Checked = e.NewElement.IsToggled;
UpdateOnColor();
UpdateThumbColor();
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == Switch.OnColorProperty.PropertyName)
UpdateOnColor();
else if (e.PropertyName == Slider.ThumbColorProperty.PropertyName)
UpdateThumbColor();
}
void UpdateOnColor()
{
if (Element != null)
{
if (Control.Checked)
{
if (Element.OnColor == Color.Default)
{
Control.TrackDrawable = _defaultTrackDrawable;
}
else
{
if (Forms.SdkInt >= BuildVersionCodes.JellyBean)
{
Control.TrackDrawable?.SetColorFilter(Element.OnColor.ToAndroid(), FilterMode.SrcAtop);
}
}
}
else
{
Control.TrackDrawable?.ClearColorFilter();
}
}
}
void UpdateThumbColor()
{
if (Element == null)
return;
if (Element.ThumbColor != Color.Default)
{
Control.ThumbDrawable.SetColorFilter(Element.ThumbColor, FilterMode.SrcAtop);
_changedThumbColor = true;
}
else
{
if (_changedThumbColor)
{
Control.ThumbDrawable?.ClearColorFilter();
_changedThumbColor = false;
}
}
Control.ThumbDrawable.SetColorFilter(Element.ThumbColor, FilterMode.SrcAtop);
}
void HandleToggled(object sender, EventArgs e)
{
Control.Checked = Element.IsToggled;
}
void UpdateEnabled()
{
Control.Enabled = Element.IsEnabled;
}
}
}