-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathNavMenuListView.cs
More file actions
280 lines (250 loc) · 10.8 KB
/
NavMenuListView.cs
File metadata and controls
280 lines (250 loc) · 10.8 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
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
//-----------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------------------------------------------------------------
using System;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
namespace PhotoSharingApp.Universal.Controls
{
/// <summary>
/// A specialized ListView to represent the items in the navigation menu.
/// </summary>
/// <remarks>
/// This class handles the following:
/// 1. Sizes the panel that hosts the items so they fit in the hosting pane. Otherwise, the keyboard
/// may appear cut off on one side b/c the Pane clips instead of affecting layout.
/// 2. Provides a single selection experience where keyboard focus can move without changing selection.
/// Both the 'Space' and 'Enter' keys will trigger selection. The up/down arrow keys can move
/// keyboard focus without triggering selection. This is different than the default behavior when
/// SelectionMode == Single. The default behavior for a ListView in single selection requires using
/// the Ctrl + arrow key to move keyboard focus without triggering selection. Users won't expect
/// this type of keyboarding model on the nav menu.
/// </remarks>
public class NavMenuListView : ListView
{
private SplitView _splitViewHost;
public NavMenuListView()
{
SelectionMode = ListViewSelectionMode.Single;
IsItemClickEnabled = true;
ItemClick += ItemClickedHandler;
Loaded += (s, a) =>
{
// Locate the hosting SplitView control
var parent = VisualTreeHelper.GetParent(this);
while (parent != null && !(parent is SplitView))
{
parent = VisualTreeHelper.GetParent(parent);
}
if (parent != null)
{
_splitViewHost = parent as SplitView;
_splitViewHost.RegisterPropertyChangedCallback(SplitView.IsPaneOpenProperty,
(sender, args) => { OnPaneToggled(); });
// Call once to ensure we're in the correct state
OnPaneToggled();
}
};
}
private void InvokeItem(object focusedItem)
{
SetSelectedItem(focusedItem as ListViewItem);
ItemInvoked?.Invoke(this, focusedItem as ListViewItem);
if (_splitViewHost.IsPaneOpen && (
_splitViewHost.DisplayMode == SplitViewDisplayMode.CompactOverlay ||
_splitViewHost.DisplayMode == SplitViewDisplayMode.Overlay))
{
_splitViewHost.IsPaneOpen = false;
if (focusedItem is ListViewItem)
{
((ListViewItem)focusedItem).Focus(FocusState.Programmatic);
}
}
}
private void ItemClickedHandler(object sender, ItemClickEventArgs e)
{
// Triggered when the item is selected using
// something other than a keyboard
var item = ContainerFromItem(e.ClickedItem);
InvokeItem(item);
}
/// <summary>
/// Occurs when an item has been selected
/// </summary>
public event EventHandler<ListViewItem> ItemInvoked;
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
// Remove the entrance animation on the item containers.
for (var i = 0; i < ItemContainerTransitions.Count; i++)
{
if (ItemContainerTransitions[i] is EntranceThemeTransition)
{
ItemContainerTransitions.RemoveAt(i);
}
}
}
/// <summary>
/// Custom keyboarding logic to enable movement via the arrow keys without triggering selection
/// until a 'Space' or 'Enter' key is pressed.
/// </summary>
/// <param name="e">The event arguments.</param>
protected override void OnKeyDown(KeyRoutedEventArgs e)
{
var focusedItem = FocusManager.GetFocusedElement();
switch (e.Key)
{
case VirtualKey.Up:
TryMoveFocus(FocusNavigationDirection.Up);
e.Handled = true;
break;
case VirtualKey.Down:
TryMoveFocus(FocusNavigationDirection.Down);
e.Handled = true;
break;
case VirtualKey.Tab:
var shiftKeyState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Shift);
var shiftKeyDown = (shiftKeyState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down;
// If we're on the header item then this will be null and
// we'll still get the default behavior.
if (focusedItem is ListViewItem)
{
var currentItem = (ListViewItem)focusedItem;
var onlastitem = currentItem != null && IndexFromContainer(currentItem) == Items.Count - 1;
var onfirstitem = currentItem != null && IndexFromContainer(currentItem) == 0;
if (!shiftKeyDown)
{
if (onlastitem)
{
TryMoveFocus(FocusNavigationDirection.Next);
}
else
{
TryMoveFocus(FocusNavigationDirection.Down);
}
}
else // Shift + Tab
{
if (onfirstitem)
{
TryMoveFocus(FocusNavigationDirection.Previous);
}
else
{
TryMoveFocus(FocusNavigationDirection.Up);
}
}
}
else if (focusedItem is Control)
{
if (!shiftKeyDown)
{
TryMoveFocus(FocusNavigationDirection.Down);
}
else // Shift + Tab
{
TryMoveFocus(FocusNavigationDirection.Up);
}
}
e.Handled = true;
break;
case VirtualKey.Space:
case VirtualKey.Enter:
// Fire our event using the item with current keyboard focus
InvokeItem(focusedItem);
e.Handled = true;
break;
default:
base.OnKeyDown(e);
break;
}
}
/// <summary>
/// Re-size the ListView's Panel when the SplitView is compact so the items
/// will fit within the visible space and correctly display a keyboard focus rect.
/// </summary>
private void OnPaneToggled()
{
if (_splitViewHost.IsPaneOpen)
{
ItemsPanelRoot.ClearValue(WidthProperty);
ItemsPanelRoot.ClearValue(HorizontalAlignmentProperty);
}
else if (_splitViewHost.DisplayMode == SplitViewDisplayMode.CompactInline ||
_splitViewHost.DisplayMode == SplitViewDisplayMode.CompactOverlay)
{
ItemsPanelRoot.SetValue(WidthProperty, _splitViewHost.CompactPaneLength);
ItemsPanelRoot.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Left);
}
}
/// <summary>
/// Mark the <paramref name="item" /> as selected and ensures everything else is not.
/// If the <paramref name="item" /> is null then everything is unselected.
/// </summary>
/// <param name="item">The item to select.</param>
public void SetSelectedItem(ListViewItem item)
{
var index = -1;
if (item != null)
{
index = IndexFromContainer(item);
}
for (var i = 0; i < Items.Count; i++)
{
var listViewItem = (ListViewItem)ContainerFromIndex(i);
if (listViewItem != null)
{
if (i != index)
{
listViewItem.IsSelected = false;
}
else if (i == index)
{
listViewItem.IsSelected = true;
}
}
}
}
/// <summary>
/// This method is a work-around until the bug in FocusManager.TryMoveFocus is fixed.
/// </summary>
/// <param name="direction">The focus navigation direction.</param>
private void TryMoveFocus(FocusNavigationDirection direction)
{
if (direction == FocusNavigationDirection.Next || direction == FocusNavigationDirection.Previous)
{
FocusManager.TryMoveFocus(direction);
}
else
{
var control = FocusManager.FindNextFocusableElement(direction) as Control;
control?.Focus(FocusState.Programmatic);
}
}
}
}