Skip to content

Commit

Permalink
Automation Peer fix for Hierarchical NavigationView (microsoft/micros…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Jun 14, 2020
1 parent 5ebdc90 commit 8624822
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 73 deletions.
2 changes: 1 addition & 1 deletion ModernWpf.Controls/NavigationView/NavigationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ bool IsRootGridOfFlyout(DependencyObject element)
return false;
}

ItemsRepeater GetParentItemsRepeaterForContainer(NavigationViewItemBase nvib)
internal ItemsRepeater GetParentItemsRepeaterForContainer(NavigationViewItemBase nvib)
{
if (VisualTreeHelper.GetParent(nvib) is { } parent)
{
Expand Down
145 changes: 73 additions & 72 deletions ModernWpf.Controls/NavigationView/NavigationViewItemAutomationPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,7 @@ protected override int GetPositionInSetCore()

if (IsOnTopNavigation())
{
if (GetParentNavigationView() is { } navigationView)
{
var topDataProvider = navigationView.GetTopDataProvider();
positionInSet = GetPositionOrSetCountInTopNavHelper(IsOnTopNavigationOverflow()
? topDataProvider.GetOverflowItems() : topDataProvider.GetPrimaryItems(), AutomationOutput.Position);
}
positionInSet = GetPositionOrSetCountInTopNavHelper(AutomationOutput.Position);
}
else
{
Expand All @@ -116,13 +111,7 @@ protected override int GetSizeOfSetCore()

if (IsOnTopNavigation())
{
if (GetParentNavigationView() is { } navview)
{
var topNavDataProvider = navview.GetTopDataProvider();
sizeOfSet = GetPositionOrSetCountInTopNavHelper(IsOnTopNavigationOverflow()
? topNavDataProvider.GetOverflowItems() : topNavDataProvider.GetPrimaryItems(), AutomationOutput.Size);

}
sizeOfSet = GetPositionOrSetCountInTopNavHelper(AutomationOutput.Size);
}
else
{
Expand Down Expand Up @@ -273,6 +262,18 @@ NavigationViewRepeaterPosition GetNavigationViewRepeaterPosition()
return NavigationViewRepeaterPosition.LeftNav;
}

ItemsRepeater GetParentRepeater()
{
if (GetParentNavigationView() is { } navview)
{
if (Owner is NavigationViewItemBase navigationViewItem)
{
return navview.GetParentItemsRepeaterForContainer(navigationViewItem);
}
}
return null;
}

// Get either the position or the size of the set for this particular item in the case of left nav.
// We go through all the items and then we determine if the listviewitem from the left listview can be a navigation view item header
// or a navigation view item. If it's the former, we just reset the count. If it's the latter, we increment the counter.
Expand All @@ -281,54 +282,51 @@ int GetPositionOrSetCountInLeftNavHelper(AutomationOutput automationOutput)
{
int returnValue = 0;

if (GetParentNavigationView() is { } navview)
if (GetParentRepeater() is { } repeater)
{
if (navview.LeftNavRepeater() is { } repeater)
if (GetParent() is AutomationPeer parent)
{
if (GetParent() is AutomationPeer parent)
if (parent.GetChildren() is { } children)
{
if (parent.GetChildren() is { } children)
{
int index = 0;
bool itemFound = false;
int index = 0;
bool itemFound = false;

foreach (var child in children)
foreach (var child in children)
{
if (repeater.TryGetElement(index) is { } dependencyObject)
{
if (repeater.TryGetElement(index) is { } dependencyObject)
if (dependencyObject is NavigationViewItemHeader)
{
if (dependencyObject is NavigationViewItemHeader)
if (automationOutput == AutomationOutput.Size && itemFound)
{
if (automationOutput == AutomationOutput.Size && itemFound)
{
break;
}
else
{
returnValue = 0;
}
break;
}
else if (dependencyObject is NavigationViewItem navviewItem)
else
{
if (navviewItem.Visibility == Visibility.Visible)
{
returnValue++;
returnValue = 0;
}
}
else if (dependencyObject is NavigationViewItem navviewItem)
{
if (navviewItem.Visibility == Visibility.Visible)
{
returnValue++;

if (FrameworkElementAutomationPeer.FromElement(navviewItem) == (this))
if (FrameworkElementAutomationPeer.FromElement(navviewItem) == (this))
{
if (automationOutput == AutomationOutput.Position)
{
break;
}
else
{
if (automationOutput == AutomationOutput.Position)
{
break;
}
else
{
itemFound = true;
}
itemFound = true;
}
}
}
}
index++;
}
index++;
}
}
}
Expand All @@ -341,49 +339,52 @@ int GetPositionOrSetCountInLeftNavHelper(AutomationOutput automationOutput)
// Basically, we do the same here as GetPositionOrSetCountInLeftNavHelper without dealing with the listview directly, because
// TopDataProvider provcides two methods: GetOverflowItems() and GetPrimaryItems(), so we can break the loop (in case of position) by
// comparing the value of the FrameworkElementAutomationPeer we can get from the item we're iterating through to this object.
int GetPositionOrSetCountInTopNavHelper(IList navigationViewElements, AutomationOutput automationOutput)
int GetPositionOrSetCountInTopNavHelper(AutomationOutput automationOutput)
{
int returnValue = 0;
bool itemFound = false;

if (GetParentNavigationView() is { } navview)
if (GetParentRepeater() is { } parentRepeater)
{
bool itemFound = false;

foreach (var child in navigationViewElements)
if (parentRepeater.ItemsSourceView is { } itemsSourceView)
{
if (navview.ContainerFromMenuItem(child) is { } childAsNavViewItem)
var numberOfElements = itemsSourceView.Count;

for (int i = 0; i < numberOfElements; i++)
{
if (child is NavigationViewItemHeader)
if (parentRepeater.TryGetElement(i) is { } child)
{
if (automationOutput == AutomationOutput.Size && itemFound)
if (child is NavigationViewItemHeader)
{
break;
}
else
{
returnValue = 0;
if (automationOutput == AutomationOutput.Size && itemFound)
{
break;
}
else
{
returnValue = 0;
}
}
}
else if (childAsNavViewItem is NavigationViewItem navviewitem)
{
if (navviewitem.Visibility == Visibility.Visible)
else if (child is NavigationViewItem navviewitem)
{
returnValue++;

if (FrameworkElementAutomationPeer.FromElement(navviewitem) == (this))
if (navviewitem.Visibility == Visibility.Visible)
{
if (automationOutput == AutomationOutput.Position)
{
break;
}
else
returnValue++;

if (FrameworkElementAutomationPeer.FromElement(navviewitem) == (this))
{
itemFound = true;
if (automationOutput == AutomationOutput.Position)
{
break;
}
else
{
itemFound = true;
}
}
}
}
}

}
}
}
Expand Down

0 comments on commit 8624822

Please sign in to comment.