summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Renderers/ScrollViewRenderer.cs
blob: c41d1d2b641d604deb55e62eb24a4afa3cc40e76 (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
´╗┐using System;
using System.ComponentModel;
using AppKit;
using RectangleF = CoreGraphics.CGRect;
using ObjCRuntime;
using Foundation;

// ReSharper disable UnusedMember.Local
// ReSharper disable UnusedParameter.Local

namespace Xamarin.Forms.Platform.MacOS
{
	public class ScrollViewRenderer : NSScrollView, IVisualElementRenderer
	{
		EventTracker _events;
		VisualElementTracker _tracker;
		ScrollToRequestedEventArgs _requestedScroll;
		IVisualElementRenderer _contentRenderer;

		public ScrollViewRenderer() : base(RectangleF.Empty)
		{
			DrawsBackground = false;
			ContentView.PostsBoundsChangedNotifications = true;
			NSNotificationCenter.DefaultCenter.AddObserver(this, new Selector(nameof(UpdateScrollPosition)),
				BoundsChangedNotification, ContentView);
		}

		IScrollViewController Controller => Element as IScrollViewController;

		public VisualElement Element { get; private set; }

		public event EventHandler<VisualElementChangedEventArgs> ElementChanged;

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

		public NSView NativeView => this;

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

			if (oldElement != null)
			{
				oldElement.PropertyChanged -= HandlePropertyChanged;
				((IScrollViewController)oldElement).ScrollToRequested -= OnScrollToRequested;
			}

			if (element != null)
			{
				element.PropertyChanged += HandlePropertyChanged;
				((IScrollViewController)element).ScrollToRequested += OnScrollToRequested;
				if (_tracker == null)
				{
					PackContent();

					_events = new EventTracker(this);
					_events.LoadEvents(this);

					_tracker = new VisualElementTracker(this);
					_tracker.NativeControlUpdated += OnNativeControlUpdated;
				}

				UpdateContentSize();
				UpdateBackgroundColor();

				OnElementChanged(new VisualElementChangedEventArgs(oldElement, element));
			}
		}

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

		public NSViewController ViewController => null;

		public override void Layout()
		{
			base.Layout();
			LayoutSubviews();
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (_tracker == null)
					return;

				SetElement(null);

				_tracker.NativeControlUpdated -= OnNativeControlUpdated;
				_tracker.Dispose();
				_tracker = null;

				_events.Dispose();
				_events = null;

				ClearContentRenderer();

				//NSNotificationCenter.DefaultCenter.RemoveObserver(this, BoundsChangedNotification);
			}

			base.Dispose(disposing);
		}

		void OnElementChanged(VisualElementChangedEventArgs e)
		{
			ElementChanged?.Invoke(this, e);
		}

		void PackContent()
		{
			ClearContentRenderer();

			if (Controller.Children.Count == 0 || !(Controller.Children[0] is VisualElement))
				return;

			var content = (VisualElement)Controller.Children[0];
			if (Platform.GetRenderer(content) == null)
				Platform.SetRenderer(content, Platform.CreateRenderer(content));

			_contentRenderer = Platform.GetRenderer(content);

			DocumentView = _contentRenderer.NativeView;
		}

		void LayoutSubviews()
		{
			if (_requestedScroll != null && Superview != null)
			{
				var request = _requestedScroll;
				_requestedScroll = null;
				OnScrollToRequested(this, request);
			}
		}

		void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == ScrollView.ContentSizeProperty.PropertyName)
				UpdateContentSize();
			else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
				UpdateBackgroundColor();
		}

		void HandleScrollAnimationEnded(object sender, EventArgs e)
		{
			Controller.SendScrollFinished();
		}

		void HandleScrolled(object sender, EventArgs e)
		{
			UpdateScrollPosition();
		}

		void OnNativeControlUpdated(object sender, EventArgs eventArgs)
		{
			UpdateContentSize();
		}

		void OnScrollToRequested(object sender, ScrollToRequestedEventArgs e)
		{
			if (Superview == null)
			{
				_requestedScroll = e;
				return;
			}

			Point scrollPoint = (e.Mode == ScrollToMode.Position)
				? new Point(e.ScrollX, Element.Height - e.ScrollY)
				: Controller.GetScrollPositionForElement(e.Element as VisualElement, e.Position);

			(DocumentView as NSView)?.ScrollPoint(scrollPoint.ToPointF());

			Controller.SendScrollFinished();
		}

		void UpdateBackgroundColor()
		{
			BackgroundColor = Element.BackgroundColor.ToNSColor(Color.Transparent);
		}

		void UpdateContentSize()
		{
			if (_contentRenderer == null)
				return;
			var contentSize = ((ScrollView)Element).ContentSize.ToSizeF();
			if (!contentSize.IsEmpty)
				_contentRenderer.NativeView.Frame = new RectangleF(0, Element.Height - contentSize.Height, contentSize.Width,
					contentSize.Height);
		}

		[Export(nameof(UpdateScrollPosition))]
		void UpdateScrollPosition()
		{
			var convertedPoint = (DocumentView as NSView)?.ConvertPointFromView(ContentView.Bounds.Location, ContentView);
			if (convertedPoint.HasValue)
				Controller.SetScrolledPosition(Math.Max(0, convertedPoint.Value.X), Math.Max(0, convertedPoint.Value.Y));
		}

		void ClearContentRenderer()
		{
			_contentRenderer?.NativeView?.RemoveFromSuperview();
			_contentRenderer?.Dispose();
			_contentRenderer = null;
		}
	}
}