mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
[Hosts]Entries context menu refactoring (#28367)
This commit is contained in:
committed by
GitHub
parent
76de2de123
commit
184c618398
@@ -41,11 +41,11 @@
|
||||
Margin="16"
|
||||
RowSpacing="8">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" /><!-- buttons -->
|
||||
<RowDefinition Height="*" /><!-- content -->
|
||||
<RowDefinition Height="Auto" /> <!-- Buttons -->
|
||||
<RowDefinition Height="*" /> <!-- Content -->
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Grid><!-- buttons -->
|
||||
<Grid> <!-- Buttons -->
|
||||
<Button
|
||||
x:Uid="AddEntryBtn"
|
||||
Command="{x:Bind NewDialogCommand}">
|
||||
@@ -183,74 +183,76 @@
|
||||
CornerRadius="{StaticResource OverlayCornerRadius}"
|
||||
IsItemClickEnabled="True"
|
||||
ItemClick="Entries_ItemClick"
|
||||
KeyDown="Entries_KeyDown"
|
||||
GotFocus="Entries_GotFocus"
|
||||
RightTapped="Entries_RightTapped"
|
||||
ItemsSource="{x:Bind ViewModel.Entries, Mode=TwoWay}"
|
||||
SelectedItem="{x:Bind ViewModel.Selected, Mode=TwoWay}">
|
||||
<ListView.ContextFlyout>
|
||||
<MenuFlyout>
|
||||
<MenuFlyoutItem
|
||||
x:Uid="Edit"
|
||||
Click="Edit_Click"
|
||||
Icon="Edit">
|
||||
<MenuFlyoutItem.KeyboardAccelerators>
|
||||
<KeyboardAccelerator
|
||||
Modifiers="Control"
|
||||
Key="E"
|
||||
ScopeOwner="{x:Bind Entries}" />
|
||||
</MenuFlyoutItem.KeyboardAccelerators>
|
||||
</MenuFlyoutItem>
|
||||
<MenuFlyoutItem
|
||||
x:Uid="Ping"
|
||||
Click="Ping_Click"
|
||||
Icon="TwoBars">
|
||||
<MenuFlyoutItem.KeyboardAccelerators>
|
||||
<KeyboardAccelerator
|
||||
Modifiers="Control"
|
||||
Key="P"
|
||||
ScopeOwner="{x:Bind Entries}" />
|
||||
</MenuFlyoutItem.KeyboardAccelerators>
|
||||
</MenuFlyoutItem>
|
||||
<MenuFlyoutItem
|
||||
x:Uid="MoveUp"
|
||||
Click="ReorderButtonUp_Click"
|
||||
IsEnabled="{Binding DataContext.Filtered, ElementName=Page, Mode=OneWay, Converter={StaticResource BoolNegationConverter}}">
|
||||
<MenuFlyoutItem.Icon>
|
||||
<FontIcon Glyph="" />
|
||||
</MenuFlyoutItem.Icon>
|
||||
</MenuFlyoutItem>
|
||||
<MenuFlyoutItem
|
||||
x:Uid="MoveDown"
|
||||
Click="ReorderButtonDown_Click"
|
||||
IsEnabled="{Binding DataContext.Filtered, ElementName=Page, Mode=OneWay, Converter={StaticResource BoolNegationConverter}}">
|
||||
<MenuFlyoutItem.Icon>
|
||||
<FontIcon Glyph="" />
|
||||
</MenuFlyoutItem.Icon>
|
||||
</MenuFlyoutItem>
|
||||
<MenuFlyoutSeparator />
|
||||
<MenuFlyoutItem
|
||||
x:Uid="Delete"
|
||||
Click="Delete_Click"
|
||||
Icon="Delete">
|
||||
<MenuFlyoutItem.KeyboardAccelerators>
|
||||
<KeyboardAccelerator
|
||||
Key="Delete"
|
||||
ScopeOwner="{x:Bind Entries}" />
|
||||
</MenuFlyoutItem.KeyboardAccelerators>
|
||||
</MenuFlyoutItem>
|
||||
</MenuFlyout>
|
||||
</ListView.ContextFlyout>
|
||||
<ListView.ItemTemplate>
|
||||
<DataTemplate x:DataType="models:Entry">
|
||||
<Grid
|
||||
AutomationProperties.Name="{x:Bind Address, Mode=OneWay}"
|
||||
Background="Transparent"
|
||||
IsRightTapEnabled="True"
|
||||
RightTapped="Grid_RightTapped"
|
||||
ColumnSpacing="8">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="256"/><!--address-->
|
||||
<ColumnDefinition Width="*"/><!--comment-->
|
||||
<ColumnDefinition Width="Auto"/><!--status-->
|
||||
<ColumnDefinition Width="Auto"/><!--duplicate-->
|
||||
<ColumnDefinition Width="Auto"/><!--toggleswitch-->
|
||||
<ColumnDefinition Width="256"/> <!-- Address -->
|
||||
<ColumnDefinition Width="*"/> <!-- Comment -->
|
||||
<ColumnDefinition Width="Auto"/> <!-- Status -->
|
||||
<ColumnDefinition Width="Auto"/> <!-- Duplicate -->
|
||||
<ColumnDefinition Width="Auto"/> <!-- ToggleSwitch -->
|
||||
</Grid.ColumnDefinitions>
|
||||
<FlyoutBase.AttachedFlyout>
|
||||
<MenuFlyout Opened="MenuFlyout_Opened">
|
||||
<MenuFlyoutItem
|
||||
x:Uid="Edit"
|
||||
Click="Edit_Click"
|
||||
Icon="Edit">
|
||||
<MenuFlyoutItem.KeyboardAccelerators>
|
||||
<KeyboardAccelerator
|
||||
Modifiers="Control"
|
||||
Key="E" />
|
||||
</MenuFlyoutItem.KeyboardAccelerators>
|
||||
</MenuFlyoutItem>
|
||||
<MenuFlyoutItem
|
||||
x:Uid="Ping"
|
||||
Click="Ping_Click"
|
||||
Icon="TwoBars">
|
||||
<MenuFlyoutItem.KeyboardAccelerators>
|
||||
<KeyboardAccelerator
|
||||
Modifiers="Control"
|
||||
Key="P" />
|
||||
</MenuFlyoutItem.KeyboardAccelerators>
|
||||
</MenuFlyoutItem>
|
||||
<MenuFlyoutItem
|
||||
x:Uid="MoveUp"
|
||||
Click="ReorderButtonUp_Click"
|
||||
IsEnabled="{Binding DataContext.Filtered, ElementName=Page, Mode=OneWay, Converter={StaticResource BoolNegationConverter}}">
|
||||
<MenuFlyoutItem.Icon>
|
||||
<FontIcon Glyph="" />
|
||||
</MenuFlyoutItem.Icon>
|
||||
</MenuFlyoutItem>
|
||||
<MenuFlyoutItem
|
||||
x:Uid="MoveDown"
|
||||
Click="ReorderButtonDown_Click"
|
||||
IsEnabled="{Binding DataContext.Filtered, ElementName=Page, Mode=OneWay, Converter={StaticResource BoolNegationConverter}}">
|
||||
<MenuFlyoutItem.Icon>
|
||||
<FontIcon Glyph="" />
|
||||
</MenuFlyoutItem.Icon>
|
||||
</MenuFlyoutItem>
|
||||
<MenuFlyoutSeparator />
|
||||
<MenuFlyoutItem
|
||||
x:Uid="Delete"
|
||||
Click="Delete_Click"
|
||||
Icon="Delete">
|
||||
<MenuFlyoutItem.KeyboardAccelerators>
|
||||
<KeyboardAccelerator Key="Delete" />
|
||||
</MenuFlyoutItem.KeyboardAccelerators>
|
||||
</MenuFlyoutItem>
|
||||
</MenuFlyout>
|
||||
</FlyoutBase.AttachedFlyout>
|
||||
<TextBlock
|
||||
Grid.Column="0"
|
||||
VerticalAlignment="Center"
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
@@ -12,10 +11,7 @@ using Hosts.Settings;
|
||||
using Hosts.ViewModels;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Controls.Primitives;
|
||||
using Microsoft.UI.Xaml.Input;
|
||||
using Windows.System;
|
||||
using Windows.UI.Core;
|
||||
|
||||
namespace Hosts.Views
|
||||
{
|
||||
@@ -97,60 +93,30 @@ namespace Hosts.Views
|
||||
ViewModel.UpdateAdditionalLines(AdditionalLines.Text);
|
||||
}
|
||||
|
||||
private void Grid_RightTapped(object sender, RightTappedRoutedEventArgs e)
|
||||
{
|
||||
var owner = sender as FrameworkElement;
|
||||
if (owner != null)
|
||||
{
|
||||
var flyoutBase = FlyoutBase.GetAttachedFlyout(owner);
|
||||
flyoutBase.ShowAt(owner, new FlyoutShowOptions
|
||||
{
|
||||
Position = e.GetPosition(owner),
|
||||
ShowMode = FlyoutShowMode.Transient, // https://github.com/microsoft/PowerToys/issues/21263
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async void Delete_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var menuFlyoutItem = sender as MenuFlyoutItem;
|
||||
|
||||
if (menuFlyoutItem != null)
|
||||
if (Entries.SelectedItem is Entry entry)
|
||||
{
|
||||
await ShowDeleteDialogAsync(menuFlyoutItem.DataContext as Entry);
|
||||
ViewModel.Selected = entry;
|
||||
DeleteDialog.Title = entry.Address;
|
||||
await DeleteDialog.ShowAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ShowDeleteDialogAsync(Entry entry)
|
||||
{
|
||||
ViewModel.Selected = entry;
|
||||
DeleteDialog.Title = entry.Address;
|
||||
await DeleteDialog.ShowAsync();
|
||||
}
|
||||
|
||||
private async void Ping_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var menuFlyoutItem = sender as MenuFlyoutItem;
|
||||
|
||||
if (menuFlyoutItem != null)
|
||||
{
|
||||
await PingAsync(menuFlyoutItem.DataContext as Entry);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PingAsync(Entry entry)
|
||||
{
|
||||
ViewModel.Selected = entry;
|
||||
await ViewModel.PingSelectedAsync();
|
||||
}
|
||||
|
||||
private async void Edit_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var menuFlyoutItem = sender as MenuFlyoutItem;
|
||||
|
||||
if (menuFlyoutItem != null)
|
||||
if (Entries.SelectedItem is Entry entry)
|
||||
{
|
||||
await ShowEditDialogAsync(menuFlyoutItem.DataContext as Entry);
|
||||
await ShowEditDialogAsync(entry);
|
||||
}
|
||||
}
|
||||
|
||||
private async void Ping_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (Entries.SelectedItem is Entry entry)
|
||||
{
|
||||
ViewModel.Selected = entry;
|
||||
await ViewModel.PingSelectedAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,11 +147,8 @@ namespace Hosts.Views
|
||||
|
||||
private void ReorderButtonUp_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var menuFlyoutItem = sender as MenuFlyoutItem;
|
||||
|
||||
if (menuFlyoutItem != null)
|
||||
if (Entries.SelectedItem is Entry entry)
|
||||
{
|
||||
var entry = menuFlyoutItem.DataContext as Entry;
|
||||
var index = ViewModel.Entries.IndexOf(entry);
|
||||
if (index > 0)
|
||||
{
|
||||
@@ -196,11 +159,8 @@ namespace Hosts.Views
|
||||
|
||||
private void ReorderButtonDown_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var menuFlyoutItem = sender as MenuFlyoutItem;
|
||||
|
||||
if (menuFlyoutItem != null)
|
||||
if (Entries.SelectedItem is Entry entry)
|
||||
{
|
||||
var entry = menuFlyoutItem.DataContext as Entry;
|
||||
var index = ViewModel.Entries.IndexOf(entry);
|
||||
if (index < ViewModel.Entries.Count - 1)
|
||||
{
|
||||
@@ -209,35 +169,6 @@ namespace Hosts.Views
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle the keyboard shortcuts at list view level since
|
||||
/// KeyboardAccelerators in FlyoutBase.AttachedFlyout works only when the flyout is open
|
||||
/// </summary>
|
||||
private async void Entries_KeyDown(object sender, KeyRoutedEventArgs e)
|
||||
{
|
||||
var listView = sender as ListView;
|
||||
if (listView != null && e.KeyStatus.WasKeyDown == false)
|
||||
{
|
||||
var entry = listView.SelectedItem as Entry;
|
||||
|
||||
if (Microsoft.UI.Input.InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down))
|
||||
{
|
||||
if (e.Key == VirtualKey.E)
|
||||
{
|
||||
await ShowEditDialogAsync(entry);
|
||||
}
|
||||
else if (e.Key == VirtualKey.P)
|
||||
{
|
||||
await PingAsync(entry);
|
||||
}
|
||||
}
|
||||
else if (e.Key == VirtualKey.Delete)
|
||||
{
|
||||
await ShowDeleteDialogAsync(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Focus the first item when the list view gets the focus with keyboard
|
||||
/// </summary>
|
||||
@@ -250,14 +181,10 @@ namespace Hosts.Views
|
||||
}
|
||||
}
|
||||
|
||||
private void MenuFlyout_Opened(object sender, object e)
|
||||
private void Entries_RightTapped(object sender, RightTappedRoutedEventArgs e)
|
||||
{
|
||||
// Focus the first item: required for workaround https://github.com/microsoft/PowerToys/issues/21263
|
||||
var menuFlyout = sender as MenuFlyout;
|
||||
if (menuFlyout != null && menuFlyout.Items.Count > 0)
|
||||
{
|
||||
menuFlyout.Items.First().Focus(FocusState.Programmatic);
|
||||
}
|
||||
var entry = (e.OriginalSource as FrameworkElement).DataContext as Entry;
|
||||
ViewModel.Selected = entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user