2020-03-23 10:44:02 -07:00
# include "pch.h"
# include "ShortcutControl.h"
//Both static members are initialized to null
HWND ShortcutControl : : EditShortcutsWindowHandle = nullptr ;
KeyboardManagerState * ShortcutControl : : keyboardManagerState = nullptr ;
2020-04-09 09:20:19 -07:00
// Initialized as new vector
std : : vector < std : : vector < Shortcut > > ShortcutControl : : shortcutRemapBuffer ;
2020-03-23 10:44:02 -07:00
// Function to add a new row to the shortcut table. If the originalKeys and newKeys args are provided, then the displayed shortcuts are set to those values.
2020-04-09 09:20:19 -07:00
void ShortcutControl : : AddNewShortcutControlRow ( StackPanel & parent , Shortcut originalKeys , Shortcut newKeys )
2020-03-23 10:44:02 -07:00
{
// Parent element for the row
Windows : : UI : : Xaml : : Controls : : StackPanel tableRow ;
tableRow . Background ( Windows : : UI : : Xaml : : Media : : SolidColorBrush { Windows : : UI : : Colors : : LightGray ( ) } ) ;
tableRow . Spacing ( 100 ) ;
tableRow . Orientation ( Windows : : UI : : Xaml : : Controls : : Orientation : : Horizontal ) ;
// ShortcutControl for the original shortcut
2020-04-09 09:20:19 -07:00
ShortcutControl originalSC ( shortcutRemapBuffer . size ( ) , 0 ) ;
2020-03-23 10:44:02 -07:00
tableRow . Children ( ) . Append ( originalSC . getShortcutControl ( ) ) ;
// ShortcutControl for the new shortcut
2020-04-09 09:20:19 -07:00
ShortcutControl newSC ( shortcutRemapBuffer . size ( ) , 1 ) ;
2020-03-23 10:44:02 -07:00
tableRow . Children ( ) . Append ( newSC . getShortcutControl ( ) ) ;
// Set the shortcut text if the two vectors are not empty (i.e. default args)
2020-04-08 09:11:58 -07:00
if ( ! originalKeys . IsEmpty ( ) & & ! newKeys . IsEmpty ( ) )
2020-03-23 10:44:02 -07:00
{
2020-04-09 09:20:19 -07:00
shortcutRemapBuffer . push_back ( std : : vector < Shortcut > { originalKeys , newKeys } ) ;
originalSC . shortcutText . Text ( originalKeys . ToHstring ( keyboardManagerState - > keyboardMap ) ) ;
newSC . shortcutText . Text ( newKeys . ToHstring ( keyboardManagerState - > keyboardMap ) ) ;
}
else
{
// Initialize both shortcuts as empty shortcuts
shortcutRemapBuffer . push_back ( std : : vector < Shortcut > { Shortcut ( ) , Shortcut ( ) } ) ;
2020-03-23 10:44:02 -07:00
}
// Delete row button
Windows : : UI : : Xaml : : Controls : : Button deleteShortcut ;
FontIcon deleteSymbol ;
deleteSymbol . FontFamily ( Xaml : : Media : : FontFamily ( L " Segoe MDL2 Assets " ) ) ;
deleteSymbol . Glyph ( L " \xE74D " ) ;
deleteShortcut . Content ( deleteSymbol ) ;
deleteShortcut . Click ( [ & ] ( IInspectable const & sender , RoutedEventArgs const & ) {
StackPanel currentRow = sender . as < Button > ( ) . Parent ( ) . as < StackPanel > ( ) ;
uint32_t index ;
parent . Children ( ) . IndexOf ( currentRow , index ) ;
parent . Children ( ) . RemoveAt ( index ) ;
2020-04-09 09:20:19 -07:00
// delete the row from the buffer. Since first child of the stackpanel is the header, the effective index starts from 1
shortcutRemapBuffer . erase ( shortcutRemapBuffer . begin ( ) + ( index - 1 ) ) ;
2020-03-23 10:44:02 -07:00
} ) ;
tableRow . Children ( ) . Append ( deleteShortcut ) ;
parent . Children ( ) . Append ( tableRow ) ;
}
// Function to return the stack panel element of the ShortcutControl. This is the externally visible UI element which can be used to add it to other layouts
StackPanel ShortcutControl : : getShortcutControl ( )
{
return shortcutControlLayout ;
}
// Function to create the detect shortcut UI window
2020-04-09 09:20:19 -07:00
void ShortcutControl : : createDetectShortcutWindow ( IInspectable const & sender , XamlRoot xamlRoot , std : : vector < std : : vector < Shortcut > > & shortcutRemapBuffer , KeyboardManagerState & keyboardManagerState , const int & rowIndex , const int & colIndex )
2020-03-23 10:44:02 -07:00
{
// ContentDialog for detecting shortcuts. This is the parent UI element.
ContentDialog detectShortcutBox ;
2020-04-08 14:31:31 -07:00
// TODO: Hardcoded light theme, since the app is not theme aware ATM.
detectShortcutBox . RequestedTheme ( ElementTheme : : Light ) ;
2020-03-23 10:44:02 -07:00
// ContentDialog requires manually setting the XamlRoot (https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.contentdialog#contentdialog-in-appwindow-or-xaml-islands)
detectShortcutBox . XamlRoot ( xamlRoot ) ;
detectShortcutBox . Title ( box_value ( L " Press the keys in shortcut: " ) ) ;
detectShortcutBox . PrimaryButtonText ( to_hstring ( L " OK " ) ) ;
detectShortcutBox . IsSecondaryButtonEnabled ( false ) ;
detectShortcutBox . CloseButtonText ( to_hstring ( L " Cancel " ) ) ;
// Get the linked text block for the "Type shortcut" button that was clicked
TextBlock linkedShortcutText = getSiblingElement ( sender ) . as < TextBlock > ( ) ;
// OK button
2020-04-09 09:20:19 -07:00
detectShortcutBox . PrimaryButtonClick ( [ = , & shortcutRemapBuffer , & keyboardManagerState ] ( Windows : : UI : : Xaml : : Controls : : ContentDialog const & sender , ContentDialogButtonClickEventArgs const & ) {
2020-03-23 10:44:02 -07:00
// Save the detected shortcut in the linked text block
2020-04-08 09:11:58 -07:00
Shortcut detectedShortcutKeys = keyboardManagerState . GetDetectedShortcut ( ) ;
2020-04-09 09:20:19 -07:00
if ( ! detectedShortcutKeys . IsEmpty ( ) )
{
shortcutRemapBuffer [ rowIndex ] [ colIndex ] = detectedShortcutKeys ;
linkedShortcutText . Text ( detectedShortcutKeys . ToHstring ( keyboardManagerState . keyboardMap ) ) ;
}
2020-03-23 10:44:02 -07:00
// Reset the keyboard manager UI state
keyboardManagerState . ResetUIState ( ) ;
} ) ;
// Cancel button
detectShortcutBox . CloseButtonClick ( [ & keyboardManagerState ] ( Windows : : UI : : Xaml : : Controls : : ContentDialog const & sender , ContentDialogButtonClickEventArgs const & ) {
// Reset the keyboard manager UI state
keyboardManagerState . ResetUIState ( ) ;
} ) ;
// StackPanel parent for the displayed text in the dialog
Windows : : UI : : Xaml : : Controls : : StackPanel stackPanel ;
2020-04-08 14:31:31 -07:00
detectShortcutBox . Content ( stackPanel ) ;
2020-03-23 10:44:02 -07:00
// Header textblock
TextBlock text ;
text . Text ( winrt : : to_hstring ( " Keys Pressed: " ) ) ;
text . Margin ( { 0 , 0 , 0 , 10 } ) ;
2020-04-08 14:31:31 -07:00
stackPanel . Children ( ) . Append ( text ) ;
2020-03-23 10:44:02 -07:00
2020-04-08 14:31:31 -07:00
// Target StackPanel to place the selected key
Windows : : UI : : Xaml : : Controls : : StackPanel keyStackPanel ;
stackPanel . Children ( ) . Append ( keyStackPanel ) ;
keyStackPanel . Orientation ( Orientation : : Horizontal ) ;
2020-03-23 10:44:02 -07:00
stackPanel . UpdateLayout ( ) ;
// Configure the keyboardManagerState to store the UI information.
2020-04-08 14:31:31 -07:00
keyboardManagerState . ConfigureDetectShortcutUI ( keyStackPanel ) ;
2020-03-23 10:44:02 -07:00
// Show the dialog
detectShortcutBox . ShowAsync ( ) ;
}