summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.UAP/PlatformUWP.cs
blob: 83cb9129039a85773b555562bd1a8be7566d1b36 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
´╗┐using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Xamarin.Forms.Platform.UWP
{
	public abstract partial class Platform
	{
		internal static StatusBar MobileStatusBar => ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar") ? StatusBar.GetForCurrentView() : null;

		IToolbarProvider _toolbarProvider;

		void InitializeStatusBar()
		{
			StatusBar statusBar = MobileStatusBar;
			if (statusBar != null)
			{
				statusBar.Showing += (sender, args) => UpdateBounds();
				statusBar.Hiding += (sender, args) => UpdateBounds();

				// UWP 14393 Bug: If RequestedTheme is Light (which it is by default), then the 
				// status bar uses White Foreground with White Background. 
				// UWP 10586 Bug: If RequestedTheme is Light (which it is by default), then the 
				// status bar uses Black Foreground with Black Background. 
				// Since the Light theme should have a Black on White status bar, we will set it explicitly. 
				// This can be overriden by setting the status bar colors in App.xaml.cs OnLaunched.

				if (statusBar.BackgroundColor == null && statusBar.ForegroundColor == null && Windows.UI.Xaml.Application.Current.RequestedTheme == ApplicationTheme.Light)
				{
					statusBar.BackgroundColor = Colors.White;
					statusBar.ForegroundColor = Colors.Black;
					statusBar.BackgroundOpacity = 1;
				}
			}
		}

		void UpdateToolbarTitle(Page page)
		{
			if (_toolbarProvider == null)
				return;

			((ToolbarProvider)_toolbarProvider).CommandBar.Content = page.Title;
		}

		async void OnPageActionSheet(Page sender, ActionSheetArguments options)
		{
			List<string> buttons = options.Buttons.ToList();

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

			var dialog = new ContentDialog
			{
				Template = (Windows.UI.Xaml.Controls.ControlTemplate)Windows.UI.Xaml.Application.Current.Resources["MyContentDialogControlTemplate"],
				Content = list,
				Style = (Windows.UI.Xaml.Style)Windows.UI.Xaml.Application.Current.Resources["ActionSheetStyle"]
			};

			if (options.Title != null)
				dialog.Title = options.Title;

			list.ItemClick += (s, e) =>
			{
				dialog.Hide();
				options.SetResult((string)e.ClickedItem);
			};

			TypedEventHandler<CoreWindow, CharacterReceivedEventArgs> onEscapeButtonPressed = delegate (CoreWindow window, CharacterReceivedEventArgs args)
			{
				if (args.KeyCode == 27)
				{
					dialog.Hide();
					options.SetResult(ContentDialogResult.None.ToString());
				}
			};

			Window.Current.CoreWindow.CharacterReceived += onEscapeButtonPressed;

			_actionSheetOptions = options;

			if (options.Cancel != null)
				dialog.SecondaryButtonText = options.Cancel;

			if (options.Destruction != null)
				dialog.PrimaryButtonText = options.Destruction;

			ContentDialogResult result = await dialog.ShowAsync();
			if (result == ContentDialogResult.Secondary)
				options.SetResult(options.Cancel);
			else if (result == ContentDialogResult.Primary)
				options.SetResult(options.Destruction);

			Window.Current.CoreWindow.CharacterReceived -= onEscapeButtonPressed;
		}

		void ClearCommandBar()
		{
			if (_toolbarProvider != null)
			{
				_toolbarProvider = null;
				if (Device.Idiom == TargetIdiom.Phone)
					_page.BottomAppBar = null;
				else
					_page.TopAppBar = null;
			}
		}

		CommandBar CreateCommandBar()
		{
			var bar = new FormsCommandBar();
			if (Device.Idiom != TargetIdiom.Phone)
				bar.Style = (Windows.UI.Xaml.Style)Windows.UI.Xaml.Application.Current.Resources["TitleToolbar"];

			_toolbarProvider = new ToolbarProvider(bar);

			if (Device.Idiom == TargetIdiom.Phone)
				_page.BottomAppBar = bar;
			else
				_page.TopAppBar = bar;

			return bar;
		}

		async Task<CommandBar> GetCommandBarAsync()
		{
			IToolbarProvider provider = GetToolbarProvider();
			if (provider == null)
			{
				return null;
			}

			return await provider.GetCommandBarAsync();
		}

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

			StatusBar statusBar = MobileStatusBar;
			if (statusBar != null)
			{
				bool landscape = Device.Info.CurrentOrientation.IsLandscape();
				bool titleBar = CoreApplication.GetCurrentView().TitleBar.IsVisible;
				double offset = landscape ? statusBar.OccludedRect.Width : statusBar.OccludedRect.Height;

				_bounds = new Rectangle(0, 0, _page.ActualWidth - (landscape ? offset : 0), _page.ActualHeight - (landscape ? 0 : offset));

				// Even if the MainPage is a ContentPage not inside of a NavigationPage, the calculated bounds
				// assume the TitleBar is there even if it isn't visible. When UpdatePageSizes is called,
				// _container.ActualWidth is correct because it's aware that the TitleBar isn't there, but the
				// bounds aren't, and things can subsequently run under the StatusBar.
				if (!titleBar)
				{
					_bounds.Width -= (_bounds.Width - _container.ActualWidth);
				}
			}
		}

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

				if (_page.BottomAppBar != null || _page.TopAppBar != null)
				{
					_page.BottomAppBar = null;
					_page.TopAppBar = null;
					_page.InvalidateMeasure();
				}
			}

			var toolBarProvider = GetToolbarProvider() as IToolBarForegroundBinder;

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

				toolBarProvider?.BindForegroundColor(commandBar);

				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)
				{
					toolBarProvider?.BindForegroundColor(button);
					commandBar.PrimaryCommands.Add(button);
				}
				else
				{
					commandBar.SecondaryCommands.Add(button);
				}
			}

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

		internal IToolbarProvider GetToolbarProvider()
		{
			IToolbarProvider provider = null;

			Page element = _currentPage;
			while (element != null)
			{
				provider = GetRenderer(element) as IToolbarProvider;
				if (provider != null)
					break;

				var pageContainer = element as IPageContainer<Page>;
				element = pageContainer?.CurrentPage;
			}

			if (provider != null && _toolbarProvider == null)
				ClearCommandBar();

			return provider;
		}

		class ToolbarProvider : IToolbarProvider
		{
			readonly Task<CommandBar> _commandBar;

			public ToolbarProvider(CommandBar commandBar)
			{
				_commandBar = Task.FromResult(commandBar);
			}

			public CommandBar CommandBar => _commandBar.Result;

			public Task<CommandBar> GetCommandBarAsync()
			{
				return _commandBar;
			}
		}
	}
}