summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.iOS/Renderers/NavigationMenuRenderer.cs
blob: 11a8558feccb97e8c39559a9c2339b42b2507584 (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
using System;
using System.Linq;
using Foundation;
using UIKit;
using RectangleF = CoreGraphics.CGRect;
using SizeF = CoreGraphics.CGSize;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Platform.iOS
{
	public class NavigationMenuRenderer : ViewRenderer
	{
		UICollectionView _collectionView;

		protected override void OnElementChanged(ElementChangedEventArgs<View> e)
		{
			base.OnElementChanged(e);
			var pad = UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad;
			var size = pad ? 220 : 142;
			var margin = pad ? 27 : 12;
			var bottomMargin = (int)(margin * 0.8);

			_collectionView = new UICollectionView(new RectangleF(0, 0, 100, 100),
				new UICollectionViewFlowLayout
				{
					ItemSize = new SizeF(size, size + 30),
					ScrollDirection = UICollectionViewScrollDirection.Vertical,
					SectionInset = new UIEdgeInsets(margin, margin, bottomMargin, margin),
					MinimumInteritemSpacing = margin,
					MinimumLineSpacing = margin
				})
			{ DataSource = new DataSource((NavigationMenu)Element), BackgroundColor = UIColor.White };

			using (var navigationCellId = new NSString("NavigationCell"))
				_collectionView.RegisterClassForCell(typeof(NavigationCell), navigationCellId);

			SetNativeControl(_collectionView);
		}

		sealed class NavigationCell : UICollectionViewCell
		{
			readonly UIButton _image = new UIButton(RectangleF.Empty);

			readonly UILabel _nameLabel = new UILabel(RectangleF.Empty) { BackgroundColor = UIColor.Clear, TextAlignment = UITextAlignment.Center, Font = UIFont.SystemFontOfSize(14) };

			string _icon;

			[Export("initWithFrame:")]
			public NavigationCell(RectangleF frame) : base(frame)
			{
				SetupLayer();
				_image.TouchUpInside += (object sender, EventArgs e) =>
				{
					if (Selected != null)
						Selected();
				};
				_image.ContentMode = UIViewContentMode.ScaleAspectFit;
				_image.Center = ContentView.Center;

				ContentView.AddSubview(_image);
				ContentView.AddSubview(_nameLabel);
			}

			public string Icon
			{
				get { return _icon; }
				set
				{
					_icon = value;
					_image.SetImage(new UIImage(_icon), UIControlState.Normal);
				}
			}

			public string Name
			{
				get { return _nameLabel.Text; }
				set { _nameLabel.Text = value; }
			}

			public new Action Selected { get; set; }

			public override void LayoutSubviews()
			{
				base.LayoutSubviews();
				_image.Frame = new RectangleF(0, 0, ContentView.Frame.Width, ContentView.Frame.Height - 30);
				var sizeThatFits = _nameLabel.SizeThatFits(ContentView.Frame.Size);
				_nameLabel.Frame = new RectangleF(0, ContentView.Frame.Height - 15 - sizeThatFits.Height / 2, ContentView.Frame.Width, sizeThatFits.Height);
			}

			void SetupLayer()
			{
				var layer = _image.Layer;

				layer.ShadowRadius = 6;
				layer.ShadowColor = UIColor.Black.CGColor;
				layer.ShadowOpacity = 0.2f;
				layer.ShadowOffset = new SizeF();

				layer.RasterizationScale = UIScreen.MainScreen.Scale;
				layer.ShouldRasterize = true;
			}
		}

		class DataSource : UICollectionViewDataSource
		{
			readonly NavigationMenu _menu;

			public DataSource(NavigationMenu menu)
			{
				_menu = menu;
			}

			INavigationMenuController MenuController => _menu;

			public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
			{
				var cell = (NavigationCell)collectionView.DequeueReusableCell(new NSString("NavigationCell"), indexPath);
				var target = _menu.Targets.Skip(indexPath.Row).FirstOrDefault();

				if (target != null)
				{
					cell.Name = target.Title;
					cell.Icon = target.Icon;
					cell.Selected = () => MenuController.SendTargetSelected(target);
				}
				else
				{
					cell.Selected = null;
					cell.Icon = "";
					cell.Name = "";
				}

				return cell;
			}

			public override nint GetItemsCount(UICollectionView collectionView, nint section)
			{
				return _menu.Targets.Count();
			}
		}
	}
}