summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/SwitchRenderer.cs
blob: c67a69a28a13db045d14888cbdfae86d22c4a8b1 (plain)
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
using System;
using System.ComponentModel;
using ElmSharp;
using Xamarin.Forms.PlatformConfiguration.TizenSpecific;
using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific.Switch;

namespace Xamarin.Forms.Platform.Tizen
{
	public class SwitchRenderer : ViewRenderer<Switch, Check>
	{
		public SwitchRenderer()
		{
			RegisterPropertyHandler(Switch.IsToggledProperty, HandleToggled);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
		{
			if (Control == null)
			{
				var _switch = new Check(Forms.Context.MainWindow)
				{
					PropagateEvents = false,
				};
				SetNativeControl(_switch);
			}

			if (e.OldElement != null)
			{
				Control.StateChanged -= CheckChangedHandler;
			}

			if (e.NewElement != null)
			{
				UpdateStyle();
				Control.StateChanged += CheckChangedHandler;
			}

			base.OnElementChanged(e);
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == Specific.SwitchStyleProperty.PropertyName)
			{
				UpdateStyle();
			}
			base.OnElementPropertyChanged(sender, e);
		}

		void CheckChangedHandler(object sender, EventArgs e)
		{
			Element.SetValue(Switch.IsToggledProperty, Control.IsChecked);
		}

		void HandleToggled()
		{
			Control.IsChecked = Element.IsToggled;
		}

		void UpdateStyle()
		{
			switch (Specific.GetSwitchStyle(Element))
			{
				case SwitchStyle.CheckBox:
					Control.Style = "default";
					break;
				case SwitchStyle.Favorite:
					Control.Style = "favorite";
					break;
				default:
					Control.Style = "toggle";
					break;
			}
			((IVisualElementController)Element).NativeSizeChanged();
		}
	}
}