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
91 lines (75 loc) · 2.06 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
using System;
using System.ComponentModel;
using System.Drawing;
using UIKit;
namespace Xamarin.Forms.Platform.iOS
{
public class SwitchRenderer : ViewRenderer<Switch, UISwitch>
{
UIColor _defaultOnColor;
UIColor _defaultThumbColor;
[Internals.Preserve(Conditional = true)]
public SwitchRenderer()
{
}
protected override void Dispose(bool disposing)
{
if (disposing)
Control.ValueChanged -= OnControlValueChanged;
base.Dispose(disposing);
}
protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
{
if (e.OldElement != null)
e.OldElement.Toggled -= OnElementToggled;
if (e.NewElement != null)
{
if (Control == null)
{
SetNativeControl(new UISwitch(RectangleF.Empty));
Control.ValueChanged += OnControlValueChanged;
}
_defaultOnColor = UISwitch.Appearance.OnTintColor;
_defaultThumbColor = UISwitch.Appearance.ThumbTintColor;
Control.On = Element.IsToggled;
e.NewElement.Toggled += OnElementToggled;
UpdateOnColor();
UpdateThumbColor();
}
base.OnElementChanged(e);
}
void UpdateOnColor()
{
if (Element != null)
{
if (Element.OnColor == Color.Default)
Control.OnTintColor = _defaultOnColor;
else
Control.OnTintColor = Element.OnColor.ToUIColor();
}
}
void UpdateThumbColor()
{
if (Element == null)
return;
Color thumbColor = Element.ThumbColor;
Control.ThumbTintColor = thumbColor.IsDefault ? _defaultThumbColor : thumbColor.ToUIColor();
}
void OnControlValueChanged(object sender, EventArgs e)
{
((IElementController)Element).SetValueFromRenderer(Switch.IsToggledProperty, Control.On);
}
void OnElementToggled(object sender, EventArgs e)
{
Control.SetState(Element.IsToggled, true);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == Switch.OnColorProperty.PropertyName)
UpdateOnColor();
if (e.PropertyName == Switch.ThumbColorProperty.PropertyName)
UpdateThumbColor();
}
}
}