-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathNavigationViewItemPresenter.cs
More file actions
282 lines (231 loc) · 11 KB
/
NavigationViewItemPresenter.cs
File metadata and controls
282 lines (231 loc) · 11 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
281
282
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using iNKORE.UI.WPF.Helpers;
using iNKORE.UI.WPF.Modern.Common;
using iNKORE.UI.WPF.Modern.Controls.Helpers;
using iNKORE.UI.WPF.Modern.Helpers;
using iNKORE.UI.WPF.Modern.Input;
using static CppWinRTHelpers;
using ControlHelper = iNKORE.UI.WPF.Modern.Controls.Helpers.ControlHelper;
namespace iNKORE.UI.WPF.Modern.Controls.Primitives
{
public class NavigationViewItemPresenter : ContentControl, IControlProtected
{
const string c_contentGrid = "PresenterContentRootGrid";
const string c_expandCollapseChevron = "ExpandCollapseChevron";
const string c_expandCollapseRotateExpandedStoryboard = "ExpandCollapseRotateExpandedStoryboard";
const string c_expandCollapseRotateCollapsedStoryboard = "ExpandCollapseRotateCollapsedStoryboard";
const string c_expandCollapseRotateTransform = "ExpandCollapseChevronRotateTransform";
const string c_iconBoxColumnDefinitionName = "IconColumn";
static NavigationViewItemPresenter()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(NavigationViewItemPresenter),
new FrameworkPropertyMetadata(typeof(NavigationViewItemPresenter)));
HorizontalContentAlignmentProperty.OverrideMetadata(
typeof(NavigationViewItemPresenter),
new FrameworkPropertyMetadata(HorizontalAlignment.Center));
VerticalContentAlignmentProperty.OverrideMetadata(
typeof(NavigationViewItemPresenter),
new FrameworkPropertyMetadata(VerticalAlignment.Center));
}
public NavigationViewItemPresenter()
{
InputHelper.SetIsTapEnabled(this, true);
}
#region Icon
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register(
nameof(Icon),
typeof(object),
typeof(NavigationViewItemPresenter),
null);
public object Icon
{
get => (object)GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
#endregion
#region InfoBadge
public static readonly DependencyProperty InfoBadgeProperty =
DependencyProperty.Register(
nameof(InfoBadge),
typeof(UIElement),
typeof(NavigationViewItemPresenter),
null);
public UIElement InfoBadge
{
get => (UIElement)GetValue(InfoBadgeProperty);
set => SetValue(InfoBadgeProperty, value);
}
#endregion
#region UseSystemFocusVisuals
public static readonly DependencyProperty UseSystemFocusVisualsProperty =
FocusVisualHelper.UseSystemFocusVisualsProperty.AddOwner(typeof(NavigationViewItemPresenter));
public bool UseSystemFocusVisuals
{
get => (bool)GetValue(UseSystemFocusVisualsProperty);
set => SetValue(UseSystemFocusVisualsProperty, value);
}
#endregion
#region CornerRadius
public static readonly DependencyProperty CornerRadiusProperty =
ControlHelper.CornerRadiusProperty.AddOwner(typeof(NavigationViewItemPresenter));
public CornerRadius CornerRadius
{
get => (CornerRadius)GetValue(CornerRadiusProperty);
set => SetValue(CornerRadiusProperty, value);
}
#endregion
public override void OnApplyTemplate()
{
IControlProtected controlProtected = this;
// Retrieve pointers to stable controls
m_helper.Init(this);
if (GetTemplateChildT<Grid>(c_contentGrid, this) is { } contentGrid)
{
m_contentGrid = contentGrid;
}
if (GetNavigationViewItem() is { } navigationViewItem)
{
if (GetTemplateChildT<Grid>(c_expandCollapseChevron, this) is { } expandCollapseChevron)
{
m_expandCollapseChevron = expandCollapseChevron;
InputHelper.SetIsTapEnabled(expandCollapseChevron, true);
InputHelper.SetStopRouting(expandCollapseChevron, true);
InputHelper.AddTappedHandler(expandCollapseChevron, navigationViewItem.OnExpandCollapseChevronTapped);
}
navigationViewItem.UpdateVisualStateNoTransition();
navigationViewItem.UpdateIsClosedCompact();
// We probably switched displaymode, so restore width now, otherwise the next time we will restore is when the CompactPaneLength changes
if (navigationViewItem.GetNavigationView() is { } navigationView)
{
if (navigationView.PaneDisplayMode != NavigationViewPaneDisplayMode.Top)
{
UpdateCompactPaneLength(m_compactPaneLengthValue, true);
}
}
}
//m_chevronExpandedStoryboard = GetTemplateChildT<Storyboard>(c_expandCollapseRotateExpandedStoryboard, this);
//m_chevronCollapsedStoryboard = GetTemplateChildT<Storyboard>(c_expandCollapseRotateCollapsedStoryboard, this);
if (this.GetTemplateRoot() is FrameworkElement templateRoot)
{
m_chevronExpandedStoryboard = templateRoot.Resources[c_expandCollapseRotateExpandedStoryboard] as Storyboard;
m_chevronCollapsedStoryboard = templateRoot.Resources[c_expandCollapseRotateCollapsedStoryboard] as Storyboard;
}
m_expandCollapseRotateTransform = GetTemplateChildT<RotateTransform>(c_expandCollapseRotateTransform, this);
UpdateMargin();
}
internal void RotateExpandCollapseChevron(bool isExpanded)
{
if (isExpanded)
{
if (m_chevronExpandedStoryboard is { } openStoryboard)
{
openStoryboard.Begin();
}
if (m_expandCollapseRotateTransform != null)
{
m_expandCollapseRotateTransform.Angle = 180;
}
}
else
{
if (m_chevronCollapsedStoryboard is { } closedStoryboard)
{
closedStoryboard.Begin();
}
if (m_expandCollapseRotateTransform != null)
{
m_expandCollapseRotateTransform.Angle = 0;
}
}
}
internal UIElement GetSelectionIndicator()
{
return m_helper.GetSelectionIndicator();
}
// TODO: WPF - GoToElementStateCore
/*
bool GoToElementStateCore(string state, bool useTransitions)
{
// GoToElementStateCore: Update visualstate for itself.
// VisualStateManager.GoToState: update visualstate for it's first child.
// If NavigationViewItemPresenter is used, two sets of VisualStateGroups are supported. One set is help to switch the style and it's NavigationViewItemPresenter itself and defined in NavigationViewItem
// Another set is defined in style for NavigationViewItemPresenter.
// OnLeftNavigation, OnTopNavigationPrimary, OnTopNavigationOverflow only apply to itself.
if (state == c_OnLeftNavigation || state == c_OnLeftNavigationReveal || state == c_OnTopNavigationPrimary
|| state == c_OnTopNavigationPrimaryReveal || state == c_OnTopNavigationOverflow)
{
return base.GoToElementStateCore(state, useTransitions);
}
return VisualStateManager.GoToState(this, state, useTransitions);
}
*/
NavigationViewItem GetNavigationViewItem()
{
NavigationViewItem navigationViewItem = null;
// winrt::DependencyObject obj = operator winrt::DependencyObject();
DependencyObject obj = this;
if (SharedHelpers.GetAncestorOfType<NavigationViewItem>(VisualTreeHelper.GetParent(obj)) is { } item)
{
navigationViewItem = item;
}
return navigationViewItem;
}
internal void UpdateContentLeftIndentation(double leftIndentation)
{
m_leftIndentation = leftIndentation;
UpdateMargin();
}
void UpdateMargin()
{
if (m_contentGrid is { } grid)
{
var oldGridMargin = grid.Margin;
grid.Margin = new Thickness(m_leftIndentation, oldGridMargin.Top, oldGridMargin.Right, oldGridMargin.Bottom);
}
}
internal void UpdateCompactPaneLength(double compactPaneLength, bool shouldUpdate)
{
m_compactPaneLengthValue = compactPaneLength;
if (shouldUpdate)
{
if (GetTemplateChildT<ColumnDefinition>(c_iconBoxColumnDefinitionName, this) is { } iconGridColumn)
{
var gridLength = compactPaneLength;
ColumnDefinitionHelper.SetPixelWidth(iconGridColumn, Math.Max(0.0, gridLength - 8));
}
}
}
internal void UpdateClosedCompactVisualState(bool isTopLevelItem, bool isClosedCompact)
{
// We increased the ContentPresenter margin to align it visually with the expand/collapse chevron. This updated margin is even applied when the
// NavigationView is in a visual state where no expand/collapse chevrons are shown, leading to more content being cut off than necessary.
// This is the case for top-level items when the NavigationView is in a compact mode and the NavigationView pane is closed. To keep the original
// cutoff visual experience intact, we restore the original ContentPresenter margin for such top-level items only (children shown in a flyout
// will use the updated margin).
var stateName = isClosedCompact && isTopLevelItem
? "ClosedCompactAndTopLevelItem"
: "NotClosedCompactAndTopLevelItem";
VisualStateManager.GoToState(this, stateName, false /*useTransitions*/);
}
DependencyObject IControlProtected.GetTemplateChild(string childName)
{
return GetTemplateChild(childName);
}
double m_compactPaneLengthValue = 40;
NavigationViewItemHelper<NavigationViewItemPresenter> m_helper = new NavigationViewItemHelper<NavigationViewItemPresenter>();
Grid m_contentGrid;
Grid m_expandCollapseChevron;
double m_leftIndentation = 0;
Storyboard m_chevronExpandedStoryboard;
Storyboard m_chevronCollapsedStoryboard;
RotateTransform m_expandCollapseRotateTransform;
}
}