Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check to only open flyout if a menubaritem has items #2889

Merged
merged 3 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions dev/MenuBar/MenuBarItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,29 +214,32 @@ void MenuBarItem::OnMenuBarItemAccessKeyInvoked(winrt::IInspectable const& sende
// Menu Flyout actions
void MenuBarItem::ShowMenuFlyout()
{
if (auto button = m_button.get())
if (Items().Size() != 0)
{
const auto width = static_cast<float>(button.ActualWidth());
const auto height = static_cast<float>(button.ActualHeight());

if (SharedHelpers::IsFlyoutShowOptionsAvailable())
{
// Sets an exclusion rect over the button that generates the flyout so that even if the menu opens upwards
// (which is the default in touch mode) it doesn't cover the menu bar button.
winrt::FlyoutShowOptions options{};
options.Position(winrt::Point(0, height));
options.Placement(winrt::FlyoutPlacementMode::Bottom);
options.ExclusionRect(winrt::Rect(0, 0, width, height));
m_flyout.get().ShowAt(button, options);
}
else
if (auto button = m_button.get())
{
m_flyout.get().ShowAt(button, winrt::Point(0, height));
const auto width = static_cast<float>(button.ActualWidth());
const auto height = static_cast<float>(button.ActualHeight());

if (SharedHelpers::IsFlyoutShowOptionsAvailable())
{
// Sets an exclusion rect over the button that generates the flyout so that even if the menu opens upwards
// (which is the default in touch mode) it doesn't cover the menu bar button.
winrt::FlyoutShowOptions options{};
options.Position(winrt::Point(0, height));
options.Placement(winrt::FlyoutPlacementMode::Bottom);
options.ExclusionRect(winrt::Rect(0, 0, width, height));
m_flyout.get().ShowAt(button, options);
}
else
{
m_flyout.get().ShowAt(button, winrt::Point(0, height));
}

// Attach keyboard event handler
auto presenter = winrt::get_self<MenuBarItemFlyout>(m_flyout.get())->m_presenter.get();
m_presenterKeyDownRevoker = presenter.KeyDown(winrt::auto_revoke, { this, &MenuBarItem::OnPresenterKeyDown });
}

// Attach keyboard event handler
auto presenter = winrt::get_self<MenuBarItemFlyout>(m_flyout.get())->m_presenter.get();
m_presenterKeyDownRevoker = presenter.KeyDown(winrt::auto_revoke, { this, &MenuBarItem::OnPresenterKeyDown });
}
}

Expand Down
24 changes: 24 additions & 0 deletions dev/MenuBar/MenuBar_InteractionTests/MenuBarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,30 @@ public void TabTest()
}
}

[TestMethod]
public void EmptyMenuBarItemNoPopupTest()
{
if (PlatformConfiguration.IsDevice(DeviceType.Phone))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need that? I don't think we are doing any tests on Windows Mobile devices any longer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the same. I think we should look at all test holistically at some point and remove obsolete checks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does look like dead code. @kmahone to verify this codepath is not hit in any of the configurations we run tests in.

{
Log.Comment("Skipping tests on phone, because menubar is not supported.");
return;
}
using (var setup = new TestSetupHelper("MenuBar Tests"))
{
FindElement.ByName<Button>("NoChildrenFlyout").Click();
VerifyElement.NotFound("Popup",FindBy.Name);

FindElement.ByName<Button>("OneChildrenFlyout").Click();
VerifyElement.Found("Popup", FindBy.Name);

// Click twice to close flyout
FindElement.ByName<Button>("RemoveItemsFromOneChildrenItem").Click();
FindElement.ByName<Button>("RemoveItemsFromOneChildrenItem").Click();

FindElement.ByName<Button>("OneChildrenFlyout").Click();
VerifyElement.NotFound("Popup", FindBy.Name);
}
}

private T GetElement<T>(ref T element, string elementName) where T : UIObject
{
Expand Down
6 changes: 6 additions & 0 deletions dev/MenuBar/MenuBar_TestUI/MenuBarPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<MenuFlyoutItem Text="Word Wrap"/>
<MenuFlyoutItem Text="Font..."/>
</muxc:MenuBarItem>

<muxc:MenuBarItem Title="No children" AutomationProperties.Name="NoChildrenFlyout"/>
<muxc:MenuBarItem Title="One children" AutomationProperties.Name="OneChildrenFlyout" x:Name="OneChildrenFlyoutMenuBarItem">
<MenuFlyoutItem Text="Sometext"/>
</muxc:MenuBarItem>
</muxc:MenuBar>

<TextBlock Text="Custom sized menu bar" Margin="0,6,0,0"/>
Expand All @@ -78,6 +83,7 @@
<Button x:Name="AddFlyoutItemButton" AutomationProperties.Name="AddFlyoutItemButton" Click="AddFlyoutItem_Click" Content="Add Flyout Item"/>
<Button x:Name="RemoveFlyoutItemButton" AutomationProperties.Name="RemoveFlyoutItemButton" Click="RemoveFlyoutItem_Click" Content="Remove Flyout Item"/>
<Button x:Name="AddItemsToEmptyMenuBar" AutomationProperties.Name="AddItemsToEmptyMenuBar" Click="AddMenuBarToEmptyMenuBarItem_Click" Content="Add a menu item to the empty menu bar"/>
<Button x:Name="RemoveItemsFromOneChildrenItem" AutomationProperties.Name="RemoveItemsFromOneChildrenItem" Click="RemoveItemsFromOneChildrenItem_Click" Content="Remove items from One Children item"/>
</StackPanel>
<StackPanel Grid.Row="3">
<TextBlock Text="Test output" Style="{ThemeResource StandardGroupHeader}"/>
Expand Down
4 changes: 4 additions & 0 deletions dev/MenuBar/MenuBar_TestUI/MenuBarPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,9 @@ private void AddMenuBarToEmptyMenuBarItem_Click(object sender, RoutedEventArgs e
EmptyMenuBar.Items.Add(mainMenuBarHelp);
}

private void RemoveItemsFromOneChildrenItem_Click(object sender, RoutedEventArgs e)
{
OneChildrenFlyoutMenuBarItem.Items.Clear();
}
}
}