summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Renderers/TableViewRenderer.cs
blob: de447dcbb8ddb9ef40b6c248dd85f782ac027e4f (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
´╗┐using System.Collections.Generic;
using System.ComponentModel;
using AppKit;

namespace Xamarin.Forms.Platform.MacOS
{
	public class TableViewRenderer : ViewRenderer<TableView, NSView>
	{
		const int DefaultRowHeight = 44;

		internal NSTableView TableView { get; set; }

		public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
		{
			return Control.GetSizeRequest(widthConstraint, heightConstraint, DefaultRowHeight, DefaultRowHeight);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				var viewsToLookAt = new Stack<NSView>(Subviews);
				while (viewsToLookAt.Count > 0)
				{
					var view = viewsToLookAt.Pop();
					var viewCellRenderer = view as IVisualElementRenderer;
					if (viewCellRenderer != null)
					{
						viewCellRenderer.Dispose();
					}
					else
					{
						foreach (var child in view.Subviews)
							viewsToLookAt.Push(child);
					}
				}
			}

			base.Dispose(disposing);
		}

		protected virtual NSTableView CreateNSTableView(TableView list)
		{
			return new NSTableView().AsListViewLook();
		}

		protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
		{
			if (e.NewElement != null)
			{
				if (Control == null)
				{
					var scroller = new NSScrollView
					{
						AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable,
						DocumentView = TableView = CreateNSTableView(e.NewElement)
					};

					SetNativeControl(scroller);
				}

				SetSource();
				UpdateRowHeight();
				UpdateBackgroundView();
			}

			base.OnElementChanged(e);
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			if (e.PropertyName == Xamarin.Forms.TableView.RowHeightProperty.PropertyName)
				UpdateRowHeight();
			else if (e.PropertyName == Xamarin.Forms.TableView.HasUnevenRowsProperty.PropertyName)
				SetSource();
			else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
				UpdateBackgroundView();
		}

		void SetSource()
		{
			var modeledView = Element;
			TableView.Source = modeledView.HasUnevenRows ? new UnEvenTableViewModelRenderer(this) : new TableViewDataSource(this);
		}

		void UpdateBackgroundView()
		{
		}

		void UpdateRowHeight()
		{
			var rowHeight = Element.RowHeight;
			TableView.RowHeight = rowHeight <= 0 ? DefaultRowHeight : rowHeight;
		}
	}
}