Skip to content

Commit

Permalink
issue fixed where short keys were not working at start
Browse files Browse the repository at this point in the history
fixes #25
  • Loading branch information
w-ahmad committed Jun 24, 2024
1 parent babc1ae commit 62f657a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
27 changes: 27 additions & 0 deletions src/WinUI.TableView/TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ protected override void OnPreviewKeyDown(KeyRoutedEventArgs e)
var ctrlKey = KeyBoardHelper.IsCtrlKeyDown();
var currentCell = CurrentCellSlot.HasValue ? GetCellFromSlot(CurrentCellSlot.Value) : default;

if (HandleShortKeys(shiftKey, ctrlKey, e.Key))
{
e.Handled = true;
return;
}

if (e.Key is VirtualKey.F2 && currentCell is not null && !IsEditing)
{
currentCell.PrepareForEdit();
Expand Down Expand Up @@ -155,6 +161,27 @@ protected override void OnPreviewKeyDown(KeyRoutedEventArgs e)
}
}

private bool HandleShortKeys(bool shiftKey, bool ctrlKey, VirtualKey key)
{
if (key == VirtualKey.A && ctrlKey && !shiftKey)
{
SelectAll();
return true;
}
else if (key == VirtualKey.A && ctrlKey && shiftKey)
{
DeselectAll();
return true;
}
else if (key == VirtualKey.C && ctrlKey)
{
CopyToClipboardInternal(shiftKey);
return true;
}

return false;
}

protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
Expand Down
12 changes: 0 additions & 12 deletions src/WinUI.TableView/TableViewHeaderRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,6 @@ protected override void OnApplyTemplate()
if (_optionsButton is not null)
{
_optionsButton.DataContext = new OptionsFlyoutViewModel(TableView);

// Hack: this will allow keyboard accelerators to get work
ShowAndHidOptionsFlyout();
async void ShowAndHidOptionsFlyout()
{
if (_optionsButton.Visibility == Visibility.Visible)
{
_optionsButton.Flyout.ShowAt(_optionsButton);
await Task.Delay(5);
_optionsButton.Flyout.Hide();
}
}
}

if (GetTemplateChild("selectAllButton") is Button selectAllButton)
Expand Down
9 changes: 3 additions & 6 deletions src/WinUI.TableView/Themes/TableViewHeaderRow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@
Icon="ClearSelection">
<MenuFlyoutItem.KeyboardAccelerators>
<KeyboardAccelerator Key="A"
Modifiers="Control,Shift"
ScopeOwner="{TemplateBinding TableView}" />
Modifiers="Control,Shift" />
</MenuFlyoutItem.KeyboardAccelerators>
</MenuFlyoutItem>
<MenuFlyoutSeparator />
Expand All @@ -120,12 +119,10 @@
ScopeOwner="{TemplateBinding TableView}" />
</MenuFlyoutItem.KeyboardAccelerators>
</MenuFlyoutItem>
<MenuFlyoutItem Command="{Binding CopyWithHeadersCommand}"
KeyboardAcceleratorPlacementTarget="{TemplateBinding TableView}">
<MenuFlyoutItem Command="{Binding CopyWithHeadersCommand}">
<MenuFlyoutItem.KeyboardAccelerators>
<KeyboardAccelerator Key="C"
Modifiers="Control,Shift"
ScopeOwner="{TemplateBinding TableView}" />
Modifiers="Control,Shift" />
</MenuFlyoutItem.KeyboardAccelerators>
</MenuFlyoutItem>
<MenuFlyoutSeparator />
Expand Down

0 comments on commit 62f657a

Please sign in to comment.