summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Cells/EntryCellRenderer.cs
blob: 43789dccc9f6b412cf95434b645ebdab0828bc66 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.ComponentModel;
using AppKit;
using CoreGraphics;
using Foundation;

namespace Xamarin.Forms.Platform.MacOS
{
	public class EntryCellRenderer : CellRenderer
	{
		static readonly Color s_defaultTextColor = Color.Black;

		public override NSView GetCell(Cell item, NSView reusableView, NSTableView tv)
		{
			NSTextField nsEntry = null;
			var tvc = reusableView as CellNSView;
			if (tvc == null)
				tvc = new CellNSView(NSTableViewCellStyle.Value2);
			else
			{
				tvc.Cell.PropertyChanged -= OnCellPropertyChanged;

				nsEntry = tvc.AccessoryView.Subviews[0] as NSTextField;
				if (nsEntry != null)
				{
					nsEntry.RemoveFromSuperview();
					nsEntry.Changed -= OnTextFieldTextChanged;
				}
			}

			SetRealCell(item, tvc);

			if (nsEntry == null)
				tvc.AccessoryView.AddSubview(nsEntry = new NSTextField());

			var entryCell = (EntryCell)item;

			tvc.Cell = item;
			tvc.Cell.PropertyChanged += OnCellPropertyChanged;
			nsEntry.Changed += OnTextFieldTextChanged;

			WireUpForceUpdateSizeRequested(item, tvc, tv);

			UpdateBackground(tvc, entryCell);
			UpdateLabel(tvc, entryCell);
			UpdateText(tvc, entryCell);
			UpdatePlaceholder(tvc, entryCell);
			UpdateLabelColor(tvc, entryCell);
			UpdateHorizontalTextAlignment(tvc, entryCell);
			UpdateIsEnabled(tvc, entryCell);

			return tvc;
		}

		internal override void UpdateBackgroundChild(Cell cell, NSColor backgroundColor)
		{
			var realCell = (CellNSView)GetRealCell(cell);

			var nsTextField = realCell.AccessoryView.Subviews[0] as NSTextField;
			if (nsTextField != null)
				nsTextField.BackgroundColor = backgroundColor;

			base.UpdateBackgroundChild(cell, backgroundColor);
		}

		static void OnCellPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			var entryCell = (EntryCell)sender;
			var realCell = (CellNSView)GetRealCell(entryCell);

			if (e.PropertyName == EntryCell.LabelProperty.PropertyName)
				UpdateLabel(realCell, entryCell);
			else if (e.PropertyName == EntryCell.TextProperty.PropertyName)
				UpdateText(realCell, entryCell);
			else if (e.PropertyName == EntryCell.PlaceholderProperty.PropertyName)
				UpdatePlaceholder(realCell, entryCell);
			else if (e.PropertyName == EntryCell.LabelColorProperty.PropertyName)
				UpdateLabelColor(realCell, entryCell);
			else if (e.PropertyName == EntryCell.HorizontalTextAlignmentProperty.PropertyName)
				UpdateHorizontalTextAlignment(realCell, entryCell);
			else if (e.PropertyName == Cell.IsEnabledProperty.PropertyName)
				UpdateIsEnabled(realCell, entryCell);
		}

		static void OnTextFieldTextChanged(object sender, EventArgs eventArgs)
		{
			var notification = (NSNotification)sender;
			var view = (NSView)notification.Object;
			var field = (NSTextField)view;

			CellNSView realCell = null;
			while (view.Superview != null && realCell == null)
			{
				view = view.Superview;
				realCell = view as CellNSView;
			}

			if (realCell != null)
				((EntryCell)realCell.Cell).Text = field.StringValue;
		}

		static void UpdateHorizontalTextAlignment(CellNSView cell, EntryCell entryCell)
		{
			var nsTextField = cell.AccessoryView.Subviews[0] as NSTextField;
			if (nsTextField != null)
				nsTextField.Alignment = entryCell.HorizontalTextAlignment.ToNativeTextAlignment();
		}

		static void UpdateIsEnabled(CellNSView cell, EntryCell entryCell)
		{
			cell.TextLabel.Enabled = entryCell.IsEnabled;
			var nsTextField = cell.AccessoryView.Subviews[0] as NSTextField;
			if (nsTextField != null)
				nsTextField.Enabled = entryCell.IsEnabled;
		}

		static void UpdateLabel(CellNSView cell, EntryCell entryCell)
		{
			cell.TextLabel.StringValue = entryCell.Label ?? "";
		}

		static void UpdateLabelColor(CellNSView cell, EntryCell entryCell)
		{
			cell.TextLabel.TextColor = entryCell.LabelColor.ToNSColor(s_defaultTextColor);
		}

		static void UpdatePlaceholder(CellNSView cell, EntryCell entryCell)
		{
			var nsTextField = cell.AccessoryView.Subviews[0] as NSTextField;
			if (nsTextField != null)
				nsTextField.PlaceholderString = entryCell.Placeholder ?? "";
		}

		static void UpdateText(CellNSView cell, EntryCell entryCell)
		{
			var nsTextField = cell.AccessoryView.Subviews[0] as NSTextField;
			if (nsTextField != null && nsTextField.StringValue == entryCell.Text)
				return;

			if (nsTextField != null)
				nsTextField.StringValue = entryCell.Text ?? "";
		}
	}
}