-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathShell.xaml.cs
More file actions
210 lines (188 loc) · 7.55 KB
/
Shell.xaml.cs
File metadata and controls
210 lines (188 loc) · 7.55 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using CommunityToolkit.App.Shared.Renderers;
using CommunityToolkit.Tooling.SampleGen.Metadata;
using CommunityToolkit.App.Shared.Helpers;
using Microsoft.UI.Xaml.Controls;
namespace CommunityToolkit.App.Shared.Pages;
/// <summary>
/// Used to display all Community Toolkit Labs sample projects in one place.
/// </summary>
public sealed partial class Shell : Page
{
public static IEnumerable<ToolkitFrontMatter>? samplePages { get; private set; }
public static Shell? Current { get; private set; }
public Shell()
{
this.InitializeComponent();
#if WINDOWS_WINAPPSDK
if (App.currentWindow is not null)
appTitleBar.Window = App.currentWindow;
#else
BackdropMaterial.SetApplyToRootOrPageBackground(this, true);
#endif
Current = this;
Loaded += Shell_Loaded;
}
/// <summary>
/// Gets the items used for navigating.
/// </summary>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
samplePages = e.Parameter as IEnumerable<ToolkitFrontMatter>;
SetupNavigationMenu();
base.OnNavigatedTo(e);
}
private void Shell_Loaded(object sender, RoutedEventArgs e)
{
searchBox.Focus(FocusState.Programmatic);
}
private void SetupNavigationMenu()
{
if (samplePages is not null)
{
NavView.MenuItems.Add(new MUXC.NavigationViewItem() { Content = "Home", Icon = new SymbolIcon() { Symbol = Symbol.Home }, Tag = "GettingStarted" });
NavView.MenuItems.Add(new MUXC.NavigationViewItemSeparator());
// Populate menu with categories, subcategories and samples
foreach (var item in NavigationViewHelper.GenerateNavItemTree(samplePages))
NavView.MenuItems.Add(item);
NavView.SelectedItem = NavView.MenuItems[0];
NavigationFrame.Navigate(typeof(GettingStartedPage), samplePages);
}
}
private void NavView_ItemInvoked(MUXC.NavigationView sender, MUXC.NavigationViewItemInvokedEventArgs args)
{
var selectedItem = (MUXC.NavigationViewItem)args.InvokedItemContainer;
if (args.IsSettingsInvoked)
{
if (NavigationFrame.CurrentSourcePageType != typeof(SettingsPage))
{
NavigationFrame.Navigate(typeof(SettingsPage));
TrackingManager.TrackPage("Settings");
}
}
// Check if Getting Started page
else if (selectedItem.Tag != null && selectedItem.Tag.GetType() == typeof(string))
{
NavigationFrame.Navigate(typeof(GettingStartedPage), samplePages);
TrackingManager.TrackPage("GettingStarted");
}
else
{
var selectedMetadata = selectedItem.Tag as ToolkitFrontMatter;
if (selectedMetadata is null)
return;
NavigationFrame.Navigate(typeof(ToolkitDocumentationRenderer), selectedMetadata);
TrackingManager.TrackSample(selectedMetadata.Title!);
}
}
public void NavigateToSample(ToolkitFrontMatter? sample)
{
if (sample is null)
{
NavigationFrame.Navigate(typeof(GettingStartedPage), samplePages);
TrackingManager.TrackPage("GettingStarted");
}
else
{
NavigationFrame.Navigate(typeof(ToolkitDocumentationRenderer), sample);
TrackingManager.TrackSample(sample.Title!);
}
EnsureNavigationSelection(sample?.FilePath);
}
private void NavigationFrameOnNavigated(object sender, NavigationEventArgs navigationEventArgs)
{
NavView.IsBackEnabled = NavigationFrame.CanGoBack;
appTitleBar.IsBackButtonVisible = NavigationFrame.CanGoBack;
// Update the NavigationViewControl selection indicator
if (navigationEventArgs.NavigationMode == NavigationMode.Back)
{
if (navigationEventArgs.SourcePageType == typeof(GettingStartedPage))
{
NavView.SelectedItem = NavView.MenuItems[0];
}
else if (navigationEventArgs.SourcePageType == typeof(SettingsPage))
{
NavView.SelectedItem = NavView.SettingsItem;
}
else if (navigationEventArgs.Parameter.GetType() == typeof(ToolkitFrontMatter))
{
EnsureNavigationSelection(((ToolkitFrontMatter)navigationEventArgs.Parameter).FilePath);
}
}
}
public void EnsureNavigationSelection(string? FilePath)
{
foreach (object rawCategory in this.NavView.MenuItems)
{
if (rawCategory is MUXC.NavigationViewItem category)
{
foreach (object rawSubcategory in category.MenuItems)
{
if (rawSubcategory is MUXC.NavigationViewItem subcategory)
{
foreach (object rawSample in subcategory.MenuItems)
{
if (rawSample is MUXC.NavigationViewItem sample)
{
if (sample.Tag != null)
{
if (((ToolkitFrontMatter)sample.Tag).FilePath == FilePath) // TO DO: file path is unique and works for now, but do we need a SampleID of some sorts?
{
category.IsExpanded = true;
subcategory.IsExpanded = true;
NavView.SelectedItem = sample;
sample.IsSelected = true;
return;
}
}
}
}
}
}
}
}
}
private void searchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
if (string.IsNullOrWhiteSpace(searchBox.Text))
{
searchBox.ItemsSource = null;
return;
}
else
{
var query = searchBox.Text.ToLower();
searchBox.ItemsSource = samplePages?.Where(s => s!.Title!.ToLower().Contains(query) || s!.Description!.ToLower().Contains(query) || s!.Keywords!.ToLower().Contains(query) || s!.Category!.ToString().ToLower().Contains(query) || s!.Subcategory!.ToString().ToLower().Contains(query)).ToArray();
return;
}
}
}
private void searchBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
if (args.ChosenSuggestion != null && args.ChosenSuggestion is ToolkitFrontMatter)
{
var selectedSample = args.ChosenSuggestion as ToolkitFrontMatter;
NavigateToSample(selectedSample);
searchBox.Text = string.Empty;
}
else
{
return;
}
}
private void TitleBar_PaneButtonClick(object sender, RoutedEventArgs e)
{
NavView.IsPaneOpen = !NavView.IsPaneOpen;
}
private void TitleBar_BackButtonClick(object sender, RoutedEventArgs e)
{
if (NavigationFrame.CanGoBack)
{
NavigationFrame.GoBack();
}
}
}