summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Renderers/MasterDetailPageRenderer.cs
blob: 8d2fd551664dab51e5138fb9654d76d661d8664e (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
´╗┐using System;
using System.ComponentModel;
using System.Linq;
using AppKit;
using CoreGraphics;

namespace Xamarin.Forms.Platform.MacOS
{
	public class MasterDetailPageRenderer : NSSplitViewController, IVisualElementRenderer, IEffectControlProvider
	{
		bool _disposed;
		EventTracker _events;
		VisualElementTracker _tracker;
		MasterDetailPage _masterDetailPage;

		IPageController PageController => Element as IPageController;

		void IEffectControlProvider.RegisterEffect(Effect effect)
		{
			var platformEffect = effect as PlatformEffect;
			if (platformEffect != null)
				platformEffect.Container = View;
		}

		protected MasterDetailPage MasterDetailPage => _masterDetailPage ?? (_masterDetailPage = (MasterDetailPage)Element);

		protected override void Dispose(bool disposing)
		{
			if (!_disposed && disposing)
			{
				PageController?.SendDisappearing();

				if (Element != null)
				{
					Element.PropertyChanged -= HandlePropertyChanged;
					Element = null;
				}

				ClearControllers();

				_tracker?.Dispose();
				_tracker = null;
				_events?.Dispose();
				_events = null;

				_disposed = true;
			}
			base.Dispose(disposing);
		}

		public NSViewController ViewController => this;

		public NSView NativeView => View;

		public VisualElement Element { get; private set; }

		public event EventHandler<VisualElementChangedEventArgs> ElementChanged;

		public SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
		{
			return NativeView.GetSizeRequest(widthConstraint, heightConstraint);
		}

		public void SetElement(VisualElement element)
		{
			var oldElement = Element;
			Element = element;

			UpdateControllers();

			OnElementChanged(new VisualElementChangedEventArgs(oldElement, element));

			EffectUtilities.RegisterEffectControlProvider(this, oldElement, element);
		}

		public void SetElementSize(Size size)
		{
			Element.Layout(new Rectangle(Element.X, Element.Y, size.Width, size.Height));
			UpdateChildrenLayout();
		}

		protected virtual void OnElementChanged(VisualElementChangedEventArgs e)
		{
			if (e.OldElement != null)
				e.OldElement.PropertyChanged -= HandlePropertyChanged;

			if (e.NewElement != null)
				e.NewElement.PropertyChanged += HandlePropertyChanged;

			ElementChanged?.Invoke(this, e);
		}

		protected virtual double MasterWidthPercentage => 0.3;

		public override void ViewWillAppear()
		{
			UpdateBackground();
			_tracker = new VisualElementTracker(this);
			_events = new EventTracker(this);
			_events.LoadEvents(NativeView);
			UpdateChildrenLayout();

			base.ViewWillAppear();
		}

		public override CGRect GetEffectiveRect(NSSplitView splitView, CGRect proposedEffectiveRect, CGRect drawnRect,
			nint dividerIndex)
		{
			return CGRect.Empty;
		}

		void UpdateChildrenLayout()
		{
			if (View.Frame.Width == -1)
				return;
			var width = View.Frame.Width;
			var masterWidth = MasterWidthPercentage * width;
			if (SplitViewItems.Length > 0)
				SplitViewItems[0].MaximumThickness = SplitViewItems[0].MinimumThickness = (nfloat)masterWidth;
		}

		void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (_tracker == null)
				return;

			if (e.PropertyName == "Master" || e.PropertyName == "Detail")
				UpdateControllers();
		}

		void UpdateControllers()
		{
			ClearControllers();

			if (Platform.GetRenderer(MasterDetailPage.Master) == null)
				Platform.SetRenderer(MasterDetailPage.Master, Platform.CreateRenderer(MasterDetailPage.Master));
			if (Platform.GetRenderer(MasterDetailPage.Detail) == null)
				Platform.SetRenderer(MasterDetailPage.Detail, Platform.CreateRenderer(MasterDetailPage.Detail));

			AddSplitViewItem(new NSSplitViewItem
			{
				ViewController = new ViewControllerWrapper(Platform.GetRenderer(MasterDetailPage.Master))
			});
			AddSplitViewItem(new NSSplitViewItem
			{
				ViewController = new ViewControllerWrapper(Platform.GetRenderer(MasterDetailPage.Detail))
			});

			UpdateChildrenLayout();
		}

		void ClearControllers()
		{
			while (SplitViewItems.Length > 0)
			{
				var splitItem = SplitViewItems.Last();
				var childVisualRenderer = splitItem.ViewController as ViewControllerWrapper;
				RemoveSplitViewItem(splitItem);
				IVisualElementRenderer render = null;
				if (childVisualRenderer.RendererWeakRef.TryGetTarget(out render))
				{
					render.Dispose();
				}
				childVisualRenderer.Dispose();
				childVisualRenderer = null;
			}
		}

		//TODO: Implement Background color on MDP
		void UpdateBackground()
		{
		}

		sealed class ViewControllerWrapper : NSViewController
		{
			internal WeakReference<IVisualElementRenderer> RendererWeakRef;

			public ViewControllerWrapper(IVisualElementRenderer renderer)
			{
				RendererWeakRef = new WeakReference<IVisualElementRenderer>(renderer);
				View = new NSView { WantsLayer = true };
				AddChildViewController(renderer.ViewController);
				View.AddSubview(renderer.NativeView);
			}

			public override void ViewWillLayout()
			{
				IVisualElementRenderer renderer;
				if (RendererWeakRef.TryGetTarget(out renderer))
					renderer?.Element?.Layout(new Rectangle(0, 0, View.Bounds.Width, View.Bounds.Height));
				base.ViewWillLayout();
			}

			protected override void Dispose(bool disposing)
			{
				if (disposing && RendererWeakRef != null)
				{
					RendererWeakRef = null;
				}
				base.Dispose(disposing);
			}
		}
	}
}