forked from microsoft/microsoft-ui-xaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigationViewItem.cpp
956 lines (819 loc) · 30.4 KB
/
NavigationViewItem.cpp
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#include <pch.h>
#include <common.h>
#include "NavigationView.h"
#include "NavigationViewItem.h"
#include "NavigationViewItemAutomationPeer.h"
#include "Utils.h"
static constexpr wstring_view c_navigationViewItemPresenterName = L"NavigationViewItemPresenter"sv;
static constexpr auto c_repeater = L"NavigationViewItemMenuItemsHost"sv;
static constexpr auto c_rootGrid = L"NVIRootGrid"sv;
static constexpr auto c_flyoutContentGrid = L"FlyoutContentGrid"sv;
// Visual States
static constexpr auto c_pressedSelected = L"PressedSelected"sv;
static constexpr auto c_pointerOverSelected = L"PointerOverSelected"sv;
static constexpr auto c_selected = L"Selected"sv;
static constexpr auto c_pressed = L"Pressed"sv;
static constexpr auto c_pointerOver = L"PointerOver"sv;
static constexpr auto c_disabled = L"Disabled"sv;
static constexpr auto c_enabled = L"Enabled"sv;
static constexpr auto c_normal = L"Normal"sv;
static constexpr auto c_chevronHidden = L"ChevronHidden"sv;
static constexpr auto c_chevronVisibleOpen = L"ChevronVisibleOpen"sv;
static constexpr auto c_chevronVisibleClosed = L"ChevronVisibleClosed"sv;
NavigationViewItem::NavigationViewItem()
{
SetDefaultStyleKey(this);
SetValue(s_MenuItemsProperty, winrt::make<Vector<winrt::IInspectable>>());
}
void NavigationViewItem::UpdateVisualStateNoTransition()
{
UpdateVisualState(false /*useTransition*/);
}
void NavigationViewItem::OnNavigationViewItemBaseDepthChanged()
{
UpdateItemIndentation();
PropagateDepthToChildren(Depth() + 1);
}
void NavigationViewItem::OnNavigationViewItemBaseIsSelectedChanged()
{
UpdateVisualStateForPointer();
}
void NavigationViewItem::OnNavigationViewItemBasePositionChanged()
{
UpdateVisualStateNoTransition();
ReparentRepeater();
}
void NavigationViewItem::OnApplyTemplate()
{
// Stop UpdateVisualState before template is applied. Otherwise the visuals may be unexpected
m_appliedTemplate = false;
UnhookEventsAndClearFields();
NavigationViewItemBase::OnApplyTemplate();
// Find selection indicator
// Retrieve pointers to stable controls
winrt::IControlProtected controlProtected = *this;
m_helper.Init(controlProtected);
if (auto rootGrid = GetTemplateChildT<winrt::Grid>(c_rootGrid, controlProtected))
{
m_rootGrid.set(rootGrid);
if (auto flyoutBase = winrt::FlyoutBase::GetAttachedFlyout(rootGrid))
{
m_flyoutClosingRevoker = flyoutBase.Closing(winrt::auto_revoke, { this, &NavigationViewItem::OnFlyoutClosing });
}
}
HookInputEvents(controlProtected);
m_isEnabledChangedRevoker = IsEnabledChanged(winrt::auto_revoke, { this, &NavigationViewItem::OnIsEnabledChanged });
m_toolTip.set(GetTemplateChildT<winrt::ToolTip>(L"ToolTip"sv, controlProtected));
if (auto splitView = GetSplitView())
{
m_splitViewIsPaneOpenChangedRevoker = RegisterPropertyChanged(splitView,
winrt::SplitView::IsPaneOpenProperty(), { this, &NavigationViewItem::OnSplitViewPropertyChanged });
m_splitViewDisplayModeChangedRevoker = RegisterPropertyChanged(splitView,
winrt::SplitView::DisplayModeProperty(), { this, &NavigationViewItem::OnSplitViewPropertyChanged });
m_splitViewCompactPaneLengthChangedRevoker = RegisterPropertyChanged(splitView,
winrt::SplitView::CompactPaneLengthProperty(), { this, &NavigationViewItem::OnSplitViewPropertyChanged });
UpdateCompactPaneLength();
UpdateIsClosedCompact();
}
// Retrieve reference to NavigationView
if (auto nvImpl = winrt::get_self<NavigationView>(GetNavigationView()))
{
if (auto repeater = GetTemplateChildT<winrt::ItemsRepeater>(c_repeater, controlProtected))
{
m_repeater.set(repeater);
// Primary element setup happens in NavigationView
m_repeaterElementPreparedRevoker = repeater.ElementPrepared(winrt::auto_revoke, { nvImpl, &NavigationView::OnRepeaterElementPrepared });
m_repeaterElementClearingRevoker = repeater.ElementClearing(winrt::auto_revoke, { nvImpl, &NavigationView::OnRepeaterElementClearing });
repeater.ItemTemplate(*(nvImpl->GetNavigationViewItemsFactory()));
}
UpdateRepeaterItemsSource();
}
m_flyoutContentGrid.set(GetTemplateChildT<winrt::Grid>(c_flyoutContentGrid, controlProtected));
m_appliedTemplate = true;
UpdateItemIndentation();
UpdateVisualStateNoTransition();
ReparentRepeater();
// We dont want to update the repeater visibilty during OnApplyTemplate if NavigationView is in a mode when items are shown in a flyout
if (!ShouldRepeaterShowInFlyout())
{
ShowHideChildren();
}
auto visual = winrt::ElementCompositionPreview::GetElementVisual(*this);
NavigationView::CreateAndAttachHeaderAnimation(visual);
}
void NavigationViewItem::UpdateRepeaterItemsSource()
{
if (auto repeater = m_repeater.get())
{
auto const itemsSource = [this]()
{
if (auto const menuItemsSource = MenuItemsSource())
{
return menuItemsSource;
}
return MenuItems().as<winrt::IInspectable>();
}();
m_itemsSourceViewCollectionChangedRevoker.revoke();
repeater.ItemsSource(itemsSource);
m_itemsSourceViewCollectionChangedRevoker = repeater.ItemsSourceView().CollectionChanged(winrt::auto_revoke, { this, &NavigationViewItem::OnItemsSourceViewChanged });
}
}
void NavigationViewItem::OnItemsSourceViewChanged(const winrt::IInspectable& /*sender*/, const winrt::NotifyCollectionChangedEventArgs& /*args*/)
{
UpdateVisualStateForChevron();
}
winrt::UIElement NavigationViewItem::GetSelectionIndicator() const
{
auto selectIndicator = m_helper.GetSelectionIndicator();
if (auto presenter = GetPresenter())
{
selectIndicator = presenter->GetSelectionIndicator();
}
return selectIndicator;
}
void NavigationViewItem::OnSplitViewPropertyChanged(const winrt::DependencyObject& /*sender*/, const winrt::DependencyProperty& args)
{
if (args == winrt::SplitView::CompactPaneLengthProperty())
{
UpdateCompactPaneLength();
}
else if (args == winrt::SplitView::IsPaneOpenProperty() ||
args == winrt::SplitView::DisplayModeProperty())
{
UpdateIsClosedCompact();
ReparentRepeater();
}
}
void NavigationViewItem::UpdateCompactPaneLength()
{
if (const auto splitView = GetSplitView())
{
SetValue(s_CompactPaneLengthProperty, winrt::PropertyValue::CreateDouble(splitView.CompactPaneLength()));
// Only update when on left
if (const auto presenter = GetPresenter())
{
presenter->UpdateCompactPaneLength(splitView.CompactPaneLength(), IsOnLeftNav());
}
}
}
void NavigationViewItem::UpdateIsClosedCompact()
{
if (const auto splitView = GetSplitView())
{
// Check if the pane is closed and if the splitview is in either compact mode.
m_isClosedCompact = !splitView.IsPaneOpen()
&& (splitView.DisplayMode() == winrt::SplitViewDisplayMode::CompactOverlay || splitView.DisplayMode() == winrt::SplitViewDisplayMode::CompactInline);
UpdateVisualState(true /*useTransitions*/);
if (const auto presenter = GetPresenter())
{
presenter->UpdateClosedCompactVisualState(IsTopLevelItem(), m_isClosedCompact);
}
}
}
void NavigationViewItem::UpdateNavigationViewItemToolTip()
{
auto toolTipContent = winrt::ToolTipService::GetToolTip(*this);
// no custom tooltip, then use suggested tooltip
if (!toolTipContent || toolTipContent == m_suggestedToolTipContent.get())
{
if (ShouldEnableToolTip())
{
winrt::ToolTipService::SetToolTip(*this, m_suggestedToolTipContent.get());
}
else
{
winrt::ToolTipService::SetToolTip(*this, nullptr);
}
}
}
void NavigationViewItem::SuggestedToolTipChanged(winrt::IInspectable const& newContent)
{
auto potentialString = newContent.try_as<winrt::IPropertyValue>();
const bool stringableToolTip = (potentialString && potentialString.Type() == winrt::PropertyType::String);
winrt::IInspectable newToolTipContent{ nullptr };
if (stringableToolTip)
{
newToolTipContent = newContent;
}
// Both customer and NavigationViewItem can update ToolTipContent by winrt::ToolTipService::SetToolTip or XAML
// If the ToolTipContent is not the same as m_suggestedToolTipContent, then it's set by customer.
// Customer's ToolTip take high priority, and we never override Customer's ToolTip.
auto toolTipContent = winrt::ToolTipService::GetToolTip(*this);
if (auto oldToolTipContent = m_suggestedToolTipContent.get())
{
if (oldToolTipContent == toolTipContent)
{
winrt::ToolTipService::SetToolTip(*this, nullptr);
}
}
m_suggestedToolTipContent.set(newToolTipContent);
}
void NavigationViewItem::OnIsExpandedPropertyChanged(const winrt::DependencyPropertyChangedEventArgs& args)
{
if (winrt::AutomationPeer peer = winrt::FrameworkElementAutomationPeer::FromElement(*this))
{
auto navViewItemPeer = peer.as<winrt::NavigationViewItemAutomationPeer>();
winrt::get_self<NavigationViewItemAutomationPeer>(navViewItemPeer)->RaiseExpandCollapseAutomationEvent(
IsExpanded() ?
winrt::ExpandCollapseState::Expanded :
winrt::ExpandCollapseState::Collapsed
);
}
}
void NavigationViewItem::OnIconPropertyChanged(const winrt::DependencyPropertyChangedEventArgs& args)
{
UpdateVisualStateNoTransition();
}
void NavigationViewItem::OnMenuItemsPropertyChanged(const winrt::DependencyPropertyChangedEventArgs& args)
{
UpdateRepeaterItemsSource();
UpdateVisualStateForChevron();
}
void NavigationViewItem::OnMenuItemsSourcePropertyChanged(const winrt::DependencyPropertyChangedEventArgs& args)
{
UpdateRepeaterItemsSource();
UpdateVisualStateForChevron();
}
void NavigationViewItem::OnHasUnrealizedChildrenPropertyChanged(const winrt::DependencyPropertyChangedEventArgs& args)
{
UpdateVisualStateForChevron();
}
void NavigationViewItem::ShowSelectionIndicator(bool visible)
{
if (auto const selectionIndicator = GetSelectionIndicator())
{
selectionIndicator.Opacity(visible ? 1.0 : 0.0);
}
}
void NavigationViewItem::UpdateVisualStateForIconAndContent(bool showIcon, bool showContent)
{
if (auto const presenter = m_navigationViewItemPresenter.get())
{
auto stateName = showIcon ? (showContent ? L"IconOnLeft" : L"IconOnly") : L"ContentOnly";
winrt::VisualStateManager::GoToState(presenter, stateName, false /*useTransitions*/);
}
}
void NavigationViewItem::UpdateVisualStateForNavigationViewPositionChange()
{
const auto position = Position();
auto stateName = c_OnLeftNavigation;
bool handled = false;
switch (position)
{
case NavigationViewRepeaterPosition::LeftNav:
case NavigationViewRepeaterPosition::LeftFooter:
if (SharedHelpers::IsRS4OrHigher() && winrt::Application::Current().FocusVisualKind() == winrt::FocusVisualKind::Reveal)
{
// OnLeftNavigationReveal is introduced in RS6.
// Will fallback to stateName for the customer who re-template rs5 NavigationViewItem
if (winrt::VisualStateManager::GoToState(*this, c_OnLeftNavigationReveal, false /*useTransitions*/))
{
handled = true;
}
}
break;
case NavigationViewRepeaterPosition::TopPrimary:
case NavigationViewRepeaterPosition::TopFooter:
if (SharedHelpers::IsRS4OrHigher() && winrt::Application::Current().FocusVisualKind() == winrt::FocusVisualKind::Reveal)
{
stateName = c_OnTopNavigationPrimaryReveal;
}
else
{
stateName = c_OnTopNavigationPrimary;
}
break;
case NavigationViewRepeaterPosition::TopOverflow:
stateName = c_OnTopNavigationOverflow;
break;
}
if (!handled)
{
winrt::VisualStateManager::GoToState(*this, stateName, false /*useTransitions*/);
}
}
void NavigationViewItem::UpdateVisualStateForKeyboardFocusedState()
{
auto focusState = L"KeyboardNormal";
if (m_hasKeyboardFocus)
{
focusState = L"KeyboardFocused";
}
winrt::VisualStateManager::GoToState(*this, focusState, false /*useTransitions*/);
}
void NavigationViewItem::UpdateVisualStateForToolTip()
{
// Since RS5, ToolTip apply to NavigationViewItem directly to make Keyboard focus has tooltip too.
// If ToolTip TemplatePart is detected, fallback to old logic and apply ToolTip on TemplatePart.
if (auto toolTip = m_toolTip.get())
{
const auto shouldEnableToolTip = ShouldEnableToolTip();
auto toolTipContent = m_suggestedToolTipContent.get();
if (shouldEnableToolTip && toolTipContent)
{
toolTip.Content(toolTipContent);
toolTip.IsEnabled(true);
}
else
{
toolTip.Content(nullptr);
toolTip.IsEnabled(false);
}
}
else
{
UpdateNavigationViewItemToolTip();
}
}
void NavigationViewItem::UpdateVisualStateForPointer()
{
const auto isEnabled = IsEnabled();
const auto enabledStateValue = isEnabled ? c_enabled : c_disabled;
// DisabledStates and CommonStates
const auto selectedStateValue = [this, isEnabled, isSelected = IsSelected()]()
{
if (isEnabled)
{
if (isSelected)
{
if (m_isPressed)
{
return c_pressedSelected;
}
else if (m_isPointerOver)
{
return c_pointerOverSelected;
}
else
{
return c_selected;
}
}
else if (m_isPointerOver)
{
if (m_isPressed)
{
return c_pressed;
}
else
{
return c_pointerOver;
}
}
else if (m_isPressed)
{
return c_pressed;
}
}
else
{
if (isSelected)
{
return c_selected;
}
}
return c_normal;
}();
// There are scenarios where the presenter may not exist.
// For example, the top nav settings item. In that case,
// update the states for the item itself.
if (auto const presenter = m_navigationViewItemPresenter.get())
{
winrt::VisualStateManager::GoToState(presenter, enabledStateValue, true);
winrt::VisualStateManager::GoToState(presenter, selectedStateValue, true);
}
else
{
winrt::VisualStateManager::GoToState(*this, enabledStateValue, true);
winrt::VisualStateManager::GoToState(*this, selectedStateValue, true);
}
}
void NavigationViewItem::UpdateVisualState(bool useTransitions)
{
if (!m_appliedTemplate)
return;
UpdateVisualStateForPointer();
UpdateVisualStateForNavigationViewPositionChange();
const bool shouldShowIcon = ShouldShowIcon();
const bool shouldShowContent = ShouldShowContent();
if (IsOnLeftNav())
{
if (auto const presenter = m_navigationViewItemPresenter.get())
{
// Backward Compatibility with RS4-, new implementation prefer IconOnLeft/IconOnly/ContentOnly
winrt::VisualStateManager::GoToState(presenter, shouldShowIcon ? L"IconVisible" : L"IconCollapsed", useTransitions);
}
}
UpdateVisualStateForToolTip();
UpdateVisualStateForIconAndContent(shouldShowIcon, shouldShowContent);
// visual state for focus state. top navigation use it to provide different visual for selected and selected+focused
UpdateVisualStateForKeyboardFocusedState();
UpdateVisualStateForChevron();
}
void NavigationViewItem::UpdateVisualStateForChevron()
{
if (auto const presenter = m_navigationViewItemPresenter.get())
{
auto const chevronState = HasChildren() && !(m_isClosedCompact && ShouldRepeaterShowInFlyout()) ? ( IsExpanded() ? c_chevronVisibleOpen : c_chevronVisibleClosed) : c_chevronHidden;
winrt::VisualStateManager::GoToState(presenter, chevronState, true);
}
}
bool NavigationViewItem::HasChildren()
{
return MenuItems().Size() > 0
|| (MenuItemsSource() != nullptr && m_repeater != nullptr && m_repeater.get().ItemsSourceView().Count() > 0)
|| HasUnrealizedChildren();
}
bool NavigationViewItem::ShouldShowIcon()
{
return static_cast<bool>(Icon());
}
bool NavigationViewItem::ShouldEnableToolTip() const
{
// We may enable Tooltip for IconOnly in the future, but not now
return IsOnLeftNav() && m_isClosedCompact;
}
bool NavigationViewItem::ShouldShowContent()
{
return static_cast<bool>(Content());
}
bool NavigationViewItem::IsOnLeftNav() const
{
auto const position = Position();
return position == NavigationViewRepeaterPosition::LeftNav || position == NavigationViewRepeaterPosition::LeftFooter;
}
bool NavigationViewItem::IsOnTopPrimary() const
{
return Position() == NavigationViewRepeaterPosition::TopPrimary;
}
winrt::UIElement const NavigationViewItem::GetPresenterOrItem() const
{
if (auto const presenter = m_navigationViewItemPresenter.get())
{
return presenter.try_as<winrt::UIElement>();
}
else
{
return this->try_as<winrt::UIElement>();
}
}
NavigationViewItemPresenter* NavigationViewItem::GetPresenter() const
{
NavigationViewItemPresenter* presenter = nullptr;
if (m_navigationViewItemPresenter)
{
presenter = winrt::get_self<NavigationViewItemPresenter>(m_navigationViewItemPresenter.get());
}
return presenter;
}
void NavigationViewItem::ShowHideChildren()
{
if (auto const repeater = m_repeater.get())
{
const bool shouldShowChildren = IsExpanded();
const auto visibility = shouldShowChildren ? winrt::Visibility::Visible : winrt::Visibility::Collapsed;
repeater.Visibility(visibility);
if (ShouldRepeaterShowInFlyout())
{
if (shouldShowChildren)
{
// Verify that repeater is parented correctly
if (!m_isRepeaterParentedToFlyout)
{
ReparentRepeater();
}
// There seems to be a race condition happening which sometimes
// prevents the opening of the flyout. Queue callback as a workaround.
SharedHelpers::QueueCallbackForCompositionRendering(
[strongThis = get_strong()]()
{
winrt::FlyoutBase::ShowAttachedFlyout(strongThis->m_rootGrid.get());
});
}
else
{
if (auto const flyout = winrt::FlyoutBase::GetAttachedFlyout(m_rootGrid.get()))
{
flyout.Hide();
}
}
}
}
}
void NavigationViewItem::ReparentRepeater()
{
if (HasChildren())
{
if (auto const repeater = m_repeater.get())
{
if (ShouldRepeaterShowInFlyout() && !m_isRepeaterParentedToFlyout)
{
// Reparent repeater to flyout
// TODO: Replace removeatend with something more specific
m_rootGrid.get().Children().RemoveAtEnd();
m_flyoutContentGrid.get().Children().Append(repeater);
m_isRepeaterParentedToFlyout = true;
PropagateDepthToChildren(0);
}
else if (!ShouldRepeaterShowInFlyout() && m_isRepeaterParentedToFlyout)
{
m_flyoutContentGrid.get().Children().RemoveAtEnd();
m_rootGrid.get().Children().Append(repeater);
m_isRepeaterParentedToFlyout = false;
PropagateDepthToChildren(1);
}
}
}
}
bool NavigationViewItem::ShouldRepeaterShowInFlyout() const
{
return (m_isClosedCompact && IsTopLevelItem()) || IsOnTopPrimary();
}
bool NavigationViewItem::IsRepeaterVisible() const
{
if (auto const repeater = m_repeater.get())
{
return repeater.Visibility() == winrt::Visibility::Visible;
}
return false;
}
void NavigationViewItem::UpdateItemIndentation()
{
// Update item indentation based on its depth
if (auto const presenter = m_navigationViewItemPresenter.get())
{
const auto newLeftMargin = Depth() * c_itemIndentation;
winrt::get_self<NavigationViewItemPresenter>(presenter)->UpdateContentLeftIndentation(static_cast<double>(newLeftMargin));
}
}
void NavigationViewItem::PropagateDepthToChildren(int depth)
{
if (auto const repeater = m_repeater.get())
{
auto itemsCount = repeater.ItemsSourceView().Count();
for (int index = 0; index < itemsCount; index++)
{
if (auto const element = repeater.TryGetElement(index))
{
if (auto const nvib = element.try_as<winrt::NavigationViewItemBase>())
{
winrt::get_self<NavigationViewItemBase>(nvib)->Depth(depth);
}
}
}
}
}
void NavigationViewItem::OnExpandCollapseChevronTapped(const winrt::IInspectable& sender, const winrt::TappedRoutedEventArgs& args)
{
IsExpanded(!IsExpanded());
args.Handled(true);
}
void NavigationViewItem::OnFlyoutClosing(const winrt::IInspectable& sender, const winrt::FlyoutBaseClosingEventArgs& args)
{
IsExpanded(false);
}
// IUIElement / IUIElementOverridesHelper
winrt::AutomationPeer NavigationViewItem::OnCreateAutomationPeer()
{
return winrt::make<NavigationViewItemAutomationPeer>(*this);
}
// IContentControlOverrides / IContentControlOverridesHelper
void NavigationViewItem::OnContentChanged(winrt::IInspectable const& oldContent, winrt::IInspectable const& newContent)
{
NavigationViewItemBase::OnContentChanged(oldContent, newContent);
SuggestedToolTipChanged(newContent);
UpdateVisualStateNoTransition();
if (!IsOnLeftNav())
{
// Content has changed for the item, so we want to trigger a re-measure
if (auto navView = GetNavigationView())
{
winrt::get_self<NavigationView>(navView)->TopNavigationViewItemContentChanged();
}
}
}
void NavigationViewItem::OnGotFocus(winrt::RoutedEventArgs const& e)
{
NavigationViewItemBase::OnGotFocus(e);
auto originalSource = e.OriginalSource().try_as<winrt::Control>();
if (originalSource)
{
// It's used to support bluebar have difference appearance between focused and focused+selection.
// For example, we can move the SelectionIndicator 3px up when focused and selected to make sure focus rectange doesn't override SelectionIndicator.
// If it's a pointer or programatic, no focus rectangle, so no action
const auto focusState = originalSource.FocusState();
if (focusState == winrt::FocusState::Keyboard)
{
m_hasKeyboardFocus = true;
UpdateVisualStateNoTransition();
}
}
}
void NavigationViewItem::OnLostFocus(winrt::RoutedEventArgs const& e)
{
NavigationViewItemBase::OnLostFocus(e);
if (m_hasKeyboardFocus)
{
m_hasKeyboardFocus = false;
UpdateVisualStateNoTransition();
}
}
void NavigationViewItem::ResetTrackedPointerId()
{
m_trackedPointerId = 0;
}
// Returns False when the provided pointer Id matches the currently tracked Id.
// When there is no currently tracked Id, sets the tracked Id to the provided Id and returns False.
// Returns True when the provided pointer Id does not match the currently tracked Id.
bool NavigationViewItem::IgnorePointerId(const winrt::PointerRoutedEventArgs& args)
{
uint32_t pointerId = args.Pointer().PointerId();
if (m_trackedPointerId == 0)
{
m_trackedPointerId = pointerId;
}
else if (m_trackedPointerId != pointerId)
{
return true;
}
return false;
}
void NavigationViewItem::OnPresenterPointerPressed(const winrt::IInspectable&, const winrt::PointerRoutedEventArgs& args)
{
if (IgnorePointerId(args))
{
return;
}
MUX_ASSERT(!m_isPressed);
MUX_ASSERT(!m_capturedPointer);
// TODO: Update to look at presenter instead
auto pointerProperties = args.GetCurrentPoint(*this).Properties();
m_isPressed = pointerProperties.IsLeftButtonPressed() || pointerProperties.IsRightButtonPressed();
auto pointer = args.Pointer();
auto presenter = GetPresenterOrItem();
MUX_ASSERT(presenter);
if (presenter.CapturePointer(pointer))
{
m_capturedPointer = pointer;
}
UpdateVisualState(true);
}
void NavigationViewItem::OnPresenterPointerReleased(const winrt::IInspectable&, const winrt::PointerRoutedEventArgs& args)
{
if (IgnorePointerId(args))
{
return;
}
if (m_isPressed)
{
m_isPressed = false;
if (m_capturedPointer)
{
auto presenter = GetPresenterOrItem();
MUX_ASSERT(presenter);
presenter.ReleasePointerCapture(m_capturedPointer);
}
UpdateVisualState(true);
}
}
void NavigationViewItem::OnPresenterPointerEntered(const winrt::IInspectable&, const winrt::PointerRoutedEventArgs& args)
{
ProcessPointerOver(args);
}
void NavigationViewItem::OnPresenterPointerMoved(const winrt::IInspectable&, const winrt::PointerRoutedEventArgs& args)
{
ProcessPointerOver(args);
}
void NavigationViewItem::OnPresenterPointerExited(const winrt::IInspectable&, const winrt::PointerRoutedEventArgs& args)
{
if (IgnorePointerId(args))
{
return;
}
m_isPointerOver = false;
if (!m_capturedPointer)
{
ResetTrackedPointerId();
}
UpdateVisualState(true);
}
void NavigationViewItem::OnPresenterPointerCanceled(const winrt::IInspectable&, const winrt::PointerRoutedEventArgs& args)
{
ProcessPointerCanceled(args);
}
void NavigationViewItem::OnPresenterPointerCaptureLost(const winrt::IInspectable&, const winrt::PointerRoutedEventArgs& args)
{
ProcessPointerCanceled(args);
}
void NavigationViewItem::OnIsEnabledChanged(const winrt::IInspectable&, const winrt::DependencyPropertyChangedEventArgs&)
{
if (!IsEnabled())
{
m_isPressed = false;
m_isPointerOver = false;
if (m_capturedPointer)
{
auto presenter = GetPresenterOrItem();
MUX_ASSERT(presenter);
presenter.ReleasePointerCapture(m_capturedPointer);
m_capturedPointer = nullptr;
}
ResetTrackedPointerId();
}
UpdateVisualState(true);
}
void NavigationViewItem::RotateExpandCollapseChevron(bool isExpanded)
{
if (auto presenter = GetPresenter())
{
presenter->RotateExpandCollapseChevron(isExpanded);
}
}
void NavigationViewItem::ProcessPointerCanceled(const winrt::PointerRoutedEventArgs& args)
{
if (IgnorePointerId(args))
{
return;
}
m_isPressed = false;
m_isPointerOver = false;
m_capturedPointer = nullptr;
ResetTrackedPointerId();
UpdateVisualState(true);
}
void NavigationViewItem::ProcessPointerOver(const winrt::PointerRoutedEventArgs& args)
{
if (IgnorePointerId(args))
{
return;
}
if (!m_isPointerOver)
{
m_isPointerOver = true;
UpdateVisualState(true);
}
}
void NavigationViewItem::HookInputEvents(const winrt::IControlProtected& controlProtected)
{
winrt::UIElement const presenter = [this, controlProtected]()
{
if (auto presenter = GetTemplateChildT<winrt::NavigationViewItemPresenter>(c_navigationViewItemPresenterName, controlProtected))
{
m_navigationViewItemPresenter.set(presenter);
return presenter.try_as<winrt::UIElement>();
}
// We don't have a presenter, so we are our own presenter.
return this->try_as<winrt::UIElement>();
}();
MUX_ASSERT(presenter);
// Handlers that set flags are skipped when args.Handled is already True.
m_presenterPointerPressedRevoker = presenter.PointerPressed(winrt::auto_revoke, { this, &NavigationViewItem::OnPresenterPointerPressed });
m_presenterPointerEnteredRevoker = presenter.PointerEntered(winrt::auto_revoke, { this, &NavigationViewItem::OnPresenterPointerEntered });
m_presenterPointerMovedRevoker = presenter.PointerMoved(winrt::auto_revoke, { this, &NavigationViewItem::OnPresenterPointerMoved });
// Handlers that reset flags are not skipped when args.Handled is already True to avoid broken states.
m_presenterPointerReleasedRevoker = AddRoutedEventHandler<RoutedEventType::PointerReleased>(
presenter,
{ this, &NavigationViewItem::OnPresenterPointerReleased },
true /*handledEventsToo*/);
m_presenterPointerExitedRevoker = AddRoutedEventHandler<RoutedEventType::PointerExited>(
presenter,
{ this, &NavigationViewItem::OnPresenterPointerExited },
true /*handledEventsToo*/);
m_presenterPointerCanceledRevoker = AddRoutedEventHandler<RoutedEventType::PointerCanceled>(
presenter,
{ this, &NavigationViewItem::OnPresenterPointerCanceled },
true /*handledEventsToo*/);
m_presenterPointerCaptureLostRevoker = AddRoutedEventHandler<RoutedEventType::PointerCaptureLost>(
presenter,
{ this, &NavigationViewItem::OnPresenterPointerCaptureLost },
true /*handledEventsToo*/);
}
void NavigationViewItem::UnhookInputEvents()
{
m_presenterPointerPressedRevoker.revoke();
m_presenterPointerEnteredRevoker.revoke();
m_presenterPointerMovedRevoker.revoke();
m_presenterPointerReleasedRevoker.revoke();
m_presenterPointerExitedRevoker.revoke();
m_presenterPointerCanceledRevoker.revoke();
m_presenterPointerCaptureLostRevoker.revoke();
}
void NavigationViewItem::UnhookEventsAndClearFields()
{
UnhookInputEvents();
m_flyoutClosingRevoker.revoke();
m_splitViewIsPaneOpenChangedRevoker.revoke();
m_splitViewDisplayModeChangedRevoker.revoke();
m_splitViewCompactPaneLengthChangedRevoker.revoke();
m_repeaterElementPreparedRevoker.revoke();
m_repeaterElementClearingRevoker.revoke();
m_isEnabledChangedRevoker.revoke();
m_itemsSourceViewCollectionChangedRevoker.revoke();
m_rootGrid.set(nullptr);
m_navigationViewItemPresenter.set(nullptr);
m_toolTip.set(nullptr);
m_repeater.set(nullptr);
m_flyoutContentGrid.set(nullptr);
}