summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT/PlatformWinRT.cs
blob: df43345bafc9e590d290ae41dc4db98f84af6995 (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
´╗┐using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Platform.WinRT
{
	public abstract partial class Platform
	{
		CommandBar AddOpenMasterButton(CommandBar commandBar)
		{
			if (!_toolbarTracker.HaveMasterDetail)
			{
				return commandBar;
			}

			if (commandBar == null)
			{
				commandBar = CreateCommandBar();
			}

			Page target = _toolbarTracker.Target;
			var mdp = target as MasterDetailPage;
			while (mdp == null)
			{
				var container = target as IPageContainer<Page>;
				if (container == null)
				{
					break;
				}

				target = container.CurrentPage;
				mdp = container.CurrentPage as MasterDetailPage;
			}

			if (mdp == null || !mdp.ShouldShowToolbarButton())
			{
				return commandBar;
			}

			var openMaster = new AppBarButton { DataContext = mdp };
			openMaster.SetBinding(AppBarButton.LabelProperty, "Master.Title");
			openMaster.SetBinding(AppBarButton.IconProperty, "Master.Icon", _fileImageSourcePathConverter);
			openMaster.Click += (s, a) => { mdp.IsPresented = !mdp.IsPresented; };

			commandBar.PrimaryCommands.Add(openMaster);

			return commandBar;
		}

		CommandBar CreateCommandBar()
		{
			var commandBar = new CommandBar();
			_page.BottomAppBar = commandBar;
			return commandBar;
		}

		void UpdateBounds()
		{
			_bounds = new Rectangle(0, 0, _page.ActualWidth, _page.ActualHeight);
		}

		Task<CommandBar> GetCommandBarAsync()
		{
			return Task.FromResult(_page.BottomAppBar as CommandBar);
		}

		void ClearCommandBar()
		{
			_page.BottomAppBar = null;
		}

		void OnPageActionSheet(Page sender, ActionSheetArguments options)
		{
			var finalArguments = new List<string>();
			if (options.Destruction != null)
				finalArguments.Add(options.Destruction);
			if (options.Buttons != null)
				finalArguments.AddRange(options.Buttons);
			if (options.Cancel != null)
				finalArguments.Add(options.Cancel);

			var list = new Windows.UI.Xaml.Controls.ListView
			{
				Style = (Windows.UI.Xaml.Style)Windows.UI.Xaml.Application.Current.Resources["ActionSheetList"],
				ItemsSource = finalArguments,
				IsItemClickEnabled = true
			};

			list.ItemClick += (s, e) =>
			{
				_currentActionSheet.IsOpen = false;
				options.SetResult((string)e.ClickedItem);
			};

			_actionSheetOptions = options;

			Size size = Device.Info.ScaledScreenSize;

			var stack = new StackPanel
			{
				MinWidth = 100,
				Children =
				{
					new TextBlock
					{
						Text = options.Title ?? string.Empty,
						Style = (Windows.UI.Xaml.Style)Windows.UI.Xaml.Application.Current.Resources["TitleTextBlockStyle"],
						Margin = new Windows.UI.Xaml.Thickness(0, 0, 0, 10),
						Visibility = options.Title != null ? Visibility.Visible : Visibility.Collapsed
					},
					list
				}
			};

			var border = new Border
			{
				Child = stack,
				BorderBrush = new SolidColorBrush(Colors.White),
				BorderThickness = new Windows.UI.Xaml.Thickness(1),
				Padding = new Windows.UI.Xaml.Thickness(15),
				Background = (Brush)Windows.UI.Xaml.Application.Current.Resources["AppBarBackgroundThemeBrush"]
			};

			Windows.UI.Xaml.Controls.Grid.SetRow(border, 1);
			Windows.UI.Xaml.Controls.Grid.SetColumn(border, 1);

			var container = new Windows.UI.Xaml.Controls.Grid
			{
				RowDefinitions =
				{
					new Windows.UI.Xaml.Controls.RowDefinition { Height = new Windows.UI.Xaml.GridLength(1, Windows.UI.Xaml.GridUnitType.Star) },
					new Windows.UI.Xaml.Controls.RowDefinition { Height = new Windows.UI.Xaml.GridLength(0, Windows.UI.Xaml.GridUnitType.Auto) },
					new Windows.UI.Xaml.Controls.RowDefinition { Height = new Windows.UI.Xaml.GridLength(1, Windows.UI.Xaml.GridUnitType.Star) }
				},
				ColumnDefinitions =
				{
					new Windows.UI.Xaml.Controls.ColumnDefinition { Width = new Windows.UI.Xaml.GridLength(1, Windows.UI.Xaml.GridUnitType.Star) },
					new Windows.UI.Xaml.Controls.ColumnDefinition { Width = new Windows.UI.Xaml.GridLength(0, Windows.UI.Xaml.GridUnitType.Auto) },
					new Windows.UI.Xaml.Controls.ColumnDefinition { Width = new Windows.UI.Xaml.GridLength(1, Windows.UI.Xaml.GridUnitType.Star) }
				},
				Height = size.Height,
				Width = size.Width,
				Children = { border }
			};

			var bgPopup = new Popup { Child = new Canvas { Width = size.Width, Height = size.Height, Background = new SolidColorBrush(new Windows.UI.Color { A = 128, R = 0, G = 0, B = 0 }) } };

			bgPopup.IsOpen = true;

			_currentActionSheet = new Popup { ChildTransitions = new TransitionCollection { new PopupThemeTransition() }, IsLightDismissEnabled = true, Child = container };

			_currentActionSheet.Closed += (s, e) =>
			{
				bgPopup.IsOpen = false;
				CancelActionSheet();
			};

			if (Device.Idiom == TargetIdiom.Phone)
			{
				double height = _page.ActualHeight;
				stack.Height = height;
				stack.Width = size.Width;
				border.BorderThickness = new Windows.UI.Xaml.Thickness(0);

				_currentActionSheet.Height = height;
				_currentActionSheet.VerticalOffset = size.Height - height;
			}

			_currentActionSheet.IsOpen = true;
		}

		internal async Task UpdateToolbarItems()
		{
			CommandBar commandBar = await GetCommandBarAsync();
			if (commandBar != null)
			{
				commandBar.PrimaryCommands.Clear();
				commandBar.SecondaryCommands.Clear();
			}

			commandBar = AddOpenMasterButton(commandBar);

			foreach (ToolbarItem item in _toolbarTracker.ToolbarItems.OrderBy(ti => ti.Priority))
			{
				if (commandBar == null)
					commandBar = CreateCommandBar();

				var button = new AppBarButton();
				button.SetBinding(AppBarButton.LabelProperty, "Text");
				button.SetBinding(AppBarButton.IconProperty, "Icon", _fileImageSourcePathConverter);
				button.Command = new MenuItemCommand(item);
				button.DataContext = item;

				ToolbarItemOrder order = item.Order == ToolbarItemOrder.Default ? ToolbarItemOrder.Primary : item.Order;
				if (order == ToolbarItemOrder.Primary)
					commandBar.PrimaryCommands.Add(button);
				else
					commandBar.SecondaryCommands.Add(button);
			}

			if (commandBar?.PrimaryCommands.Count + commandBar?.SecondaryCommands.Count == 0)
				ClearCommandBar();
		}
	}
}