mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 20:27:36 +02: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"
|
Margin="16"
|
||||||
RowSpacing="8">
|
RowSpacing="8">
|
||||||
<Grid.RowDefinitions>
|
<Grid.RowDefinitions>
|
||||||
<RowDefinition Height="Auto" /><!-- buttons -->
|
<RowDefinition Height="Auto" /> <!-- Buttons -->
|
||||||
<RowDefinition Height="*" /><!-- content -->
|
<RowDefinition Height="*" /> <!-- Content -->
|
||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
|
|
||||||
<Grid><!-- buttons -->
|
<Grid> <!-- Buttons -->
|
||||||
<Button
|
<Button
|
||||||
x:Uid="AddEntryBtn"
|
x:Uid="AddEntryBtn"
|
||||||
Command="{x:Bind NewDialogCommand}">
|
Command="{x:Bind NewDialogCommand}">
|
||||||
@@ -183,74 +183,76 @@
|
|||||||
CornerRadius="{StaticResource OverlayCornerRadius}"
|
CornerRadius="{StaticResource OverlayCornerRadius}"
|
||||||
IsItemClickEnabled="True"
|
IsItemClickEnabled="True"
|
||||||
ItemClick="Entries_ItemClick"
|
ItemClick="Entries_ItemClick"
|
||||||
KeyDown="Entries_KeyDown"
|
|
||||||
GotFocus="Entries_GotFocus"
|
GotFocus="Entries_GotFocus"
|
||||||
|
RightTapped="Entries_RightTapped"
|
||||||
ItemsSource="{x:Bind ViewModel.Entries, Mode=TwoWay}"
|
ItemsSource="{x:Bind ViewModel.Entries, Mode=TwoWay}"
|
||||||
SelectedItem="{x:Bind ViewModel.Selected, 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>
|
<ListView.ItemTemplate>
|
||||||
<DataTemplate x:DataType="models:Entry">
|
<DataTemplate x:DataType="models:Entry">
|
||||||
<Grid
|
<Grid
|
||||||
AutomationProperties.Name="{x:Bind Address, Mode=OneWay}"
|
AutomationProperties.Name="{x:Bind Address, Mode=OneWay}"
|
||||||
Background="Transparent"
|
Background="Transparent"
|
||||||
IsRightTapEnabled="True"
|
|
||||||
RightTapped="Grid_RightTapped"
|
|
||||||
ColumnSpacing="8">
|
ColumnSpacing="8">
|
||||||
<Grid.ColumnDefinitions>
|
<Grid.ColumnDefinitions>
|
||||||
<ColumnDefinition Width="256"/><!--address-->
|
<ColumnDefinition Width="256"/> <!-- Address -->
|
||||||
<ColumnDefinition Width="*"/><!--comment-->
|
<ColumnDefinition Width="*"/> <!-- Comment -->
|
||||||
<ColumnDefinition Width="Auto"/><!--status-->
|
<ColumnDefinition Width="Auto"/> <!-- Status -->
|
||||||
<ColumnDefinition Width="Auto"/><!--duplicate-->
|
<ColumnDefinition Width="Auto"/> <!-- Duplicate -->
|
||||||
<ColumnDefinition Width="Auto"/><!--toggleswitch-->
|
<ColumnDefinition Width="Auto"/> <!-- ToggleSwitch -->
|
||||||
</Grid.ColumnDefinitions>
|
</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
|
<TextBlock
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using CommunityToolkit.Mvvm.Input;
|
using CommunityToolkit.Mvvm.Input;
|
||||||
@@ -12,10 +11,7 @@ using Hosts.Settings;
|
|||||||
using Hosts.ViewModels;
|
using Hosts.ViewModels;
|
||||||
using Microsoft.UI.Xaml;
|
using Microsoft.UI.Xaml;
|
||||||
using Microsoft.UI.Xaml.Controls;
|
using Microsoft.UI.Xaml.Controls;
|
||||||
using Microsoft.UI.Xaml.Controls.Primitives;
|
|
||||||
using Microsoft.UI.Xaml.Input;
|
using Microsoft.UI.Xaml.Input;
|
||||||
using Windows.System;
|
|
||||||
using Windows.UI.Core;
|
|
||||||
|
|
||||||
namespace Hosts.Views
|
namespace Hosts.Views
|
||||||
{
|
{
|
||||||
@@ -97,60 +93,30 @@ namespace Hosts.Views
|
|||||||
ViewModel.UpdateAdditionalLines(AdditionalLines.Text);
|
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)
|
private async void Delete_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var menuFlyoutItem = sender as MenuFlyoutItem;
|
if (Entries.SelectedItem is Entry entry)
|
||||||
|
|
||||||
if (menuFlyoutItem != null)
|
|
||||||
{
|
{
|
||||||
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)
|
private async void Edit_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var menuFlyoutItem = sender as MenuFlyoutItem;
|
if (Entries.SelectedItem is Entry entry)
|
||||||
|
|
||||||
if (menuFlyoutItem != null)
|
|
||||||
{
|
{
|
||||||
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)
|
private void ReorderButtonUp_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var menuFlyoutItem = sender as MenuFlyoutItem;
|
if (Entries.SelectedItem is Entry entry)
|
||||||
|
|
||||||
if (menuFlyoutItem != null)
|
|
||||||
{
|
{
|
||||||
var entry = menuFlyoutItem.DataContext as Entry;
|
|
||||||
var index = ViewModel.Entries.IndexOf(entry);
|
var index = ViewModel.Entries.IndexOf(entry);
|
||||||
if (index > 0)
|
if (index > 0)
|
||||||
{
|
{
|
||||||
@@ -196,11 +159,8 @@ namespace Hosts.Views
|
|||||||
|
|
||||||
private void ReorderButtonDown_Click(object sender, RoutedEventArgs e)
|
private void ReorderButtonDown_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var menuFlyoutItem = sender as MenuFlyoutItem;
|
if (Entries.SelectedItem is Entry entry)
|
||||||
|
|
||||||
if (menuFlyoutItem != null)
|
|
||||||
{
|
{
|
||||||
var entry = menuFlyoutItem.DataContext as Entry;
|
|
||||||
var index = ViewModel.Entries.IndexOf(entry);
|
var index = ViewModel.Entries.IndexOf(entry);
|
||||||
if (index < ViewModel.Entries.Count - 1)
|
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>
|
/// <summary>
|
||||||
/// Focus the first item when the list view gets the focus with keyboard
|
/// Focus the first item when the list view gets the focus with keyboard
|
||||||
/// </summary>
|
/// </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 entry = (e.OriginalSource as FrameworkElement).DataContext as Entry;
|
||||||
var menuFlyout = sender as MenuFlyout;
|
ViewModel.Selected = entry;
|
||||||
if (menuFlyout != null && menuFlyout.Items.Count > 0)
|
|
||||||
{
|
|
||||||
menuFlyout.Items.First().Focus(FocusState.Programmatic);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user