Move History and Memory context menus to ListViewItemContainer so users can open them with keyboards

This commit is contained in:
Rudy Huyn 2019-04-04 01:36:24 -07:00
commit a0d4565515
5 changed files with 29 additions and 54 deletions

View file

@ -51,7 +51,7 @@
Invoked="OnDeleteSwipeInvoked"/>
</muxc:SwipeItems>
<MenuFlyout x:Key="HistoryContextMenu">
<MenuFlyout x:Name="HistoryContextMenu">
<MenuFlyoutItem x:Uid="DeleteHistoryMenuItem"
Click="OnDeleteMenuItemClicked"
Icon="Delete"/>
@ -59,9 +59,7 @@
<DataTemplate x:Key="HistoryItemTemplate" x:DataType="model:HistoryItemViewModel">
<muxc:SwipeControl RightItems="{StaticResource HistorySwipeItems}">
<StackPanel Margin="0,6,4,6"
Background="Transparent"
ContextFlyout="{StaticResource HistoryContextMenu}">
<StackPanel Margin="0,6,4,6"Background="Transparent">
<TextBlock x:Name="exprTextBlock"
Margin="0,0,0,4"
HorizontalAlignment="Right"
@ -87,6 +85,7 @@
BasedOn="{StaticResource HistoryMemoryItemContainerStyle}"
TargetType="ListViewItem">
<Setter Property="Margin" Value="0,0,0,20"/>
<Setter Property="ContextFlyout" Value="{StaticResource HistoryContextMenu}"/>
</Style>
</ResourceDictionary>
</UserControl.Resources>

View file

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
@ -67,9 +67,13 @@ void HistoryList::ListView_ItemClick(_In_ Object^ sender, _In_ ItemClickEventArg
void HistoryList::OnDeleteMenuItemClicked(_In_ Object^ sender, _In_ RoutedEventArgs^ e)
{
auto clickedItem = safe_cast<HistoryItemViewModel^>(safe_cast<FrameworkElement^>(sender)->DataContext);
auto listViewItem = HistoryContextMenu->Target;
auto itemViewModel = safe_cast<HistoryItemViewModel^>(HistoryListView->ItemFromContainer(listViewItem));
Model->DeleteItem(clickedItem);
if (itemViewModel != nullptr)
{
Model->DeleteItem(itemViewModel);
}
}
void HistoryList::OnDeleteSwipeInvoked(_In_ MUXC::SwipeItem^ sender, _In_ MUXC::SwipeItemInvokedEventArgs^ e)

View file

@ -19,7 +19,7 @@
<converters:BooleanToVisibilityNegationConverter x:Key="BooleanToVisibilityNegationConverter"/>
<converters:BooleanNegationConverter x:Key="BooleanNegationConverter"/>
<MenuFlyout x:Key="MemoryContextMenu">
<MenuFlyout x:Name="MemoryContextMenu">
<MenuFlyoutItem x:Uid="ClearMemoryMenuItem" Click="OnClearMenuItemClicked">
<MenuFlyoutItem.Icon>
<FontIcon FontFamily="{StaticResource CalculatorFontFamily}" Glyph="&#xf754;"/>
@ -44,6 +44,7 @@
BasedOn="{StaticResource HistoryMemoryItemContainerStyle}"
TargetType="ListViewItem">
<Setter Property="Margin" Value="0,0,0,8"/>
<Setter Property="ContextFlyout" Value="{StaticResource MemoryContextMenu}"/>
</Style>
</UserControl.Resources>
@ -107,8 +108,6 @@
<ListView x:Name="MemoryListView"
Padding="0,12,0,0"
HorizontalAlignment="Stretch"
ContextCanceled="OnContextCanceled"
ContextRequested="OnContextRequested"
IsItemClickEnabled="true"
ItemClick="MemoryListItemClick"
ItemContainerStyle="{ThemeResource MemoryItemContainerStyle}"

View file

@ -39,7 +39,6 @@ Memory::Memory() :
m_isErrorVisualState(false)
{
InitializeComponent();
m_memoryItemFlyout = safe_cast<MenuFlyout^>(Resources->Lookup("MemoryContextMenu"));
MemoryPaneEmpty->FlowDirection = LocalizationService::GetInstance()->GetFlowDirection();
}
@ -56,53 +55,31 @@ void Memory::MemoryListItemClick(_In_ Object^ sender, _In_ ItemClickEventArgs^ e
}
}
void Memory::OnContextRequested(Windows::UI::Xaml::UIElement^ sender, Windows::UI::Xaml::Input::ContextRequestedEventArgs^ e)
{
// Walk up the tree to find the ListViewItem.
// There may not be one if the click wasn't on an item.
auto requestedElement = safe_cast<FrameworkElement^>(e->OriginalSource);
while ((requestedElement != sender) && !dynamic_cast<ListViewItem^>(requestedElement))
{
requestedElement = safe_cast<FrameworkElement^>(VisualTreeHelper::GetParent(requestedElement));
}
if (requestedElement != sender)
{
// The context menu request was for a ListViewItem.
auto memorySlot = safe_cast<MemoryItemViewModel^>(MemoryListView->ItemFromContainer(requestedElement));
Point point;
if (e->TryGetPosition(requestedElement, &point))
{
m_memoryItemFlyout->ShowAt(requestedElement, point);
}
else
{
// Not invoked via pointer, so let XAML choose a default location.
m_memoryItemFlyout->ShowAt(requestedElement);
}
e->Handled = true;
}
}
void Memory::OnContextCanceled(Windows::UI::Xaml::UIElement^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
m_memoryItemFlyout->Hide();
}
void Memory::OnClearMenuItemClicked(_In_ Object^ sender, _In_ RoutedEventArgs^ e)
{
GetMemoryItemForCurrentFlyout()->Clear();
auto memoryItem = GetMemoryItemForCurrentFlyout();
if (memoryItem != nullptr)
{
memoryItem->Clear();
}
}
void Memory::OnMemoryAddMenuItemClicked(_In_ Object^ sender, _In_ RoutedEventArgs^ e)
{
GetMemoryItemForCurrentFlyout()->MemoryAdd();
auto memoryItem = GetMemoryItemForCurrentFlyout();
if (memoryItem != nullptr)
{
memoryItem->MemoryAdd();
}
}
void Memory::OnMemorySubtractMenuItemClicked(_In_ Object^ sender, _In_ RoutedEventArgs^ e)
{
GetMemoryItemForCurrentFlyout()->MemorySubtract();
auto memoryItem = GetMemoryItemForCurrentFlyout();
if (memoryItem != nullptr)
{
memoryItem->MemorySubtract();
}
}
bool Memory::IsErrorVisualState::get()
@ -135,7 +112,6 @@ void Memory::MemoryList_Unloaded(_In_ Object^ sender, _In_ RoutedEventArgs^ e)
MemoryItemViewModel^ Memory::GetMemoryItemForCurrentFlyout()
{
auto listViewItem = m_memoryItemFlyout->Target;
auto listViewItem = MemoryContextMenu->Target;
return safe_cast<MemoryItemViewModel^>(MemoryListView->ItemFromContainer(listViewItem));
}

View file

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
@ -37,14 +37,11 @@ namespace CalculatorApp
}
private:
Windows::UI::Xaml::Controls::MenuFlyout^ m_memoryItemFlyout;
Windows::Foundation::Rect m_visibleBounds;
Windows::Foundation::Rect m_coreBounds;
bool m_isErrorVisualState;
void MemoryListItemClick(_In_ Platform::Object^ sender, _In_ Windows::UI::Xaml::Controls::ItemClickEventArgs^ e);
void OnContextRequested(Windows::UI::Xaml::UIElement^ sender, Windows::UI::Xaml::Input::ContextRequestedEventArgs^ e);
void OnContextCanceled(Windows::UI::Xaml::UIElement^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void OnClearMenuItemClicked(_In_ Platform::Object^ sender, _In_ Windows::UI::Xaml::RoutedEventArgs^ e);
void OnMemoryAddMenuItemClicked(_In_ Platform::Object^ sender, _In_ Windows::UI::Xaml::RoutedEventArgs^ e);
void OnMemorySubtractMenuItemClicked(_In_ Platform::Object^ sender, _In_ Windows::UI::Xaml::RoutedEventArgs^ e);