summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.UAP/MasterDetailControl.cs
blob: 9ec03f8d5420e23b8d676309d46a87ac3a9347a5 (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
215
216
217
218
219
220
221
222
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Xamarin.Forms.Platform.UWP
{
	public class MasterDetailControl : Control, IToolbarProvider
	{
		public static readonly DependencyProperty MasterProperty = DependencyProperty.Register("Master", typeof(FrameworkElement), typeof(MasterDetailControl),
			new PropertyMetadata(default(FrameworkElement)));

		public static readonly DependencyProperty MasterTitleProperty = DependencyProperty.Register("MasterTitle", typeof(string), typeof(MasterDetailControl), new PropertyMetadata(default(string)));

		public static readonly DependencyProperty DetailProperty = DependencyProperty.Register("Detail", typeof(FrameworkElement), typeof(MasterDetailControl),
			new PropertyMetadata(default(FrameworkElement)));

		public static readonly DependencyProperty IsPaneOpenProperty = DependencyProperty.Register("IsPaneOpen", typeof(bool), typeof(MasterDetailControl), new PropertyMetadata(default(bool)));

		public static readonly DependencyProperty ShouldShowSplitModeProperty = DependencyProperty.Register("ShouldShowSplitMode", typeof(bool), typeof(MasterDetailControl),
			new PropertyMetadata(default(bool), OnShouldShowSplitModeChanged));

		public static readonly DependencyProperty DetailTitleProperty = DependencyProperty.Register("DetailTitle", typeof(string), typeof(MasterDetailControl), new PropertyMetadata(default(string)));

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

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

		public static readonly DependencyProperty MasterTitleVisibilityProperty = DependencyProperty.Register("MasterTitleVisibility", typeof(Visibility), typeof(MasterDetailControl),
			new PropertyMetadata(default(Visibility)));

		public static readonly DependencyProperty DetailTitleVisibilityProperty = DependencyProperty.Register("DetailTitleVisibility", typeof(Visibility), typeof(MasterDetailControl),
			new PropertyMetadata(default(Visibility)));

		public static readonly DependencyProperty MasterToolbarVisibilityProperty = DependencyProperty.Register("MasterToolbarVisibility", typeof(Visibility), typeof(MasterDetailControl),
			new PropertyMetadata(default(Visibility)));

		CommandBar _commandBar;

		TaskCompletionSource<CommandBar> _commandBarTcs;
		FrameworkElement _masterPresenter;
		FrameworkElement _detailPresenter;
		SplitView _split;

		public MasterDetailControl()
		{
			DefaultStyleKey = typeof(MasterDetailControl);
			MasterTitleVisibility = Visibility.Collapsed;
			DetailTitleVisibility = Visibility.Collapsed;
			if (Device.Idiom != TargetIdiom.Phone)
				MasterToolbarVisibility = Visibility.Collapsed;
		}

		public FrameworkElement Detail
		{
			get { return (FrameworkElement)GetValue(DetailProperty); }
			set { SetValue(DetailProperty, value); }
		}

		public Windows.Foundation.Size DetailSize
		{
			get
			{
				double height = ActualHeight;
				double width = ActualWidth;

				if (_commandBar != null)
					height -= _commandBar.ActualHeight;

				if (ShouldShowSplitMode && IsPaneOpen)
				{
					if (_split != null)
						width -= _split.OpenPaneLength;
					else if (_detailPresenter != null)
						width -= _masterPresenter.ActualWidth;
				}

				return new Windows.Foundation.Size(width, height);
			}
		}

		public string DetailTitle
		{
			get { return (string)GetValue(DetailTitleProperty); }
			set { SetValue(DetailTitleProperty, value); }
		}

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

		public bool IsPaneOpen
		{
			get { return (bool)GetValue(IsPaneOpenProperty); }
			set { SetValue(IsPaneOpenProperty, value); }
		}

		public FrameworkElement Master
		{
			get { return (FrameworkElement)GetValue(MasterProperty); }
			set { SetValue(MasterProperty, value); }
		}

		public Windows.Foundation.Size MasterSize
		{
			get
			{
				double height = ActualHeight;
				double width = 0;

				if (_commandBar != null)
					height -= _commandBar.ActualHeight;

				if (_split != null)
					width = _split.OpenPaneLength;
				else if (_masterPresenter != null)
					width = _masterPresenter.ActualWidth;

				return new Windows.Foundation.Size(width, height);
			}
		}

		public string MasterTitle
		{
			get { return (string)GetValue(MasterTitleProperty); }
			set { SetValue(MasterTitleProperty, value); }
		}

		public Visibility MasterTitleVisibility
		{
			get { return (Visibility)GetValue(MasterTitleVisibilityProperty); }
			set { SetValue(MasterTitleVisibilityProperty, value); }
		}

		public Visibility MasterToolbarVisibility
		{
			get { return (Visibility)GetValue(MasterToolbarVisibilityProperty); }
			set { SetValue(MasterToolbarVisibilityProperty, value); }
		}

		public bool ShouldShowSplitMode
		{
			get { return (bool)GetValue(ShouldShowSplitModeProperty); }
			set { SetValue(ShouldShowSplitModeProperty, 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); }
		}

		Task<CommandBar> IToolbarProvider.GetCommandBarAsync()
		{
			if (_commandBar != null)
				return Task.FromResult(_commandBar);

			_commandBarTcs = new TaskCompletionSource<CommandBar>();
			ApplyTemplate();
			return _commandBarTcs.Task;
		}

		protected override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			_split = GetTemplateChild("SplitView") as SplitView;
			if (_split == null)
				return;

			var paneToggle = GetTemplateChild("PaneTogglePane") as Windows.UI.Xaml.Controls.Button;
			if (paneToggle != null)
				paneToggle.Click += OnToggleClicked;

			var contentToggle = GetTemplateChild("ContentTogglePane") as Windows.UI.Xaml.Controls.Button;
			if (contentToggle != null)
				contentToggle.Click += OnToggleClicked;

			_masterPresenter = GetTemplateChild("MasterPresenter") as FrameworkElement;
			_detailPresenter = GetTemplateChild("DetailPresenter") as FrameworkElement;

			_commandBar = GetTemplateChild("CommandBar") as CommandBar;

			UpdateMode();

			TaskCompletionSource<CommandBar> tcs = _commandBarTcs;
			if (tcs != null)
			{
				_commandBarTcs = null;
				tcs.SetResult(_commandBar);
			}
		}

		static void OnShouldShowSplitModeChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
		{
			((MasterDetailControl)dependencyObject).UpdateMode();
		}

		void OnToggleClicked(object sender, RoutedEventArgs args)
		{
			IsPaneOpen = !IsPaneOpen;
		}

		void UpdateMode()
		{
			if (_split == null)
				return;

			_split.DisplayMode = ShouldShowSplitMode ? SplitViewDisplayMode.Inline : SplitViewDisplayMode.Overlay;
		}
	}
}