-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathShell.SamplePicker.cs
335 lines (274 loc) · 11.7 KB
/
Shell.SamplePicker.cs
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
// 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.SampleApp.Pages;
using Microsoft.Toolkit.Uwp.UI;
using Microsoft.Toolkit.Uwp.UI.Animations;
using Microsoft.Toolkit.Uwp.UI.Controls;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media.Animation;
namespace Microsoft.Toolkit.Uwp.SampleApp
{
public sealed partial class Shell
{
private Sample _currentSample;
private SampleCategory _selectedCategory;
public CollectionViewSource SampleView { get; } = new CollectionViewSource();
private Sample CurrentSample
{
get
{
return _currentSample;
}
set
{
_currentSample = value;
_ = SetNavViewSelectionAsync();
}
}
private async Task SetNavViewSelectionAsync()
{
if (_currentSample != null)
{
var category = await Samples.GetCategoryBySample(_currentSample);
if ((NavView.MenuItemsSource as IEnumerable<SampleCategory>).Contains(category))
{
NavView.SelectedItem = category;
}
}
else
{
NavView.SelectedItem = null;
}
}
private void HideSamplePicker()
{
SamplePickerGrid.Visibility = Visibility.Collapsed;
_selectedCategory = null;
_ = SetNavViewSelectionAsync();
}
private async void ShowSamplePicker(Sample[] samples = null, bool group = false)
{
if (samples == null && _currentSample != null)
{
var category = await Samples.GetCategoryBySample(_currentSample);
if (category != null)
{
samples = category.Samples;
}
}
if (samples == null)
{
samples = (await Samples.GetCategoriesAsync()).FirstOrDefault()?.Samples;
}
if (samples == null)
{
return;
}
if (SamplePickerGrid.Visibility == Windows.UI.Xaml.Visibility.Visible &&
SamplePickerGridView.ItemsSource is Sample[] currentSamples &&
currentSamples.Count() == samples.Count() &&
currentSamples.Except(samples).Count() == 0)
{
return;
}
SamplePickerGridView.ItemsSource = samples;
var groups = samples.GroupBy(sample => sample.Subcategory);
if (group && groups.Count() > 1)
{
SampleView.IsSourceGrouped = true;
SampleView.Source = groups.OrderBy(g => g.Key);
}
else
{
SampleView.IsSourceGrouped = false;
SampleView.Source = samples;
}
SamplePickerGridView.ItemsSource = SampleView.View;
if (_currentSample != null && samples.Contains(_currentSample))
{
SamplePickerGridView.SelectedItem = _currentSample;
}
else
{
SamplePickerGridView.SelectedItem = null;
}
SamplePickerGrid.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
private void NavView_ItemInvoked(Microsoft.UI.Xaml.Controls.NavigationView sender, Microsoft.UI.Xaml.Controls.NavigationViewItemInvokedEventArgs args)
{
if (args.InvokedItem is SampleCategory category)
{
if (SamplePickerGrid.Visibility != Visibility.Collapsed && _selectedCategory == category)
{
// The NavView fires this event twice when the current selected item is clicked
// This makes sure the event get's processed correctly
var nop = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => HideSamplePicker());
}
else
{
_selectedCategory = category;
ShowSamplePicker(category.Samples, true);
// Then Focus on Picker
dispatcherQueue.EnqueueAsync(() => SamplePickerGridView.Focus(FocusState.Keyboard));
}
}
}
private void SettingsTopNavPaneItem_PointerReleased(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
// Can't get FooterMenuItems to work properly right now with ItemInvoked above, bug?
// For now just hard-code an event.
HideSamplePicker();
if (NavigationFrame.CurrentSourcePageType != typeof(About))
{
NavigateToSample(null);
}
}
private void ContentShadow_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
HideSamplePicker();
}
private void MoreInfoCanvas_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
HideMoreInfo();
}
private void SamplePickerGridView_ChoosingItemContainer(ListViewBase sender, ChoosingItemContainerEventArgs args)
{
if (args.ItemContainer != null)
{
return;
}
GridViewItem container = (GridViewItem)args.ItemContainer ?? new GridViewItem();
container.Loaded -= ContainerItem_Loaded;
container.PointerEntered -= ItemContainer_PointerEntered;
container.PointerExited -= ItemContainer_PointerExited;
container.Loaded += ContainerItem_Loaded;
container.PointerEntered += ItemContainer_PointerEntered;
container.PointerExited += ItemContainer_PointerExited;
args.ItemContainer = container;
}
private void ContainerItem_Loaded(object sender, RoutedEventArgs e)
{
var itemsPanel = (ItemsWrapGrid)SamplePickerGridView.ItemsPanelRoot;
var itemContainer = (GridViewItem)sender;
itemContainer.Loaded -= this.ContainerItem_Loaded;
var button = itemContainer.FindDescendant<Button>();
if (button != null)
{
button.Click -= MoreInfoClicked;
button.LostFocus -= MoreInfoLostFocus;
button.Click += MoreInfoClicked;
button.LostFocus += MoreInfoLostFocus;
}
var itemIndex = SamplePickerGridView.IndexFromContainer(itemContainer);
var referenceIndex = itemsPanel.FirstVisibleIndex;
if (SamplePickerGridView.SelectedIndex >= 0)
{
referenceIndex = SamplePickerGridView.SelectedIndex;
}
var relativeIndex = Math.Abs(itemIndex - referenceIndex);
if (itemContainer.Content != CurrentSample && itemIndex >= 0 && itemIndex >= itemsPanel.FirstVisibleIndex && itemIndex <= itemsPanel.LastVisibleIndex)
{
var staggerDelay = TimeSpan.FromMilliseconds(relativeIndex * 30);
VisualExtensions.SetNormalizedCenterPoint(itemContainer, "0.5");
AnimationBuilder.Create()
.Opacity(from: 0, to: 1, delay: staggerDelay)
.Scale(from: 0.9, to: 1, delay: staggerDelay)
.Start(itemContainer);
}
}
private void ItemContainer_PointerExited(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
if ((sender as FrameworkElement)?.FindDescendant<DropShadowPanel>() is FrameworkElement panel)
{
AnimationBuilder.Create().Opacity(0, duration: TimeSpan.FromMilliseconds(1200)).Start(panel);
if (panel.Parent is UIElement parent)
{
AnimationBuilder.Create().Scale(1, duration: TimeSpan.FromMilliseconds(1200)).Start(parent);
}
}
}
private void ItemContainer_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse &&
(sender as FrameworkElement)?.FindDescendant<DropShadowPanel>() is FrameworkElement panel)
{
panel.Visibility = Visibility.Visible;
AnimationBuilder.Create().Opacity(1, duration: TimeSpan.FromMilliseconds(600)).Start(panel);
if (panel.Parent is UIElement parent)
{
AnimationBuilder.Create().Scale(1.1, duration: TimeSpan.FromMilliseconds(600)).Start(parent);
}
}
}
private void MoreInfoClicked(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
var sampleData = button.DataContext as Sample;
var container = button.FindAscendant<GridViewItem>();
if (container == null)
{
return;
}
InitMoreInfoContentContainer(container);
MoreInfoContent.DataContext = sampleData;
if (MoreInfoCanvas.Visibility == Visibility.Visible)
{
HideMoreInfo();
}
else
{
MoreInfoCanvas.Visibility = Visibility.Visible;
}
}
private void MoreInfoLostFocus(object sender, RoutedEventArgs e)
{
HideMoreInfo();
}
private void InitMoreInfoContentContainer(GridViewItem container)
{
if (MoreInfoContent == null)
{
return;
}
var point = container.TransformToVisual(this).TransformPoint(new Windows.Foundation.Point(0, 0));
var x = point.X - ((MoreInfoContent.Width - container.ActualWidth) / 2);
var y = point.Y - ((MoreInfoContent.Height - container.ActualHeight) / 2);
x = Math.Max(x, 10);
x = Math.Min(x, ActualWidth - MoreInfoContent.Width - 10);
y = Math.Max(y, 10);
y = Math.Min(y, ActualHeight - MoreInfoContent.Height - 10);
Canvas.SetLeft(MoreInfoContent, x);
Canvas.SetTop(MoreInfoContent, y);
var centerX = (point.X + (container.ActualWidth / 2)) - x;
var centerY = (point.Y + (container.ActualHeight / 2)) - y;
VisualExtensions.SetCenterPoint(MoreInfoContent, new Vector3((float)centerX, (float)centerY, 0).ToString());
}
private void HideMoreInfo()
{
if (MoreInfoImage != null && MoreInfoContent.DataContext != null)
{
ConnectedAnimationService.GetForCurrentView().PrepareToAnimate("sample_icon", MoreInfoImage);
}
MoreInfoCanvas.Visibility = Visibility.Collapsed;
if (MoreInfoImage != null && MoreInfoContent.DataContext != null)
{
var animation = ConnectedAnimationService.GetForCurrentView().GetAnimation("sample_icon");
animation.Configuration = new DirectConnectedAnimationConfiguration();
_ = SamplePickerGridView.TryStartConnectedAnimationAsync(animation, MoreInfoContent.DataContext, "SampleIcon");
}
MoreInfoContent.DataContext = null;
}
private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
{
HideMoreInfo();
}
}
}