summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/CoreGallery.cs
blob: 26e9a5905d2abe087d21eea048939e521a56c2d7 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms.Controls.GalleryPages;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls
{
	public static class Messages
	{
		public const string ChangeRoot = "com.xamarin.ChangeRoot";
	}

	internal class CoreCarouselPage : CarouselPage
	{
		public CoreCarouselPage ()
		{
			AutomationId = "CarouselPageRoot";
			Children.Add (new CoreRootPage (this, NavigationBehavior.PushModalAsync) { Title = "Page 1" });
			Children.Add (new CoreRootPage (this, NavigationBehavior.PushModalAsync) { Title = "Page 2" });
		}
	}

	internal class CoreContentPage : ContentPage
	{
		public CoreContentPage ()
		{
			AutomationId = "ContentPageRoot";
			Content = new StackLayout { Children = { new CoreRootView (), new CorePageView (this, NavigationBehavior.PushModalAsync) } };
		}
	}

	internal class CoreMasterDetailPage : MasterDetailPage
	{
		public CoreMasterDetailPage ()
		{
			AutomationId = "MasterDetailPageRoot";

			var toCrashButton = new Button {Text = "Crash Me"};

			var masterPage = new ContentPage {Title = "Menu", Icon = "bank.png", Content = toCrashButton};
			var detailPage = new CoreRootPage (this, NavigationBehavior.PushModalAsync) { Title = "DetailPage" };

			bool toggle = false;
			toCrashButton.Clicked += (sender, args) => {
				if (toggle)
					Detail = new ContentPage { BackgroundColor = Color.Green, };
				else
					Detail = detailPage;

				toggle = !toggle;
			};

			Master = masterPage;
			Detail = detailPage;
		}
	}

	internal class CoreNavigationPage : NavigationPage
	{
		public CoreNavigationPage ()
		{
			AutomationId = "NavigationPageRoot";

			BarBackgroundColor = Color.Maroon;
			BarTextColor = Color.Yellow;

			Device.StartTimer(TimeSpan.FromSeconds(2), () => {
				BarBackgroundColor = Color.Default;
				BarTextColor = Color.Default;

				return false;
			});

			Navigation.PushAsync (new CoreRootPage (this));
		}
	}

	[Preserve (AllMembers = true)]
	[Issue (IssueTracker.Github, 2456, "StackOverflow after reordering tabs in a TabbedPageView", PlatformAffected.All)]
	public class CoreTabbedPage : TestTabbedPage
	{
		protected override void Init ()
		{
		}
#if APP
		public CoreTabbedPage ()
		{
			AutomationId = "TabbedPageRoot";


			Device.StartTimer(TimeSpan.FromSeconds(6), () => {
				BarBackgroundColor = Color.Maroon;
				BarTextColor = Color.Yellow;

				Device.StartTimer(TimeSpan.FromSeconds(6), () => {
					BarBackgroundColor = Color.Default;
					BarTextColor = Color.Default;

					return false;
				});

				return false;
			});

			Children.Add(new CoreRootPage(this, NavigationBehavior.PushModalAsync) { Title = "Tab 1" });
			Children.Add(new CoreRootPage(this, NavigationBehavior.PushModalAsync) { Title = "Tab 2" });
			Children.Add(new NavigationPage(new Page())
				{
					Title = "Rubriques",
					Icon = "coffee.png",
					BarBackgroundColor = Color.Blue,
					BarTextColor = Color.Aqua
				});

			Children.Add(new NavigationPage(new Page())
				{
					Title = "Le Club"
				});

			Children.Add(new NavigationPage(new Page { Title = "Bookmarks" })
				{
					Title = "Bookmarks",
				});

			Children.Add(new NavigationPage(new Page { Title = "Alertes" })
				{
					Title = "Notifications",  
				});

			Children.Add(new NavigationPage(new Page { Title = "My account" })
				{
					Title = "My account",
				});

			Children.Add(new NavigationPage(new Page { Title = "About" })
				{
					Title = "About",
				});
		}
#endif

#if UITest
		[Test]
		[Issue (IssueTracker.Github, 2456, "StackOverflow after reordering tabs in a TabbedPageView", PlatformAffected.iOS)]
		public void TestReorderTabs ()
		{
			App.Tap (c => c.Marked("More"));
			App.Tap (c => c.Marked("Edit"));
			var bookmarks = App.Query (c => c.Marked ("Bookmarks"))[0];
			var notifications = App.Query (c => c.Marked ("Notifications"))[0];
			var tab2 = App.Query (c => c.Marked ("Tab 2"))[2];
			var rubriques = App.Query (c => c.Marked ("Rubriques"))[2];
			App.DragCoordinates (bookmarks.Rect.CenterX, bookmarks.Rect.CenterY, rubriques.Rect.CenterX, rubriques.Rect.CenterY);
			App.DragCoordinates (notifications.Rect.CenterX, notifications.Rect.CenterY, tab2.Rect.CenterX, tab2.Rect.CenterY);
			App.Tap (c => c.Marked("Done"));
			App.Tap (c => c.Marked("Tab 1"));
			App.Tap (c => c.Marked("Le Club"));
			App.Tap (c => c.Marked("Bookmarks"));
			App.Tap (c => c.Marked("Notifications"));
		}
#endif
	}

	[Preserve (AllMembers = true)]
	internal class CoreViewContainer
	{
		public string Name { get; private set; }
		public Type PageType { get; private set; }

		public CoreViewContainer (string name, Type pageType)
		{
			Name = name;
			PageType = pageType;
		}
	}

	public class CoreRootView : ListView
	{
		public CoreRootView ()
		{
			var roots = new [] {
				new CoreViewContainer ("SwapRoot - CarouselPage", typeof(CoreCarouselPage)), 
				new CoreViewContainer ("SwapRoot - ContentPage", typeof(CoreContentPage)),
				new CoreViewContainer ("SwapRoot - MasterDetailPage", typeof(CoreMasterDetailPage)),
				new CoreViewContainer ("SwapRoot - NavigationPage", typeof(CoreNavigationPage)),
				new CoreViewContainer ("SwapRoot - TabbedPage", typeof(CoreTabbedPage)),
			};

			var template = new DataTemplate (typeof(TextCell));
			template.SetBinding (TextCell.TextProperty, "Name");

			ItemTemplate = template;
			ItemsSource = roots;

#if PRE_APPLICATION_CLASS
			ItemSelected += (sender, args) => MessagingCenter.Send (this, Messages.ChangeRoot, ((CoreViewContainer)args.SelectedItem).PageType);
#else			
			ItemSelected += (sender, args) => {
				var app = Application.Current as App;
				if (app != null) {
					var page = (Page)Activator.CreateInstance (((CoreViewContainer)args.SelectedItem).PageType);
					app.SetMainPage (page);
				}		
			};
#endif
			SetValue(AutomationProperties.NameProperty, "SwapRoot");
		}
	}

	

	internal class CorePageView : ListView
	{
		internal class GalleryPageFactory
		{
			public GalleryPageFactory(Func<Page> create, string title)
			{
				Realize = () =>
				{
					var p = create();
					p.Title = title;
					return p;
				};
			
				Title = title;
			}

			public Func<Page> Realize { get; set; }
			public string Title { get; set; }

			public override string ToString()
			{	
				// a11y: let Narrator read a friendly string instead of the default ToString()
				return Title;
			}
		}

		List<GalleryPageFactory> _pages = new List<GalleryPageFactory> {
				new GalleryPageFactory(() => new AutomationPropertiesGallery(), "Accessibility"),
				new GalleryPageFactory(() => new PlatformSpecificsGallery(), "Platform Specifics"),
				new GalleryPageFactory(() => new NativeBindingGalleryPage(), "Native Binding Controls Gallery"),
				new GalleryPageFactory(() => new XamlNativeViews(), "Xaml Native Views Gallery"),
				new GalleryPageFactory(() => new AppLinkPageGallery(), "App Link Page Gallery"),
				new GalleryPageFactory(() => new NestedNativeControlGalleryPage(), "Nested Native Controls Gallery"),
				new GalleryPageFactory(() => new CellForceUpdateSizeGalleryPage(), "Cell Force Update Size Gallery"),
				new GalleryPageFactory(() => new AppearingGalleryPage(), "Appearing Gallery"),
				new GalleryPageFactory(() => new EntryCoreGalleryPage(), "Entry Gallery"),
				new GalleryPageFactory(() => new NavBarTitleTestPage(), "Titles And Navbar Windows"),
				new GalleryPageFactory(() => new PanGestureGalleryPage(), "Pan gesture Gallery"),
				new GalleryPageFactory(() => new PinchGestureTestPage(), "Pinch gesture Gallery"),
				new GalleryPageFactory(() => new AutomationIdGallery(), "AutomationID Gallery"),
				new GalleryPageFactory(() => new LayoutPerformanceGallery(), "Layout Perf Gallery"),
				new GalleryPageFactory(() => new ListViewSelectionColor(), "ListView SelectionColor Gallery"),
				new GalleryPageFactory(() => new AlertGallery(), "DisplayAlert Gallery"),
				new GalleryPageFactory(() => new ToolbarItems(), "ToolbarItems Gallery"),
				new GalleryPageFactory(() => new ActionSheetGallery(), "ActionSheet Gallery"),
				new GalleryPageFactory(() => new ActivityIndicatorCoreGalleryPage(), "ActivityIndicator Gallery"),
				new GalleryPageFactory(() => new BehaviorsAndTriggers(), "BehaviorsTriggers Gallery"),
				new GalleryPageFactory(() => new ContextActionsGallery(), "ContextActions List Gallery"),
				new GalleryPageFactory(() => new ContextActionsGallery (tableView: true), "ContextActions Table Gallery"),
				new GalleryPageFactory(() => new CoreBoxViewGalleryPage(), "BoxView Gallery"),
				new GalleryPageFactory(() => new ButtonCoreGalleryPage(), "Button Gallery"),
				new GalleryPageFactory(() => new DatePickerCoreGalleryPage(), "DatePicker Gallery"),
				new GalleryPageFactory(() => new EditorCoreGalleryPage(), "Editor Gallery"),
				new GalleryPageFactory(() => new FrameCoreGalleryPage(), "Frame Gallery"),
				new GalleryPageFactory(() => new ImageCoreGalleryPage(), "Image Gallery"),
				new GalleryPageFactory(() => new KeyboardCoreGallery(), "Keyboard Gallery"),
				new GalleryPageFactory(() => new LabelCoreGalleryPage(), "Label Gallery"),
				new GalleryPageFactory(() => new ListViewCoreGalleryPage(), "ListView Gallery"),
				new GalleryPageFactory(() => new OpenGLViewCoreGalleryPage(), "OpenGLView Gallery"),
				new GalleryPageFactory(() => new PickerCoreGalleryPage(), "Picker Gallery"),
				new GalleryPageFactory(() => new ProgressBarCoreGalleryPage(), "ProgressBar Gallery"),
				new GalleryPageFactory(() => new ScrollGallery(), "ScrollView Gallery"),
				new GalleryPageFactory(() => new ScrollGallery(ScrollOrientation.Horizontal), "ScrollView Gallery Horizontal"),
				new GalleryPageFactory(() => new ScrollGallery(ScrollOrientation.Both), "ScrollView Gallery 2D"),
				new GalleryPageFactory(() => new SearchBarCoreGalleryPage(), "SearchBar Gallery"),
				new GalleryPageFactory(() => new SliderCoreGalleryPage(), "Slider Gallery"),
				new GalleryPageFactory(() => new StepperCoreGalleryPage(), "Stepper Gallery"),
				new GalleryPageFactory(() => new SwitchCoreGalleryPage(), "Switch Gallery"),
				new GalleryPageFactory(() => new TableViewCoreGalleryPage(), "TableView Gallery"),
				new GalleryPageFactory(() => new TimePickerCoreGalleryPage(), "TimePicker Gallery"),
				new GalleryPageFactory(() => new WebViewCoreGalleryPage(), "WebView Gallery"),
				//pages
 				new GalleryPageFactory(() => new RootContentPage ("Content"), "RootPages Gallery"),
				new GalleryPageFactory(() => new MasterDetailPageTabletPage(), "MasterDetailPage Tablet Page"),
				// legacy galleries
				new GalleryPageFactory(() => new AbsoluteLayoutGallery(), "AbsoluteLayout Gallery - Legacy"),
				new GalleryPageFactory(() => new BoundContentPage(), "BoundPage Gallery - Legacy"),
				new GalleryPageFactory(() => new BackgroundImageGallery(), "BackgroundImage gallery"),
				new GalleryPageFactory(() => new ButtonGallery(), "Button Gallery - Legacy"),
				new GalleryPageFactory(() => new CarouselPageGallery(), "CarouselPage Gallery - Legacy"),
				new GalleryPageFactory(() => new CellTypesListPage(), "Cells Gallery - Legacy"),
				new GalleryPageFactory(() => new ClipToBoundsGallery(), "ClipToBounds Gallery - Legacy"),
				new GalleryPageFactory(() => new ControlTemplatePage(), "ControlTemplated Gallery - Legacy"),
				new GalleryPageFactory(() => new ControlTemplateXamlPage(), "ControlTemplated XAML Gallery - Legacy"),
				new GalleryPageFactory(() => new DisposeGallery(), "Dispose Gallery - Legacy"),
				new GalleryPageFactory(() => new EditorGallery(), "Editor Gallery - Legacy"),
				new GalleryPageFactory(() => new EntryGallery(), "Entry Gallery - Legacy"),
				new GalleryPageFactory(() => new FrameGallery (), "Frame Gallery - Legacy"),
				new GalleryPageFactory(() => new GridGallery(), "Grid Gallery - Legacy"),
				new GalleryPageFactory(() => new GroupedListActionsGallery(), "GroupedListActions Gallery - Legacy"),
				new GalleryPageFactory(() => new GroupedListContactsGallery(), "GroupedList Gallery - Legacy"),
				new GalleryPageFactory(() => new ImageGallery (), "Image Gallery - Legacy"),
				new GalleryPageFactory(() => new ImageLoadingGallery (), "ImageLoading Gallery - Legacy"),
				new GalleryPageFactory(() => new InputIntentGallery(), "InputIntent Gallery - Legacy"),
				new GalleryPageFactory(() => new LabelGallery(), "Label Gallery - Legacy"),
				new GalleryPageFactory(() => new LayoutAddPerformance(), "Layout Add Performance - Legacy"),
				new GalleryPageFactory(() => new LayoutOptionsGallery(), "LayoutOptions Gallery - Legacy"),
				new GalleryPageFactory(() => new LineBreakModeGallery(), "LineBreakMode Gallery - Legacy"),
				new GalleryPageFactory(() => new ListPage(), "ListView Gallery - Legacy"),
				new GalleryPageFactory(() => new ListScrollTo(), "ListView.ScrollTo"),
				new GalleryPageFactory(() => new ListRefresh(), "ListView.PullToRefresh"),
				new GalleryPageFactory(() => new ListViewDemoPage(), "ListView Demo Gallery - Legacy"),
				new GalleryPageFactory(() => new MapGallery(), "Map Gallery - Legacy"),
				new GalleryPageFactory(() => new MinimumSizeGallery(), "MinimumSize Gallery - Legacy"),
				new GalleryPageFactory(() => new MultiGallery(), "Multi Gallery - Legacy"),
				new GalleryPageFactory(() => new NavigationMenuGallery(), "NavigationMenu Gallery - Legacy"),
				new GalleryPageFactory(() => new NavigationPropertiesGallery(), "Navigation Properties"),
#if HAVE_OPENTK
				new GalleryPageFactory(() => new OpenGLGallery(), "OpenGLGallery - Legacy"),
#endif
				new GalleryPageFactory(() => new PickerGallery(), "Picker Gallery - Legacy"),
				new GalleryPageFactory(() => new ProgressBarGallery(), "ProgressBar Gallery - Legacy"),
				new GalleryPageFactory(() => new RelativeLayoutGallery(), "RelativeLayout Gallery - Legacy"),
				new GalleryPageFactory(() => new ScaleRotate(), "Scale Rotate Gallery - Legacy"),
				new GalleryPageFactory(() => new SearchBarGallery(), "SearchBar Gallery - Legacy"),
				new GalleryPageFactory(() => new SettingsPage(), "Settings Page - Legacy"),
				new GalleryPageFactory(() => new SliderGallery(), "Slider Gallery - Legacy"),
				new GalleryPageFactory(() => new StackLayoutGallery(), "StackLayout Gallery - Legacy"),
				new GalleryPageFactory(() => new StepperGallery(), "Stepper Gallery - Legacy"),
				new GalleryPageFactory(() => new StyleGallery(), "Style Gallery"),
				new GalleryPageFactory(() => new StyleXamlGallery(), "Style Gallery in Xaml"),
				new GalleryPageFactory(() => new SwitchGallery(), "Switch Gallery - Legacy"),
				new GalleryPageFactory(() => new TableViewGallery(), "TableView Gallery - Legacy"),
				new GalleryPageFactory(() => new TemplatedCarouselGallery(), "TemplatedCarouselPage Gallery - Legacy"),
				new GalleryPageFactory(() => new TemplatedTabbedGallery(), "TemplatedTabbedPage Gallery - Legacy"),
				 new GalleryPageFactory(() => new UnevenViewCellGallery(), "UnevenViewCell Gallery - Legacy"),
				new GalleryPageFactory(() => new UnevenListGallery(), "UnevenList Gallery - Legacy"),
				new GalleryPageFactory(() => new ViewCellGallery(), "ViewCell Gallery - Legacy"),
				new GalleryPageFactory(() => new WebViewGallery(), "WebView Gallery - Legacy"),
			};

		public CorePageView (Page rootPage, NavigationBehavior navigationBehavior = NavigationBehavior.PushAsync)
		{
			_titleToPage = _pages.ToDictionary (o => o.Title);

			// avoid NRE for root pages without NavigationBar
			if (navigationBehavior == NavigationBehavior.PushAsync && rootPage.GetType () == typeof (CoreNavigationPage)) {
				_pages.Add (new GalleryPageFactory(() => new NavigationBarGallery((NavigationPage)rootPage), "NavigationBar Gallery - Legacy"));
			}

			var template = new DataTemplate (typeof(TextCell));
			template.SetBinding (TextCell.TextProperty, "Title");

			BindingContext = _pages;
			ItemTemplate = template;
			ItemsSource = _pages;

			ItemSelected += async (sender, args) => {
				if (SelectedItem == null)
					return;

				var item = args.SelectedItem;
				var page = item as GalleryPageFactory;
				if (page != null)
					await PushPage (page.Realize());

				SelectedItem = null;
			};

			SetValue(AutomationProperties.NameProperty, "Core Pages");
		}

		NavigationBehavior navigationBehavior;

		async Task PushPage (Page contentPage)
		{
			if (Insights.IsInitialized) {
				Insights.Track ("Navigation", new Dictionary<string, string> {
					{ "Pushing", contentPage.GetType().Name }
				});
			}

			if (navigationBehavior == NavigationBehavior.PushModalAsync) {
				await Navigation.PushModalAsync (contentPage);
			} else {
				await Navigation.PushAsync (contentPage);
			}
		}

		readonly Dictionary<string, GalleryPageFactory> _titleToPage;
		public async Task PushPage (string pageTitle)
		{

			GalleryPageFactory pageFactory = null;
			if (!_titleToPage.TryGetValue (pageTitle, out pageFactory))
				return;

			var page = pageFactory.Realize();

			if (Insights.IsInitialized) {
				Insights.Track ("Navigation", new Dictionary<string, string> {
					{ "Pushing", page.GetType().Name }
				});
			}

			await PushPage (page);
		}
	}

	internal class CoreRootPage : ContentPage
	{
		public CoreRootPage (Page rootPage, NavigationBehavior navigationBehavior = NavigationBehavior.PushAsync)
		{
			IStringProvider stringProvider = DependencyService.Get<IStringProvider> ();

			Title = stringProvider.CoreGalleryTitle;

			var corePageView = new CorePageView (rootPage, navigationBehavior);

			var searchBar = new SearchBar () {
				AutomationId = "SearchBar"
			};

			var testCasesButton = new Button {
				Text = "Go to Test Cases",
				AutomationId = "GoToTestButton",
				Command = new Command (async () => {
					if (!string.IsNullOrEmpty (searchBar.Text))
						await corePageView.PushPage (searchBar.Text);
					else
						await Navigation.PushModalAsync (TestCases.GetTestCases ());
				})
			};

			var stackLayout = new StackLayout () { 
				Children = {
					testCasesButton,
					searchBar,
					new Button {
						Text = "Click to Force GC", 
						Command = new Command(() => {
							GC.Collect ();
							GC.WaitForPendingFinalizers ();
							GC.Collect ();
						})
					}

				}
			};

			Content = new AbsoluteLayout {
				Children = {
					{ new CoreRootView (), new Rectangle(0, 0.0, 1, 0.35), AbsoluteLayoutFlags.All },
					{ stackLayout, new Rectangle(0, 0.5, 1, 0.30), AbsoluteLayoutFlags.All },
					{ corePageView, new Rectangle(0, 1.0, 1.0, 0.35), AbsoluteLayoutFlags.All },
				}
			};
		}
	}

	public interface IStringProvider
	{
		string CoreGalleryTitle { get; }
	}

	public static class CoreGallery
	{
		public static Page GetMainPage ()
		{
			return new CoreNavigationPage ();
		}
	}
}