summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/TableViewRenderer.cs
blob: 22e16a679d709a3ac337336a814afd4f0bb4aab0 (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
´╗┐using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Phone.Shell;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Platform.WinPhone
{
	public class TableViewRenderer : ViewRenderer<Xamarin.Forms.TableView, TableView>
	{
		TableView _view;
		ITableViewController Controller => Element;

		public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
		{
			SizeRequest result = base.GetDesiredSize(widthConstraint, heightConstraint);
			result.Minimum = new Size(40, 40);
			return result;
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TableView> e)
		{
			base.OnElementChanged(e);

			Controller.ModelChanged += OnModelChanged;

			_view = new TableView { DataContext = Element.Root };
			_view.Tap += OnTapTable;
			_view.Hold += OnLongPressTable;
			SetNativeControl(_view);
		}

		bool FindIndices(GestureEventArgs e, out int sectionIndex, out int cellIndex)
		{
			sectionIndex = 0;
			cellIndex = 0;

			TableSection section = null;
			Cell cell = null;

			System.Windows.Point pos = e.GetPosition(System.Windows.Application.Current.RootVisual);
			if (Device.Info.CurrentOrientation.IsLandscape())
			{
				double x = pos.Y;
				double y = System.Windows.Application.Current.RootVisual.RenderSize.Width - pos.X + (SystemTray.IsVisible ? 72 : 0);
				pos = new System.Windows.Point(x, y);
			}
			IEnumerable<UIElement> elements = VisualTreeHelper.FindElementsInHostCoordinates(pos, System.Windows.Application.Current.RootVisual);
			foreach (FrameworkElement element in elements.OfType<FrameworkElement>())
			{
				if (cell == null)
					cell = element.DataContext as Cell;
				else if (section == null)
					section = element.DataContext as TableSection;
				else
					break;
			}

			if (cell == null || section == null)
				return false;

			sectionIndex = Element.Root.IndexOf(section);
			cellIndex = section.IndexOf(cell);
			return true;
		}

		void OnLongPressTable(object sender, GestureEventArgs e)
		{
			int sectionIndex, cellIndex;
			if (!FindIndices(e, out sectionIndex, out cellIndex))
				return;

			Controller.Model.RowLongPressed(sectionIndex, cellIndex);
		}

		void OnModelChanged(object sender, EventArgs eventArgs)
		{
			_view.DataContext = Element.Root;
		}

		void OnTapTable(object sender, GestureEventArgs e)
		{
			int sectionIndex, cellIndex;
			if (!FindIndices(e, out sectionIndex, out cellIndex))
				return;

			Controller.Model.RowSelected(sectionIndex, cellIndex);
		}
	}
}