summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Cells/EntryCellRenderer.cs
blob: 1088d5c91a1d447f77b6a485e6b01af1a32aab59 (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
102
103
104
105
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 double s_defaultHeight = 65;
		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 pixelHeight = Forms.ConvertToScaledPixel(entryCell.RenderHeight);
				pixelHeight = pixelHeight > 0 ? pixelHeight : Forms.ConvertToPixel(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;
				layout.BindingContext = entryCell;
				layout.MinimumHeightRequest = Forms.ConvertToScaledDP(pixelHeight);

				var renderer = Platform.GetOrCreateRenderer(layout);
				(renderer as LayoutRenderer)?.RegisterOnLayoutUpdated();

				var nativeView = renderer.NativeView;
				nativeView.MinimumHeight = pixelHeight;
				_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 = Forms.ConvertToScaledPixel(cell.RenderHeight);
			height = height > 0 ? height : Forms.ConvertToPixel(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;
			}
		}
	}
}