2020-03-23 10:44:02 -07:00
# include "pch.h"
# include "ShortcutControl.h"
2020-04-18 16:12:26 -07:00
# include "KeyDropDownControl.h"
2020-03-23 10:44:02 -07:00
//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-23 09:14:16 -07:00
void ShortcutControl : : AddNewShortcutControlRow ( Grid & parent , std : : vector < std : : vector < std : : unique_ptr < ShortcutControl > > > & keyboardRemapControlObjects , Shortcut originalKeys , Shortcut newKeys )
2020-03-23 10:44:02 -07:00
{
2020-04-18 16:12:26 -07:00
// Create new ShortcutControl objects dynamically so that we does not get destructed
std : : vector < std : : unique_ptr < ShortcutControl > > newrow ;
2020-05-11 10:10:36 -07:00
newrow . push_back ( std : : move ( std : : unique_ptr < ShortcutControl > ( new ShortcutControl ( parent , 0 ) ) ) ) ;
newrow . push_back ( std : : move ( std : : unique_ptr < ShortcutControl > ( new ShortcutControl ( parent , 1 ) ) ) ) ;
2020-04-18 16:12:26 -07:00
keyboardRemapControlObjects . push_back ( std : : move ( newrow ) ) ;
2020-04-23 09:14:16 -07:00
// Add to grid
parent . RowDefinitions ( ) . Append ( RowDefinition ( ) ) ;
2020-05-08 17:34:24 -07:00
parent . SetColumn ( keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 0 ] - > getShortcutControl ( ) , KeyboardManagerConstants : : ShortcutTableOriginalColIndex ) ;
2020-04-23 09:14:16 -07:00
parent . SetRow ( keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 0 ] - > getShortcutControl ( ) , parent . RowDefinitions ( ) . Size ( ) - 1 ) ;
2020-05-08 17:34:24 -07:00
parent . SetColumn ( keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 1 ] - > getShortcutControl ( ) , KeyboardManagerConstants : : ShortcutTableNewColIndex ) ;
2020-04-23 09:14:16 -07:00
parent . SetRow ( keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 1 ] - > getShortcutControl ( ) , parent . RowDefinitions ( ) . Size ( ) - 1 ) ;
2020-03-23 10:44:02 -07:00
// ShortcutControl for the original shortcut
2020-04-23 09:14:16 -07:00
parent . Children ( ) . Append ( keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 0 ] - > getShortcutControl ( ) ) ;
2020-05-08 17:34:24 -07:00
// Arrow icon
FontIcon arrowIcon ;
arrowIcon . FontFamily ( Xaml : : Media : : FontFamily ( L " Segoe MDL2 Assets " ) ) ;
arrowIcon . Glyph ( L " \xE72A " ) ;
arrowIcon . VerticalAlignment ( VerticalAlignment : : Center ) ;
arrowIcon . HorizontalAlignment ( HorizontalAlignment : : Center ) ;
parent . SetColumn ( arrowIcon , KeyboardManagerConstants : : ShortcutTableArrowColIndex ) ;
parent . SetRow ( arrowIcon , parent . RowDefinitions ( ) . Size ( ) - 1 ) ;
parent . Children ( ) . Append ( arrowIcon ) ;
2020-03-23 10:44:02 -07:00
// ShortcutControl for the new shortcut
2020-04-23 09:14:16 -07:00
parent . Children ( ) . Append ( keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 1 ] - > getShortcutControl ( ) ) ;
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 ) ;
2020-05-06 08:34:26 -07:00
deleteShortcut . Background ( Media : : SolidColorBrush ( Colors : : Transparent ( ) ) ) ;
2020-05-08 17:34:24 -07:00
deleteShortcut . HorizontalAlignment ( HorizontalAlignment : : Center ) ;
2020-04-20 08:22:36 -07:00
deleteShortcut . Click ( [ & ] ( winrt : : Windows : : Foundation : : IInspectable const & sender , RoutedEventArgs const & ) {
2020-04-23 09:14:16 -07:00
Button currentButton = sender . as < Button > ( ) ;
2020-03-23 10:44:02 -07:00
uint32_t index ;
2020-04-23 09:14:16 -07:00
// Get index of delete button
UIElementCollection children = parent . Children ( ) ;
children . IndexOf ( currentButton , index ) ;
2020-05-08 17:34:24 -07:00
uint32_t lastIndexInRow = index + ( ( KeyboardManagerConstants : : ShortcutTableColCount - 1 ) - KeyboardManagerConstants : : ShortcutTableRemoveColIndex ) ;
2020-04-23 09:14:16 -07:00
// Change the row index of elements appearing after the current row, as we will delete the row definition
2020-04-26 15:09:40 -07:00
for ( uint32_t i = lastIndexInRow + 1 ; i < children . Size ( ) ; i + + )
2020-04-23 09:14:16 -07:00
{
int32_t elementRowIndex = parent . GetRow ( children . GetAt ( i ) . as < FrameworkElement > ( ) ) ;
parent . SetRow ( children . GetAt ( i ) . as < FrameworkElement > ( ) , elementRowIndex - 1 ) ;
}
2020-05-08 17:34:24 -07:00
for ( int i = 0 ; i < KeyboardManagerConstants : : ShortcutTableColCount ; i + + )
{
parent . Children ( ) . RemoveAt ( lastIndexInRow - i ) ;
}
2020-04-23 09:14:16 -07:00
// Calculate row index in the buffer from the grid child index (first two children are header elements and then three children in each row)
2020-05-08 17:34:24 -07:00
int bufferIndex = ( lastIndexInRow - KeyboardManagerConstants : : ShortcutTableHeaderCount ) / KeyboardManagerConstants : : ShortcutTableColCount ;
2020-04-23 09:14:16 -07:00
// Delete the row definition
parent . RowDefinitions ( ) . RemoveAt ( bufferIndex + 1 ) ;
// delete the row from the buffer
shortcutRemapBuffer . erase ( shortcutRemapBuffer . begin ( ) + bufferIndex ) ;
2020-04-18 16:12:26 -07:00
// delete the ShortcutControl objects so that they get destructed
2020-04-23 09:14:16 -07:00
keyboardRemapControlObjects . erase ( keyboardRemapControlObjects . begin ( ) + bufferIndex ) ;
2020-03-23 10:44:02 -07:00
} ) ;
2020-05-08 17:34:24 -07:00
parent . SetColumn ( deleteShortcut , KeyboardManagerConstants : : ShortcutTableRemoveColIndex ) ;
2020-04-23 09:14:16 -07:00
parent . SetRow ( deleteShortcut , parent . RowDefinitions ( ) . Size ( ) - 1 ) ;
parent . Children ( ) . Append ( deleteShortcut ) ;
2020-04-18 16:12:26 -07:00
parent . UpdateLayout ( ) ;
// Set the shortcut text if the two vectors are not empty (i.e. default args)
if ( originalKeys . IsValidShortcut ( ) & & newKeys . IsValidShortcut ( ) )
{
shortcutRemapBuffer . push_back ( std : : vector < Shortcut > { Shortcut ( ) , Shortcut ( ) } ) ;
2020-05-11 10:10:36 -07:00
keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 0 ] - > AddShortcutToControl ( originalKeys , parent , keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 0 ] - > shortcutDropDownStackPanel , * keyboardManagerState , 0 ) ;
keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 1 ] - > AddShortcutToControl ( newKeys , parent , keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 1 ] - > shortcutDropDownStackPanel , * keyboardManagerState , 1 ) ;
2020-04-18 16:12:26 -07:00
}
else
{
// Initialize both shortcuts as empty shortcuts
shortcutRemapBuffer . push_back ( std : : vector < Shortcut > { Shortcut ( ) , Shortcut ( ) } ) ;
}
}
// Function to add a shortcut to the shortcut control as combo boxes
2020-05-11 10:10:36 -07:00
void ShortcutControl : : AddShortcutToControl ( Shortcut & shortcut , Grid table , StackPanel parent , KeyboardManagerState & keyboardManagerState , const int colIndex )
2020-04-18 16:12:26 -07:00
{
// Delete the existing drop down menus
parent . Children ( ) . Clear ( ) ;
// Remove references to the old drop down objects to destroy them
keyDropDownControlObjects . clear ( ) ;
std : : vector < DWORD > shortcutKeyCodes = shortcut . GetKeyCodes ( ) ;
std : : vector < DWORD > keyCodeList = keyboardManagerState . keyboardMap . GetKeyCodeList ( true ) ;
if ( shortcutKeyCodes . size ( ) ! = 0 )
{
2020-05-11 10:10:36 -07:00
KeyDropDownControl : : AddDropDown ( table , shortcutControlLayout , parent , colIndex , shortcutRemapBuffer , keyDropDownControlObjects ) ;
2020-04-18 16:12:26 -07:00
for ( int i = 0 ; i < shortcutKeyCodes . size ( ) ; i + + )
{
// New drop down gets added automatically when the SelectedIndex is set
if ( i < ( int ) parent . Children ( ) . Size ( ) )
{
ComboBox currentDropDown = parent . Children ( ) . GetAt ( i ) . as < ComboBox > ( ) ;
auto it = std : : find ( keyCodeList . begin ( ) , keyCodeList . end ( ) , shortcutKeyCodes [ i ] ) ;
if ( it ! = keyCodeList . end ( ) )
{
currentDropDown . SelectedIndex ( ( int32_t ) std : : distance ( keyCodeList . begin ( ) , it ) ) ;
}
}
}
}
parent . UpdateLayout ( ) ;
2020-03-23 10:44:02 -07:00
}
// 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-05-11 10:10:36 -07:00
void ShortcutControl : : createDetectShortcutWindow ( winrt : : Windows : : Foundation : : IInspectable const & sender , XamlRoot xamlRoot , std : : vector < std : : vector < Shortcut > > & shortcutRemapBuffer , KeyboardManagerState & keyboardManagerState , const int colIndex , Grid table )
2020-03-23 10:44:02 -07:00
{
// ContentDialog for detecting shortcuts. This is the parent UI element.
ContentDialog detectShortcutBox ;
// 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: " ) ) ;
2020-04-16 09:16:48 -07:00
detectShortcutBox . IsPrimaryButtonEnabled ( false ) ;
2020-03-23 10:44:02 -07:00
detectShortcutBox . IsSecondaryButtonEnabled ( false ) ;
// Get the linked text block for the "Type shortcut" button that was clicked
2020-04-20 08:22:36 -07:00
StackPanel linkedShortcutStackPanel = KeyboardManagerHelper : : getSiblingElement ( sender ) . as < StackPanel > ( ) ;
2020-03-23 10:44:02 -07:00
2020-04-16 09:16:48 -07:00
auto unregisterKeys = [ & keyboardManagerState ] ( ) {
std : : thread t1 ( & KeyboardManagerState : : UnregisterKeyDelay , & keyboardManagerState , VK_ESCAPE ) ;
std : : thread t2 ( & KeyboardManagerState : : UnregisterKeyDelay , & keyboardManagerState , VK_RETURN ) ;
t1 . detach ( ) ;
t2 . detach ( ) ;
} ;
auto selectDetectedShortcutAndResetKeys = [ & keyboardManagerState ] ( DWORD key ) {
keyboardManagerState . SelectDetectedShortcut ( key ) ;
keyboardManagerState . ResetDetectedShortcutKey ( key ) ;
} ;
2020-05-13 10:13:44 -07:00
auto onPressEnter = [ this ,
linkedShortcutStackPanel ,
detectShortcutBox ,
& keyboardManagerState ,
& shortcutRemapBuffer ,
unregisterKeys ,
colIndex ,
table ] {
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 ( ) )
{
2020-04-18 16:12:26 -07:00
// The shortcut buffer gets set in this function
2020-05-11 10:10:36 -07:00
AddShortcutToControl ( detectedShortcutKeys , table , linkedShortcutStackPanel , keyboardManagerState , colIndex ) ;
2020-04-09 09:20:19 -07:00
}
2020-05-13 10:13:44 -07:00
// Hide the type shortcut UI
detectShortcutBox . Hide ( ) ;
} ;
2020-03-23 10:44:02 -07:00
2020-05-13 10:13:44 -07:00
auto onReleaseEnter = [ & keyboardManagerState ,
unregisterKeys ] {
2020-03-23 10:44:02 -07:00
// Reset the keyboard manager UI state
keyboardManagerState . ResetUIState ( ) ;
2020-05-04 15:49:37 -07:00
// Revert UI state back to Edit Shortcut window
keyboardManagerState . SetUIState ( KeyboardManagerUIState : : EditShortcutsWindowActivated , EditShortcutsWindowHandle ) ;
2020-04-16 09:16:48 -07:00
unregisterKeys ( ) ;
2020-05-13 10:13:44 -07:00
} ;
auto onAccept = [ onPressEnter ,
onReleaseEnter ] {
onPressEnter ( ) ;
onReleaseEnter ( ) ;
2020-04-16 09:16:48 -07:00
} ;
TextBlock primaryButtonText ;
2020-04-21 14:14:50 -07:00
primaryButtonText . Text ( L " OK " ) ;
2020-04-16 09:16:48 -07:00
Button primaryButton ;
primaryButton . HorizontalAlignment ( HorizontalAlignment : : Stretch ) ;
primaryButton . Margin ( { 2 , 2 , 2 , 2 } ) ;
primaryButton . Content ( primaryButtonText ) ;
// OK button
2020-04-20 08:22:36 -07:00
primaryButton . Click ( [ onAccept ] ( winrt : : Windows : : Foundation : : IInspectable const & sender , RoutedEventArgs const & ) {
2020-04-16 09:16:48 -07:00
onAccept ( ) ;
2020-03-23 10:44:02 -07:00
} ) ;
2020-04-16 09:16:48 -07:00
keyboardManagerState . RegisterKeyDelay (
VK_RETURN ,
selectDetectedShortcutAndResetKeys ,
2020-05-13 10:13:44 -07:00
[ primaryButton , onPressEnter , detectShortcutBox ] ( DWORD ) {
2020-04-16 09:16:48 -07:00
detectShortcutBox . Dispatcher ( ) . RunAsync (
Windows : : UI : : Core : : CoreDispatcherPriority : : Normal ,
2020-05-13 10:13:44 -07:00
[ primaryButton , onPressEnter ] {
2020-04-21 13:42:06 -07:00
// Use the base medium low brush to be consistent with the theme
primaryButton . Background ( Windows : : UI : : Xaml : : Application : : Current ( ) . Resources ( ) . Lookup ( box_value ( L " SystemControlBackgroundBaseMediumLowBrush " ) ) . as < Windows : : UI : : Xaml : : Media : : SolidColorBrush > ( ) ) ;
2020-05-13 10:13:44 -07:00
onPressEnter ( ) ;
2020-04-16 09:16:48 -07:00
} ) ;
} ,
2020-05-13 10:13:44 -07:00
[ onReleaseEnter ] ( DWORD ) {
onReleaseEnter ( ) ;
2020-04-16 09:16:48 -07:00
} ) ;
TextBlock cancelButtonText ;
2020-04-21 14:14:50 -07:00
cancelButtonText . Text ( L " Cancel " ) ;
2020-04-16 09:16:48 -07:00
Button cancelButton ;
cancelButton . HorizontalAlignment ( HorizontalAlignment : : Stretch ) ;
cancelButton . Margin ( { 2 , 2 , 2 , 2 } ) ;
cancelButton . Content ( cancelButtonText ) ;
2020-03-23 10:44:02 -07:00
// Cancel button
2020-04-20 08:22:36 -07:00
cancelButton . Click ( [ detectShortcutBox , unregisterKeys , & keyboardManagerState ] ( winrt : : Windows : : Foundation : : IInspectable const & sender , RoutedEventArgs const & ) {
2020-03-23 10:44:02 -07:00
// Reset the keyboard manager UI state
keyboardManagerState . ResetUIState ( ) ;
2020-05-04 15:49:37 -07:00
// Revert UI state back to Edit Shortcut window
keyboardManagerState . SetUIState ( KeyboardManagerUIState : : EditShortcutsWindowActivated , EditShortcutsWindowHandle ) ;
2020-04-16 09:16:48 -07:00
unregisterKeys ( ) ;
detectShortcutBox . Hide ( ) ;
2020-03-23 10:44:02 -07:00
} ) ;
2020-04-16 09:16:48 -07:00
keyboardManagerState . RegisterKeyDelay (
VK_ESCAPE ,
selectDetectedShortcutAndResetKeys ,
[ & keyboardManagerState , detectShortcutBox , unregisterKeys ] ( DWORD ) {
detectShortcutBox . Dispatcher ( ) . RunAsync (
Windows : : UI : : Core : : CoreDispatcherPriority : : Normal ,
[ detectShortcutBox ] {
detectShortcutBox . Hide ( ) ;
} ) ;
keyboardManagerState . ResetUIState ( ) ;
2020-05-04 15:49:37 -07:00
// Revert UI state back to Edit Shortcut window
keyboardManagerState . SetUIState ( KeyboardManagerUIState : : EditShortcutsWindowActivated , EditShortcutsWindowHandle ) ;
2020-04-16 09:16:48 -07:00
unregisterKeys ( ) ;
} ,
nullptr ) ;
2020-03-23 10:44:02 -07:00
// 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 ;
2020-04-21 14:14:50 -07:00
text . Text ( L " Keys Pressed: " ) ;
2020-03-23 10:44:02 -07:00
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-05-14 09:23:51 -07:00
// Target StackPanel to place the selected key - first row (for 1-3 keys)
Windows : : UI : : Xaml : : Controls : : StackPanel keyStackPanel1 ;
keyStackPanel1 . Orientation ( Orientation : : Horizontal ) ;
stackPanel . Children ( ) . Append ( keyStackPanel1 ) ;
// Target StackPanel to place the selected key - second row (for 4-5 keys)
Windows : : UI : : Xaml : : Controls : : StackPanel keyStackPanel2 ;
keyStackPanel2 . Orientation ( Orientation : : Horizontal ) ;
keyStackPanel2 . Margin ( { 0 , 20 , 0 , 0 } ) ;
keyStackPanel2 . Visibility ( Visibility : : Collapsed ) ;
stackPanel . Children ( ) . Append ( keyStackPanel2 ) ;
2020-03-23 10:44:02 -07:00
2020-04-16 09:16:48 -07:00
TextBlock holdEscInfo ;
2020-04-21 14:14:50 -07:00
holdEscInfo . Text ( L " Hold Esc to discard " ) ;
2020-04-16 09:16:48 -07:00
holdEscInfo . FontSize ( 12 ) ;
holdEscInfo . Margin ( { 0 , 20 , 0 , 0 } ) ;
stackPanel . Children ( ) . Append ( holdEscInfo ) ;
TextBlock holdEnterInfo ;
2020-05-13 10:13:44 -07:00
holdEnterInfo . Text ( L " Hold Enter to continue " ) ;
2020-04-16 09:16:48 -07:00
holdEnterInfo . FontSize ( 12 ) ;
holdEnterInfo . Margin ( { 0 , 0 , 0 , 0 } ) ;
stackPanel . Children ( ) . Append ( holdEnterInfo ) ;
ColumnDefinition primaryButtonColumn ;
ColumnDefinition cancelButtonColumn ;
Grid buttonPanel ;
buttonPanel . Margin ( { 0 , 20 , 0 , 0 } ) ;
buttonPanel . HorizontalAlignment ( HorizontalAlignment : : Stretch ) ;
buttonPanel . ColumnDefinitions ( ) . Append ( primaryButtonColumn ) ;
buttonPanel . ColumnDefinitions ( ) . Append ( cancelButtonColumn ) ;
buttonPanel . SetColumn ( primaryButton , 0 ) ;
buttonPanel . SetColumn ( cancelButton , 1 ) ;
buttonPanel . Children ( ) . Append ( primaryButton ) ;
buttonPanel . Children ( ) . Append ( cancelButton ) ;
stackPanel . Children ( ) . Append ( buttonPanel ) ;
2020-03-23 10:44:02 -07:00
stackPanel . UpdateLayout ( ) ;
// Configure the keyboardManagerState to store the UI information.
2020-05-14 09:23:51 -07:00
keyboardManagerState . ConfigureDetectShortcutUI ( keyStackPanel1 , keyStackPanel2 ) ;
2020-03-23 10:44:02 -07:00
// Show the dialog
detectShortcutBox . ShowAsync ( ) ;
2020-05-11 10:10:36 -07:00
}