summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT/MasterDetailControl.cs
blob: c325b42a5b29988c61bef3cd6b38e87a28ccffef (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
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;

namespace Xamarin.Forms.Platform.WinRT
{
	public class MasterDetailControl : Control
	{
		public static readonly DependencyProperty MasterContentProperty = DependencyProperty.Register("MasterContent", typeof(object), typeof(MasterDetailControl),
			new PropertyMetadata(null, (d, e) => ((MasterDetailControl)d).UpdateMaster()));

		public static readonly DependencyProperty DetailContentProperty = DependencyProperty.Register("DetailContent", typeof(object), typeof(MasterDetailControl),
			new PropertyMetadata(null, (d, e) => ((MasterDetailControl)d).UpdateDetail()));

		public static readonly DependencyProperty IsMasterVisibleProperty = DependencyProperty.Register("IsMasterVisible", typeof(bool), typeof(MasterDetailControl),
			new PropertyMetadata(false, (d, e) => ((MasterDetailControl)d).UpdateMaster()));

		public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(MasterDetailControl),
			new PropertyMetadata("", (d, e) => ((MasterDetailControl)d).UpdateTitle()));

		public static readonly DependencyProperty DetailTitleVisibilityProperty = DependencyProperty.Register("DetailTitleVisibility", typeof(Visibility), typeof(MasterDetailControl),
			new PropertyMetadata(Visibility.Collapsed, (d, e) => ((MasterDetailControl)d).UpdateToolbar()));

		public static readonly DependencyProperty ToolbarForegroundProperty = DependencyProperty.Register("ToolbarForeground", typeof(Brush), typeof(MasterDetailControl),
			new PropertyMetadata(default(Brush), (d, e) => ((MasterDetailControl)d).UpdateToolbar()));

		public static readonly DependencyProperty ToolbarBackgroundProperty = DependencyProperty.Register("ToolbarBackground", typeof(Brush), typeof(MasterDetailControl),
			new PropertyMetadata(default(Brush), (d, e) => ((MasterDetailControl)d).UpdateToolbar()));

		Windows.UI.Xaml.Controls.Grid _grd;

		Windows.UI.Xaml.Controls.ContentPresenter _masterPresenter;
		Windows.UI.Xaml.Controls.ContentPresenter _detailPresenter;
		Popup _popup;
		TextBlock _txbTitle;

		public object DetailContent
		{
			get { return GetValue(DetailContentProperty); }
			set { SetValue(DetailContentProperty, value); }
		}

		public Visibility DetailTitleVisibility
		{
			get { return (Visibility)GetValue(DetailTitleVisibilityProperty); }
			set { SetValue(DetailTitleVisibilityProperty, value); }
		}

		public bool IsMasterVisible
		{
			get { return (bool)GetValue(IsMasterVisibleProperty); }
			set { SetValue(IsMasterVisibleProperty, value); }
		}

		public object MasterContent
		{
			get { return GetValue(MasterContentProperty); }
			set { SetValue(MasterContentProperty, value); }
		}

		public string Title
		{
			get { return (string)GetValue(TitleProperty); }
			set { SetValue(TitleProperty, value); }
		}

		public Brush ToolbarBackground
		{
			get { return (Brush)GetValue(ToolbarBackgroundProperty); }
			set { SetValue(ToolbarBackgroundProperty, value); }
		}

		public Brush ToolbarForeground
		{
			get { return (Brush)GetValue(ToolbarForegroundProperty); }
			set { SetValue(ToolbarForegroundProperty, value); }
		}

		public event EventHandler UserClosedPopover;

		protected override void OnApplyTemplate()
		{
			if (_masterPresenter != null)
			{
				// Despite itself being unparented when the template changes, the presenters' children still think they're
				// parented and so the new presenters throw when the content is assigned to them.
				_masterPresenter.Content = null;
				_masterPresenter = null;
			}

			if (_detailPresenter != null)
			{
				_detailPresenter.Content = null;
				_detailPresenter = null;
			}

			if (_popup != null)
			{
				_popup.Closed -= OnPopupClosed;
				_popup = null;
			}

			base.OnApplyTemplate();

			_masterPresenter = GetTemplateChild("masterPresenter") as Windows.UI.Xaml.Controls.ContentPresenter;
			_detailPresenter = GetTemplateChild("detailPresenter") as Windows.UI.Xaml.Controls.ContentPresenter;
			_txbTitle = GetTemplateChild("txbTitle") as TextBlock;
			_grd = GetTemplateChild("grdToolbar") as Windows.UI.Xaml.Controls.Grid;

			_popup = GetTemplateChild("popup") as Popup;
			if (_popup != null)
				_popup.Closed += OnPopupClosed;

			UpdateMaster();
			UpdateDetail();
			UpdateTitle();
			UpdateToolbar();
		}

		void OnPopupClosed(object sender, object e)
		{
			EventHandler closed = UserClosedPopover;
			if (closed != null)
				closed(this, EventArgs.Empty);
		}

		void UpdateDetail()
		{
			if (_detailPresenter == null)
				return;

			_detailPresenter.Content = DetailContent;
		}

		void UpdateMaster()
		{
			if (_masterPresenter == null)
				return;

			bool visible = IsMasterVisible;
			_masterPresenter.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
			_masterPresenter.Content = MasterContent;

			if (_popup != null)
				_popup.IsOpen = visible;
		}

		void UpdateTitle()
		{
			if (_txbTitle == null)
				return;

			_txbTitle.Text = Title ?? "";
		}

		void UpdateToolbar()
		{
			if (_txbTitle == null)
				return;

			_grd.Visibility = DetailTitleVisibility;
			_grd.Background = ToolbarBackground;
			_txbTitle.Foreground = ToolbarForeground;
		}
	}
}