summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Cells/CellNSView.cs
blob: 08043ce8970eea9c9476116a85c8c5a3f720debb (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
´╗┐using System;
using System.Linq;
using System.ComponentModel;
using AppKit;
using CoreGraphics;

namespace Xamarin.Forms.Platform.MacOS
{
	internal class CellNSView : NSView, INativeElementView
	{
		static readonly NSColor s_defaultChildViewsBackground = NSColor.Clear;
		static readonly CGColor s_defaultHeaderViewsBackground = NSColor.LightGray.CGColor;
		Cell _cell;
		readonly NSTableViewCellStyle _style;
		NSView _contexActionsTrackingView;

		public Action<object, PropertyChangedEventArgs> PropertyChanged;

		public CellNSView(NSTableViewCellStyle style)
		{
			WantsLayer = true;
			_style = style;
			CreateUI();
		}

		public NSTextField TextLabel { get; private set; }

		public NSTextField DetailTextLabel { get; private set; }

		public NSImageView ImageView { get; private set; }

		public NSView AccessoryView { get; private set; }

		public virtual Element Element => Cell;

		public Cell Cell
		{
			get { return _cell; }
			set
			{
				if (_cell == value)
					return;

				ICellController cellController = _cell;

				if (cellController != null)
					Device.BeginInvokeOnMainThread(cellController.SendDisappearing);

				_cell = value;
				cellController = value;

				if (cellController != null)
					Device.BeginInvokeOnMainThread(cellController.SendAppearing);
			}
		}

		public void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			PropertyChanged?.Invoke(this, e);
		}

		public override void Layout()
		{
			const int padding = 10;
			nfloat availableHeight = Frame.Height;
			nfloat availableWidth = Frame.Width - padding * 2;
			nfloat imageWidth = 0;
			nfloat accessoryViewWidth = 0;

			if (ImageView != null)
			{
				nfloat imageHeight = imageWidth = availableHeight;
				ImageView.Frame = new CGRect(padding, 0, imageWidth, imageHeight);
			}

			if (AccessoryView != null)
			{
				accessoryViewWidth = _style == NSTableViewCellStyle.Value1 ? 50 : availableWidth - 100;
				AccessoryView.Frame = new CGRect(availableWidth - accessoryViewWidth + padding, 0, accessoryViewWidth,
					availableHeight);
				foreach (var subView in AccessoryView.Subviews)
				{
					//try to find the size the control wants, if no width use default width
					var size = subView.FittingSize;
					if (size.Width == 0)
						size.Width = accessoryViewWidth;

					var x = AccessoryView.Bounds.Width - size.Width;
					var y = (AccessoryView.Bounds.Height - size.Height) / 2;
					subView.Frame = new CGRect(new CGPoint(x, y), size);
				}
			}

			nfloat labelHeights = availableHeight;
			nfloat labelWidth = availableWidth - imageWidth - accessoryViewWidth;

			if (DetailTextLabel != null)
			{

				if (!string.IsNullOrEmpty(DetailTextLabel?.StringValue))
				{
					labelHeights = availableHeight / 2;
					DetailTextLabel.CenterTextVertically(new CGRect(imageWidth + padding, 0, labelWidth, labelHeights));
				}
			}

			TextLabel?.CenterTextVertically(new CGRect(imageWidth + padding, availableHeight - labelHeights, labelWidth,
				labelHeights));

			var topNSView = Subviews.LastOrDefault();
			if (_contexActionsTrackingView != topNSView)
			{
				_contexActionsTrackingView.RemoveFromSuperview();
				_contexActionsTrackingView.Frame = Frame;
				AddSubview(_contexActionsTrackingView, NSWindowOrderingMode.Above, Subviews.LastOrDefault());
			}
			base.Layout();
		}

		internal static NSView GetNativeCell(NSTableView tableView, Cell cell, string templateId = "", bool isHeader = false,
			bool isRecycle = false)
		{
			var reusable = tableView.MakeView(templateId, tableView);
			NSView nativeCell;
			if (reusable == null || !isRecycle)
			{
				var renderer = (CellRenderer)Registrar.Registered.GetHandler(cell.GetType());
				nativeCell = renderer.GetCell(cell, null, tableView);
			}
			else
			{
				nativeCell = reusable;
			}

			if (string.IsNullOrEmpty(nativeCell.Identifier))
				nativeCell.Identifier = templateId;

			if (!isHeader) return nativeCell;
			if (nativeCell.Layer != null) nativeCell.Layer.BackgroundColor = s_defaultHeaderViewsBackground;
			return nativeCell;
		}

		void CreateUI()
		{
			var style = _style;
			if (style != NSTableViewCellStyle.Empty)
			{
				AddSubview(TextLabel = new NSTextField
				{
					Bordered = false,
					Selectable = false,
					Editable = false,
					Font = NSFont.LabelFontOfSize(NSFont.SystemFontSize)
				});

				TextLabel.Cell.BackgroundColor = s_defaultChildViewsBackground;

				if (style == NSTableViewCellStyle.Image || style == NSTableViewCellStyle.Subtitle ||
					style == NSTableViewCellStyle.ImageSubtitle)
				{
					AddSubview(DetailTextLabel = new NSTextField
					{
						Bordered = false,
						Selectable = false,
						Editable = false,
						Font = NSFont.LabelFontOfSize(NSFont.SmallSystemFontSize)
					});
					DetailTextLabel.Cell.BackgroundColor = s_defaultChildViewsBackground;
				}

				if (style == NSTableViewCellStyle.Image || style == NSTableViewCellStyle.ImageSubtitle)
					AddSubview(ImageView = new NSImageView());

				if (style == NSTableViewCellStyle.Value1 || style == NSTableViewCellStyle.Value2)
				{
					var accessoryView = new NSView { WantsLayer = true };
					accessoryView.Layer.BackgroundColor = s_defaultChildViewsBackground.CGColor;
					AddSubview(AccessoryView = accessoryView);
				}
			}
			AddSubview(_contexActionsTrackingView = new TrackingClickNSView());
		}
	}

	class TrackingClickNSView : NSView
	{
		public override void RightMouseDown(NSEvent theEvent)
		{
			HandleContextActions(theEvent);

			base.RightMouseDown(theEvent);
		}

		void HandleContextActions(NSEvent theEvent)
		{
			var contextActionCell = (Superview as INativeElementView).Element as Cell;
			var contextActionsCount = contextActionCell.ContextActions.Count;
			if (contextActionsCount > 0)
			{
				NSMenu menu = new NSMenu();
				for (int i = 0; i < contextActionsCount; i++)
				{
					var contextAction = contextActionCell.ContextActions[i];
					var nsMenuItem = GetNSMenuItem(i, contextAction);
					menu.AddItem(nsMenuItem);
				}

				NSMenu.PopUpContextMenu(menu, theEvent, this);
			}
		}

		static NSMenuItem GetNSMenuItem(int i, MenuItem contextAction)
		{
			var menuItem = new NSMenuItem(contextAction.Text ?? "");
			menuItem.Tag = i;
			menuItem.Enabled = contextAction.IsEnabled;
			if (menuItem.Enabled)
				menuItem.Activated += (sender, e) =>
				{
					((IMenuItemController)contextAction).Activate();
				};
			if (!string.IsNullOrEmpty(contextAction.Icon))
				menuItem.Image = new NSImage(contextAction.Icon);

			return menuItem;
		}
	}
}