summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Cells/EntryCellRenderer.cs
blob: b74a6f96e34b5873656e929e2089de8febddabe8 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Globalization;
using ElmSharp;
using EColor = ElmSharp.Color;

namespace Xamarin.Forms.Platform.Tizen
{
	public class EntryCellRenderer : ViewCellRenderer
	{
		static readonly int s_defaultHeight = 120;
		static readonly EColor s_defaultLabelColor = EColor.Black;

		readonly Dictionary<EvasObject, VisualElement> _cacheCandidate = new Dictionary<EvasObject, VisualElement>();

		public EntryCellRenderer()
		{
		}

		protected override EvasObject OnGetContent(Cell cell, string part)
		{
			if (part == MainContentPart)
			{
				var entryCell = cell as EntryCell;
				int height = (int)entryCell.RenderHeight;
				height = height > 0 ? height : s_defaultHeight;

				var label = new Label()
				{
					HorizontalOptions = LayoutOptions.Start,
					VerticalOptions = LayoutOptions.Center,
					VerticalTextAlignment = TextAlignment.Center,
					FontSize = -1
				};
				label.SetBinding(Label.TextProperty, new Binding(EntryCell.LabelProperty.PropertyName));
				label.SetBinding(Label.TextColorProperty, new Binding(EntryCell.LabelColorProperty.PropertyName, converter: new DefaultColorConverter()));

				var entry = new Entry()
				{
					HorizontalOptions = LayoutOptions.FillAndExpand,
					VerticalOptions = LayoutOptions.Center,
					FontSize = -1,
				};
				entry.SetBinding(Entry.TextProperty, new Binding(EntryCell.TextProperty.PropertyName, BindingMode.TwoWay));
				entry.SetBinding(Entry.PlaceholderProperty, new Binding(EntryCell.PlaceholderProperty.PropertyName));
				entry.SetBinding(InputView.KeyboardProperty, new Binding(EntryCell.KeyboardProperty.PropertyName));
				entry.SetBinding(Entry.HorizontalTextAlignmentProperty, new Binding(EntryCell.HorizontalTextAlignmentProperty.PropertyName));

				var layout = new StackLayout()
				{
					Orientation = StackOrientation.Horizontal,
					Children = {
						label,
						entry
					}
				};
				layout.Parent = cell.Parent;
				layout.BindingContext = entryCell;
				layout.MinimumHeightRequest = height;

				var nativeView = Platform.GetOrCreateRenderer(layout).NativeView;
				nativeView.MinimumHeight = height;
				_cacheCandidate[nativeView] = layout;
				nativeView.Deleted += (sender, e) =>
				{
					_cacheCandidate.Remove(sender as EvasObject);
				};
				return nativeView;
			}
			return null;
		}

		protected override EvasObject OnReusableContent(Cell cell, string part, EvasObject old)
		{
			if (!_cacheCandidate.ContainsKey(old))
			{
				return null;
			}

			var layout = _cacheCandidate[old];
			layout.BindingContext = cell;
			int height = (int)cell.RenderHeight;
			height = height > 0 ? height : s_defaultHeight;
			old.MinimumHeight = height;
			return old;
		}

		class DefaultColorConverter : IValueConverter
		{
			public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
			{
				return ((Color)value).IsDefault ? s_defaultLabelColor : value;
			}

			public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
			{
				return value;
			}
		}
	}
}