summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Renderers/CustomNSTableHeaderView.cs
blob: 79628b6232ff38cfddbcf24741546a1867fe1b29 (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
´╗┐using AppKit;
using CoreGraphics;
using Foundation;

namespace Xamarin.Forms.Platform.MacOS
{
	sealed class CustomNSTableHeaderView : NSTableHeaderView
	{
		public CustomNSTableHeaderView() : this(0, null) { }
		public CustomNSTableHeaderView(double width, IVisualElementRenderer headerRenderer)
		{
			var view = new NSView { WantsLayer = true, Layer = { BackgroundColor = NSColor.Clear.CGColor } };
			AddSubview(view);
			Update(width, headerRenderer);
		}

		public void Update(double width, IVisualElementRenderer headerRenderer)
		{
			double height = 1;
			if (headerRenderer != null)
			{
				var headerView = headerRenderer.Element;
				var request = headerView.Measure(double.PositiveInfinity, double.PositiveInfinity, MeasureFlags.IncludeMargins);
				height = request.Request.Height;
				var bounds = new Rectangle(0, 0, width, height);
				Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(headerView, bounds);
				headerRenderer.NativeView.Frame = bounds.ToRectangleF();
				AddSubview(headerRenderer.NativeView);

			}
			Frame = new CGRect(0, 0, width, height);
		}

		//hides default text field
		public override NSAttributedString PageHeader => new NSAttributedString("");

		public override void DrawRect(CGRect dirtyRect) { }

		public override void Layout()
		{
			foreach (var view in Subviews)
				view.Frame = Frame;
			base.Layout();
		}
	}
}