summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Renderers/PickerRenderer.cs
blob: ac07bfd214fd39c4caa41a066cc19559490366db (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
145
146
147
148
149
150
151
152
153
154
´╗┐using System;
using AppKit;
using System.ComponentModel;
using Foundation;

namespace Xamarin.Forms.Platform.MacOS
{
	public class PickerRenderer : ViewRenderer<Picker, NSComboBox>
	{
		bool _disposed;
		NSColor _defaultTextColor;
		NSColor _defaultBackgroundColor;

		IElementController ElementController => Element;

		protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
		{
			if (e.NewElement != null)
			{
				if (Control == null)
					SetNativeControl(new NSComboBox { Editable = false });

				_defaultTextColor = Control.TextColor;
				_defaultBackgroundColor = Control.BackgroundColor;

				Control.UsesDataSource = true;
				Control.DataSource = new ComboDataSource(this);

				Control.SelectionChanged += ComboBoxSelectionChanged;

				UpdatePicker();
				UpdateTextColor();
			}

			base.OnElementChanged(e);
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);
			if (e.PropertyName == Picker.TitleProperty.PropertyName)
				UpdatePicker();
			if (e.PropertyName == Picker.SelectedIndexProperty.PropertyName)
				UpdatePicker();
			if (e.PropertyName == Picker.TextColorProperty.PropertyName ||
				e.PropertyName == VisualElement.IsEnabledProperty.PropertyName)
				UpdateTextColor();
		}

		protected override void SetBackgroundColor(Color color)
		{
			base.SetBackgroundColor(color);

			if (Control == null)
				return;

			Control.BackgroundColor = color == Color.Default ? _defaultBackgroundColor : color.ToNSColor();
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (!_disposed)
				{
					_disposed = true;
					if (Element != null)
					{
						//TODO: Implement ObservableList picker source change 
						//((ObservableList<string>)Element.Items).CollectionChanged -= RowsCollectionChanged;
					}

					if (Control != null)
						Control.SelectionChanged -= ComboBoxSelectionChanged;
				}
			}
			base.Dispose(disposing);
		}

		void ComboBoxSelectionChanged(object sender, EventArgs e)
		{
			ElementController?.SetValueFromRenderer(Picker.SelectedIndexProperty, (int)Control.SelectedIndex);
		}

		void OnEnded(object sender, EventArgs eventArgs)
		{
			ElementController?.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
		}

		void OnStarted(object sender, EventArgs eventArgs)
		{
			ElementController?.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true);
		}

		void RowsCollectionChanged(object sender, EventArgs e)
		{
			UpdatePicker();
		}

		void UpdatePicker()
		{
			if (Control == null || Element == null)
				return;

			var selectedIndex = Element.SelectedIndex;
			var items = Element.Items;
			Control.PlaceholderString = Element.Title ?? string.Empty;
			Control.ReloadData();
			if (items == null || items.Count == 0 || selectedIndex < 0)
				return;

			Control.SelectItem(selectedIndex);
		}

		void UpdateTextColor()
		{
			if (Control == null || Element == null)
				return;

			var textColor = Element.TextColor;

			if (textColor.IsDefault || !Element.IsEnabled)
				Control.TextColor = _defaultTextColor;
			else
				Control.TextColor = textColor.ToNSColor();
		}

		class ComboDataSource : NSComboBoxDataSource
		{
			readonly PickerRenderer _renderer;

			public ComboDataSource(PickerRenderer model)
			{
				_renderer = model;
			}

			public override nint ItemCount(NSComboBox comboBox)
			{
				return _renderer.Element.Items?.Count ?? 0;
			}

			public override NSObject ObjectValueForItem(NSComboBox comboBox, nint index)
			{
				return new NSString(_renderer.Element.Items[(int)index]);
			}

			public override nint IndexOfItem(NSComboBox comboBox, string value)
			{
				var index = _renderer.Element.Items?.IndexOf(value) ?? -1;
				return index;
			}
		}
	}
}