summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Cells/SwitchCellRenderer.cs
blob: 6c5a038049b61f6daa08f2829aceceb8b3a67256 (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
using System.Collections.Generic;
using ElmSharp;

namespace Xamarin.Forms.Platform.Tizen
{
	public class SwitchCellRenderer : CellRenderer
	{
		protected SwitchCellRenderer(string style) : base(style)
		{
		}
		public SwitchCellRenderer() : this("end_icon")
		{
			MainPart = "elm.text";
			SwitchPart = "elm.swallow.end";
		}

		protected string MainPart { get; set; }
		protected string SwitchPart { get; set; }

		protected override Span OnGetText(Cell cell, string part)
		{
			if (part == MainPart)
			{
				return new Span()
				{
					Text = (cell as SwitchCell).Text
				};
			}
			return null;
		}

		protected override EvasObject OnGetContent(Cell cell, string part)
		{
			if (part == SwitchPart)
			{
				var switchCell = cell as SwitchCell;
				var checkbox = new Check(Forms.Context.MainWindow)
				{
					Style = "on&off",
					IsChecked = switchCell.On,
				};

				checkbox.StateChanged += (sender, e) =>
				{
					switchCell.On = e.NewState;
				};

				checkbox.SetAlignment(-1.0, -1.0);  // fill
				checkbox.SetWeight(1.0, 1.0);  // expand
				return checkbox;
			}
			return null;
		}

		protected override bool OnCellPropertyChanged(Cell cell, string property, Dictionary<string, EvasObject> realizedView)
		{
			if (property == SwitchCell.OnProperty.PropertyName && realizedView.ContainsKey(SwitchPart))
			{
				var checkbox = realizedView[SwitchPart] as Check;
				if (checkbox != null)
				{
					checkbox.IsChecked = (cell as SwitchCell).On;
				}
				return false;
			}
			else if (property == SwitchCell.TextProperty.PropertyName)
			{
				return true;
			}

			return base.OnCellPropertyChanged(cell, property, realizedView);
		}
	}
}