summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Renderers/MasterDetailContainer.cs
blob: a7bebf1e12c9e6d7bf8fde6ed98cb4ec71009e63 (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
using System;
using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Platform.Android
{
	internal class MasterDetailContainer : ViewGroup
	{
		const int DefaultMasterSize = 320;
		const int DefaultSmallMasterSize = 240;
		readonly bool _isMaster;
		readonly MasterDetailPage _parent;
		VisualElement _childView;

		public MasterDetailContainer(MasterDetailPage parent, bool isMaster, Context context) : base(context)
		{
			_parent = parent;
			_isMaster = isMaster;
		}

		public MasterDetailContainer(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }

		IMasterDetailPageController MasterDetailPageController => _parent as IMasterDetailPageController;

		public VisualElement ChildView
		{
			get { return _childView; }
			set
			{
				if (_childView == value)
					return;

				RemoveAllViews();
				if (_childView != null)
					DisposeChildRenderers();

				_childView = value;

				if (_childView == null)
					return;
				
				AddChildView(_childView);
			}
		}

		protected virtual void AddChildView(VisualElement childView)
		{
			IVisualElementRenderer renderer = Platform.GetRenderer(childView);
			if (renderer == null)
				Platform.SetRenderer(childView, renderer = Platform.CreateRenderer(childView));

			if (renderer.ViewGroup.Parent != this)
			{
				if (renderer.ViewGroup.Parent != null)
					renderer.ViewGroup.RemoveFromParent();
				SetDefaultBackgroundColor(renderer);
				AddView(renderer.ViewGroup);
				renderer.UpdateLayout();
			}
		}

		public int TopPadding { get; set; }

		double DefaultWidthMaster
		{
			get
			{
				double w = Context.FromPixels(Resources.DisplayMetrics.WidthPixels);
				return w < DefaultSmallMasterSize ? w : (w < DefaultMasterSize ? DefaultSmallMasterSize : DefaultMasterSize);
			}
		}

		public override bool OnInterceptTouchEvent(MotionEvent ev)
		{
			bool isShowingPopover = _parent.IsPresented && !MasterDetailPageController.ShouldShowSplitMode;
			if (!_isMaster && isShowingPopover)
				return true;
			return base.OnInterceptTouchEvent(ev);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				RemoveAllViews();
				DisposeChildRenderers();
			}
			base.Dispose(disposing);
		}

		protected override void OnLayout(bool changed, int l, int t, int r, int b)
		{
			if (_childView == null)
				return;

			Rectangle bounds = GetBounds(_isMaster, l, t, r, b);
			if (_isMaster)
				MasterDetailPageController.MasterBounds = bounds;
			else
				MasterDetailPageController.DetailBounds = bounds;

			IVisualElementRenderer renderer = Platform.GetRenderer(_childView);
			renderer?.UpdateLayout();
		}

		void DisposeChildRenderers()
		{
			IVisualElementRenderer childRenderer = Platform.GetRenderer(_childView);
			if (childRenderer != null)
				childRenderer.Dispose();
			_childView.ClearValue(Platform.RendererProperty);
		}

		Rectangle GetBounds(bool isMasterPage, int left, int top, int right, int bottom)
		{
			double width = Context.FromPixels(right - left);
			double height = Context.FromPixels(bottom - top);
			double xPos = 0;
			bool supressPadding = false;

			//splitview
			if (MasterDetailPageController.ShouldShowSplitMode)
			{
				//to keep some behavior we have on iPad where you can toggle and it won't do anything 
				bool isDefaultNoToggle = _parent.MasterBehavior == MasterBehavior.Default;
				xPos = isMasterPage ? 0 : (_parent.IsPresented || isDefaultNoToggle ? DefaultWidthMaster : 0);
				width = isMasterPage ? DefaultWidthMaster : _parent.IsPresented || isDefaultNoToggle ? width - DefaultWidthMaster : width;
			}
			else
			{
				//if we are showing the normal popover master doesn't have padding
				supressPadding = isMasterPage;
				//popover make the master smaller
				width = isMasterPage && (Device.Info.CurrentOrientation.IsLandscape() || Device.Idiom == TargetIdiom.Tablet) ? DefaultWidthMaster : width;
			}

			double padding = supressPadding ? 0 : Context.FromPixels(TopPadding);
			return new Rectangle(xPos, padding, width, height - padding);
		}

		protected void SetDefaultBackgroundColor(IVisualElementRenderer renderer)
		{
			if (ChildView.BackgroundColor == Color.Default)
			{
				TypedArray colors = Context.Theme.ObtainStyledAttributes(new[] { global::Android.Resource.Attribute.ColorBackground });
				renderer.ViewGroup.SetBackgroundColor(new global::Android.Graphics.Color(colors.GetColor(0, 0)));
			}
		}
	}
}